[issue42973] argparse: mixing optional and positional arguments... not again

Tadek Kijkowski report at bugs.python.org
Tue Jan 26 04:22:27 EST 2021


Tadek Kijkowski <tkijkowski at gmail.com> added the comment:

>> In the docs fruits example, though, I think the input and output are inconsistent: you have a brown banana reported as yellow?

Yeah, I noticed that and fixed already :)


I agree with your ideas. My goal was just to provide bare minimum that would allow users to implement this functionality on their own, but turning it into something useful out of the box sounds like a good idea.

There could be new predefined action, e.g. 'extend_with_capture' (or just 'extend_capture') which could be used like this:

  parser.add_argument('fruits', nargs='*', action='extend_capture', capture_once=['color'])

Where 'capture' would be one of: a list of parameters to capture (could be a string instead of a list if it's just one parameter) or '*' to capture all optional parameters and 'capture_once' would be similar list of parameters to capture and reset to default.

>> Does the namespace still contain the default value for an optional after it has been used?  It looks like it might

I think it does - not the namespace, but ArgumentParser, which is also available in Action.__call__, has 'get_defaults'.

>> I'm not sure how that plays with optionals that use append or append_const storage methods, nor do I fully understand the comment in "def _copy_items(items):"

I don't see any danger here, the action will just have to make sure to capture copy of the array, rather than reference to it. The same applies to 'extend'.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue42973>
_______________________________________


More information about the Python-bugs-list mailing list