I am new on Argparse module. I have almost finished my script but there is something I cannot find a way to do it.
Here is the script:
import argparse
def function1(a, b, c):
#mystaff
def function2(e, f):
#mystaff
def function3(g):
#mystaff
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='blahblah')
parser.add_argument('-a','--a', help='blahblah', required=False, default="defaultvalue")
parser.add_argument('-b','--b', help='blahblah', required=False)
.......
args = parser.parse_args()
function1(args.a,args.b,args.c)
I want to call the script from command prompt. Now, I can use this for example to call the function1
: python myscript.py -a <var>
What I want is:
python myscript.py -a <var>
: call the function1 as it is now
python myscript.py function2 -c <var>
: call the function2 only
python myscript.py function3 -g <var>
: call the function3 only
So, if I don't specify the function, function1 is the default, otherwise I have to pass the function name from the command prompt too. Any ideas?
Default subcommands
If you want to have subcommands, and make one of them default if no subcommand is specified, then you can't use the typical subparser method.
You need to do your argparse in two passes:
parser = ArgumentParser()
parser.add_argument("function",
nargs="?",
choices=['function1', 'function2', 'function2'],
default='function1',
)
args, sub_args = parser.parse_known_args()
if args.function == "function1":
parser = ArgumentParser()
parser.add_argument('-a','--a')
parser.add_argument('-b','--b')
parser.add_argument('-c','--c')
args = parser.parse_args(sub_args)
function1(args.a, args.b, args.c)
elif args.function == "function2":
...
elif args.function == "function3":
...
Handling --help
If you want --help
option to be useful, you need to do a bit more work:
This should do the trick:
# Parse the subcommand argument first
parser = ArgumentParser(add_help=False)
parser.add_argument("function",
nargs="?",
choices=['function1', 'function2', 'function2'],
)
parser.add_argument('--help', action='store_true')
args, sub_args = parser.parse_known_args(['--help'])
# Manually handle help
if args.help:
# If no subcommand was specified, give general help
if args.function is None:
print parser.format_help()
sys.exit(1)
# Otherwise pass the help option on to the subcommand
sub_args.append('--help')
# Manually handle the default for "function"
function = "function1" if args.function is None else args.function
# Parse the remaining args as per the selected subcommand
parser = ArgumentParser(prog="%s %s" % (os.path.basename(sys.argv[0]), function))
if function == "function1":
parser.add_argument('-a','--a')
parser.add_argument('-b','--b')
parser.add_argument('-c','--c')
args = parser.parse_args(sub_args)
function1(args.a, args.b, args.c)
elif function == "function2":
...
elif function == "function3":
...
You can do it this way. Use set_defaults(func = your_function)
_parser = argparse.ArgumentParser(description="blalbabla awesome Framework")
_subparsers = _parser.add_subparsers(help="sub command help")
_check_parser = _subparsers.add_parser("check", help="check the code")
_check_parser.add_argument("--script_path", help="can be a python file or dir", required=True)
_check_parser.add_argument("--verbose", help='turn on debug level logging', action='store_true', default=False)
_check_parser.set_defaults(func=do_function)
def do_function(args):
if args.script_path is file:
xxxx
And then you can have correct access to do_function()
parser.add_argument('-a','--a', dest='action', action='store_const', const=function1)
to call perticular function you need add const=function1
you can see this problem for right answer
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.