如何检查特定的子解析器?

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)

可以在这里找到更多说明:

python argparse - add action to subparser with no arguments?

关于python - 如何检查特定的子解析器?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29938335/

10-12 19:05