-
Notifications
You must be signed in to change notification settings - Fork 3
Add support for selecting subcommands as flags #228
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
base: main
Are you sure you want to change the base?
Conversation
|
Is this meant to call multiple subcommands in one call? Why is it needed? |
Ishirui
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| helpers.dedent( | ||
| """ | ||
| import click | ||
| from dda.cli.base import dynamic_group | ||
| @dynamic_group( | ||
| invoke_without_command=True, | ||
| add_subcommand_selectors=True, | ||
| ) | ||
| @click.pass_context | ||
| def cmd(ctx, **selections): | ||
| if not ctx.invoked_subcommand: | ||
| ctx.obj.display(" ".join(cmd.get_selected_subcommands(ctx, selections))) | ||
| """ | ||
| ) | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Instead of writing at runtime, why not include a new file in testdata/ ? I've already used this pattern for some of my tests and imo it makes it easier to understand what is going on inside the test since you can open your test files with your IDE, get syntax highlighting etc.
|
|
||
| return cmd_object | ||
|
|
||
| def get_params(self, ctx: DynamicContext) -> list[click.Parameter]: # type: ignore[override] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Add @override for clarity (maybe with the link to the source, as is added below)
| if self.__add_subcommand_selectors: | ||
| params.extend( | ||
| click.Option(param_decls=[f"--{command}"], is_flag=True) for command in self.list_commands(ctx) | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of specifying an option on the group to add all subcommands as flags, would it be possible to add an include_as_flag option on the subcommand itself ?
Within one group, we might not always want to have all subcommands available as flags.
It's also unclear to me how this will work if subcommands have flag arguments themselves ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively, would it make sense to create an entirely new subclass of Group ? Considering the main usecase of dda check, it feels like the semantics of this new kind of command group are different enough that it would make sense.
Maybe something like this:
class FlagGroup(DynamicGroup):
# Implement it such that all commands in this group automatically get registered as flags
def cmd(...):
# Here we could implement cmd such that a `FlagGroup` always implements the behavior in your example: running all subcommands sequentially by default, and only selecting some of them if flags have been passed
I assume this is because the main usecase is, for example, "run all linters", with the ability to select only some of them if we want. |
This adds support for selecting subcommands as flags in their parent group which will be used when we soon migrate the static analysis checks/repo validations. Every command group in the chain will be able to run a subset of checks using flags.