Code Monkey home page Code Monkey logo

clickforward's Introduction

clickforward

Forward all click arguments without any parsing and any options from the first non-option positional argument.

See pallets/click#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

clickforward's People

Contributors

kamilcuk avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.