如何检查特定的子解析器?
问题描述:
如何检查特定的子解析器?
How do I check for a particular subparser?
import argparse
if __name__ == "__main__":
mainparser = argparse.ArgumentParser()
submainadder = mainparser.add_subparsers(title='subcommands')
parser_ut = submainadder.add_parser('unittest')
stuff = mainparser.parse_args()
# if 'unittest' was selected:
# do_things()
答
也许是这样的东西?
import argparse
def do_things(args):
print args
# Do your stuff
mainparser = argparse.ArgumentParser()
submainadder = mainparser.add_subparsers(title='subcommands')
parser_ut = submainadder.add_parser('unittest')
parser_ut.set_defaults(func=do_things)
if __name__ == "__main__":
stuff = mainparser.parse_args()
stuff.func(stuff)
更多说明可以在这里找到:
More explanations can be found here: