An argument parser for Python built from functional first principles
Project description
$λ
Not the parser that we need, but the parser we deserve.
$λ
is an argument parser for python.
It was built with minimal dependencies from functional first principles.
As a result, it is the most
- versatile
- type-safe
- and concise
argument parser on the market.
Versatile
$λ
provides high-level functionality equivalent to other parsers. But unlike other parsers,
it permits low-level customization to handle arbitrarily complex parsing patterns.
Type-safe
$λ
uses type annotations as much as Python allows. Types are checked
using MyPy
and exported with the package
so that users can also benefit from the type system.
Concise
$λ
provides a variety of syntactic sugar options that enable users
to write parsers with minimal boilerplate.
Documentation
Installation
pip install -U dollar-lambda
Example Usage
For simple settings,@command
can infer the parser for the function signature:
from dollar_lambda import command
@command()
def main(foo: int = 0, bar: str = "hello", baz: bool = False):
return dict(foo=foo, bar=bar, baz=baz)
main("-h")
usage: --foo FOO --bar BAR --baz
This handles defaults:
main()
{'foo': 0, 'bar': 'hello', 'baz': False}
And of course allows the user to supply arguments:
main("--foo", "1", "--bar", "goodbye", "--baz")
{'foo': 1, 'bar': 'goodbye', 'baz': True}
$λ
can also handle far more complex parsing patterns:
from dataclasses import dataclass, field
from dollar_lambda import Args, done
@dataclass
class Args1(Args):
many: int
args: list = field(default_factory=list)
from dollar_lambda import field
@dataclass
class Args2(Args):
different: bool
args: set = field(type=lambda s: {int(x) for x in s}, help="this is a set!")
p = (Args1.parser() | Args2.parser()) >> done()
You can run this parser with one set of args:
p.parse_args("--many", "2", "--args", "abc")
{'many': 2, 'args': ['a', 'b', 'c']}
Or the other set of args:
p.parse_args("--args", "123", "--different") # order doesn't matter
{'args': {1, 2, 3}, 'different': True}
But not both:
p.parse_args("--many", "2", "--different", "--args", "abc")
usage: [--many MANY --args ARGS | --different --args ARGS]
args: this is a set!
Expected '--args'. Got '--different'
Thanks
Special thanks to "Functional Pearls" by Graham Hutton and Erik Meijer for bringing these topics to life.
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
Hashes for dollar_lambda-0.1.8-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c91794ee09733fafe3e52e39bea23d274eafc8e1f831f2b43ac6789969a72a32 |
|
MD5 | 51c67105703e1bba19ca6a5544415e01 |
|
BLAKE2b-256 | 73dd29fefa16e77b17391e97e84994a3934e79cfa7b28daeb3545823bf801246 |