From c01e2e116671ab5230753565b37031435f9be261 Mon Sep 17 00:00:00 2001 From: eugen-ajechiloae-clearml <97950284+eugen-ajechiloae-clearml@users.noreply.github.com> Date: Sat, 12 Feb 2022 00:01:44 +0200 Subject: [PATCH] Fix group arguments in click (#561) --- clearml/binding/click_bind.py | 17 +++++++++-------- examples/frameworks/click/click_multi_cmd.py | 10 ++++++---- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/clearml/binding/click_bind.py b/clearml/binding/click_bind.py index 0b9bb0ae..c6ffbb08 100644 --- a/clearml/binding/click_bind.py +++ b/clearml/binding/click_bind.py @@ -66,15 +66,15 @@ class PatchClick: @staticmethod def _command_init(original_fn, self, *args, **kwargs): - if self and isinstance(self, Command) and 'name' in kwargs: - PatchClick._num_commands += 1 - if running_remotely(): - pass - else: + if isinstance(self, (Command, Group)) and 'name' in kwargs: + if isinstance(self, Command): + PatchClick._num_commands += 1 + if not running_remotely(): name = kwargs['name'] if name: PatchClick._args[name] = False - PatchClick._args_type[name] = PatchClick._command_type + if isinstance(self, Command): + PatchClick._args_type[name] = PatchClick._command_type # maybe we should take it post initialization if kwargs.get('help'): PatchClick._args_desc[name] = str(kwargs.get('help')) @@ -108,7 +108,7 @@ class PatchClick: ret = original_fn(self, *args, **kwargs) - if isinstance(self, Command) and not isinstance(self, Group): + if isinstance(self, Command): ctx = kwargs.get('ctx') or args[0] if running_remotely(): PatchClick._load_task_params() @@ -118,7 +118,8 @@ class PatchClick: ctx.params[p.name] = p.process_value( ctx, cast_str_to_bool(value, strip=True) if isinstance(p.type, BoolParamType) else value) else: - PatchClick._args[self.name] = True + if not isinstance(self, Group): + PatchClick._args[self.name] = True for k, v in ctx.params.items(): # store passed value PatchClick._args[self.name + '/' + str(k)] = str(v or '') diff --git a/examples/frameworks/click/click_multi_cmd.py b/examples/frameworks/click/click_multi_cmd.py index c9482a5a..40fe3687 100644 --- a/examples/frameworks/click/click_multi_cmd.py +++ b/examples/frameworks/click/click_multi_cmd.py @@ -3,9 +3,12 @@ from clearml import Task @click.group() -def cli(): - task = Task.init(project_name='examples', task_name='click multi command') - print('done') +@click.option('--print-something/--dont-print-something', default=True) +@click.option('--what-to-print', default='something') +def cli(print_something, what_to_print): + Task.init(project_name='examples', task_name='click multi command') + if print_something: + print(what_to_print) @cli.command('hello', help='test help') @@ -15,7 +18,6 @@ def hello(count, name): """Simple program that greets NAME for a total of COUNT times.""" for x in range(count): click.echo("Hello {}!".format(name)) - print('done') CONTEXT_SETTINGS = dict(