Skip to content

Commit

Permalink
#84 Added get_opt_name() and get_bool_opt_name() functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ahsimb committed Oct 18, 2024
1 parent c76ef87 commit ac53788
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
4 changes: 4 additions & 0 deletions doc/changes/unreleased.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# Unreleased

## Features

* #84 Added get_opt_name() and get_bool_opt_name() functions to the CLI framework.
28 changes: 21 additions & 7 deletions exasol/python_extension_common/cli/std_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)


Expand Down
10 changes: 10 additions & 0 deletions test/unit/cli/test_std_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ac53788

Please sign in to comment.