Use ArgumentParser to specify options when running python
option_argparser.py
from argparse import ArgumentParser
def get_option(level):
argparser = ArgumentParser()
argparser.add_argument('-l', '--level', type=int,
default=level,
help='Specify level')
return argparser.parse_args()
level=1
args = get_option(level)
print('level : ' + str(args.level))
Execution result
$ python3 option_argparser.py
level : 1
$ python3 option_argparser.py -l 3
level : 3
$ python3 option_argparser.py --level 3
level : 3
$ python3 option_argparser.py --help
usage: option_argparser.py [-h] [-l LEVEL]
optional arguments:
-h, --help show this help message and exit
-l LEVEL, --level LEVEL
Specify level
argparse --- command line options, arguments, subcommand parsers A brief summary of how to use ArgumentParser
Recommended Posts