use argparse to call function and use argument in function

Solutions on MaxInterview for use argparse to call function and use argument in function by the best coders in the world

showing results for - "use argparse to call function and use argument in function"
Alonso
16 Mar 2017
1# Parse the subcommand argument first
2parser = ArgumentParser(add_help=False)
3parser.add_argument("function", 
4                    nargs="?",
5                    choices=['function1', 'function2', 'function2'],
6                    )
7parser.add_argument('--help', action='store_true')
8args, sub_args = parser.parse_known_args(['--help'])
9
10# Manually handle help
11if args.help:
12    # If no subcommand was specified, give general help
13    if args.function is None: 
14        print parser.format_help()
15        sys.exit(1)
16    # Otherwise pass the help option on to the subcommand
17    sub_args.append('--help')
18
19# Manually handle the default for "function"
20function = "function1" if args.function is None else args.function
21
22# Parse the remaining args as per the selected subcommand
23parser = ArgumentParser(prog="%s %s" % (os.path.basename(sys.argv[0]), function))
24if function == "function1":
25    parser.add_argument('-a','--a')
26    parser.add_argument('-b','--b')
27    parser.add_argument('-c','--c')
28    args = parser.parse_args(sub_args)
29    function1(args.a, args.b, args.c)
30elif function == "function2":
31    ...
32elif function == "function3":
33    ...
34
queries leading to this page
python pass arguments argparsehow to handle arguments action using argparseparams for args argparseusing argparseargparse file argumentpython argparse run functionargparse add argument parametersargparse keyword argumentshow to pass arguments to python argparsepass a string with in as arguement in python argparsecalling function in argparsepython argparse number of argumentsargparse argument meaningargparse argument with actionpython argparse argument nameargparse python3 call functionargparse argument parserargparse type function parameterspython call argparse function within codehow to call a particular function when an argument is passed in argparse in pythonpython argparse file argumentargparse python 3 argumentsargparse get parampython argparse pass single argumentcall function with argparsealternative argument argparseargparse call function automaticallyget arguments from argparseargparse run functionusing argparse with a functionwhat is the use of argparse in python 3fhow to store a value in an argument with argparse python3pass arguments to python script example argparseargparse argument choices meaningfunction based on the arguments given argparseargparse argument optionsargparse add argumentargparse get argumentargparse without passing argumentsuse argparse to call function and run functionpython argparse add argument help functionpython argparse the following arguments are requiredhow to call argparse inside method in pythonargparse call functionuse argparse to pass string as argumentargparse add argument optionsargparse all parametersargparse argumentparser 1 argumentuse argparse pythonargparse command line argument examplehow to use argparsehow to call function with argparseargparse all arguemtnspass function in argparsepython argparse file with argumentspython script with custom functions called on command line with argparse iterate through filesargparse argumentpython call from py with argparseargparse type function with argumentsdefine custom argparse argumentspython argparse get argument valueargparse examplewhat is the use of argparse in pythonpython argparse receive array of elementsargparse give arguments from arrayget name of argument in type function argparseget number of arguments argparseargparse add argumentuse argparse to call functionargparse get list of argumentsargparse add argumentargparser add argumentargparse type argumentpython argparse get argument typeargparser parse argspython argparse argument that sets other argumentsargparse javascriptargparse args getargparse get functionality helphow to use argparse to call functionuse argparse argument to call a functionoptions argument in argparsepython argparse use argumentsargparse access argumentsargparse argumenterroruse argparse to call function and use argument in function