◐ Shell
clean mode source ↗

Message 128728 - Python tracker

[I doubt my terminology is exactly correct in this post, but I've tried my best to make it so.)

The more I think about this the more I realize we can't implement a parser that doesn't make guesses about '-' prefixed args and that works with arparse's existing behavior with respect to optional arguments.

For example:
parser = argparse.ArgumentParser()
parser.add_argument('--foo', nargs='?')
parser.add_argument('--bar', nargs='?')
print parser.parse_args(['--foo', '--bar', 'a'])
print parser.parse_args(['--foo', 'x', '--bar', 'a'])

Unless the parser tries to guess that --bar is an optional argument by itself, it can't know that --foo has an argument or not.

I guess it could look and say that if you called this with '--foo --baz', then '--baz' must be an argument for '--foo', but then you could never have an argument to '--foo' named '--bar', plus it all seems fragile.

Maybe this new parser (as Steven described it) wouldn't allow a variable number of arguments to optional arguments? That is, nargs couldn't be '?', '*', or '+', only a number.