Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SNOW-1874231] Fixes for click 8.1.8 (custom help option) #1979

Merged
merged 5 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ requires-python = ">=3.10"
description = "Snowflake CLI"
readme = "README.md"
dependencies = [
"click==8.1.7",
"jinja2==3.1.5",
"pluggy==1.5.0",
"PyYAML==6.0.2",
Expand Down
1 change: 0 additions & 1 deletion snyk/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
click==8.1.7
jinja2==3.1.5
pluggy==1.5.0
PyYAML==6.0.2
Expand Down
22 changes: 22 additions & 0 deletions src/snowflake/cli/_app/cli_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ def _docs_callback(value: bool):
_exit_with_cleanup()


@_do_not_execute_on_completion
@_commands_registration.after
def _help_callback(value: bool):
if value:
ctx = click.get_current_context()
typer.echo(ctx.get_help())
_exit_with_cleanup()


@_do_not_execute_on_completion
@_commands_registration.after
def _commands_structure_callback(value: bool):
Expand Down Expand Up @@ -157,10 +166,23 @@ def app_factory() -> SnowCliMainTyper:
invoke_without_command=True,
epilog=new_version_msg,
result_callback=show_new_version_banner_callback(new_version_msg),
add_help_option=False, # custom_help option added below
help=f"Snowflake CLI tool for developers [v{__about__.VERSION}]",
)
def default(
ctx: typer.Context,
# We need a custom help option with _help_callback called after command registration
# to have all commands visible in the help.
# This is required since click 8.1.8, when the default help option
# has started to being executed before our eager options, including command registration.
custom_help: bool = typer.Option(
None,
"--help",
"-h",
help="Show this message and exit.",
callback=_help_callback,
is_eager=True,
),
version: bool = typer.Option(
None,
"--version",
Expand Down
4 changes: 2 additions & 2 deletions tests/__snapshots__/test_help_messages.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Snowflake CLI tool for developers [v0.0.0-test_patched]

+- Options --------------------------------------------------------------------+
| --help -h Show this message and exit. |
| --version Shows version of the Snowflake CLI |
| --info Shows information about the Snowflake |
| CLI |
Expand All @@ -18,7 +19,6 @@
| --show-completion Show completion for the current shell, |
| to copy it or customize the |
| installation. |
| --help -h Show this message and exit. |
+------------------------------------------------------------------------------+
+- Commands -------------------------------------------------------------------+
| app Manages a Snowflake Native App |
Expand Down Expand Up @@ -10914,6 +10914,7 @@
Snowflake CLI tool for developers [v0.0.0-test_patched]

+- Options --------------------------------------------------------------------+
| --help -h Show this message and exit. |
| --version Shows version of the Snowflake CLI |
| --info Shows information about the Snowflake |
| CLI |
Expand All @@ -10925,7 +10926,6 @@
| --show-completion Show completion for the current shell, |
| to copy it or customize the |
| installation. |
| --help -h Show this message and exit. |
+------------------------------------------------------------------------------+
+- Commands -------------------------------------------------------------------+
| app Manages a Snowflake Native App |
Expand Down
10 changes: 1 addition & 9 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,8 @@ def _check(command: Command, path: t.Optional[t.List] = None):
_check(command_info, [*path, command_name])

def check_options_for_duplicates(params: t.List[TyperOption]) -> t.Set[str]:
RESERVED_FLAGS = ["--help"] # noqa: N806

flags = [flag for param in params for flag in param.opts]
return set(
[
flag
for flag in flags
if (flags.count(flag) > 1 or flag in RESERVED_FLAGS)
]
)
return set([flag for flag in flags if (flags.count(flag) > 1)])

_check(ctx.command)

Expand Down
2 changes: 1 addition & 1 deletion tests_e2e/__snapshots__/test_installation.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Snowflake CLI tool for developers [v3.3.0.dev0]

+- Options --------------------------------------------------------------------+
| --help -h Show this message and exit. |
| --version Shows version of the Snowflake CLI |
| --info Shows information about the Snowflake |
| CLI |
Expand All @@ -28,7 +29,6 @@
| --show-completion Show completion for the current shell, |
| to copy it or customize the |
| installation. |
| --help -h Show this message and exit. |
+------------------------------------------------------------------------------+
+- Commands -------------------------------------------------------------------+
| app Manages a Snowflake Native App |
Expand Down
Loading