Parsing Command Line Parameters Using argparse in Python
- 2021-12-12 09:19:12
- OfStack
Use
argparse
Module sets command-line options for the application.
There are 1 third-party libraries for command-line parsing, but the standard library
argparse
Compared with it, it is not inferior.
Without adding a lot of dependencies, you can write nice command-line tools with practical parameter resolution.
1. Parameter analysis in Python
When parsing command line parameters using argparse, the first step is to configure a
ArgumentParser
Object. This is usually done within the global module, because only _ configuring _ 1 parsers has no side effects.
import argparse
PARSER = argparse.ArgumentParser()
ArgumentParser
The most important method in is
.add_argument()
, it has several variants. By default, it adds 1 parameter and expects 1 value.
PARSER.add_argument("--value")
To view the actual effect, call
.parse_args():
PARSER.parse_args(["--value", "some-value"])
Namespace(value='some-value')
You can also use the = syntax:
PARSER.parse_args(["--value=some-value"])
Namespace(value='some-value')
To shorten the commands entered on the command line, you can also specify a short "alias" for the option:
PARSER.add_argument("--thing", "-t")
You can pass in short options:
PARSER.parse_args("-t some-thing".split())
Namespace(value=None, thing='some-thing')
Or long options:
PARSER.parse_args("--thing some-thing".split())
Namespace(value=None, thing='some-thing')
2. Type
There are many types of parameters available to you. Besides default types, the two most popular ones are Boolean types and counters. Boolean types have 1 variant that defaults to True and 1 variant that defaults to True
False
The variant of.
PARSER.add_argument("--active", action="store_true")
PARSER.add_argument("--no-dry-run", action="store_false", dest="dry_run")
PARSER.add_argument("--verbose", "-v", action="count")
Unless you explicitly pass in--
active
Otherwise, active is False.
dry-run
The default is True, unless--
no-dry-run
. Short options with no value can be juxtaposed.
Passing all parameters results in a non-default state:
PARSER.parse_args("--active --no-dry-run -vvvv".split())
Namespace(value=None, thing=None, active=True, dry_run=False, verbose=4)
The default value is compared to single 1:
PARSER.parse_args("".split())
Namespace(value=None, thing=None, active=False, dry_run=True, verbose=None)
3. Subcommands
Classical
argparse
0
The order inherits "do only one thing at a time and do it to the extreme", but the modern trend puts "several closely related operations" in one.
git
,
podman
And
kubectl
It fully illustrates the popularity of this paradigm.
The argparse library can also do:
PARSER.add_argument("--value")
0
PARSER.add_argument("--value")
1
PARSER.add_argument("--value")
2
MULTI_PARSER.parse_args("search --query name~awesome".split())
PARSER.add_argument("--value")
4
4. Program architecture
One way to use argparse is to use the following structure:
PARSER.add_argument("--value")
5
PARSER.add_argument("--value")
6
In this case, use the
python -m my_package
Run. Alternatively, you can use the
console_scprits
Entry point.
Summary:
argparse
Module is a powerful parser for command line parameters, and there are many functions that can't be introduced here. It can achieve 1 cut as you imagine.