Skip to content

Commit 176de2c

Browse files
jsotuyodremkop
authored andcommitted
Update tests to pass
1 parent b946871 commit 176de2c

6 files changed

+349
-54
lines changed

src/test/java/picocli/AutoCompleteTest.java

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,41 @@ private String expectedCompletionScriptForAutoCompleteApp() {
756756
" echo \"$result\"\n" +
757757
"}\n" +
758758
"\n" +
759+
"# compReplyArray generates a list of completion suggestions based on an array, ensuring all values are properly escaped.\n" +
760+
"#\n" +
761+
"# compReplyArray takes a single parameter: the array of options to be displayed\n" +
762+
"#\n" +
763+
"# The output is echoed to std_out, one option per line.\n" +
764+
"#\n" +
765+
"# Example usage:\n" +
766+
"# local options=(\"foo\", \"bar\", \"baz\")\n" +
767+
"# local IFS=$'\\n'\n" +
768+
"# COMPREPLY=$(compReplyArray \"${options[@]}\")\n" +
769+
"function compReplyArray() {\n" +
770+
" declare -a options\n" +
771+
" options=(\"$@\")\n" +
772+
" local curr_word=${COMP_WORDS[COMP_CWORD]}\n" +
773+
" local i\n" +
774+
" local quoted\n" +
775+
" local optionList=()\n" +
776+
"\n" +
777+
" for (( i=0; i<${#options[@]}; i++ )); do\n" +
778+
" # Double escape, since we want escaped values, but compgen -W expands the argument\n" +
779+
" printf -v quoted %%q \"${options[i]}\"\n" +
780+
" quoted=\\'${quoted//\\'/\\'\\\\\\'\\'}\\'\n" +
781+
"\n" +
782+
" optionList[i]=$quoted\n" +
783+
" done\n" +
784+
"\n" +
785+
" # We also have to add another round of escaping to $curr_word.\n" +
786+
" curr_word=${curr_word//\\\\/\\\\\\\\}\n" +
787+
" curr_word=${curr_word//\\'/\\\\\\'}\n" +
788+
"\n" +
789+
" # Actually generate completions.\n" +
790+
" local IFS=$'\\n'\n" +
791+
" echo -e \"$(compgen -W \"${optionList[*]}\" -- \"$curr_word\")\"\n" +
792+
"}\n" +
793+
"\n" +
759794
"# Bash completion entry point function.\n" +
760795
"# _complete_picocli.AutoComplete finds which commands and subcommands have been specified\n" +
761796
"# on the command line and delegates to the appropriate function\n" +
@@ -974,6 +1009,41 @@ private String expectedCompletionScriptForNonDefault() {
9741009
" echo \"$result\"\n" +
9751010
"}\n" +
9761011
"\n" +
1012+
"# compReplyArray generates a list of completion suggestions based on an array, ensuring all values are properly escaped.\n" +
1013+
"#\n" +
1014+
"# compReplyArray takes a single parameter: the array of options to be displayed\n" +
1015+
"#\n" +
1016+
"# The output is echoed to std_out, one option per line.\n" +
1017+
"#\n" +
1018+
"# Example usage:\n" +
1019+
"# local options=(\"foo\", \"bar\", \"baz\")\n" +
1020+
"# local IFS=$'\\n'\n" +
1021+
"# COMPREPLY=$(compReplyArray \"${options[@]}\")\n" +
1022+
"function compReplyArray() {\n" +
1023+
" declare -a options\n" +
1024+
" options=(\"$@\")\n" +
1025+
" local curr_word=${COMP_WORDS[COMP_CWORD]}\n" +
1026+
" local i\n" +
1027+
" local quoted\n" +
1028+
" local optionList=()\n" +
1029+
"\n" +
1030+
" for (( i=0; i<${#options[@]}; i++ )); do\n" +
1031+
" # Double escape, since we want escaped values, but compgen -W expands the argument\n" +
1032+
" printf -v quoted %%q \"${options[i]}\"\n" +
1033+
" quoted=\\'${quoted//\\'/\\'\\\\\\'\\'}\\'\n" +
1034+
"\n" +
1035+
" optionList[i]=$quoted\n" +
1036+
" done\n" +
1037+
"\n" +
1038+
" # We also have to add another round of escaping to $curr_word.\n" +
1039+
" curr_word=${curr_word//\\\\/\\\\\\\\}\n" +
1040+
" curr_word=${curr_word//\\'/\\\\\\'}\n" +
1041+
"\n" +
1042+
" # Actually generate completions.\n" +
1043+
" local IFS=$'\\n'\n" +
1044+
" echo -e \"$(compgen -W \"${optionList[*]}\" -- \"$curr_word\")\"\n" +
1045+
"}\n" +
1046+
"\n" +
9771047
"# Bash completion entry point function.\n" +
9781048
"# _complete_nondefault finds which commands and subcommands have been specified\n" +
9791049
"# on the command line and delegates to the appropriate function\n" +
@@ -1540,6 +1610,41 @@ private String getCompletionScriptText(String cmdName) {
15401610
" echo \"$result\"\n" +
15411611
"}\n" +
15421612
"\n" +
1613+
"# compReplyArray generates a list of completion suggestions based on an array, ensuring all values are properly escaped.\n" +
1614+
"#\n" +
1615+
"# compReplyArray takes a single parameter: the array of options to be displayed\n" +
1616+
"#\n" +
1617+
"# The output is echoed to std_out, one option per line.\n" +
1618+
"#\n" +
1619+
"# Example usage:\n" +
1620+
"# local options=(\"foo\", \"bar\", \"baz\")\n" +
1621+
"# local IFS=$'\\n'\n" +
1622+
"# COMPREPLY=$(compReplyArray \"${options[@]}\")\n" +
1623+
"function compReplyArray() {\n" +
1624+
" declare -a options\n" +
1625+
" options=(\"$@\")\n" +
1626+
" local curr_word=${COMP_WORDS[COMP_CWORD]}\n" +
1627+
" local i\n" +
1628+
" local quoted\n" +
1629+
" local optionList=()\n" +
1630+
"\n" +
1631+
" for (( i=0; i<${#options[@]}; i++ )); do\n" +
1632+
" # Double escape, since we want escaped values, but compgen -W expands the argument\n" +
1633+
" printf -v quoted %%q \"${options[i]}\"\n" +
1634+
" quoted=\\'${quoted//\\'/\\'\\\\\\'\\'}\\'\n" +
1635+
"\n" +
1636+
" optionList[i]=$quoted\n" +
1637+
" done\n" +
1638+
"\n" +
1639+
" # We also have to add another round of escaping to $curr_word.\n" +
1640+
" curr_word=${curr_word//\\\\/\\\\\\\\}\n" +
1641+
" curr_word=${curr_word//\\'/\\\\\\'}\n" +
1642+
"\n" +
1643+
" # Actually generate completions.\n" +
1644+
" local IFS=$'\\n'\n" +
1645+
" echo -e \"$(compgen -W \"${optionList[*]}\" -- \"$curr_word\")\"\n" +
1646+
"}\n" +
1647+
"\n" +
15431648
"# Bash completion entry point function.\n" +
15441649
"# _complete_%1$s finds which commands and subcommands have been specified\n" +
15451650
"# on the command line and delegates to the appropriate function\n" +
@@ -1752,6 +1857,41 @@ private String getCompletionScriptTextWithHidden(String commandName) {
17521857
" echo \"$result\"\n" +
17531858
"}\n" +
17541859
"\n" +
1860+
"# compReplyArray generates a list of completion suggestions based on an array, ensuring all values are properly escaped.\n" +
1861+
"#\n" +
1862+
"# compReplyArray takes a single parameter: the array of options to be displayed\n" +
1863+
"#\n" +
1864+
"# The output is echoed to std_out, one option per line.\n" +
1865+
"#\n" +
1866+
"# Example usage:\n" +
1867+
"# local options=(\"foo\", \"bar\", \"baz\")\n" +
1868+
"# local IFS=$'\\n'\n" +
1869+
"# COMPREPLY=$(compReplyArray \"${options[@]}\")\n" +
1870+
"function compReplyArray() {\n" +
1871+
" declare -a options\n" +
1872+
" options=(\"$@\")\n" +
1873+
" local curr_word=${COMP_WORDS[COMP_CWORD]}\n" +
1874+
" local i\n" +
1875+
" local quoted\n" +
1876+
" local optionList=()\n" +
1877+
"\n" +
1878+
" for (( i=0; i<${#options[@]}; i++ )); do\n" +
1879+
" # Double escape, since we want escaped values, but compgen -W expands the argument\n" +
1880+
" printf -v quoted %%q \"${options[i]}\"\n" +
1881+
" quoted=\\'${quoted//\\'/\\'\\\\\\'\\'}\\'\n" +
1882+
"\n" +
1883+
" optionList[i]=$quoted\n" +
1884+
" done\n" +
1885+
"\n" +
1886+
" # We also have to add another round of escaping to $curr_word.\n" +
1887+
" curr_word=${curr_word//\\\\/\\\\\\\\}\n" +
1888+
" curr_word=${curr_word//\\'/\\\\\\'}\n" +
1889+
"\n" +
1890+
" # Actually generate completions.\n" +
1891+
" local IFS=$'\\n'\n" +
1892+
" echo -e \"$(compgen -W \"${optionList[*]}\" -- \"$curr_word\")\"\n" +
1893+
"}\n" +
1894+
"\n" +
17551895
"# Bash completion entry point function.\n" +
17561896
"# _complete_%1$s finds which commands and subcommands have been specified\n" +
17571897
"# on the command line and delegates to the appropriate function\n" +

src/test/resources/bashify_completion.bash

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,41 @@ function currentPositionalIndex() {
113113
echo "$result"
114114
}
115115

116+
# compReplyArray generates a list of completion suggestions based on an array, ensuring all values are properly escaped.
117+
#
118+
# compReplyArray takes a single parameter: the array of options to be displayed
119+
#
120+
# The output is echoed to std_out, one option per line.
121+
#
122+
# Example usage:
123+
# local options=("foo", "bar", "baz")
124+
# local IFS=$'\n'
125+
# COMPREPLY=$(compReplyArray "${options[@]}")
126+
function compReplyArray() {
127+
declare -a options
128+
options=("$@")
129+
local curr_word=${COMP_WORDS[COMP_CWORD]}
130+
local i
131+
local quoted
132+
local optionList=()
133+
134+
for (( i=0; i<${#options[@]}; i++ )); do
135+
# Double escape, since we want escaped values, but compgen -W expands the argument
136+
printf -v quoted %%q "${options[i]}"
137+
quoted=\'${quoted//\'/\'\\\'\'}\'
138+
139+
optionList[i]=$quoted
140+
done
141+
142+
# We also have to add another round of escaping to $curr_word.
143+
curr_word=${curr_word//\\/\\\\}
144+
curr_word=${curr_word//\'/\\\'}
145+
146+
# Actually generate completions.
147+
local IFS=$'\n'
148+
echo -e "$(compgen -W "${optionList[*]}" -- "$curr_word")"
149+
}
150+
116151
# Bash completion entry point function.
117152
# _complete_bashify finds which commands and subcommands have been specified
118153
# on the command line and delegates to the appropriate function
@@ -143,7 +178,7 @@ function _picocli_bashify() {
143178
case ${prev_word} in
144179
-x)
145180
local IFS=$'\n'
146-
COMPREPLY=( $( compgen -W "${_AB_C_option_args[*]}" -- "${curr_word}" ) )
181+
COMPREPLY=( $( compReplyArray "${_AB_C_option_args[@]}" ) )
147182
return $?
148183
;;
149184
esac

src/test/resources/basic.bash

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,41 @@ function currentPositionalIndex() {
113113
echo "$result"
114114
}
115115

116+
# compReplyArray generates a list of completion suggestions based on an array, ensuring all values are properly escaped.
117+
#
118+
# compReplyArray takes a single parameter: the array of options to be displayed
119+
#
120+
# The output is echoed to std_out, one option per line.
121+
#
122+
# Example usage:
123+
# local options=("foo", "bar", "baz")
124+
# local IFS=$'\n'
125+
# COMPREPLY=$(compReplyArray "${options[@]}")
126+
function compReplyArray() {
127+
declare -a options
128+
options=("$@")
129+
local curr_word=${COMP_WORDS[COMP_CWORD]}
130+
local i
131+
local quoted
132+
local optionList=()
133+
134+
for (( i=0; i<${#options[@]}; i++ )); do
135+
# Double escape, since we want escaped values, but compgen -W expands the argument
136+
printf -v quoted %%q "${options[i]}"
137+
quoted=\'${quoted//\'/\'\\\'\'}\'
138+
139+
optionList[i]=$quoted
140+
done
141+
142+
# We also have to add another round of escaping to $curr_word.
143+
curr_word=${curr_word//\\/\\\\}
144+
curr_word=${curr_word//\'/\\\'}
145+
146+
# Actually generate completions.
147+
local IFS=$'\n'
148+
echo -e "$(compgen -W "${optionList[*]}" -- "$curr_word")"
149+
}
150+
116151
# Bash completion entry point function.
117152
# _complete_basicExample finds which commands and subcommands have been specified
118153
# on the command line and delegates to the appropriate function
@@ -143,7 +178,7 @@ function _picocli_basicExample() {
143178
case ${prev_word} in
144179
-u|--timeUnit)
145180
local IFS=$'\n'
146-
COMPREPLY=( $( compgen -W "${timeUnit_option_args[*]}" -- "${curr_word}" ) )
181+
COMPREPLY=( $( compReplyArray "${timeUnit_option_args[@]}" ) )
147182
return $?
148183
;;
149184
-t|--timeout)

src/test/resources/hyphenated_completion.bash

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,41 @@ function currentPositionalIndex() {
113113
echo "$result"
114114
}
115115

116+
# compReplyArray generates a list of completion suggestions based on an array, ensuring all values are properly escaped.
117+
#
118+
# compReplyArray takes a single parameter: the array of options to be displayed
119+
#
120+
# The output is echoed to std_out, one option per line.
121+
#
122+
# Example usage:
123+
# local options=("foo", "bar", "baz")
124+
# local IFS=$'\n'
125+
# COMPREPLY=$(compReplyArray "${options[@]}")
126+
function compReplyArray() {
127+
declare -a options
128+
options=("$@")
129+
local curr_word=${COMP_WORDS[COMP_CWORD]}
130+
local i
131+
local quoted
132+
local optionList=()
133+
134+
for (( i=0; i<${#options[@]}; i++ )); do
135+
# Double escape, since we want escaped values, but compgen -W expands the argument
136+
printf -v quoted %%q "${options[i]}"
137+
quoted=\'${quoted//\'/\'\\\'\'}\'
138+
139+
optionList[i]=$quoted
140+
done
141+
142+
# We also have to add another round of escaping to $curr_word.
143+
curr_word=${curr_word//\\/\\\\}
144+
curr_word=${curr_word//\'/\\\'}
145+
146+
# Actually generate completions.
147+
local IFS=$'\n'
148+
echo -e "$(compgen -W "${optionList[*]}" -- "$curr_word")"
149+
}
150+
116151
# Bash completion entry point function.
117152
# _complete_rcmd finds which commands and subcommands have been specified
118153
# on the command line and delegates to the appropriate function

0 commit comments

Comments
 (0)