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 to 1
if an argument is supplied for that option, otherwise it's set to 0.
This must be enabled on a per-argument basis by declaring:
ARGBASH_INDICATE_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" = 1 ]; then
MYOPT="$_arg_myopt" # We override
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
+
ARGBASH_INDICATE_SUPPLIED([long opt arg name], [another long opt arg name (optional)], ...)
312
+
313
+
This macro takes a list of long optional argument names and will generate a variable for each optional that will be set if that argument was explicitly provided on the command line. This only works for optional arguments.
314
+
315
+
For example, if you have `ARG_OPTIONAL_BOOLEAN([quiet], , , [off])`, followed by `ARGBASH_INDICATE_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 `[ "$_supplied_arg_quiet" = 1 ]`. If the argument was not passed to the program then this variable will be set to `0`.
0 commit comments