A command line argument is a character string specified when starting a program from the command input screen (command line) of a computer. Also, the value of the variable etc. that received this on the program side.
http://e-words.jp/w/%E3%82%B3%E3%83%9E%E3%83%B3%E3%83%89%E3%83%A9%E3%82%A4%E3%83%B3%E5%BC%95%E6%95%B0.html
Command line arguments are the arguments of the main function. You can give values to command line arguments from the command line when you run it.
http://www.ritsumei.ac.jp/~mmr14135/johoWeb/cmnds.html
>>> ls -a
↑ here
There are three main ways to receive command line arguments in Python.
sys.argv
click
is.
sys.argv
This is the most recognized (I think) method. Get it using sys, the Python standard library.
args.py
import sys
def main() -> None:
args = sys.argv
print(args)
if __name__=='__main__':
main()
Run
>>> python args.py aaa 123 hoge
['args.py', 'aaa', '123', 'hoge']
merit | デmerit |
---|---|
Easy to write | I don't know what type will come |
Python standard library | No help |
argparse
A Python standard library dedicated to command line arguments. You can specify the Help function and type. There is a habit of handling.
args2.py
import argparse
def main() -> None:
parser = argparse.ArgumentParser(description='Description')
parser.add_argument('description', help='Argument description') #If not specified at runtime, an error will occur.
parser.add_argument('hoge', help='hogehoge') #The order is fixed.
parser.add_argument('--foo', help='option') #1 hyphen~If you attach two, you do not have to specify it at runtime.
args = parser.parse_args()
print(args.description)
print(args.hoge)
print(args.foo)
if __name__ == "__main__":
main()
Run
>>> python args2.py -h
usage: aaa.py [-h] [--foo FOO] description hoge
Description
positional arguments:
description Argument description
hoge hogehoge
optional arguments:
-h, --help show this help message and exit
--foo FOO option
>>> python args2.py aaa bbb
aaa
bbb
None
>>> python args2.py aaa bbb --foo 123
aaa
bbb
123
merit | デmerit |
---|---|
Rich in features | complexity |
There is help | |
Python standard library |
click
Python command line parser. In other words, a module for command line analysis.
Since it is not a Python standard library, it needs to be installed separately, but the above two are omitted for ease of understanding and usability. Therefore, it is OK if you remember this
By the way, click
, Flask
, and jinja
are same project.
#Installation
pip install click
args3.py
import click
@click.command()
@click.option('--args', prompt=True, help='Description')
@click.option('--hoge', prompt=True, is_flag=True, help='hogehoge')
def main(args: str, hoge: bool) -> None:
if hoge:
print(args)
if __name__ == "__main__":
main()
Run
>>> python args3.py --args foo --hoge
foo
>>> python args3.py
Args: aaaaa
Hoge [y/N]: y
aaaaa
merit | デmerit |
---|---|
Rich in features | Not a Python standard library |
Easy to use | |
Can be displayed at the prompt | |
You can also display the progress bar | |
You can hide your password entry | |
Multiple inputs are possible |
How to handle command line arguments in Python (sys.argv, argparse) [How to pass command line arguments in Python](https://intellectual-curiosity.tokyo/2018/12/14/python%E3%81%A7%E3%82%B3%E3%83%9E%E3%83 % B3% E3% 83% 89% E3% 83% A9% E3% 82% A4% E3% 83% B3% E5% BC% 95% E6% 95% B0% E3% 82% 92% E6% B8% A1 % E3% 81% 99% E6% 96% B9% E6% B3% 95 /) A brief summary of how to use ArgumentParser Python: Click on the command line parser was too convenient
Recommended Posts