Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that required flag is passed to script, not just that it has a value when provided #10458

Closed
oyarsa opened this issue Apr 24, 2024 · 2 comments
Labels

Comments

@oyarsa
Copy link

oyarsa commented Apr 24, 2024

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:

argparse 'm/max=' -- $argv; or return
echo $_flag_max

Output:

❯ fish argparse.fish -m1
1

❯ fish argparse.fish -m
argparse: -m: option requires an argument

❯ fish argparse.fish 

For the last one, I expected some kind of message, like this:

❯ fish argparse.fish -m
argparse: -m: option is required

For example, this is what Python does:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--max", "-m", required=True)
args = parser.parse_args()
print(args.max)

Output:

❯ 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.

@faho
Copy link
Member

faho commented Apr 24, 2024

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

if not set -q _flag_X _flag_Y _flag_Z
    echo "Please provide all necessary flags" >&2
    return 1
end

or a for-loop:

for flag in X Y Z
    set -l name _flag_$flag
    if not set -q $name
        echo "error: -$flag is required" >&2
        return 1
    end
end

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).

@oyarsa
Copy link
Author

oyarsa commented Apr 24, 2024

Thank you for replying so quickly!

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.

@faho faho added the question label Apr 27, 2024
@faho faho closed this as completed Apr 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants