clap-rs / clap

A full featured, fast Command Line Argument Parser for Rust
docs.rs/clap
Apache License 2.0
13.66k stars 1.02k forks source link

fix(complete): Prevent filenames splitting in bash completion #5336

Closed sudotac closed 5 months ago

sudotac commented 5 months ago

Fix #5313 (and continuation of #5240)

The root cause is field splitting by bash. It is erroneously performed when processing compgen output. The delimiters used for field splitting are defined in IFS environment variable, which is $' \t\n' (space, tab, newline) by default. If the output of compgen contains these characters, they are split out.

This patch (partially) fixes that by restricting IFS to newline. Now we can handle filenames with spaces and/or tabs, but not newlines because we cannot distinguish "newline as a delimiter" from "newline contained in filename".

Handling newlines would be more difficult than handling spaces. It seems that other completion scripts cannot handle this. I think this should be a separate issue if we really try to fix this.

epage commented 5 months ago

Thanks!