Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect autocomplete if file name contains space #1473

Merged
merged 2 commits into from
Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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