Skip to content

Commit 45d81f2

Browse files
Elevate monkey patch of click commands to a exposed lib function.
1 parent b0e4ce1 commit 45d81f2

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/planet_auth_utils/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
opt_username,
9797
opt_yes_no,
9898
)
99-
from .commands.cli.util import recast_exceptions_to_click
99+
from .commands.cli.util import recast_exceptions_to_click, monkeypatch_hide_click_cmd_options
100100
from planet_auth_utils.constants import EnvironmentVariables
101101
from planet_auth_utils.plauth_factory import PlanetAuthFactory
102102
from planet_auth_utils.builtins import Builtins
@@ -158,7 +158,9 @@
158158
"opt_token_file",
159159
"opt_username",
160160
"opt_yes_no",
161+
#
161162
"recast_exceptions_to_click",
163+
"monkeypatch_hide_click_cmd_options",
162164
#
163165
"Builtins",
164166
"EnvironmentVariables",

src/planet_auth_utils/commands/cli/util.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import click
1616
import functools
1717
import json
18-
from typing import Optional
18+
from typing import List, Optional
1919

2020
import planet_auth
2121
from planet_auth.constants import AUTH_CONFIG_FILE_SOPS, AUTH_CONFIG_FILE_PLAIN
@@ -26,7 +26,25 @@
2626
from .prompts import prompt_and_change_user_default_profile_if_different
2727

2828

29+
def monkeypatch_hide_click_cmd_options(cmd, hide_options: List[str]):
30+
"""
31+
Monkey patch a click command to hide the specified command options.
32+
Useful when reusing click commands in contexts where you do not
33+
wish to expose all the options.
34+
"""
35+
for hide_option in hide_options:
36+
for param in cmd.params:
37+
if param.name == hide_option:
38+
param.hidden = True
39+
break
40+
41+
2942
def recast_exceptions_to_click(*exceptions, **params): # pylint: disable=W0613
43+
"""
44+
Decorator to catch exceptions and raise them as ClickExceptions.
45+
Useful to apply to `click` commands to supress stack traces that
46+
might be otherwise exposed to the end-user.
47+
"""
3048
if not exceptions:
3149
exceptions = (Exception,)
3250
# params.get('some_arg', 'default')

0 commit comments

Comments
 (0)