-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
default (sub)command for Group #430
Comments
Replace this:
with:
|
No go :)
Oh, and this is python 3.5.0. (No that it likely matters, but just to be sure.) |
Then I'd recommend against conflating parameters and subcommands like that though. |
Thanks, but no luck with that either. Sure, we might have to separate this "default" behavior (a task queue runner daemon) from the "run this and this task with such and such parameters" executable. |
It's probably barely implementable, but I guess it's worth a discussion at least. |
I wanted the same behavior. Here's how I implemented an implicit sub command: https://github.com/what-studio/profiling/blob/e6894bcfb975c723b00d764682a785df71c10b70/profiling/__main__.py#L87 |
https://github.com/sublee/click-default-group I made a separate repository for default subcommand. |
@sublee thanks for your example. I wound up using something similar but with a slightly different interface: class DefaultGroup(click.Group):
ignore_unknown_options = True
def __init__(self, *args, **kwargs):
default_command = kwargs.pop('default_command', None)
super(DefaultGroup, self).__init__(*args, **kwargs)
self.default_cmd_name = None
if default_command is not None:
self.set_default_command(default_command)
def set_default_command(self, command):
if isinstance(command, basestring):
cmd_name = command
else:
cmd_name = command.name
self.add_command(command)
self.default_cmd_name = cmd_name
def parse_args(self, ctx, args):
if not args and self.default_cmd_name is not None:
args.insert(0, self.default_cmd_name)
return super(DefaultGroup, self).parse_args(ctx, args)
def get_command(self, ctx, cmd_name):
if cmd_name not in self.commands and self.default_cmd_name is not None:
ctx.args0 = cmd_name
cmd_name = self.default_cmd_name
return super(DefaultGroup, self).get_command(ctx, cmd_name)
def resolve_command(self, ctx, args):
cmd_name, cmd, args = super(DefaultGroup, self).resolve_command(ctx, args)
args0 = getattr(ctx, 'args0', None)
if args0 is not None:
args.insert(0, args0)
return cmd_name, cmd, args This has the additional benefit that I can call |
To elaborate on the last point, heres two different ways to get the same thing: @click.group(cls=DefaultGroup, default_command='a')
def cli():
pass
@cli.command()
@click.option('--test')
def a(test):
click.echo(test)
@cli.command()
def sync():
click.echo('The subcommand')
cli() @click.group(cls=DefaultGroup)
def cli():
pass
@click.command()
@click.option('--test')
def a(test):
click.echo(test)
@click.command()
def sync():
click.echo('The subcommand')
cli.add_command(a)
cli.add_command(sync)
cli.set_default_command(a)
cli() |
Because BTW @sublee your project would be a good fit for https://github.com/click-contrib |
Yeah, that's fine. My interface is different enough that it would require a breaking change for him to incorporate the changes, so it's probably not worth the effort (especially since he's already pushed it to pypi). The snippet is short so I figured I'd just comment here in case anyone else is looking for similar functionality. If/when we add this to click-contrib we can discuss further there. |
Cool |
@llchan Your approach looks better than mine. Can I borrow the approach on |
Yeah, of course! Btw, you should enable issues on your repo so we can move the discussion outside of this core click repo. |
@sublee Please enable issues in your repo @ https://github.com/sublee/click-default-group |
@tony Oh, I didn't know. I've just enabled. |
@sublee You should actually consider moving click-default-group to click-contrib as proposed by @untitaker. The project really looks interesting, but I only found it by chance. It would be a lot more visible in click-contrib! |
@glehmann I just transferred click-default-group to click-contrib. Check it out! |
Using Why is it preferred versus the following anyway (which works, and does not break click-repl? @click.group(invoke_without_command=True)
@click.option(
'--profile',
help='profile to use',
default=u'default'
)
@click.pass_context
def cli(ctx, profile):
ctx.obj = Config(_app, profile)
if ctx.invoked_subcommand is None:
ctx.invoke(cmd_shell) |
I think this discussion should be continued in a new issue on click-default-group, not in this old thread. |
I've a shameless support question or request for enhancement.
We'd like to make a command the default for a group of commands, so basically Group(invoke_without_command=True), plus somehow invoking a command as if it would have been invoked via Command.main().
Currently it turns out that somewhere deep in click the args parsing results in a no such command found error if I use any (positional) arguments.
Thanks!
The text was updated successfully, but these errors were encountered: