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
I'm trying to use argparse in a script, but I can't get it to do something: ensure required arguments. I can use 'n/name= to ensure that if -n or --name is provided, it must contain a value. However, I can't seem to find out how to make it so parsing fails if the flag isn't passed.
❯ python args.py
usage: args.py [-h] --max MAX
args.py: error: the following arguments are required: --max/-m
I know I can just test each individual flag variable with if set -q _flag_X, but when you have multiple required flags, that gets tedious quickly.
Also, it would be good to specify the positional arguments, including optional ones, but I can see how that would be more complex than just parsing flags.
The text was updated successfully, but these errors were encountered:
We do not currently have a way to specify that a flag is required.
I know I can just test each individual flag variable with if set -q _flag_X, but when you have multiple required flags, that gets tedious quickly.
set -q returns 0 only if all given variables are defined, so you can do
ifnotset-q _flag_X _flag_Y _flag_Z
echo"Please provide all necessary flags">&2return 1
end
or a for-loop:
for flag in X Y Z
set-l name _flag_$flagifnotset-q$nameecho"error: -$flag is required">&2return 1
endend
To be honest I'm not sure this warrants a special option - it's not something that comes up constantly and I think it's awkward CLI design (I'd use positional arguments).
To be honest I'm not sure this warrants a special option - it's not something that comes up constantly and I think it's awkward CLI design (I'd use positional arguments).
I find that when I have a CLI that has multiple options, it's more readable to have required options than positional arguments.
Anyway, I didn't realise that you could set -q multiple variables. That should do for now.
fish version: 3.6.1 (homebrew)
I'm trying to use argparse in a script, but I can't get it to do something: ensure required arguments. I can use
'n/name=
to ensure that if-n
or--name
is provided, it must contain a value. However, I can't seem to find out how to make it so parsing fails if the flag isn't passed.Example script:
Output:
For the last one, I expected some kind of message, like this:
For example, this is what Python does:
Output:
I know I can just test each individual flag variable with
if set -q _flag_X
, but when you have multiple required flags, that gets tedious quickly.Also, it would be good to specify the positional arguments, including optional ones, but I can see how that would be more complex than just parsing flags.
The text was updated successfully, but these errors were encountered: