9

I have the following test-code

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--verbose", default = 0, type=int)

subparsers = parser.add_subparsers(dest = "parser_name")

parser_lan = subparsers.add_parser('car')
parser_lan.add_argument("--boo")
parser_lan.add_argument("--foo")

parser_serial = subparsers.add_parser('bus')
parser_serial.add_argument("--fun")

print parser.parse_args()

which defines two sub-parsers, having a different set of arguments. When I call the testcode as

tester.py  --verbose 3 car --boo 1 --foo 2

I get the expected result

Namespace(boo='1', foo='2', parser_name='car', verbose=3)

What I want to have instead is the values from each subparser in a separate namespace or dict, something like

Namespace(subparseargs={boo:'1', foo:'2'}, parser_name='car', verbose=3)

so that the arguments from each subparser are logical separated from the arguments from the main parser (as verbose in this example).

How can I achieve this, with the arguments for each subparser in the same namespace (subparseargs in the example).

Alex
  • 41,580
  • 88
  • 260
  • 469
  • Doesn't answer your question, but modern [argparse wrappers](http://stackoverflow.com/q/13248487/989121) are much more fun to work with than raw argparse. – georg Apr 03 '13 at 09:40

4 Answers4

2

You need to go into the bowels of argparse a bit but changing your script to the following should do the trick:

import argparse
from argparse import _HelpAction, _SubParsersAction

class MyArgumentParser(argparse.ArgumentParser):
    def parse_args(self, *args, **kw):
        res = argparse.ArgumentParser.parse_args(self, *args, **kw)
        from argparse import _HelpAction, _SubParsersAction
        for x in parser._subparsers._actions:
            if not isinstance(x, _SubParsersAction):
                continue
            v = x.choices[res.parser_name] # select the subparser name
            subparseargs = {}
            for x1 in v._optionals._actions: # loop over the actions
                if isinstance(x1, _HelpAction): # skip help
                    continue
                n = x1.dest
                if hasattr(res, n): # pop the argument
                    subparseargs[n] = getattr(res, n)
                    delattr(res, n)
            res.subparseargs = subparseargs
        return res

parser = MyArgumentParser()
parser.add_argument("--verbose", default = 0, type=int)

subparsers = parser.add_subparsers(dest = "parser_name")

parser_lan = subparsers.add_parser('car')
parser_lan.add_argument("--boo")
parser_lan.add_argument("--foo")

parser_serial = subparsers.add_parser('bus')
parser_serial.add_argument("--fun")

print parser.parse_args()
Anthon
  • 69,918
  • 32
  • 186
  • 246
1

I have started to develop a different approach (but similar to the suggestion by Anthon) and come up with a much shorter code. However, I am not sure my approach is a general solution for the problem.

To similar what Anthon is proposing, I define a new method which creates a list of 'top-level' arguments which are kept in args, while all the other arguments are returned as an additional dictionary:

class MyArgumentParser(argparse.ArgumentParser):
    def parse_subargs(self, *args, **kw):
        # parse as usual
        args = argparse.ArgumentParser.parse_args(self, *args, **kw)

        # extract the destination names for top-level arguments
        topdest = [action.dest for action in parser._actions]

        # loop over all arguments given in args
        subargs = {}
        for key, value in args.__dict__.items():

            # if sub-parser argument found ...
            if key not in topdest:

                # ... remove from args and add to dictionary
                delattr(args,key)
                subargs[key] = value

        return args, subargs

Comments on this approach welcome, especially any loopholes I overlooked.

Alex
  • 41,580
  • 88
  • 260
  • 469
0

Or manually, you could parse the args and create a dict with details:

# parse args
args = parser.parse_args()
args_dict = {}
for group in parser._action_groups:
    # split into groups based on title
    args_dict[group.title] = {}
    for arg in group._group_actions:
        if hasattr(args, arg.dest):
            args_dict[group.title][arg.dest] = getattr(args, arg.dest)
            # or args_dict[arg.dest] = getattr(args, arg.dest)
            delattr(args, arg.dest)
# add remaining items into subparser options
args_dict["subparser"] |= vars(args)
return args_dict
George Ogden
  • 596
  • 9
  • 15
0

To have the subparsers' args filtered, you could use parse_known_args method:

car_subparser_args = parser_lan.parse_known_args()[0]
bus_subparser_args = parser_serial.parse_known_args()[0]
print(vars(car_subparser_args)) # -> {'boo': '1', 'foo': '2'}
print(vars(bus_subparser_args)) # -> {'fun': None}
HARLEEN HANSPAL
  • 121
  • 1
  • 4