Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions core/cli_root/autocomplete_helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,13 @@ _mycli_extract_parameters() {
# _mycli_extract_parameters "$usage" # --> "--foo --help --some-flag"
local -r usage=$1

# Extract the parameters that:
# - start with a dash ("-")
# - are not preceded by any of "[a-zA-Z0-9_<" ("[" must appear in the beginning of the negation "[^...]")
# - end before any of "] =,": "--foo --foo=42 --bar]" or "-f, --foo" (only appears in the options)
# - exclude the 'parameter' "--"
# Normalize separators used in docopt alternatives/groups, split by whitespace, then extract
# option-like tokens while ignoring plain words that contain dashes (for example: "some-command").
echo "$usage" |
sed 's/^/ /' |
grep -oE -- '[^[a-zA-Z0-9_<]-[^] =,]+' |
sed 's/^[[:space:]]*//' |
grep -vE '^--$' || :
tr '()|' ' ' |
tr -s '[:space:]' '\n' |
sed -E 's/^\[+// ; s/[=,].*$// ; s/\]+$//' |
grep -E -- '^--?[[:alnum:]_][[:alnum:]_-]*$' || :
Comment on lines +94 to +95
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably we won't have this usage.

}

_mycli_extract_additional_commands() {
Expand Down Expand Up @@ -260,7 +257,10 @@ _mycli_get_arg_description() {
# the function `_mycli_extract_parameter_names`.
local -r docopt_options_first_lines=$(sed -E '/^[[:space:]]{6,}/d' <<<"$docopt_options")

if line_number=$(grep -nE -- "(^| )$arg( |$)" <<<"$parameter_names_in_options" | cut -d: -f1); then
# Use exact token matching instead of regex so arguments containing regex characters
# never trigger parsing errors (for example: "(--my").
line_number=$(awk -v needle="$arg" '{for (i = 1; i <= NF; i++) if ($i == needle) {print NR; exit}}' <<<"$parameter_names_in_options")
if [[ -n "$line_number" ]]; then
sed -n "${line_number}p" <<<"$docopt_options_first_lines" |
sed 's/^[[:space:]]*//' |
grep -Eo ' {2,}.*' |
Expand Down
14 changes: 14 additions & 0 deletions tests/core/cli_root/test_autocomplete_helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ EOF
)
assertEquals "$expected" "$result"

# Parenthesized option groups should not leak parentheses into parameter names.
result=$(_mycli_extract_parameters "foo bar baz (--name=NAME | --id=ID)")
expected=$(
cat <<-EOF
--name
--id
EOF
)
assertEquals "$expected" "$result"

# Concatenating the usage line and the "Options" section
result=$(_mycli_extract_parameters "$(echo -e "foo bar --foo Options:\n--bar Some description\n-f, --foo")")
expected=$(
Expand Down Expand Up @@ -262,6 +272,10 @@ test__mycli_get_arg_description() {
expected=""
assertEquals "$expected" "$result"

result=$(_mycli_get_arg_description "(--my" "$DOCOPT_OPTIONS" "$PARAMETER_NAMES_IN_OPTIONS")
expected=""
assertEquals "$expected" "$result"

result=$(_mycli_get_arg_description "x" "$DOCOPT_OPTIONS" "$PARAMETER_NAMES_IN_OPTIONS")
expected=""
assertEquals "$expected" "$result"
Expand Down
Loading