Skip to content

Commit

Permalink
Fix option argument handling in autocompletion scripts
Browse files Browse the repository at this point in the history
Closes #466.
  • Loading branch information
dwalluck authored and remkop committed Sep 8, 2018
1 parent 2f37bc8 commit ca98f95
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/java/picocli/AutoComplete.java
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,8 @@ private static List<String> extract(Iterable<String> generator) {
private static String generateOptionsSwitch(List<OptionSpec> argOptions, List<OptionSpec> enumOptions) {
StringBuilder buff = new StringBuilder(1024);
buff.append("\n");
buff.append(" compopt +o default\n");
buff.append("\n");
buff.append(" case ${CURR_WORD} in\n"); // outer case
String outerCases = generateOptionsCases(argOptions, enumOptions, "", "\"\"");
if (outerCases.length() == 0) {
Expand Down Expand Up @@ -559,6 +561,10 @@ private static String generateOptionsCases(List<OptionSpec> argOptionFields, Lis
buff.append(format("%s COMPREPLY=( $( compgen -A hostname -- %s ) )\n", indent, currWord));
buff.append(format("%s return $?\n", indent));
buff.append(format("%s ;;\n", indent));
} else {
buff.append(format("%s %s)\n", indent, concat("|", option.names()))); // no completions available
buff.append(format("%s return\n", indent));
buff.append(format("%s ;;\n", indent));
}
}
return buff.toString();
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/picocli/AutoCompleteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -396,14 +396,22 @@ public void testAutoCompleteAppBothScriptFilesForceOverwrite() throws Exception
" FLAG_OPTS=\"-w --writeCommandScript -f --force -h --help\"\n" +
" ARG_OPTS=\"-n --name -o --completionScript\"\n" +
"\n" +
" compopt +o default\n" +
"\n" +
" case ${CURR_WORD} in\n" +
" -n|--name)\n" +
" return\n" +
" ;;\n" +
" -o|--completionScript)\n" +
" compopt -o filenames\n" +
" COMPREPLY=( $( compgen -f -- \"\" ) ) # files\n" +
" return $?\n" +
" ;;\n" +
" *)\n" +
" case ${PREV_WORD} in\n" +
" -n|--name)\n" +
" return\n" +
" ;;\n" +
" -o|--completionScript)\n" +
" compopt -o filenames\n" +
" COMPREPLY=( $( compgen -f -- $CURR_WORD ) ) # files\n" +
Expand Down
8 changes: 8 additions & 0 deletions src/test/resources/basic.bash
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,25 @@ function _picocli_basicExample() {
ARG_OPTS="-u --timeUnit -t --timeout"
timeUnit_OPTION_ARGS="%2$s" # TimeUnit values

compopt +o default

case ${CURR_WORD} in
-u|--timeUnit)
COMPREPLY=( $( compgen -W "${timeUnit_OPTION_ARGS}" -- "" ) )
return $?
;;
-t|--timeout)
return
;;
*)
case ${PREV_WORD} in
-u|--timeUnit)
COMPREPLY=( $( compgen -W "${timeUnit_OPTION_ARGS}" -- $CURR_WORD ) )
return $?
;;
-t|--timeout)
return
;;
esac
esac

Expand Down
28 changes: 28 additions & 0 deletions src/test/resources/hyphenated_completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,20 @@ function _picocli_rcmd_sub1() {
FLAG_OPTS="flag1 -h --help -V --version"
ARG_OPTS="option1"

compopt +o default

case ${CURR_WORD} in
option1)
return
;;
*)
case ${PREV_WORD} in
option1)
return
;;
esac
esac

if [[ "${CURR_WORD}" == -* ]]; then
COMPREPLY=( $(compgen -W "${FLAG_OPTS} ${ARG_OPTS}" -- ${CURR_WORD}) )
else
Expand All @@ -127,6 +141,20 @@ function _picocli_rcmd_sub2() {
FLAG_OPTS="flag-2 -h --help -V --version"
ARG_OPTS="option-2"

compopt +o default

case ${CURR_WORD} in
option-2)
return
;;
*)
case ${PREV_WORD} in
option-2)
return
;;
esac
esac

if [[ "${CURR_WORD}" == -* ]]; then
COMPREPLY=( $(compgen -W "${FLAG_OPTS} ${ARG_OPTS}" -- ${CURR_WORD}) )
else
Expand Down
32 changes: 32 additions & 0 deletions src/test/resources/picocompletion-demo_completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,27 @@ function _picocli_picocompletion-demo_sub1() {
ARG_OPTS="--num --str --candidates"
str2_OPTION_ARGS="a b c" # --candidates values

compopt +o default

case ${CURR_WORD} in
--num)
return
;;
--str)
return
;;
--candidates)
COMPREPLY=( $( compgen -W "${str2_OPTION_ARGS}" -- "" ) )
return $?
;;
*)
case ${PREV_WORD} in
--num)
return
;;
--str)
return
;;
--candidates)
COMPREPLY=( $( compgen -W "${str2_OPTION_ARGS}" -- $CURR_WORD ) )
return $?
Expand All @@ -146,14 +160,22 @@ function _picocli_picocompletion-demo_sub2() {
FLAG_OPTS=""
ARG_OPTS="--num2 --directory -d"

compopt +o default

case ${CURR_WORD} in
--num2)
return
;;
--directory|-d)
compopt -o filenames
COMPREPLY=( $( compgen -f -- "" ) ) # files
return $?
;;
*)
case ${PREV_WORD} in
--num2)
return
;;
--directory|-d)
compopt -o filenames
COMPREPLY=( $( compgen -f -- $CURR_WORD ) ) # files
Expand All @@ -179,6 +201,8 @@ function _picocli_picocompletion-demo_sub2_subsub1() {
FLAG_OPTS=""
ARG_OPTS="-h --host"

compopt +o default

case ${CURR_WORD} in
-h|--host)
compopt -o filenames
Expand Down Expand Up @@ -213,17 +237,25 @@ function _picocli_picocompletion-demo_sub2_subsub2() {
ARG_OPTS="-u --timeUnit -t --timeout"
timeUnit_OPTION_ARGS="%2$s" # TimeUnit values

compopt +o default

case ${CURR_WORD} in
-u|--timeUnit)
COMPREPLY=( $( compgen -W "${timeUnit_OPTION_ARGS}" -- "" ) )
return $?
;;
-t|--timeout)
return
;;
*)
case ${PREV_WORD} in
-u|--timeUnit)
COMPREPLY=( $( compgen -W "${timeUnit_OPTION_ARGS}" -- $CURR_WORD ) )
return $?
;;
-t|--timeout)
return
;;
esac
esac

Expand Down

0 comments on commit ca98f95

Please sign in to comment.