Skip to content

Commit

Permalink
DynamicEntryPointCommandGroup: Add support for shared options
Browse files Browse the repository at this point in the history
The idea for the `DynamicEntryPointCommandGroup` is to easily create a
command that has subcommands that are created dynamically based on the
entry points that are registered in a certain group. Each entry point
would provide the specific CLI options that it would require. However,
often there will be shared options that are not specific to any entry
point but all of them would require.

Here the `shared_options` argument is added to the constructor of the
`DynamicEntryPointCommandGroup`. It takes a list of `click.Option`
instances and when defined, these options will be added in reverse order
after the options of the specific entry point have been added. This
ensures that the shared options will be available to all dynamically
generated subcommands.
  • Loading branch information
sphuber committed May 17, 2023
1 parent 7de711b commit 220a65c
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion aiida/cmdline/groups/dynamic.py
Expand Up @@ -41,12 +41,20 @@ def cmd_create():
"""

def __init__(self, command, entry_point_group: str, entry_point_name_filter=r'.*', **kwargs):
def __init__(
self,
command,
entry_point_group: str,
entry_point_name_filter: str = r'.*',
shared_options: list[click.Option] | None = None,
**kwargs
):
super().__init__(**kwargs)
self.command = command
self.entry_point_group = entry_point_group
self.entry_point_name_filter = entry_point_name_filter
self.factory = ENTRY_POINT_GROUP_FACTORY_MAPPING[entry_point_group]
self.shared_options = shared_options

def list_commands(self, ctx) -> list[str]:
"""Return the sorted list of subcommands for this group.
Expand Down Expand Up @@ -97,6 +105,12 @@ def apply_options(func):
for option in options_list:
func = option(func)

shared_options = self.shared_options or []
shared_options.reverse()

for option in shared_options:
func = option(func)

return func

return apply_options
Expand Down

0 comments on commit 220a65c

Please sign in to comment.