Tadek Kijkowski <[email protected]> added the comment:
> I'm a little confused by the mention of the "key" keyword argument. I suspect
> that is an internal concept to argparse, possibly passed that way to internal
> methods, but on the add_argument interface, it doesn't exist... instead there
> is "name or flags" positional arguments, from which, together with the "dest"
> argument, the "key" keyword argument is derived. This is described under the
> explanation for the "dest" parameter.
Hmm... that may be confusing. The "key" keyword argument is not internal
concept, is new parameter that can be passed to add_argument, specifically for
'capture' actions. The "key" name may be unfortunate, maybe it could be
"dest_key" or "dict_key"? It can't be "dest", because it is goes to
add_argument together with "dest". In most cases it is not needed and "dest" is
used as dictionary key, but user may want to override that.
Like in this example:
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument("--user", default=None)
>>> parser.add_argument("--server", default="localhost")
>>> parser.add_argument("src1", action="store_capture", key="file",
capture_reset=["user", "server"])
>>> parser.add_argument("src2", action="store_capture", key="file",
capture_reset=["user", "server"])
>>> parser.add_argument("dst", action="store_capture", key="file",
capture_reset=["user", "server"])
>>> parser.parse_args("first --user guest --server no_such second --server
not_found third".split())
Namespace(user=None, server='localhost',
src1={'user': None, 'server': 'localhost', 'file': 'first'},
src2={'user': 'guest', 'server': 'no_such', 'file': 'second'},
dst={'user': None, 'server': 'not_found', 'file': 'third'})
The 'dest' is 'src1', 'src2' and 'dst' respectively, but we want to have
unified layout of the dictionaries,
so 'key' is 'file' in all three cases.
Oh, and I forgot to update add_argument signature in rst earlier.
ArgumentParser.add_argument(name or flags..., [action], [nargs], \
[const], [default], [type], [choices], [required], \
[help], [metavar], [dest], [capture], \
[capture_reset], [key])
...
...
* dest - The name of the attribute to be added to the object returned by
parse_args.
-> these are new:
* capture - A name or a list of names of attributes to capture by one of
capture actions.
* capture_reset - A name or a list of name of attributes to capture and
reset to default value by one of capture actions.
* key - The key to use putting command-line argument to dictionary object
created by capture actions.
----------
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue43046>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com