module llutil.argparser
parse arguments for functions and command line.
This module provides helper functions for commonly used argument processing for functions,
and a FlexibleArgParser for command line argument parsing. The default singleton of this
argument parser is accessable via ice.args
.
Global Variables
- REQUIRED
function isa
an alias for python built-in isinstance
.
function parse_scalar
function as_list
helps to regularize input into list of element.
No matter what is input, will output a list for your iteration.
Basic Examples:
assert as_list("string") == ["string"]
assert as_list(["string", "string"]) == ["string", "string"]
assert as_list(("string", "string")) == ["string", "string"]
assert as_list([["string", "string"]]) == ["string", "string"]
An Application Example:
def func(*args):
return as_list(args)
assert func("a", "b") == ["a", "b"]
assert func(["a", "b"]) == ["a", "b"]
function as_dict
helps to regularize input into a dict.
if maybe_element
is not a dict, will return a dict with single
key as {key:maybe_element}
, else will return maybe_element
.
Args:
-
maybe_element
: a dict or any object. -
key
: the sole key.
Returns:
dict
: ensures to be a dict.
Example:
function is_seq_of
Check whether it is a sequence of some type.
Args:
-
seq
(Sequence): The sequence to be checked. -
expected_type
(type): Expected type of sequence items. -
seq_type
(type, optional): Expected sequence type.
Returns:
bool
: Whether the sequence is valid.
function is_list_of
Check whether it is a list of some type.
A partial method of :func:is_seq_of
.
function is_tuple_of
Check whether it is a tuple of some type.
A partial method of :func:is_seq_of
.
function get_hostname
class ArgumentMissingError
Raised when a required argument is missing from command line.
class ArgumentTypeError
Raised when converting an argument failed.
class FlexibleArgParser
A flexible and lightweight argument parser that saves loads of code.
This module works differently compared to python built-in argparse
module.
-
It accepts two types of command line arguments, i.e. positional and keyword based (options).
-
The keyword based arguments (options) should be specified as
key=value
orkey="value"
. -
The positional arguments is indexed directly using an integer, but this feature is not recommended.
Example:
import ice
# same as `python <script>.py 2 k1=4` in shell.
ice.args.parse_args(["2", "k1=4"])
# setdefault() generally is optional.
ice.args.setdefault("k1", 8, int)
ice.args.setdefault("k2", 8)
assert len(ice.args) == 3
assert 2 == int(ice.args[0]) # default type is str.
assert 4 == ice.args["k1"] # as setdefault specified a type, here a conversion is not needed.
assert 4 == ice.args.k1 # attribute also works.
assert 8 == ice.args.k2 # use default value.
ice.args["k1"] = 1
ice.args.k3 = 1
ice.args.update(k2=0)
ice.args.update({0: -1})
assert -1 == ice.args[0]
assert 1 == ice.args["k3"]
assert 0 == ice.args.k2
Note:
If you manually call
parse_args()
, call it beforesetdefault()
.
method __init__
method get
method hparam_dict
method parse_args
Manually parse args.
Args:
argv
(List[str]): simillar tosys.argv[1:]
.
method set
method setdefault
Set argument value under key
as value
, only if original entry does not exists.
Args:
-
key
(int|str): the keyword. -
value
: default_value to be set when orginal entry does not exists.
Returns:
original or updated value.
method update
simillar to dict.update().