clickdc
This is a package to define click command line options from a python dataclass.
You can define a python dataclass from dataclasses with fields that have
proper types when assigned by click parsing library. Then each field is
initialized with a option, argument, command or group from clickdc library.
from dataclasses import dataclass
import clickdc
import click
@dataclass
class Args:
option: bool = clickdc.option(is_flag=True, help="This is an option")
command: int = clickdc.argument(type=int)
When the dataclass is decorated with clickdc.adddc to a function, this
library collects all options and arguments from the dataclass definition and
decorates the function. Then upon calling the function, this library
reconstructs the object using only arguments with names equal to the fields in
the dataclass, removing the arguments in the process.
@click.command(help="This is a command")
@clickdc.adddc("args", Args)
def cli(args: Args):
print(args)
print(clickdc.to_args(args)) # converts dataclass back to string '--option command'
If a keyword argument clickdc is missing, the field name is added with
an underscore replaced by dashes with two front dashes in front, and the
argument is added as a string as positional argument to click.argument
call. If the argument already exists, it is not added twice, for ease
of porting.
long_option: Optional[str] = clickdc.option("-o", help="--long-option is added automatically")
args: str = clickdc.option(help="Positional argument 'args' is added automatically")
Additionally, some keyword arguments to the underlying click.option or
click.argument are inferred from the dataclass field type depending on
on the following conditions. Use field type Any or add type=, for example
type=str, to ad-hoc disable the mechanisms.
- If the field:
- is initialized using an
optionorargument, - does not have the type
Any, - does not have any keyword argument
type required is_flag default nargs count flag_value - and does not have an argument
clickdcpassed withNone
- is initialized using an
- Then:
- if the field is an option, then if the field type
- is
bool, addis_flag=True - is
Optional[T], addtype=T - is
Tuple[T, ...], add type=T, multiple=True, - is any other type
T, addtype=T, required=True
- is
- if the field is an argument, then if the field type:
- is
Tuple[T, ...], addtype=T, nargs=-1 - is any other type
T, addtype=T
- is
- if the field is an option, then if the field type
The correct type for multiple arguments returned by click is Tuple[T, ...].
custom: Tuple[float, ...] = clickdc.option(type=float, multiple=True)
moreargs: Tuple[int, ...] = clickdc.argument(type=int, nargs=5)
options: Tuple[float, ...] = clickdc.option()
arguments: Tuple[int, ...] = clickdc.argument()
The dataclass field initializes functions - command, group, option, argument - take the
same options as their click counterparts with one additional positional
argument called clickdc for this module options. The clickdc can be assigned
just to None to disable automatic adding of the options and keyword
arguments by this module.
option: bool = clickdc.option("--option", is_flag=True, clickdc=None)
command: int = clickdc.argument("command", type=int, clickdc=None)
You can use from pydantic.dataclasses import dataclass to have type checking
for your dataclass.
You can safely mix any clickdc options with click options. Typically, I
would do:
@dataclass
class Args:
options: bool = clickdc.option(help="Options here")
@click.command(help="But command here")
@clickdc.adddc("args", Args)
@click.option("-v", "--verbose", count=True)
def cli(args: Args, verbose: int):
print(args)
print(verbose)
You can inherit dataclasses and decorate using multiple. It works just by
decorating the function with the proper click. function inferred from the
field type.
TODO
dataclasses(default, default_factory) require some questionable polishing.
Epilogue
Written by Kamil Cukrowski
Metadata
Release files for clickdc 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| clickdc-0.1.1.tar.gz | 10.1 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| clickdc-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 18.0 kB
Release files / clickdc-0.1.1.tar.gz
| Download URL | clickdc-0.1.1.tar.gz |
|---|---|
| Size | 10.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
077f04d7ad00509b43aa8e37a9c7df307c00a73f7fef265235ef98ca54235b9b
|
|
BLAKE2b-256 checksum How to use checksums |
c6c6bae77666e82b78efeb11f53adbffef757141d7c236ab933e522b469910b4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.1 CPython/3.12.7
|
Release files / clickdc-0.1.1-py3-none-any.whl
| Download URL | clickdc-0.1.1-py3-none-any.whl |
|---|---|
| Size | 7.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
de5ac1c1af8253b45f9a6f75468efd1f0bb487d52fecb43dc3d9e970ca689fe7
|
|
BLAKE2b-256 checksum How to use checksums |
ec3960a317ee2c67250bf2e70c1791ffd0d2e218927f019147ea89f7a6320727
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/5.1.1 CPython/3.12.7
|
Security for clickdc 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.