New submission from Tadek Kijkowski <tkijkow...@gmail.com>:

This is spinoff from issue42973.

The purpose of this issue is to provide set of additional action classes for 
argparse, that could capture mutual relationship between command line arguments 
for more advanced parsers, where order of command line arguments is meaningful. 
It is hoped that this will work together with enhancement introduced in 
issue42973, providing also ability to capture relationship between options and 
positional parameters.

There will be four new action classes: 'extend_capture', 'append_capture', 
'store_capture' and 'capture'.

Here are excerpts from documentation being prepared for the new action classes 
and an example of use:

* 'extend_capture' - Captures preceding argument values listed in the
  'capture' and 'capture_reset' keyword arguments and creates a dictionary 
object
  from those values, then adds actual command-line value to the dictionary using
  the 'key' keyword argument, finally the created dictionary is appended to the
  'dest' list. After capturing, all arguments listed in 'capture_reset' are 
reset
  to their default values. If there are more than one command-line values, a new
  dictionary object is created, and appended to the list for each value, with
  'capture_reset' arguments reset to their default values after first value is
  added.
  Example usage:

    >>> parser = argparse.ArgumentParser()
    >>> parser.add_argument("--load-addr", type=lambda s: int(s, 16))
    >>> parser.add_argument("--exec-addr", type=lambda s: int(s, 16))
    >>> parser.add_argument("--replace", action="store_true")
    >>> parser.add_argument("--file", nargs="*", action="extend_capture",
    ...     capture="*", capture_reset=["load_addr", "exec_addr"])
    >>> parser.parse_args("--replace --load-addr 1900 --exec-addr 8023 "
    ...     "--file CALC !BOOT".split())
    Namespace(load_addr=None, exec_addr=None, replace=True, file=[
        {'replace': True, 'exec_addr': 32803, 'load_addr': 6400, 'file': 
'CALC'},
        {'replace': True, 'exec_addr': None, 'load_addr': None, 'file': 
'!BOOT'}])

capture

The list of attributes to capture. This can be either a single attribute name,
a list (or other iterable type) of names or special value '*'. Name of
attribute associated with each argument is determined by the dest keyword
passed to add_argument method when the argument was
created. If capture_ is '*', all attributes are captured, except for this
argument's own value.

This keyword argument is valid only for 'extend_capture',
'append_capture', 'store_capture' and 'capture' actions.


capture_reset

The list of attributes to capture and reset to default value. As with capture,
this can be '*' to capture and reset all attributes except for this
argument's own value.

This keyword argument is valid only for 'extend_capture',
'append_capture', 'store_capture' and 'capture' actions.


key

The key to use for adding this argument's own command-line value to dictionary
of captured values. If this keyword argument is not specified, the dest is
used.

This keyword argument is valid only for 'extend_capture',
'append_capture' and 'store_capture' actions.

----------
components: Library (Lib)
messages: 385824
nosy: monkeyman79
priority: normal
severity: normal
status: open
title: argparse: capturing actions
type: enhancement
versions: Python 3.10

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue43046>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to