1

I have a python file which looks something like this :

import argparse
parser = argparse.ArgumentParser()
parser.add_argument() 
etc...
args = parser.parse_args()

function_one(a=args.a, b=args.b)
function_two(c=args.c, d=args.d, e=args.e)

What I would like to have is the following

import argparse
parser = argparse.ArgumentParser()
parser_one = parser.add_parser(name1) 
parser_one.add_argument('--a')

parser_two = parser.add_parser(name2)
parser_two.add_argument('--d')
args = parser.parse_args()

So that args would be something like a dictionary of dictionary and args.name1 would be the usual NameSpace.

This would enable me to divide my parser into subparser, and call the functions with :

function_one(**vars(args.name1))
function_two(**vars(args.name2))

I am aware of the function add_argument_group, but this merges the arguments into one NameSpace after calling parser_args.

And add_subparsers is also not the solution as it is exclusive between subparsers.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
mpariente
  • 660
  • 4
  • 12
  • 1
    There's nothing in `argparse` that does this. Conceivably you could write custom `Action` or `Namespace` classes to do this, but I think it would be simpler to just do the grouping or split after parsing. (there's at least on earlier SO question like this). – hpaulj Nov 07 '19 at 21:16
  • 1
    https://stackoverflow.com/questions/18668227/argparse-subcommands-with-nested-namespaces; https://stackoverflow.com/questions/38884513/python-argparse-how-can-i-get-namespace-objects-for-argument-groups-separately – hpaulj Nov 08 '19 at 03:36
  • Thank you very much! The solution to my problem was there : https://stackoverflow.com/questions/38884513/python-argparse-how-can-i-get-namespace-objects-for-argument-groups-separately – mpariente Nov 08 '19 at 13:06

0 Answers0