This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author monkeyman79
Recipients monkeyman79, paul.j3, r.david.murray, rhettinger, v+python
Date 2021-01-28.02:41:12
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
> 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

-> 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.
Date User Action Args
2021-01-28 02:41:12monkeyman79setrecipients: + monkeyman79, rhettinger, v+python, r.david.murray, paul.j3
2021-01-28 02:41:12monkeyman79setmessageid: <>
2021-01-28 02:41:12monkeyman79linkissue43046 messages
2021-01-28 02:41:12monkeyman79create