Forward all arguments using click without any parsing and without -
Project description
clickforward
Forward all click arguments without any parsing and any options from the first non-option positional argument.
See https://github.com/pallets/click/pull/2686
Usage
This change introduces a new clickforward.FORWARD
argument type, which
stops the parser from further parsing arguments. Is it now possible to
forward arguments without any parsing.
Set the argument
with nargs=-1
and type=clickforward.FORWARD
to
perfectly capture all arguments.
Example implementation of docker run` command:
import clickforward
import click
@click.group()
@click.option("-v", "--verbose", is_flag=True)
def docker(verbose):
pass
@docker.command()
@click.option("-v", "--verbose", is_flag=True)
@click.option("-u", "--user")
@click.argument("image")
@click.argument("command", nargs=-1, type=clickforward.FORWARD)
def run(verbose, user, image, command):
cmdline: List[str] = (
["docker"]
+ ["run"]
+ ([f"-u{user}"] if user else [])
+ [image]
+ list(command)
)
click.echo(" ".join(shlex.quote(x) for x in cmdline))
docker()
Allows for forwarding sh --help -u -v
to the docker container.
$ python ./docker.py -v run -u kamil alpine sh --help -u -v
['docker', 'run', '-ukamil', 'alpine', 'sh', '--help', '-u', '-v']
How it works
The act of importing the module replaces the click.parser.OptionParser
parsing function of arguments with custom logic.
I have only tested with click==8.1.7.
Epilogue
Written by Kamil Cukrowski
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
Hashes for clickforward-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 229ca6ab5e11a23205409e9623f029241d287c5351e33aa9e15870f6fcf8dd8f |
|
MD5 | 4028bdf80a3aa37308138b5254d02fcd |
|
BLAKE2b-256 | 603e58aaaaf927f68aaeb8f64e88c6ddfeee5880ec7646bcbcdce3b7b4b086c3 |