Skip to main content

Declarative CLIs with argparse and dataclasses

Project description

Declarative CLIs with argparse and dataclasses.

https://travis-ci.org/mivade/argparse_dataclass.svg?branch=master PyPI

Features

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

  • [✓] Positional arguments

  • [✓] 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

Using dataclass decorator

>>> from argparse_dataclass import dataclass
>>> @dataclass
... class Options:
...     x: int = 42
...     y: bool = False
...
>>> print(Options.parse_args(['--y']))
Options(x=42, y=True)

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)

Enabling choices for an option:

>>> from dataclasses import dataclass, field
>>> from argparse_dataclass import ArgumentParser
>>> from typing import Literal
>>> @dataclass
... class Options:
...     small_integer: Literal[1, 2, 3]
...
>>> parser = ArgumentParser(Options)
>>> print(parser.parse_args(["--small-integer", "3"]))
Options(small_integer=3)

Using different flag names and positional arguments:

>>> from dataclasses import dataclass, field
>>> from argparse_dataclass import ArgumentParser
>>> @dataclass
... class Options:
...     x: int = field(metadata=dict(args=["-x", "--long-name"]))
...     positional: str = field(metadata=dict(args=["positional"]))
...
>>> parser = ArgumentParser(Options)
>>> print(parser.parse_args(["-x", "0", "positional"]))
Options(x=0, positional='positional')
>>> print(parser.parse_args(["--long-name", 0, "positional"]))
Options(x=0, positional='positional')

Using a custom type converter:

>>> from dataclasses import dataclass, field
>>> from argparse_dataclass import ArgumentParser
>>> @dataclass
... class Options:
...     name: str = field(metadata=dict(type=str.title))
...
>>> parser = ArgumentParser(Options)
>>> print(parser.parse_args(["--name", "john doe"]))
Options(name='John Doe')

Configuring a flag to have a default value of True:

>>> from dataclasses import dataclass, field
>>> from argparse_dataclass import ArgumentParser
>>> @dataclass
... class Options:
...     verbose: bool = True
...     logging: bool = field(default=True, metadata=dict(args=["--logging-off"]))
...
>>> parser = ArgumentParser(Options)
>>> print(parser.parse_args([]))
Options(verbose=True, logging=True)
>>> print(parser.parse_args(["--no-verbose", "--logging-off"]))
Options(verbose=False, logging=False)

Configuring a flag so it is required to set:

>>> from dataclasses import dataclass, field
>>> from argparse_dataclass import ArgumentParser
>>> @dataclass
... class Options:
...     logging: bool = field(metadata=dict(required=True))
...
>>> parser = ArgumentParser(Options)
>>> print(parser.parse_args(["--logging"]))
Options(logging=True)
>>> print(parser.parse_args(["--no-logging"]))
Options(logging=False)

Parsing only the known arguments:

>>> from dataclasses import dataclass, field
>>> from argparse_dataclass import ArgumentParser
>>> @dataclass
... class Options:
...     name: str
...     logging: bool = False
...
>>> parser = ArgumentParser(Options)
>>> print(parser.parse_known_args(["--name", "John", "--other-arg", "foo"]))
(Options(name='John', logging=False), ['--other-arg', 'foo'])

Configuring a field with the Optional generic type:

>>> from dataclasses import dataclass, field
>>> from typing import Optional
>>> from argparse_dataclass import ArgumentParser
>>> @dataclass
... class Options:
...     name: str
...     id: Optional[int] = None
...
>>> parser = ArgumentParser(Options)
>>> print(parser.parse_args(["--name", "John"]))
Options(name='John', id=None)
>>> print(parser.parse_args(["--name", "John", "--id", "1234"]))
Options(name='John', id=1234)

Contributors

  • @adsharma

  • @asasine

  • @frank113

  • @jayvdb

  • @jcal-15

  • @mivade

  • @rafi-cohen

License

MIT License

Copyright (c) 2019-2023 argparse_dataclass contributors

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.

Source Distribution

argparse_dataclass-2.0.0.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

argparse_dataclass-2.0.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file argparse_dataclass-2.0.0.tar.gz.

File metadata

  • Download URL: argparse_dataclass-2.0.0.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for argparse_dataclass-2.0.0.tar.gz
Algorithm Hash digest
SHA256 09ab641c914a2f12882337b9c3e5086196dbf2ee6bf0ef67895c74002cc9297f
MD5 f64d70c44a2d76fa19acfabf464bb090
BLAKE2b-256 1affa2e4e328075ddef2ac3c9431eb12247e4ba707a70324894f1e6b4f43c286

See more details on using hashes here.

File details

Details for the file argparse_dataclass-2.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for argparse_dataclass-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3ffc8852a88d9d98d1364b4441a712491320afb91fb56049afd8a51d74bb52d2
MD5 50d8cb5f5efc450c18f85a26703a5d7c
BLAKE2b-256 b366e6c0a808950ba5a4042e2fcedd577fc7401536c7db063de4d7c36be06f84

See more details on using hashes here.

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