Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add FORWARD parameter type and stop parsing arguments when FORWARD is… #2686

Closed
wants to merge 2 commits into from

Conversation

Kamilcuk
Copy link

@Kamilcuk Kamilcuk commented Mar 2, 2024

This change introduces a new click.FORWARD argument type, which stops the parser from further parsing arguments. Is it now possible to forward arguments without any parsing.
For example:

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=click.FORWARD)
def run(verbose, user, image, command):
    cmdline = ["docker"] + ([f"-u{user}"] if user else []) + ["run", image] + list(command)
    print(cmdline)


docker()

Allows for the following:

$ python ./docker.py -v run -u kamil alpine sh --help -u -v
['docker', '-ukamil', 'run', 'alpine', 'sh', '--help', '-u', '-v']

Issues:

Checklist:

  • Add tests that demonstrate the correct behavior of the change. Tests should fail without the change.
  • Add or update relevant docs, in the docs folder and in code.
  • Add an entry in CHANGES.rst summarizing the change and linking to the issue.
  • Add .. versionchanged:: entries in any relevant code docs.
  • Run pre-commit hooks and fix any issues.
  • Run pytest and tox, no tests failed.

@davidism
Copy link
Member

davidism commented Mar 2, 2024

I'm not really clear what this does compared to adding a nargs=-1 argument, or using --. Regardless, I'm currently rewriting the entire parser, so this code won't be correct anyway.

@davidism davidism closed this Mar 2, 2024
@Kamilcuk
Copy link
Author

Kamilcuk commented Mar 2, 2024

or using --

It doesn't require it. See the attached issues.

docker run does not require --, you can run docker run alpine ls --help and get help for ls, not for docker. In contrast, that is impossible with click.

I'm currently rewriting the entire parser, so this code won't be correct anyway.

Ok, good luck! In that case, I will not fix the checklist.

I really hope that the new parser will not require -- to stop parsing. If it does, please let me know, I would want to invest time that it does not.

In any case, I use python3.7. I will back port this and publish on pip as I need it.

Thanks! Click is great, thanks for making it!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants