How do I check for a particular subparser?

好久不见. 提交于 2019-12-10 15:33:02

问题


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()

回答1:


Maybe something like this ?

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:

python argparse - add action to subparser with no arguments?



来源:https://stackoverflow.com/questions/29938335/how-do-i-check-for-a-particular-subparser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!