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

Pass top-level args to subcommand invocation #62

Conversation

theazureshadow
Copy link
Contributor

Passes the original parsed argument values as defaults to invoke the new context. Open to suggestions for a better fix, but this addresses the major issue.

Fixes #58

@theazureshadow
Copy link
Contributor Author

#!/usr/bin/env python

from click import echo, group, command, option, argument, pass_context
from click_repl import repl

@group(invoke_without_command=True)
@option('--user', required=True)
@pass_context
def cli(ctx, user):
    if ctx.invoked_subcommand is None:
        echo('Top-level user: {}'.format(user))
        repl(ctx)

@cli.command()
@option('--user')
def c1(user):
    echo('Executed C1 with {}!'.format(user))

cli()

And running it looks like:

./tmp --user csullins
Top-level user: csullins
> c1
Executed C1 with None!
> c1 --user bob
Executed C1 with bob!

@theazureshadow
Copy link
Contributor Author

theazureshadow commented Sep 20, 2019

Looks like there are two cases: one where the REPL is invoked from user code (repl(ctx)) and one where the REPL is invoked as a subcommand (register_repl(cli)). The difference is on this line:

group_ctx = old_ctx.parent or old_ctx

The current fix only covers one of those two cases. I was able to cover both by selecting the correct context to forward params, but it makes me feel like this solution is fairly fragile. Any suggestions?

Copy link
Collaborator

@auvipy auvipy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you provide some unit tests to check the codes?

GhostOps77 added a commit to GhostOps77/click-repl that referenced this pull request Feb 2, 2023
	solved failinf test case:   tests/test_repl.py
GhostOps77 added a commit to GhostOps77/click-repl that referenced this pull request Feb 4, 2023
	Added some test cases:   tests/test_command_collection.py
auvipy pushed a commit that referenced this pull request Feb 5, 2023
	Added some test cases:   tests/test_command_collection.py
@auvipy auvipy closed this Feb 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Main group with required options
2 participants