argread: Parse command-line arguments and options¶
This class provides the ArgReader class, instances of
which can be used to customize available command-line options for a
given interface.
The ArgReader class has the function ArgReader.parse(),
which interprets sys.argv and splits it into a list of args and
kwargs. The base ArgReader class will parse command-line
arguments in useful ways, but one of the main intents is to subclass
ArgReader and customize how various CLI arguments are parsed.
Here are some examples of the base interpreter, which can also be used
with the convenience function readkeys().
>>> readkeys(["prog", "arg1", "arg2", "-v", "--name", "argread"])
("arg1", "arg2"), {"v": True, "name": "argread"}
>>> readkeys(["prog", "-v", "1", "2", "-name", "argread", "d=3"])
("2",), {"v": "1", "name": "argread", "d": "3"}
Notice that the 1 goes as the value of the option v. The 2
is not preceded by a str starting with a dash, so it gets
interpreted as an arg.
There is an alternate function readflags() that interprets
multi-letter args starting with a single dash differently:
>>> readflags(["prog", "-lh", "fname"])
("fname",), {"l": True, "h": True}
There is a third function readflagstar() that interprets arguments
like tar
>>> readflagstar(["prog", "-cvf", "fname"])
(), {"c": True, "v": True, "f": "fname"}
These convenience functions make it easy to parse many command-line
inputs with minimal effort, but it is also possible to declare richer
and more specific interfaces by subclassing ArgReader.
For example
class MyParser(ArgReader):
_optlist_noval = (
"default",
"verbose",
)
_optmap = {
"d": "default",
"v": verbose",
}
has two properties. First -d and -v are abbreviations for
--default and --verbose, respectively, because of the entries in
MyParser._optmap. Second, neither option can take a “value”, so
$ prog -d hub url
would be parsed as
("hub", "url"), {"default": True}
In other words, the word hub didn’t get interpreted as the value for
the option d as it would with the default ArgReader.
Another common use case is to convert CLI strings to another type. For
example suppose you have an option i that takes in the index of
something you want to work with, like
$ prog -i 3
The default readkeys() in this case would return
(), {"i": "3"}
you can convert this str "3" to an int with the
following subclass
class MyConverter(ArgReader):
_optconverters = {
"i": int,
}
- class lfc._vendor.argread.ArgReader¶
Class to parse command-line interface arguments
- Call:
>>> parser = ArgReader(**kw)
- Outputs:
- parser:
ArgReader Instance of command-line argument parser
- parser:
- Attributes:
- See also:
- _restrict = False¶
Option to enforce
_optlist
- equal_sign_key = True¶
Option to allow equal-sign options, e.g.
key=valbecomes{"key": "val"}
- exc_cls¶
Base exception class:
Exceptionalias of
ArgReadError
- get_args()¶
Get full list of args and options from parsed inputs
- param_sequence¶
list[str,object] – List of option name and value as parsed (includes duplicates in their original order)
- parse(argv=None)¶
Parse CLI args
- Call:
>>> a, kw = parser.parse(argv=None)
- Inputs:
- Outputs:
- Versions:
2021-11-21
@ddalle: v1.0
- save_arg(arg)¶
Save a positional argument
- save_double_dash(k, v=True)¶
Save a double-dash keyword and value
- save_equal_key(k, v)¶
Save an equal-sign key/value pair, like
"mach=0.9"
- save_single_dash(k, v=True)¶
Save a single-dash keyword and value
- single_dash_lastkey = False¶
Option to interpret multi-char words with a single dash the way POSIX command
tardoes, e.g.-cvf valbecomes{"c": True, "v": True, "f": "val"}
- single_dash_split = False¶
Option to interpret multi-char words with a single dash as single-letter boolean options, e.g.
-lhbecomes{"l": True, "h": True}
- exception lfc._vendor.argread.ArgReadError¶
Base error class for this package
- class lfc._vendor.argread.FlagsArgReader¶
Subclass of
ArgReaderforreadflags()The class attribute
FlagsArgRead.single_dash_splitis set toTrueso that-opt valis interpreted as{"o": True, "p": True, "t": True}
and
"val"becomes an argument.- single_dash_split = True¶
Option to interpret multi-char words with a single dash as single-letter boolean options, e.g.
-lhbecomes{"l": True, "h": True}
- class lfc._vendor.argread.KeysArgReader¶
Subclass of
ArgReaderforreadkeys()The class attribute
KeysArgRead.single_dash_splitis set toFalseso that-opt valis interpreted as{"opt": "val"}
- single_dash_split = False¶
Option to interpret multi-char words with a single dash as single-letter boolean options, e.g.
-lhbecomes{"l": True, "h": True}
- class lfc._vendor.argread.TarFlagsArgReader¶
Subclass of
ArgReaderforreadflags()The class attributes are
TarArgRead.single_dash_split = True TarArgRead.single_dash_lastkey = True
so that
-opt valis interpreted as{"o": True, "p": True, "t": "val"}
- single_dash_lastkey = True¶
Option to interpret multi-char words with a single dash the way POSIX command
tardoes, e.g.-cvf valbecomes{"c": True, "v": True, "f": "val"}
- single_dash_split = True¶
Option to interpret multi-char words with a single dash as single-letter boolean options, e.g.
-lhbecomes{"l": True, "h": True}
- lfc._vendor.argread.readflags(argv=None)¶
Parse args where
-cjbecomesc=True, j=True
- lfc._vendor.argread.readflagstar(argv=None)¶
Parse args where
-cf abecomesc=True, f="a"