argparse is a built-in module for Python to write user-friendly command-line interfaces. With argparse, you can easily add parameter parsing to Python scripts, allowing them to accept command-line options and arguments. It's also relatively easy to learn, as I'll explain in more detail next.
Inside argparse, there are positional arguments and optional arguments. Positional parameters are essential, in the command line can not be missing this option, and this option does not need to be added before the - or - a kind of symbols; optional parameters can be used with or without the need to use the option before the addition of - or -.
import argparse
parser = ()
parser.add_argument("square", type=int,
help="display a square of a given number")
parser.add_argument("-v", "--verbosity", type=int,
help="increase output verbosity")
args = parser.parse_args()
answer = **2
if == 2:
print(f"the square of {} equals {answer}")
elif == 1:
print(f"{}^2 == {answer}")
else:
print(answer)
In this example, an ArgumentParser object needs to be created first, and then the desired arguments can be added via add_argument." square" is not preceded by - or --, which is a positional argument, and -v or --verbose, which are optional. In addition, you can set the type parameter in add_argument to specify the type of the argument. After setting the parameters, call parse_args to complete the parsing of the parameters, after which you can happily go to parse the value of the specific parameters.
python 20 -v 1/python 20 --verbose 1/python -v 1 20/python --verbose 1 20
>>20^2 == 400
// Note that the positional order of the positional arguments can be switched.
python 20 -v 2/python 20 --verbose 2/python -v 2 20/python --verbose 2 20
>>the square of 20 equals 400
It can also be played like this.
parser.add_argument("-v", "--verbosity", action='count',
help="increase output verbosity")
python 20 -v/python 20 -vv
Quite a few Linux command lines are capable of outputting more detailed information in this way, so that you don't have to specify parameters manually. This code is still flawed though, take a look:
import argparse
parser = ()
parser.add_argument("square", type=int,
help="display a square of a given number")
parser.add_argument("-v", "--verbosity", action='count',
help="increase output verbosity")
args = parser.parse_args()
answer = **2
if >= 2:
print(f"the square of {} equals {answer}")
elif >= 1:
print(f"{}^2 == {answer}")
else:
print(answer)
When we specify the parameter -vvvv or -vvvvv or more v, but instead can only output the result of the simplest calculation, so the code of the branching conditions inside the "==" changed to ">=", so it is done. In order to be more perfect, you can also introduce silent mode.
import argparse
parser = ()
parser.add_argument("square", type=int,
help="display a square of a given number")
parser.add_argument("-v", "--verbosity", action='store_true',
help="increase output verbosity")
parser.add_argument('-q', '--quiet', action='store_true')
args = parser.parse_args()
answer = **2
if :
print(answer)
elif :
print(f"the square of {} equals {answer}")
else:
print(f"{}^2 == {answer}")
This is decent, and the basic functionality of the command line is complete. You can also combine parameters:
import argparse
parser = ()
parser.add_argument("-v", "--verbosity", action='store_true',
help="increase output verbosity")
parser.add_argument('-q', '--quiet', action='store_true')
args = parser.parse_args()
if :
print('v is enabled')
if :
print('q is enabled')
python -v/python -q/python -vq/python -qv
If you see this, please like or share it with your friends, we will continue to bring more interesting knowledge about C++ or Python language in the future.