Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dollar-lambda-0.1.7.tar.gz (26.4 kB view hashes)

Uploaded Source

Built Distribution

dollar_lambda-0.1.7-py3-none-any.whl (28.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page