Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check for a particular subparser?

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()
like image 800
n611x007 Avatar asked Nov 09 '22 14:11

n611x007


1 Answers

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?

like image 191
Timothée Jeannin Avatar answered Nov 14 '22 23:11

Timothée Jeannin