You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since boolean optionals default to "off" we have no way to
check if a boolean optional was actually specified on the command line.
The ability to check if a boolean optional has been given is useful in
cases where we source values from a configuration file where we want any
explicitly given arguments to override the configuration.
An example that mostly works is (for plain optionals with an empty default):
test -f ~/.standard-values-for-this-script && source "$_"
MYOPT="${MYOPT:-$_arg_myopt}"
... # Code using MYOPT
This works since optionals can default to an empty string. We still are
not entirely sure if the optional was actually passed here, but it tends
to be "good enough" in most cases.
In the case where `myopt` is boolean, the above case would always use
"off" if the argument was not specified, thus overriding the sourced
configuration file.
This patch introduces `_supplied_<argname>`, a value that gets set if
and only if an argument is supplied for that option. This must be
enabled on a per-argument basis by declaring
ARG_SUPPLIED([long arg name])
Where `long arg name` matches the argument name for any
`ARG_OPTIONAL_*`.
This allows us to do the following:
test -f ~/.standard-values-for-this-script && source "$_"
if [[ "${_supplied_arg_myopt-x}" = 1 ]]; then
MYOPT="$_arg_myopt" # We override
fi
# Alternatively
if [[ -v _supplied_arg_myopt ]]; then
# ...
fi
... # Code using MYOPT
Copy file name to clipboardExpand all lines: doc/guide.rst
+9Lines changed: 9 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -305,6 +305,15 @@ Special arguments
305
305
A use case for this is wrapping of scripts that are completely ``Argbash``-agnostic.
306
306
Therefore, your script can take its own arguments and the rest that is not recognized can go to the wrapped script.
307
307
308
+
* Detect supplied arguments:
309
+
::
310
+
311
+
ARG_SUPPLIED([long arg name])
312
+
313
+
This macro will cause a variable to be set if the argument was explicitly provided. This only works for optional arguments.
314
+
315
+
For example, if you have `ARG_OPTIONAL_BOOLEAN([quiet], , , [off])`, followed by `ARG_SUPPLIED([quiet])`, then if `--quiet` was provided on the command line the variable `_supplied_arg_quiet=1` would be set. This allows you to see if an argument was explicitly provided using `[[ -v _supplied_arg_quiet ]]` or `[ "${_supplied_arg_quiet-x}" = 1 ]`.
0 commit comments