33#
44# # @file
55# # @author Damien Nadé <bash-argsparse@livna.org>
6- # # @brief .. .
6+ # # @brief Bash completion for scripts using argsparse library .
77# # @copyright WTFPLv2
88# # @version 1.7
99#
2626#
2727# ########
2828#
29+ # # @details
30+ # # @par URL
31+ # # https://github.com/Anvil/bash-argsparse @n
32+ #
33+ # # @par Purpose
34+ #
35+ # # To automatically enable, for bash-completion users, completion for
36+ # # scripts using the argsparse library.
37+
38+ # # @par Usage
39+ #
40+ # # In you ~/.bashrc, add the following lines to enable completion for
41+ # # all your argsparse-written scripts:
42+ # #
43+ # # @code
44+ # # . argsparse-completion.sh
45+ # # complete -F _argsparse_complete [ your scripts names ... ]
46+ # # @endcode
47+ #
48+ # # @par Required configuration
49+ #
50+ # # argsparse-completion relies on a few shell settings:
51+
52+ # # @li "expand_aliases" @n
53+ # # This the expansion of an alias. Aliases are enabled by default in
54+ # # interactive mode.
55+ # #
56+ # # @code
57+ # # shopt expand_aliases
58+ # # @endcode
59+ # #
60+
61+ # #
62+ # # @li "sourcepath" shell option must be enabled. This should be
63+ # # enabled by default, but you can enforce it by running:
64+ # #
65+ # # @code
66+ # # shopt -s sourcepath
67+ # # @endcode
68+ # #
69+ # # If correctly enabled, the following command below should return
70+ # # this output.
71+ # #
72+ # # @code
73+ # # $ shopt sourcepath
74+ # # sourcepath on
75+ # # @endcode
76+ # #
77+ # # @par Limitations
78+ # # @li The completed script will be sourced, up to the
79+ # # argsparse_parse_options function() call. This means the script
80+ # # should not performed any side effect (like file system alteration
81+ # # - file creation, ), and should avoid time-consuming tasks up to
82+ # # this point
83+ # #
84+ # #
85+ #
86+ # # @defgroup ArgsparseCompletion Bash Completion-related functions.
2987
88+ # # @fn __argsparse_compgen()
89+ # # @private
90+ # # @brief A compgen wrapper.
91+ # # @details This function will just call compgen with given argument,
92+ # # safely adding $cur in the command line. Also if compgen_prefix is
93+ # # set, a -P option will be provided to compgen.
94+ # # @param param... any set of compgen options
95+ # # @return
96+ # # @ingroup ArgsparseCompletion
3097__argsparse_compgen () {
3198 if [[ -v compgen_prefix ]]
3299 then
@@ -35,14 +102,17 @@ __argsparse_compgen() {
35102 compgen " $@ " -- " $cur "
36103}
37104
105+ # # @fn __argsparse_complete_value()
106+ # # @brief complete a value
107+ # # @ingroup ArgsparseCompletion
38108__argsparse_complete_value () {
39109 local option array option_type
40110 local -a values
41111 option=$( __argsparse_complete_get_long " $prev " " ${longs[@]} " ) && \
42112 argsparse_has_option_property " $long " value || return 1
43113 if array=$( __argsparse_values_array_identifier " $option " )
44114 then
45- values=( ${! array} )
115+ values=( " ${! array} " )
46116 __argsparse_compgen -W " ${values[*]} "
47117 elif option_type=$( argsparse_has_option_property " $option " type)
48118 then
@@ -70,6 +140,11 @@ __argsparse_complete_value() {
70140 fi
71141}
72142
143+ # # @fn __argsparse_complete_get_long()
144+ # # @brief
145+ # # @details
146+ # # @param word
147+ # # @ingroup ArgsparseCompletion
73148__argsparse_complete_get_long () {
74149 [[ $# -ge 1 ]] || return 1
75150 local word=$1
@@ -90,12 +165,16 @@ __argsparse_complete_get_long() {
90165 argsparse_has_option_property " $long " value
91166}
92167
168+ # # @fn __argsparse_complete()
169+ # # @brief
170+ # # @details
171+ # # @ingroup ArgsparseCompletion
93172__argsparse_complete () {
94173 local script=${words[0]}
95174 (
96175 set +o posix
97176 ARGSPARSE_COMPLETION_MODE=1
98- . " $script " 2> /dev/null
177+ . " $script " 2> /dev/null || return
99178 longs=( " ${! __argsparse_options_descriptions[@]} " )
100179 longs=( " ${longs[@]/#/ --} " )
101180 option=${prev# --}
@@ -112,13 +191,14 @@ __argsparse_complete() {
112191 # Complete the --foo=something pattern as if
113192 # prev=--foo and cur=something
114193 # Complete -fsomething as if prev=-f and cur=something
115- if [[ " $cur " =~ ^(( -- [^= ]+ )= )(.* )$ || "$cur " =~ ^((- [^- ])) (.* )$ ]]
194+ if [[ " $cur " =~ ^(( -- [^= ]+ )= )(.* )$ ||
195+ "$cur " =~ ^((-[^-])) (.* )$ ]]
116196 then
117197 compgen_prefix=${BASH_REMATCH[1]}
118198 option=${BASH_REMATCH[2]}
119199 cur=${BASH_REMATCH[3]}
120- long=$(
121- __argsparse_complete_get_long " $option " " ${longs[@]} " ) && \
200+ long=$( __argsparse_complete_get_long \
201+ " $option " " ${longs[@]} " ) && \
122202 __argsparse_complete_value " $long "
123203 fi
124204 ;;
@@ -138,6 +218,9 @@ __argsparse_complete() {
138218 )
139219}
140220
221+ # # @fn _argsparse_complete()
222+ # # @brief
223+ # # @details
141224_argsparse_complete () {
142225 local cur prev words cword split
143226 _init_completion -s || return
0 commit comments