Pydargs allows configuring a dataclass through command line arguments.
Project description
pydargs
Pydargs allows configuring a dataclass through command line arguments.
Installation
Pydargs can be installed with your favourite package manager. For example:
pip install pydargs
Usage
A minimal usage example would be:
from dataclasses import dataclass
from pydargs import parse
@dataclass
class Config:
number: int
some_string: str = "abc"
if __name__ == "__main__":
config = parse(Config)
After which this entrypoint can be called with
entrypoint --number 42
or
entrypoint --number 42 --some-string abcd
Supported Types
The base types are supported: int
, float
, str
, bool
, as well as:
- Enums or literals comprised of those types.
- Date and datetime, with an optional
date_format
metadata field:your_date: date = field(metadata=dict(date_format="%m-%d-%Y"))
. When not provided dates in ISO 8601 format are accepted. - Lists of those types, either denoted as e.g.
list[int]
orSequence[int]
. Multiple arguments to anumbers: list[int]
field can be provided as--numbers 1 2 3
. A list-field without a default will require at least a single value to be provided. If a default is provided, it will be completely replaced by any arguments, if provided. - Optional types, denoted as e.g.
typing.Optional[int]
orint | None
(for Python 3.10 and above). Any argument passed is assumed to be of the provided type and can never beNone
. - Unions of types, denoted as e.g.
typing.Union[int, str]
orint | str
. Each argument will be parsed into the first type that returns a valid result. Note that this means thatstr | int
will always result in a value of typestr
. - Any other type that can be instantiated from a string, such as
Path
.
Metadata
Additional options can be provided to the dataclass field metadata.
The following metadata fields are supported:
positional
Set positional=True
to create a positional argument instead of an option.
from dataclasses import dataclass, field
@dataclass
class Config:
argument: str = field(metadata=dict(positional=True))
as_flags
Set as_flags=True
for a boolean field:
from dataclasses import dataclass, field
@dataclass
class Config:
verbose: bool = field(default=False, metadata=dict(as_flags=True))
which would create the arguments --verbose
and --no-verbose
to
set the value of verbose
to True
or False
respectively, instead
of a single option that requires a value like --verbose True
.
parser
Provide a custom type converter that parses the argument into the desired type. For example:
from dataclasses import dataclass, field
from json import loads
@dataclass
class Config:
list_of_numbers: list[int] = field(metadata=dict(parser=loads))
This would parse --list-of-numbers [1, 2, 3]
into the list [1, 2, 3]
. Note that the error message returned
when providing invalid input is lacking any details. Also, no validation is performed to verify that the returned
type matches the field type. In the above example, --list-of-numbers '{"a": "b"}'
would result in list_of_numbers
being the dictionary {"a": "b"}
without any kind of warning.
Ignoring fields
Fields can be ignored by adding the ignore_arg
metadata field:
@dataclass
class Config:
number: int
ignored: str = field(metadata=dict(ignore_arg=True))
When indicated, this field is not added to the parser and cannot be overridden with an argument.
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 pydargs-0.5.1.tar.gz
.
File metadata
- Download URL: pydargs-0.5.1.tar.gz
- Upload date:
- Size: 10.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 03e315d4991ed4ff42d6b127ad4b1c55568cedd37313221894a497550ce1dc52 |
|
MD5 | 6685a2e72d5192fd2b551b8697ac0d91 |
|
BLAKE2b-256 | 934592a0f12a7ef4882c067a954addd5746bb9cde25b5e37edb114b072bcbba0 |
Provenance
File details
Details for the file pydargs-0.5.1-py3-none-any.whl
.
File metadata
- Download URL: pydargs-0.5.1-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e458c52a49f506b33f59f2e2143cb0c496ebc01baa54dd787eb1f891d8e59b5f |
|
MD5 | 0588a30a1ecde6eb1778ba5ae575940d |
|
BLAKE2b-256 | 8a294912789c176b962630e7be18454b358f14672c57880b4c23a5223c11181d |