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

The argparse command should have an option to skip over unknown options #5367

Closed
mattmc3 opened this issue Nov 23, 2018 · 5 comments
Closed

Comments

@mattmc3
Copy link
Contributor

mattmc3 commented Nov 23, 2018

I have a small feature request.

When using the argparse fish builtin command, you are required to define all the possible options you'll be receiving before hand, and cannot cherry pick the options you care about and then just ignore the rest. I have found that I frequently don't care about all the options being passed to my script - I just want to parse what I have defined and skip over the rest, or handle the remaining elsewhere. This is especially useful if you are wrapping other functionality or farming off small bits of processing to other functions.

I propose adding an -i/--ignore-missing or -u/--skip-undefined flag to parse what we can and skip the rest.

Example:

# works great when I know about all the flags (-a,-b,-c)
argparse 'a=+' 'b=+' 'c=+' -- -a alpha -b bravo -c charlie

# but not when there are others
argparse 'a=+' 'b=+' 'c=+' -- -a alpha -b bravo -c charlie -t tango
# > Unknown option '-t'

# proposing a -i/--ignore-missing flag
argparse --ignore-missing 'a=+' 'b=+' 'c=+' -- -a alpha -b bravo -c charlie -t tango
# $_flag_a, $_flag_b, $_flag_c are set
# $_flag_t wasn't defined, so it is not...

Currently, in places where I would love to use argparse, I find myself picking apart $argv in a loop or using string match magic to regex my way through the problem. While that's workable, it feels like it's a deficiency in argparse, which really should be the one best way to handle command arguments.

Note: I looked into the -s/--stop-nonopt option, but it only concerns itself with arguments and not flags, and isn't really the behavior I am describing.

@zanchey zanchey added this to the fish-future milestone Nov 24, 2018
@faho
Copy link
Member

faho commented Nov 24, 2018

Yes, it should. I had mentioned this in #4226. For completions it's nice because it allows us to just ignore uninteresting options entirely.

However, this will break if there's an unknown option that requires an argument. So I'm not sure if we should call it "ignore-unknown" or "assume-bool" or something, to make clear that it can't really work with those.

This particular case here:

# but not when there are others
argparse 'a=+' 'b=+' 'c=+' -- -a alpha -b bravo -c charlie -t tango
# > Unknown option '-t'

The "-t" can only be assumed to be a boolean option (i.e. like "t", not "t=+"), so the "tango" would remain in $argv.

@mattmc3
Copy link
Contributor Author

mattmc3 commented Nov 24, 2018

The "-t" can only be assumed to be a boolean option (i.e. like "t", not "t=+"), so the "tango" would remain in $argv.

Yes, I agree completely. argparse won't be able tell the difference between -t tango as one argument with a value, or a boolean flag -t and another argument tango - and that's perfectly fine. This new option in argparse should leave anything not defined in $argv, letting you parse your arguments incrementally with future calls to argparse or just throw the rest away.

@faho
Copy link
Member

faho commented Apr 29, 2019

The solution I went with was to keep all unknown options in $argv (in the correct order, which meant disabling getopt's reordering!). That's the most flexible because it allows you to deal with them later. The alternative, to remove all unknown options, seems mostly useless.

@lesderid
Copy link

@faho Any idea when this will be in a release? I could use this for a script I'm working on.

@faho
Copy link
Member

faho commented Apr 29, 2019

@lesderid It'll be in 3.1.0, which doesn't have a due date yet. I'd guess it'll come out in a month or two, but that depends on some of the kinks introduced by some refactoring being ironed out quickly enough.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants