Skip to content

Commit

Permalink
Fix incorrect autocomplete if file name contains space (#1473)
Browse files Browse the repository at this point in the history
Co-authored-by: Remko Popma <remkop@yahoo.com>
  • Loading branch information
NewbieOrange and remkop committed Nov 25, 2021
1 parent 92ecd2b commit 6e59e18
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main/java/picocli/AutoComplete.java
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ private static class CommandDescriptor {
final String parentWithoutTopLevelCommand;
final String commandName;
final CommandLine commandLine;

CommandDescriptor(String functionName, String parentWithoutTopLevelCommand, String commandName, CommandLine commandLine) {
this.functionName = functionName;
this.parentWithoutTopLevelCommand = parentWithoutTopLevelCommand;
Expand Down Expand Up @@ -719,6 +719,7 @@ private static String generatePositionalParamsCases(List<PositionalParamSpec> po
buff.append(format("%s positionals=$( compgen -W \"$%s_pos_param_args\" -- \"%s\" )\n", indent, paramName, currWord));
} else if (type.equals(File.class) || "java.nio.file.Path".equals(type.getName())) {
buff.append(format("%s %s (( currIndex >= %d && currIndex <= %d )); then\n", indent, ifOrElif, min, max));
buff.append(format("%s local IFS=$'\\n'\n", indent));
buff.append(format("%s compopt -o filenames\n", indent));
buff.append(format("%s positionals=$( compgen -f -- \"%s\" ) # files\n", indent, currWord));
} else if (type.equals(InetAddress.class)) {
Expand Down Expand Up @@ -763,6 +764,7 @@ private static String generateOptionsCases(List<OptionSpec> argOptionFields, Str
buff.append(format("%s ;;\n", indent));
} else if (type.equals(File.class) || "java.nio.file.Path".equals(type.getName())) {
buff.append(format("%s %s)\n", indent, concat("|", option.names()))); // " -f|--file)\n"
buff.append(format("%s local IFS=$'\\n'\n", indent));
buff.append(format("%s compopt -o filenames\n", indent));
buff.append(format("%s COMPREPLY=( $( compgen -f -- \"%s\" ) ) # files\n", indent, currWord));
buff.append(format("%s return $?\n", indent));
Expand Down
1 change: 1 addition & 0 deletions src/test/java/picocli/AutoCompleteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,7 @@ private String expectedCompletionScriptForAutoCompleteApp() {
" return\n" +
" ;;\n" +
" -o|--completionScript)\n" +
" local IFS=$'\\n'\n" +
" compopt -o filenames\n" +
" COMPREPLY=( $( compgen -f -- \"${curr_word}\" ) ) # files\n" +
" return $?\n" +
Expand Down
2 changes: 2 additions & 0 deletions src/test/resources/picocompletion-demo-help_completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ function _picocli_picocompletion-demo-help_sub2() {
return
;;
--directory|-d)
local IFS=$'\n'
compopt -o filenames
COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
return $?
Expand Down Expand Up @@ -480,6 +481,7 @@ function _picocli_picocompletion-demo-help_sub2_subsub3() {
if (( currIndex >= 0 && currIndex <= 0 )); then
positionals=$( compgen -W "$cands_pos_param_args" -- "${curr_word}" )
elif (( currIndex >= 1 && currIndex <= 2 )); then
local IFS=$'\n'
compopt -o filenames
positionals=$( compgen -f -- "${curr_word}" ) # files
elif (( currIndex >= 3 && currIndex <= 2147483647 )); then
Expand Down
2 changes: 2 additions & 0 deletions src/test/resources/picocompletion-demo_completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ function _picocli_picocompletion-demo_sub2() {
return
;;
--directory|-d)
local IFS=$'\n'
compopt -o filenames
COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
return $?
Expand Down Expand Up @@ -461,6 +462,7 @@ function _picocli_picocompletion-demo_sub2_subsub3() {
if (( currIndex >= 0 && currIndex <= 0 )); then
positionals=$( compgen -W "$cands_pos_param_args" -- "${curr_word}" )
elif (( currIndex >= 1 && currIndex <= 2 )); then
local IFS=$'\n'
compopt -o filenames
positionals=$( compgen -f -- "${curr_word}" ) # files
elif (( currIndex >= 3 && currIndex <= 2147483647 )); then
Expand Down

0 comments on commit 6e59e18

Please sign in to comment.