How do I check for a specific sub-parameter?

How do I check for a specific sub-parameter?

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

      

+3


source to share


1 answer


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)

      



Additional explanations can be found here:

python argparse - add action to subparameter with no arguments?

+1


source







All Articles