Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 19 additions & 15 deletions src/databao_cli/commands/context_engine_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,31 @@
class ClickUserInputCallback(UserInputCallback):
def prompt(
self,
text: str,
property_key: str,
required: bool,
type: Choice | Any | None = None,
default_value: Any | None = None,
is_secret: bool = False,
) -> Any:
show_default: bool = default_value is not None and default_value != ""
show_default = default_value is not None and default_value != ""
final_type = click.Choice(type.choices) if isinstance(type, Choice) else str

# click goes infinite loop if user gives emptry string as an input AND default_value is None
# in order to exit this loop we need to set default value to '' (so it gets accepted)
#
# Code snippet from click:
# while True:
# value = prompt_func(prompt)
# if value:
# break
# elif default is not None:
# value = default
# break
default_value = default_value if default_value else "" if final_type is str else None
return click.prompt(text=text, default=default_value, hide_input=is_secret, type=final_type, show_default=show_default)
# click.prompt does not let optional string fields be skipped cleanly when
# default is None, so use "" and let config_wizard normalize it back to None.
click_default = default_value
if final_type is str and default_value is None:
click_default = ""

return click.prompt(
text=property_key,
default=click_default,
hide_input=is_secret,
type=final_type,
show_default=show_default,
)

def confirm(self, text: str) -> bool:
return click.confirm(text=text)

def on_validation_error(self, property_key: str, input_value: Any, error_message: str) -> None:
click.echo(f"Invalid value for '{property_key}': {error_message}", err=True)
Loading