From ac537881682eb077dfa2aa0a694fb27cf1bca065 Mon Sep 17 00:00:00 2001 From: mibe Date: Fri, 18 Oct 2024 09:53:40 +0100 Subject: [PATCH] #84 Added get_opt_name() and get_bool_opt_name() functions --- doc/changes/unreleased.md | 4 +++ .../cli/std_options.py | 28 ++++++++++++++----- test/unit/cli/test_std_options.py | 10 +++++++ 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/doc/changes/unreleased.md b/doc/changes/unreleased.md index 79e701b..a3b4e40 100644 --- a/doc/changes/unreleased.md +++ b/doc/changes/unreleased.md @@ -1 +1,5 @@ # Unreleased + +## Features + +* #84 Added get_opt_name() and get_bool_opt_name() functions to the CLI framework. diff --git a/exasol/python_extension_common/cli/std_options.py b/exasol/python_extension_common/cli/std_options.py index 38534c4..6c33631 100644 --- a/exasol/python_extension_common/cli/std_options.py +++ b/exasol/python_extension_common/cli/std_options.py @@ -183,6 +183,24 @@ def make_option_secret(option_params: dict[str, Any], prompt: str) -> None: option_params['callback'] = secret_callback +def get_opt_name(std_param: StdParamOrName) -> str: + """ + Converts a parameter, e.g. "db_user", to the option definition, e.g. "--db-user" + """ + std_param_name = _get_param_name(std_param) + return f'--{std_param_name.replace("_", "-")}' + + +def get_bool_opt_name(std_param: StdParamOrName) -> str: + """ + Converts a boolean parameter, e.g. "alter_system", to the option definition, e.g. + "--alter-system/--no-alter-system". + """ + std_param_name = _get_param_name(std_param) + opt_name = std_param_name.replace("_", "-") + return f'--{opt_name}/--no-{opt_name}' + + def create_std_option(std_param: StdParamOrName, **kwargs) -> click.Option: """ Creates a Click option. @@ -195,14 +213,10 @@ def create_std_option(std_param: StdParamOrName, **kwargs) -> click.Option: kwargs: The option properties. """ - std_param_name = _get_param_name(std_param) - option_name = std_param_name.replace('_', '-') - if kwargs.get('type') == bool: - param_decls = [f'--{option_name}/--no-{option_name}'] - else: - param_decls = [f'--{option_name}'] + param_decls = [get_bool_opt_name(std_param) if kwargs.get('type') == bool + else get_opt_name(std_param)] if kwargs.get('hide_input', False): - make_option_secret(kwargs, prompt=std_param_name.replace('_', ' ')) + make_option_secret(kwargs, prompt=_get_param_name(std_param).replace('_', ' ')) return click.Option(param_decls, **kwargs) diff --git a/test/unit/cli/test_std_options.py b/test/unit/cli/test_std_options.py index 4602000..f0ef80d 100644 --- a/test/unit/cli/test_std_options.py +++ b/test/unit/cli/test_std_options.py @@ -10,6 +10,8 @@ StdParams, create_std_option, select_std_options, + get_opt_name, + get_bool_opt_name, get_cli_arg, kwargs_to_cli_args, check_params @@ -43,6 +45,14 @@ def test_parameter_formatters_2params(): assert ctx.params[container_name_param] == 'downloaded-1.3.2' +def test_get_opt_name(): + assert get_opt_name('db_user') == '--db-user' + + +def test_get_bool_opt_name(): + assert get_bool_opt_name('alter_system') == '--alter-system/--no-alter-system' + + def test_create_std_option(): opt = create_std_option(StdParams.bucketfs_name, type=str) assert opt.name == StdParams.bucketfs_name.name