Skip to main content

``argparse_dataclass``

Project description

Declarative CLIs with argparse and dataclasses.

Features

Features marked with a ✓ are currently implemented; features marked with a ⊘ are not yet implemented.

  • [✓] Boolean flags
  • [✓] Integer, string, float, and other simple types as arguments
  • [✓] Default values
  • [⊘] Arguments with a finite set of choices
  • [⊘] Subcommands
  • [⊘] Mutually exclusive groups

Examples

A simple parser with flags:

>>> from dataclasses import dataclass
>>> from argparse_dataclass import ArgumentParser
>>> @dataclass
... class Options:
...     verbose: bool
...     other_flag: bool
...
>>> parser = ArgumentParser(Options)
>>> print(parser.parse_args([]))
Options(verbose=False, other_flag=False)
>>> print(parser.parse_args(["--verbose", "--other-flag"]))
Options(verbose=True, other_flag=True)

Using defaults:

>>> from dataclasses import dataclass, field
>>> from argparse_dataclass import ArgumentParser
>>> @dataclass
... class Options:
...     x: int = 1
...     y: int = field(default=2)
...     z: float = field(default_factory=lambda: 3.14)
...
>>> parser = ArgumentParser(Options)
>>> print(parser.parse_args([]))
Options(x=1, y=2, z=3.14)

License

MIT License

Copyright (c) 2019 Michael V. DePalatis

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Project details


Download files

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

Files for argparse_dataclass, version 0.1.dev1
Filename, size File type Python version Upload date Hashes
Filename, size argparse_dataclass-0.1.dev1-py3-none-any.whl (4.9 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size argparse_dataclass-0.1.dev1.tar.gz (3.6 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page