8383# # @details This function will just call compgen with given argument,
8484# # safely adding $cur in the command line. Also if compgen_prefix is
8585# # set, a -P option will be provided to compgen.
86+ # # @note __argsparse_compgen() makes use of the bash-completion
87+ # # standard variables.
8688# # @param param... any set of compgen options
87- # # @return
89+ # # @return compgen output and return code.
8890# # @ingroup ArgsparseCompletion
8991__argsparse_compgen () {
9092 if [[ -v compgen_prefix ]]
@@ -95,15 +97,23 @@ __argsparse_compgen() {
9597}
9698
9799# # @fn __argsparse_complete_value()
98- # # @brief complete a value
100+ # # @brief Complete the value an option.
101+ # # @details Run compgen with values matching given option. If an array
102+ # # "option_<optionname>_values" exists, complete with its values. Else
103+ # # if option has a type, complete values according to type when
104+ # # possible. Else do nothing.
105+ # # @note __argsparse_complete_value() makes use of the bash-completion
106+ # # standard variables.
107+ # # @param option a long option name.
99108# # @ingroup ArgsparseCompletion
100109__argsparse_complete_value () {
101- local option array option_type
110+ [[ $# -eq 1 ]] || return 1
111+ local option=$1
112+ local array option_type
102113 local -a values
103- option=$( __argsparse_complete_get_long " $prev " " ${longs[@]} " ) && \
104- argsparse_has_option_property " $long " value || return 1
105114 if array=$( __argsparse_values_array_identifier " $option " )
106115 then
116+ # Option accepts an enumeration of values.
107117 values=( " ${! array} " )
108118 __argsparse_compgen -W " ${values[*]} "
109119 elif option_type=$( argsparse_has_option_property " $option " type)
@@ -133,9 +143,15 @@ __argsparse_complete_value() {
133143}
134144
135145# # @fn __argsparse_complete_get_long()
136- # # @brief
137- # # @details
138- # # @param word
146+ # # @brief Find the option we want to complete.
147+ # # @details If given word parameter is a recognized option, print the
148+ # # matching long option name. Also if "$cur" should be this option
149+ # # value, then return 0.
150+ # # @param word any word.
151+ # # @return the long option matching given parameter.
152+ # # @retval 0 if given word matches an option and if that option
153+ # # accepts a value.
154+ # # @private
139155# # @ingroup ArgsparseCompletion
140156__argsparse_complete_get_long () {
141157 [[ $# -ge 1 ]] || return 1
@@ -146,20 +162,25 @@ __argsparse_complete_get_long() {
146162 if [[ $word = -+ ([! -]) ]] && \
147163 long=$( argsparse_short_to_long " ${word: 1: 1} " ) # XXX: change this
148164 then
165+ # -<char><something>, char being a recognized short option
166+ # which is wrong.
149167 long=$long
150168 elif __argsparse_index_of " $word " " ${longs[@]} " > /dev/null
151169 then
152170 long=${word# --}
153171 else
172+ # Unknown option
154173 return 1
155174 fi
156175 printf %s " $long "
157176 argsparse_has_option_property " $long " value
158177}
159178
160179# # @fn __argsparse_complete()
161- # # @brief
162- # # @details
180+ # # @brief Completion for the command stored in ${words[0]}.
181+ # # @details Will load the script to complete, and invoke compgen
182+ # # according to context.
183+ # # @retval non-zero if completed command cannot be sourced.
163184# # @ingroup ArgsparseCompletion
164185__argsparse_complete () {
165186 local script=${words[0]}
@@ -174,12 +195,15 @@ __argsparse_complete() {
174195 option=${prev# --}
175196 if __argsparse_index_of -- " ${words[@]: 0: ${# words[@]} -1} " > /dev/null
176197 then
177- # Complete positionnal arguments
198+ # We're after the -- parameter, complete positionnal arguments
178199 __argsparse_compgen -A file
179200 elif long=$( __argsparse_complete_get_long " $prev " " ${longs[@]} " )
180201 then
202+ # We're right after an option that accepts a value, so
203+ # complete a value.
181204 __argsparse_complete_value " $long "
182205 else
206+ # Complete current token
183207 case " $cur " in
184208 --?* =* |-[!-]?* )
185209 # Complete the --foo=something pattern as if
@@ -213,8 +237,15 @@ __argsparse_complete() {
213237}
214238
215239# # @fn _argsparse_complete()
216- # # @brief
217- # # @details
240+ # # @brief The argsparse completion function.
241+ # # @details To enable completion on a script that uses the argsparse
242+ # # library, call the "complete" built-in as following: @n
243+ # # @code
244+ # # complete -F _argsparse_complete <your script>
245+ # # @endcode
246+ # # @note Technically, this function gets a parameter (the name of the
247+ # # command to complete), but it is ignored.
248+ # # @ingroup ArgsparseCompletion
218249_argsparse_complete () {
219250 local cur prev words cword split
220251 _init_completion -s || return
0 commit comments