Skip to content

Commit

Permalink
[#1464] Fix Bash error compopt: command not found
Browse files Browse the repository at this point in the history
Closes #1464
  • Loading branch information
remkop committed Jan 18, 2022
1 parent 280c765 commit abd0180
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 65 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Picocli follows [semantic versioning](http://semver.org/).
* [#1531] Bugfix: Options defined as annotated methods should reset between `parseArgs` invocations when `CommandLine` instance is reused. Thanks to [kaushalkumar](https://github.com/kaushalkumar) for raising this.
* [#1458][#1473] Enhancement: autocompletion now supports file names containing spaces. Thanks to [zpater345](https://github.com/zpater345) for raising this and thanks to [NewbieOrange](https://github.com/NewbieOrange) for the pull request.
* [#1477] Enhancement: Remove file name extension and local dir prefix from the command name in generated autocomplete scripts. Thanks to [Andrea Peruffo](https://github.com/andreaTP) for the pull request.
* [#1464] Enhancement: Fix Bash error `compopt: command not found` on older versions Bash. Thanks to [Andres Almiray](https://github.com/aalmiray) for raising this.
* [#1476] Enhancement: improve error message in `AbstractCommandSpecProcessor#extractTypedMember`. Thanks to [Ross Goldberg](https://github.com/rgoldberg) for raising this.
* [#1475] Enhancement: Fix typo in annotation target-type error message. Thanks to [Ross Goldberg](https://github.com/rgoldberg) for the pull request.
* [#1366][#1370] Enhancement: show in usage help that the built-in `help` command only works on the first argument. Thanks to [Patrice Duroux](https://github.com/peutch) for the pull request.
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/picocli/AutoComplete.java
Original file line number Diff line number Diff line change
Expand Up @@ -731,11 +731,11 @@ private static String generatePositionalParamsCases(List<PositionalParamSpec> po
} 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 type compopt &>/dev/null && compopt -o filenames\n", indent)); // #1464 workaround for old bash
buff.append(format("%s positionals=$( compgen -f -- \"%s\" ) # files\n", indent, currWord));
} else if (type.equals(InetAddress.class)) {
buff.append(format("%s %s (( currIndex >= %d && currIndex <= %d )); then\n", indent, ifOrElif, min, max));
buff.append(format("%s compopt -o filenames\n", indent));
buff.append(format("%s type compopt &>/dev/null && compopt -o filenames\n", indent)); // #1464 workaround for old bash
buff.append(format("%s positionals=$( compgen -A hostname -- \"%s\" )\n", indent, currWord));
}
}
Expand All @@ -753,7 +753,7 @@ private static String generateOptionsSwitch(List<OptionSpec> argOptions) {
}

return "\n"
+ " compopt +o default\n"
+ " type compopt &>/dev/null && compopt +o default\n" // #1464 workaround for old bash
+ "\n"
+ " case ${prev_word} in\n"
+ optionsCases
Expand All @@ -776,13 +776,13 @@ private static String generateOptionsCases(List<OptionSpec> argOptionFields, Str
} 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 type compopt &>/dev/null && compopt -o filenames\n", indent)); // #1464 workaround for old bash
buff.append(format("%s COMPREPLY=( $( compgen -f -- \"%s\" ) ) # files\n", indent, currWord));
buff.append(format("%s return $?\n", indent));
buff.append(format("%s ;;\n", indent));
} else if (type.equals(InetAddress.class)) {
buff.append(format("%s %s)\n", indent, concat("|", option.names()))); // " -h|--host)\n"
buff.append(format("%s compopt -o filenames\n", indent));
buff.append(format("%s type compopt &>/dev/null && compopt -o filenames\n", indent)); // #1464 workaround for old bash
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));
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/picocli/AutoCompleteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ private String expectedCompletionScriptForAutoCompleteApp() {
" local flag_opts=\"-w --writeCommandScript -f --force -h --help -V --version\"\n" +
" local arg_opts=\"-c --factory -n --name -o --completionScript\"\n" +
"\n" +
" compopt +o default\n" +
" type compopt &>/dev/null && compopt +o default\n" +
"\n" +
" case ${prev_word} in\n" +
" -c|--factory)\n" +
Expand All @@ -781,7 +781,7 @@ private String expectedCompletionScriptForAutoCompleteApp() {
" ;;\n" +
" -o|--completionScript)\n" +
" local IFS=$'\\n'\n" +
" compopt -o filenames\n" +
" type compopt &>/dev/null && compopt -o filenames\n" +
" COMPREPLY=( $( compgen -f -- \"${curr_word}\" ) ) # files\n" +
" return $?\n" +
" ;;\n" +
Expand Down Expand Up @@ -984,7 +984,7 @@ private String expectedCompletionScriptForNonDefault() {
" local flag_opts=\"\"\n" +
" local arg_opts=\"-t --timeout\"\n" +
"\n" +
" compopt +o default\n" +
" type compopt &>/dev/null && compopt +o default\n" +
"\n" +
" case ${prev_word} in\n" +
" -t|--timeout)\n" +
Expand Down Expand Up @@ -1754,7 +1754,7 @@ private String getCompletionScriptTextWithHidden(String commandName) {
" local flag_opts=\"\"\n" +
" local arg_opts=\"--apples --bbb\"\n" + // NOTE: no --aaa: this option is hidden
"\n" +
" compopt +o default\n" +
" type compopt &>/dev/null && compopt +o default\n" +
"\n" +
" case ${prev_word} in\n" +
" --apples)\n" +
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/bashify_completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function _picocli_bashify() {
local arg_opts="-x"
local _AB_C_option_args="1" # -x values

compopt +o default
type compopt &>/dev/null && compopt +o default

case ${prev_word} in
-x)
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/basic.bash
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function _picocli_basicExample() {
local arg_opts="-u --timeUnit -t --timeout"
local timeUnit_option_args="%2$s" # --timeUnit values

compopt +o default
type compopt &>/dev/null && compopt +o default

case ${prev_word} in
-u|--timeUnit)
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/hyphenated_completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ function _picocli_rcmd_sub1() {
local flag_opts="flag1 -h --help -V --version"
local arg_opts="option1"

compopt +o default
type compopt &>/dev/null && compopt +o default

case ${prev_word} in
option1)
Expand All @@ -181,7 +181,7 @@ function _picocli_rcmd_sub2() {
local flag_opts="flag-2 -h --help -V --version"
local arg_opts="option-2"

compopt +o default
type compopt &>/dev/null && compopt +o default

case ${prev_word} in
option-2)
Expand Down
52 changes: 26 additions & 26 deletions src/test/resources/picocompletion-demo-help_completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ function _picocli_picocompletion-demo-help_sub1() {
local arg_opts="--num --str --candidates"
local str2_option_args="aaa bbb ccc" # --candidates values

compopt +o default
type compopt &>/dev/null && compopt +o default

case ${prev_word} in
--num)
Expand Down Expand Up @@ -220,7 +220,7 @@ function _picocli_picocompletion-demo-help_sub1alias() {
local arg_opts="--num --str --candidates"
local str2_option_args="aaa bbb ccc" # --candidates values

compopt +o default
type compopt &>/dev/null && compopt +o default

case ${prev_word} in
--num)
Expand Down Expand Up @@ -253,15 +253,15 @@ function _picocli_picocompletion-demo-help_sub2() {
local flag_opts=""
local arg_opts="--num2 --directory -d"

compopt +o default
type compopt &>/dev/null && compopt +o default

case ${prev_word} in
--num2)
return
;;
--directory|-d)
local IFS=$'\n'
compopt -o filenames
type compopt &>/dev/null && compopt -o filenames
COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
return $?
;;
Expand Down Expand Up @@ -291,15 +291,15 @@ function _picocli_picocompletion-demo-help_sub2alias() {
local flag_opts=""
local arg_opts="--num2 --directory -d"

compopt +o default
type compopt &>/dev/null && compopt +o default

case ${prev_word} in
--num2)
return
;;
--directory|-d)
local IFS=$'\n'
compopt -o filenames
type compopt &>/dev/null && compopt -o filenames
COMPREPLY=( $( compgen -f -- "${curr_word}" ) ) # files
return $?
;;
Expand Down Expand Up @@ -346,11 +346,11 @@ function _picocli_picocompletion-demo-help_sub2_subsub1() {
local flag_opts=""
local arg_opts="-h --host"

compopt +o default
type compopt &>/dev/null && compopt +o default

case ${prev_word} in
-h|--host)
compopt -o filenames
type compopt &>/dev/null && compopt -o filenames
COMPREPLY=( $( compgen -A hostname -- "${curr_word}" ) )
return $?
;;
Expand All @@ -374,11 +374,11 @@ function _picocli_picocompletion-demo-help_sub2_sub2child1alias() {
local flag_opts=""
local arg_opts="-h --host"

compopt +o default
type compopt &>/dev/null && compopt +o default

case ${prev_word} in
-h|--host)
compopt -o filenames
type compopt &>/dev/null && compopt -o filenames
COMPREPLY=( $( compgen -A hostname -- "${curr_word}" ) )
return $?
;;
Expand All @@ -403,7 +403,7 @@ function _picocli_picocompletion-demo-help_sub2_subsub2() {
local arg_opts="-u --timeUnit -t --timeout"
local timeUnit_option_args="%2$s" # --timeUnit values

compopt +o default
type compopt &>/dev/null && compopt +o default

case ${prev_word} in
-u|--timeUnit)
Expand Down Expand Up @@ -440,7 +440,7 @@ function _picocli_picocompletion-demo-help_sub2_sub2child2alias() {
local arg_opts="-u --timeUnit -t --timeout"
local timeUnit_option_args="%2$s" # --timeUnit values

compopt +o default
type compopt &>/dev/null && compopt +o default

case ${prev_word} in
-u|--timeUnit)
Expand Down Expand Up @@ -486,10 +486,10 @@ function _picocli_picocompletion-demo-help_sub2_subsub3() {
positionals=$( compgen -W "$cands_pos_param_args" -- "${curr_word}" )
elif (( currIndex >= 1 && currIndex <= 2 )); then
local IFS=$'\n'
compopt -o filenames
type compopt &>/dev/null && compopt -o filenames
positionals=$( compgen -f -- "${curr_word}" ) # files
elif (( currIndex >= 3 && currIndex <= 2147483647 )); then
compopt -o filenames
type compopt &>/dev/null && compopt -o filenames
positionals=$( compgen -A hostname -- "${curr_word}" )
fi
COMPREPLY=( $(compgen -W "${commands} ${positionals}" -- "${curr_word}") )
Expand All @@ -516,10 +516,10 @@ function _picocli_picocompletion-demo-help_sub2_sub2child3alias() {
positionals=$( compgen -W "$cands_pos_param_args" -- "${curr_word}" )
elif (( currIndex >= 1 && currIndex <= 2 )); then
local IFS=$'\n'
compopt -o filenames
type compopt &>/dev/null && compopt -o filenames
positionals=$( compgen -f -- "${curr_word}" ) # files
elif (( currIndex >= 3 && currIndex <= 2147483647 )); then
compopt -o filenames
type compopt &>/dev/null && compopt -o filenames
positionals=$( compgen -A hostname -- "${curr_word}" )
fi
COMPREPLY=( $(compgen -W "${commands} ${positionals}" -- "${curr_word}") )
Expand All @@ -536,11 +536,11 @@ function _picocli_picocompletion-demo-help_sub2alias_subsub1() {
local flag_opts=""
local arg_opts="-h --host"

compopt +o default
type compopt &>/dev/null && compopt +o default

case ${prev_word} in
-h|--host)
compopt -o filenames
type compopt &>/dev/null && compopt -o filenames
COMPREPLY=( $( compgen -A hostname -- "${curr_word}" ) )
return $?
;;
Expand All @@ -564,11 +564,11 @@ function _picocli_picocompletion-demo-help_sub2alias_sub2child1alias() {
local flag_opts=""
local arg_opts="-h --host"

compopt +o default
type compopt &>/dev/null && compopt +o default

case ${prev_word} in
-h|--host)
compopt -o filenames
type compopt &>/dev/null && compopt -o filenames
COMPREPLY=( $( compgen -A hostname -- "${curr_word}" ) )
return $?
;;
Expand All @@ -593,7 +593,7 @@ function _picocli_picocompletion-demo-help_sub2alias_subsub2() {
local arg_opts="-u --timeUnit -t --timeout"
local timeUnit_option_args="%2$s" # --timeUnit values

compopt +o default
type compopt &>/dev/null && compopt +o default

case ${prev_word} in
-u|--timeUnit)
Expand Down Expand Up @@ -630,7 +630,7 @@ function _picocli_picocompletion-demo-help_sub2alias_sub2child2alias() {
local arg_opts="-u --timeUnit -t --timeout"
local timeUnit_option_args="%2$s" # --timeUnit values

compopt +o default
type compopt &>/dev/null && compopt +o default

case ${prev_word} in
-u|--timeUnit)
Expand Down Expand Up @@ -676,10 +676,10 @@ function _picocli_picocompletion-demo-help_sub2alias_subsub3() {
positionals=$( compgen -W "$cands_pos_param_args" -- "${curr_word}" )
elif (( currIndex >= 1 && currIndex <= 2 )); then
local IFS=$'\n'
compopt -o filenames
type compopt &>/dev/null && compopt -o filenames
positionals=$( compgen -f -- "${curr_word}" ) # files
elif (( currIndex >= 3 && currIndex <= 2147483647 )); then
compopt -o filenames
type compopt &>/dev/null && compopt -o filenames
positionals=$( compgen -A hostname -- "${curr_word}" )
fi
COMPREPLY=( $(compgen -W "${commands} ${positionals}" -- "${curr_word}") )
Expand All @@ -706,10 +706,10 @@ function _picocli_picocompletion-demo-help_sub2alias_sub2child3alias() {
positionals=$( compgen -W "$cands_pos_param_args" -- "${curr_word}" )
elif (( currIndex >= 1 && currIndex <= 2 )); then
local IFS=$'\n'
compopt -o filenames
type compopt &>/dev/null && compopt -o filenames
positionals=$( compgen -f -- "${curr_word}" ) # files
elif (( currIndex >= 3 && currIndex <= 2147483647 )); then
compopt -o filenames
type compopt &>/dev/null && compopt -o filenames
positionals=$( compgen -A hostname -- "${curr_word}" )
fi
COMPREPLY=( $(compgen -W "${commands} ${positionals}" -- "${curr_word}") )
Expand Down
Loading

0 comments on commit abd0180

Please sign in to comment.