Specify options when running python

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

reference

argparse --- command line options, arguments, subcommand parsers A brief summary of how to use ArgumentParser

Recommended Posts

Specify options when running python
Specify options when running flake8 with flycheck
Trap trapped when running a Python Windows executable
What's in that variable (when running a Python script)
Story when iterating python tuple
Specify multiple list indexes (Python)
[GCP] A memorandum when running a Python program on Cloud Functions
Attention when os.mkdir in Python
Error when playing with python
Try running Python with Try Jupyter
Specify python version with virtualenv
Wrapper running Hadoop in Python
Tkinter canvas (window) does not start when running Python 3.7.4 with Atom
What to do if Insecure Platform Warning appears when running Python
A note on using tab completion when running Python interactively on Windows
Determine if standard output is piped when running a Python script
[python] Error when installing library ramkan
Robot running with Arduino and python
Precautions when using pit in Python
Check the behavior when assigning Python
Error when building mac python environment
Set python compile options with pyenv
Support when installing pillow on python3.9
Start Docker container when running Pytest
Python development environment options for May 2020
Yara error when running Cuckoo Sandbox
[Python] Be careful when using print
I tried Grumpy (Go running Python).
Precautions when creating a Python generator
Kill specific python guys running together
Determining which OS is running Python
Python --Symbols used when performing operations
Precautions when using phantomjs from python
When matplotlib doesn't work with python2.7
I tried running prolog with python 3.8.2.
Proxy error when running "pip install"
Use gcc-4.2 when compiling Python (MacOS)
When using MeCab with virtualenv python
Precautions when using six with Python 2.5
[VS Code] ~ Tips when using python ~
When using regular expressions in Python
When writing a program in Python
Eliminate WARNING when starting Python IDLE
[Python] Format when to_csv with pandas
How to hide the command prompt when running python in visual studio 2015
[Django] sqlite version error when running python manage.py in aws cloud9 environment