collection of click extensions
Project description
click-plus
Introduction
This is a work in progress to provide a collection of click extensions.
- click.plus.extension
- support for reusable groups of click arguments/options across scripts.
click.plus.extension example
The use case for this package is to collect and organize common options shared across scripts:
@click.command()
@click.argument("value", type=int)
@click.plus.extension.configure(["my-common-args"], factor=2)
def main(value):
The my-common-args
name is defined somewhere and provides to the main a common group of arguments.
Example
Let's say we have a set of (fictional) scripts needing
a common input int argument (value
) a flag (--boost
) to multiply the value with:
.
├── by-ten.py
├── by-two.py
└── my_common_args.py
$> by-two.py --boost 3 9
54 (eg. value=9 * boost=3 * factor=2)
$> by-ten.py --boost 4 2
80 (eg. value=2 * boost=4 * factor=10)
The implementation of by-two.py/by-ten.py looks like:
import click
import click.plus.extension
import my_common_args
@click.command()
@click.argument("value", type=int)
@click.plus.extension.configure(["myarguments"], factor=2)
def main(value):
print("Got", value)
if __name__ == "__main__":
main()
NOTE: in the by-ten.py just replace the factor=2 with factor=10
The my_common_args.py
module contains the "myarguments" definition:
import click
from click.plus.extension import api
class MyArguments(api.ExtensionBase):
# this is the name used in the extension.configure()
# otherwise it will fall back to MyArguments
NAME = "myarguments"
# here you can add as many click arguments/options
# note the form click.option()(fn) calling style
def setup(self, fn, arguments):
fn = click.option("--boost", type=int, default=1)(fn)
return fn
# arguments
# are the keyword arguments to extension.configure()
# (eg. factor=2 or factor=10)
# kwargs
# are the keyword arguments to be fed to main()
# you can return a new dict here or alter in place
def process(self, kwargs, arguments):
value = kwargs["value"]
# because boost is popped, it won't reach the main()
boost = kwargs.pop("boost")
factor = arguments["factor"]
kwargs["value"] = value * boost * factor
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
click-plus-0.0.1b24.tar.gz
(6.0 kB
view hashes)
Built Distribution
Close
Hashes for click_plus-0.0.1b24-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 85850c62b26e7cf8eb152d3ea30ee1fa26f59489a2a6e03f2fa680df2144d864 |
|
MD5 | 30710234ca1c1d9fbb860b7c0157b870 |
|
BLAKE2b-256 | df98a1e42b9bcc1b0dda598d3583b2b0545ddf89487e998e756c7b1356439a54 |