subcommand

argparse optional subparser (for --version)

二次信任 提交于 2019-11-27 01:50:36
问题 I have the following code (using Python 2.7): # shared command line options, like --version or --verbose parser_shared = argparse.ArgumentParser(add_help=False) parser_shared.add_argument('--version', action='store_true') # the main parser, inherits from `parser_shared` parser = argparse.ArgumentParser(description='main', parents=[parser_shared]) # several subcommands, which can't inherit from the main parser, since # it would expect subcommands ad infinitum subparsers = parser.add_subparsers