Python argparse : How can I get Namespace objects for argument groups separately?

python argparse check if argument exists
python argparse choices from file
python argparse kwargs
python argparse boolean
argparse namespace
argparse subparser
python namespace object
python argparse complex example

I have some command line arguments categorized in groups as follows:

cmdParser = argparse.ArgumentParser()
cmdParser.add_argument('mainArg')

groupOne = cmdParser.add_argument_group('group one')
groupOne.add_argument('-optA')
groupOne.add_argument('-optB')

groupTwo = cmdParser.add_argument_group('group two')
groupTwo.add_argument('-optC')
groupTwo.add_argument('-optD')

How can I parse the above, such that I end up with three different Namespace objects?

global_args - containing all the arguments not part of any group
groupOne_args - containing all the arguments in groupOne
groupTwo_args - containing all the arguments in groupTwo

Thank you!

you can do it in this way:

import argparse
parser = argparse.ArgumentParser()

group1 = parser.add_argument_group('group1')
group1.add_argument('--test1', help="test1")

group2 = parser.add_argument_group('group2')
group2.add_argument('--test2', help="test2")

args = parser.parse_args('--test1 one --test2 two'.split())

arg_groups={}

for group in parser._action_groups:
    group_dict={a.dest:getattr(args,a.dest,None) for a in group._group_actions}
    arg_groups[group.title]=argparse.Namespace(**group_dict)

This will give you the normal args, plus dictionary arg_groups containing namespaces for each of the added groups.

(Adapted from this answer)

argparse — Parser for command-line options, arguments and sub , The first step in using the argparse is creating an ArgumentParser object: In most cases, this means a simple Namespace object will be built up from attributes I have indented it exactly the way I want it optional arguments: -h, -- help show this argument, but displays the argument in a separate group for help messages. Defining Arguments¶. argparse is a complete argument processing library. Arguments can trigger different actions, specified by the action argument to add_argument().Supported actions include storing the argument (singly, or as part of a list), storing a constant value when the argument is encountered (including special handling for true/false values for boolean switches), counting the number

Nothing in argparse is designed to do that.

For what it's worth, the parser starts off with two argument groups, one that displays as positionals and the other as optionals (I forget the exact titles). So in your example there will actually be 4 groups.

The parser only uses argument groups when formatting the help. For parsing all arguments are put in a master parser._actions list. And during parsing the parser only passes around one namespace object.

You could define separate parsers, with different sets of arguments, and call each with parse_known_args. That works better with optionals (flagged) arguments than with positionals. And it fragments your help.

I have explored in other SO questions a novel Namespace class that could nest values based on some sort of dotted dest (names like group1.optA, group2.optC, etc). I don't recall whether I had to customize the Action classes or not.

The basic point is that when saving a value to the namespace, a parser, or actually a Action (argument) object does:

setattr(namespace, dest, value)

That (and getattr/hasattr) is all that the parser expects of the namespace. The default Namespace class is simple, little more than a plain object subclass. But it could be more elaborate.

argparse – Command line option and argument parsing., The first step in using the argparse is creating an ArgumentParser object: In most cases, this means a simple Namespace object will be built up from attributes I have indented it exactly the way I want it optional arguments: -h, -- help show this argument, but displays the argument in a separate group for help messages. 0 Python argparse : How can I get Namespace objects for argument groups separately? Dec 8 '19. 0 create array of int which is a bit field Jul 8 '16.

I was looking for a solution for this for a very long time, And I think I finally got it. So I will just put it here...

from argparse import ArgumentParser

def _parse_args():
    parser = ArgumentParser()
    parser.add_argument('-1', '--flag-1', action='store_true', default=False)
    parser.add_argument('-2', '--flag-2', action='store_true', default=False)
    parser.add_argument('-3', '--flag-3', action='store_true', default=False)

    args, unknown = parser.parse_known_args()
    print(f"args        : {args}")
    print(f"unknown     : {unknown}")

    hidden = ArgumentParser(add_help=False)
    hidden.add_argument('-d', '--debug', action='store_true', default=False)
    hidden_args = hidden.parse_args(unknown)
    print(f"hidden_args : {hidden_args}")

if __name__ == "__main__":
    _parse_args()

as a result: show help:

ubuntu → playAround $ ./test.py -h
usage: test.py [-h] [-1] [-2] [-3]

optional arguments:
  -h, --help    show this help message and exit
  -1, --flag-1
  -2, --flag-2
  -3, --flag-3

With debug flag:

ubuntu → playAround $ ./test.py -d
args        : Namespace(flag_1=False, flag_2=False, flag_3=False)
unknown     : ['-d']
hidden_args : Namespace(debug=True)

with flags 1 and 2:

ubuntu → playAround $ ./test.py -12
args        : Namespace(flag_1=True, flag_2=True, flag_3=False)
unknown     : []
hidden_args : Namespace(debug=False)

with flags 1 and 2 and debug:

ubuntu → playAround $ ./test.py -12 -d
args        : Namespace(flag_1=True, flag_2=True, flag_3=False)
unknown     : ['-d']
hidden_args : Namespace(debug=True)

The only thing you can't do with this approch is to pass the debug short flag along side to the other short flags:

ubuntu → playAround $ ./test.py -12d
usage: test.py [-h] [-1] [-2] [-3]
test.py: error: argument -2/--flag-2: ignored explicit argument 'd'

argparse – Command line option and argument parsing. — Python , If you have written extra code to work around limitations of optparse, you may want to The first step when using argparse is to create a parser object and tell it what the Namespace object returned when the command line arguments are parsed. In the example above, +a and -a are separate arguments, and //noarg can� 13 Python argparse : How can I get Namespace objects for argument groups separately? Aug 10 '16. 11 Right order of doing feature selection, PCA and normalization?

python_libs/argparse/doc/source/argparse.rst, The implementation of argparse supports features that would not have been easy to The first step when using argparse is to create a parser object and tell it what the Namespace object returned when the command line arguments are parsed . In the example above, +a and -a are separate arguments, and //noarg can� Namespaces in Python. A namespace is a collection of currently defined symbolic names along with information about the object that each name references. You can think of a namespace as a dictionary in which the keys are the object names and the values are the objects themselves. Each key-value pair maps a name to its corresponding object.

The Argparse4j User Manual — argparse4j 0.8.1 documentation, :mod:`argparse` --- Parser for command line options, arguments and sub- -- sum sum the integers (default: find the max) In most cases, this means a simple namespace object will be built up from parents_ - A list of :class:` ArgumentParser` objects whose arguments should. also be separate group for help messages. With it, you can add content like headers, form data, multipart files, and parameters via simple Python libraries. It also allows you to access the response data of Python in the same way. In programming, a library is a collection or pre-configured selection of routines, functions, and operations that a program can use.

pds4_tools.extern.argparse — PDS4 Python Tools 1.0 documentation, This manual was written based on argparse's manual and most of the the Thanks to Python community for the great module and documentation. In most cases, this means a simple Namespace object will have attributes parsed out of the command line. To parse the command line, pass this object to ArgumentParser. The left argument, x, is the accumulated value and the right argument, y, is the update value from the sequence. If the optional initializer is present, it is placed before the items of the sequence in the calculation, and serves as a default when the sequence is empty.

Comments
  • I don't think argparse has that feature.
  • Thanks for the details explanation. I ended up manually grouping the Namespace attributes as I needed them. It would be a good addition to argparse if it could allow user to retrieve options by group name (i.e. use 'group' concept for more than just displaying help screen)