Skip to content Skip to sidebar Skip to footer

Nested ArgumentParser

I'm trying to build nested parsers for a command line tool. I'm currently using add_subparsers, but it seems not powerful enough for one specific case. I cannot add same named argu

Solution 1:

I think your question is answered here:

argparse subcommands with nested namespaces

One of my answers uses a custom action.

But a simpler way of handling duplicate argument names, is to give one, or both, different 'dest' values. It distinguishes between the two without extra machinery.

argparser = argparse.ArgumentParser()
argparser.add_argument("-H", action="store_true")
subparser = argparser.add_subparsers(dest='sp')
cmd = subparser.add_parser("cmd")
cmd.add_argument("-H", dest='cmd_H')

print argparser.parse_args()

Post a Comment for "Nested ArgumentParser"