Skip to content

Commit f7a8d94

Browse files
author
Damien Nadé
committed
argsparse-completion.sh: support for -fvalue and --foo=value completion schemes
1 parent f3d10ef commit f7a8d94

1 file changed

Lines changed: 27 additions & 7 deletions

File tree

argsparse-completion.sh

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,18 @@
2828
#
2929

3030
__argsparse_compgen() {
31+
if [[ -v compgen_prefix ]]
32+
then
33+
set -- "$@" -P "$compgen_prefix"
34+
fi
3135
compgen "$@" -- "$cur"
3236
}
3337

3438
__argsparse_complete_value() {
35-
local option=$1
36-
local array option_type
39+
local option array option_type
3740
local -a values
41+
option=$(__argsparse_complete_get_long "$prev" "${longs[@]}") && \
42+
argsparse_has_option_property "$long" value || return 1
3843
if array=$(__argsparse_values_array_identifier "$option")
3944
then
4045
values=( ${!array} )
@@ -72,15 +77,17 @@ __argsparse_complete_get_long() {
7277
local -a longs=( "$@" )
7378
local long
7479
if [[ $word = -+([!-]) ]] && \
75-
long=$(argsparse_short_to_long "${word:${#word}-1}")
80+
long=$(argsparse_short_to_long "${word:1:1}") # XXX: change this
7681
then
77-
printf %s "$long"
82+
long=$long
7883
elif __argsparse_index_of "$word" "${longs[@]}" >/dev/null
7984
then
80-
printf %s "${word#--}"
85+
long=${word#--}
8186
else
8287
return 1
8388
fi
89+
printf %s "$long"
90+
argsparse_has_option_property "$long" value
8491
}
8592

8693
__argsparse_complete() {
@@ -96,12 +103,25 @@ __argsparse_complete() {
96103
then
97104
# Complete positionnal arguments
98105
__argsparse_compgen -A file
99-
elif long=$(__argsparse_complete_get_long "$prev" "${longs[@]}") && \
100-
argsparse_has_option_property "$long" value
106+
elif long=$(__argsparse_complete_get_long "$prev" "${longs[@]}")
101107
then
102108
__argsparse_complete_value "$long"
103109
else
104110
case "$cur" in
111+
--?*=*|-[!-]?*)
112+
# Complete the --foo=something pattern as if
113+
# prev=--foo and cur=something
114+
# Complete -fsomething as if prev=-f and cur=something
115+
if [[ "$cur" =~ ^((--[^=]+)=)(.*)$ || "$cur" =~ ^((-[^-]))(.*)$ ]]
116+
then
117+
compgen_prefix=${BASH_REMATCH[1]}
118+
option=${BASH_REMATCH[2]}
119+
cur=${BASH_REMATCH[3]}
120+
long=$(
121+
__argsparse_complete_get_long "$option" "${longs[@]}") && \
122+
__argsparse_complete_value "$long"
123+
fi
124+
;;
105125
""|-)
106126
shorts=( "${!__argsparse_short_options[@]}" )
107127
shorts=( "${shorts[@]/#/-}" )

0 commit comments

Comments
 (0)