cape.argread: Command-Line Argument Processor

Parse command-line inputs based on one of two methods. The first method counts both “-” and “–” as prefixes for keyword names; this is common among many advanced programs. For example, the two following examples would be treated as equivalent (assuming it is called by some script `myScript.py.

$ myScript.py --v --i test.txt
$ myScript.py -v -i test.txt

The second method assumes single-hyphen options are single-character flags that can be combined. This is common in many built-in Unix/Linux utilities. Consider how ls -lh is interpreted. The following two examples would be interpreted equivalently.

$ myScript.py -v -i
$ myScript.py -vi

A third method is provided to have similar behavior to the Unix tar command. In this case, the following two commands will be different.

$ myScript.py -cf mytar.tar
$ myScript.py --cf mytar.tar

The first example sets c to True and f to "mytar.tar"; the second command sets cf to "mytar.tar".

cape.argread.readflags(argv=None)

Parse args where -cj becomes c=True, j=True

Call:
>>> a, kw = readflags(argv=None)
Inputs:
argv: {None} | list[str]

List of args other than sys.argv

Outputs:
a: list[str]

List of positional args

kw: dict[str | bool]

Keyword arguments

Versions:
  • 2021-12-01 @ddalle: Version 1.0

cape.argread.readflagstar(argv=None)

Parse args where -cf a becomes c=True, f="a"

Call:
>>> a, kw = readflags(argv=None)
Inputs:
argv: {None} | list[str]

List of args other than sys.argv

Outputs:
a: list[str]

List of positional args

kw: dict[str | bool]

Keyword arguments

Versions:
  • 2021-12-01 @ddalle: Version 1.0

cape.argread.readkeys(argv=None)

Parse args where -cj becomes cj=True

Call:
>>> a, kw = readkeys(argv=None)
Inputs:
argv: {None} | list[str]

List of args other than sys.argv

Outputs:
a: list[str]

List of positional args

kw: dict[str | bool]

Keyword arguments

Versions:
  • 2021-12-01 @ddalle: Version 1.0