python argument parser default value
parser.add_argument("-v", "--verbose", action="store_true",
default="your default value", help="verbose output")
python argument parser default value
parser.add_argument("-v", "--verbose", action="store_true",
default="your default value", help="verbose output")
python argparser flags
parser.add_argument("-v", "--verbose", action="store_true",
help="verbose output")
use argparse to call function and use argument in function
# 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":
...
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us