A minimal library to make your option-parsing easier.
Project description
Lethargy - Option parsing, for simple apps
Lethargy takes care of option parsing in your scripts, so you can be more productive when writing the important stuff. It's simple, concise, explicit, and Pythonic.
Unlike Click and Argparse, Lethargy is succinct, can be implemented without changing the structure of a program, and requires no boilerplate code. This makes it especially suited to scripting and prototyping.
By design, it is not a full argument parser. If you're building a complete CLI application, you're probably better off using Click.
Installation
Lethargy only depends on the standard library. You can use pip to install lethargy.
pip install lethargy
Usage
from lethargy import Opt, argv
# --use-headers
headers = Opt("use headers").take_flag(argv)
# -f|--file <value>
output_file = Opt("f", "file").takes(1).take_args(argv)
Lethargy returns values appropriate to the option, safely mutating the list.
Getting Started
The default argv
To save you an additional import, lethargy provides lethargy.argv
- a clone of the original argument list. Mutating it will not affect sys.argv
.
Options
Options will automatically convert their names to the appropriate format (-o
or --option
). Casing will be preserved.
>>> from lethargy import Opt
>>> args = ["-", "--debug", "file.txt"]
>>> Opt("debug").take_flag(args)
True
>>> args
['-', 'file.txt']
To take arguments, use the Opt.takes
method.
>>> args = ["-", "--height", "185cm", "people.csv"]
>>> Opt("height").takes(1).take_args(args)
'185cm'
>>> args
['-', 'people.csv']
Taking 1 argument will return a single value. Taking multiple will return a list (see the Argument unpacking section for details).
You can also use a "greedy" value, to take every remaining argument. The canonical way to do this is using the Ellipsis literal (...
).
>>> args = ["--exclude", ".zshrc", ".bashrc"]
>>> Opt("exclude").takes(...).take_args(args)
['.zshrc', '.bashrc']
Argument unpacking
lethargy.Opt
makes sure it's safe to unpack a returned list of values, unless you override the default
parameter.
>>> Opt("x").takes(2).take_args(["-x", "1", "2"])
['1', '2']
>>> Opt("y").takes(2).take_args([])
[None, None]
If there are fewer arguments than expected, lethargy.ArgsError
will be raised and no mutation will occur. Lethargy has clear and readable error messages.
>>> args = ["-z", "bad"]
>>> Opt("z").takes(2).take_args(args)
Traceback (most recent call last):
...
lethargy.ArgsError: expected 2 arguments for '-z <value> <value>', found 1 ('bad')
>>> args
['-z', 'bad']
--debug
and -v
/--verbose
As these are such common options, lethargy includes functions out of the box to take these options.
>>> import lethargy
>>> args = ["-", "--debug", "--verbose", "sheet.csv"]
>>> lethargy.take_verbose(args) # -v or --verbose
True
>>> lethargy.take_debug(args)
True
>>> args
['-', 'sheet.csv']
By convention, passing --verbose
will cause a program to output more information. To make implementing this behaviour easier, lethargy has the print_if
function, which will return print
if its input is true and a dumb function if not.
from lethargy import take_verbose, print_if, argv
debug_print = print_if(take_verbose(argv))
Using str
and repr
Opt
instances provide a logical and consistent string form.
>>> str(Opt("flag"))
'--flag'
>>> str(Opt("e", "example").takes(1))
'-e|--example <value>'
>>> str(Opt("xyz").takes(...))
'--xyz [value]...'
The repr
form makes debugging easy. Note that the order of the names is not guaranteed.
>>> Opt("f", "flag")
<Opt('flag', 'f').takes(0)>
Raising instead of defaulting
If Opt.take_args
is called with raises=True
, lethargy.MissingOption
will be raised instead of returning a default, even if the default is set explicitly.
This behaviour makes it easy to implement mandator options.
from lethargy import Opt, argv, MissingOption
opt = Opt('example').takes(1)
try:
value = opt.take_args(argv, raises=True)
except MissingOption:
print(f'Missing required option: {opt}')
exit(1)
Contributing
Any contributions and feedback are welcome! I'd appreciate it if you could open an issue to discuss changes before submitting a PR, but it's not enforced.
License
Lethargy is released under the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file lethargy-0.2.3.tar.gz
.
File metadata
- Download URL: lethargy-0.2.3.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/0.12.17 CPython/3.7.4 Darwin/19.0.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ba728415be35708120232f59f4b3528c226bd2a9bf274b00a35ba9bf268f4f98 |
|
MD5 | 1346cdeba10ee893d0a934e83e2fdd75 |
|
BLAKE2b-256 | 129291d56ca7aa3cfe23b99ad62fbf81bc0d0ff7de46e860f0d836d3860dfdcb |
File details
Details for the file lethargy-0.2.3-py3-none-any.whl
.
File metadata
- Download URL: lethargy-0.2.3-py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/0.12.17 CPython/3.7.4 Darwin/19.0.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5fbd0a0b1489167f263721140053fca58662c05630154f3d33c0f254280a8449 |
|
MD5 | e0fc99e60f7a3cbd106840c1ddf16acf |
|
BLAKE2b-256 | 7f02bc0b9bb5d5691983053dec2b4447e64499c1495972b74fb58111de04d402 |