0

I would like to add groups in a subparser.

parser = argparse.ArgumentParser(description="A Pipeline.")
subparsers = parser.add_subparsers(
    help="Choose imagery source", dest="imagery_source"
)

animal_parser = subparsers.add_parser("animal")
group1_parser = animal_parser.add_argument_group("for_dog_config")
group1_parser.add_argument("--dog", type=str)

group2_parser = animal_parser.add_argument_group("for_cat_config")
group2_parser.add_argument("--cat", type=str)

args = parser.parse_args()
print(args)

run_pipeline animal --dog hello --cat world
Here is the output of namespace.

Namespace(cat='world', dog='hello', imagery_source='animal')

What I would like is that the cat and dog are in different namespaces or dictionaries respectively. Is it possible? Note: I know there are other answers using add_argument_group in ArgumentParser object, but I need to use subparsers = prser.add_subparsers here.

=========== Update ===========
This code block almost do what I need.

def get_arg(parser):
    args = parser.parse_args()
    args_groups = {}
    for group in parser._action_groups:
        group_dict = {a.dest: getattr(args, a.dest, None) for a in group._group_actions}
        args_groups.update({group.title: argparse.Namespace(**group_dict)})
    return args_groups

parser = argparse.ArgumentParser(description="A Pipeline.")
subparsers = parser.add_subparsers(
    help="Choose imagery source", dest="imagery_source"
)
group1_parser = parser.add_argument_group("for_dog_config")
group1_parser.add_argument("--dog", type=str)

group2_parser = parser.add_argument_group("for_cat_config")
group2_parser.add_argument("--cat", type=str)

args = get_arg(parser)
print(args)

Execute run_stereo_pipeline --dog hello --cat world The output is

{'positional arguments': Namespace(imagery_source=None), 'optional arguments': Namespace(help=None), 'for_dog_config': Namespace(dog='hello'), 'for_cat_config': Namespace(cat='world')}

As you can see, now I can do things like args["for_cat_config"] to get all arguments in the "for_cat_config" group. But in this approach, I can not specify animal source which is what I want in the question.

6
  • add_argument_group for animal_parser works the same as for parser. subparsers is a special Action object, but animal_parser is a parser, just like the main. That said, argument_groups only group the help lines; they don't affect parsing. Commented Jul 14, 2021 at 6:16
  • What other answers do you have in mind? I have explored creating subdirectories or namespaces within in the main namespace, but it requires some special games with the argparse.Namespace class, or with the argument Action subclass. I don't argument_groups help with that. Commented Jul 14, 2021 at 6:22
  • stackoverflow.com/questions/66831906/… explains the relation between the subparser's namespace and the main parser's - values are just copied from one to the other. Commented Jul 14, 2021 at 6:29
  • stackoverflow.com/questions/38884513/… - an example of how convoluted it is to create separate namespace objects. Or stackoverflow.com/questions/18668227/… Commented Jul 14, 2021 at 6:31
  • @hpaulj Thanks for your reply. But I have read through the link you provide which does not really help in my case, unfortunately. Btw, I update my question. Please take a look. Thanks. Commented Jul 14, 2021 at 6:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.