Skip to content

Commit e985fb6

Browse files
authored
feat: better ux when missing validate (#469)
1 parent 36ad7cb commit e985fb6

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/stactools/cli/__init__.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# flake8: noqa
22

3+
import sys
4+
from typing import Callable
5+
6+
from click import Group, Command
7+
38
import stactools.core
49

510
try:
@@ -59,11 +64,44 @@ def register_plugin(registry: "Registry") -> None:
5964
from stactools.cli.commands import validate
6065

6166
registry.register_subcommand(validate.create_validate_command)
67+
else:
68+
registry.register_subcommand(
69+
_missing_optional_dependency(command="validate", dependency="validate")
70+
)
6271

6372
if HAS_STAC_CHECK:
6473
from stactools.cli.commands import lint
6574

6675
registry.register_subcommand(lint.create_lint_command)
76+
else:
77+
registry.register_subcommand(
78+
_missing_optional_dependency(command="lint", dependency="validate")
79+
)
80+
81+
82+
def _missing_optional_dependency(
83+
command: str, dependency: str
84+
) -> Callable[[Group], Command]:
85+
def f(cli: Group) -> Command:
86+
@cli.command(
87+
command,
88+
short_help=f"Unsupported (needs `pip install 'stactools[{dependency}]')",
89+
)
90+
def inner() -> None:
91+
print(f"Error: No such command '{command}'", file=sys.stderr)
92+
print(
93+
f"This command is provided by an optional dependency '{dependency}'",
94+
file=sys.stderr,
95+
)
96+
print(
97+
f"To enable, install stactools with the optional dependency: pip install 'stactools[{dependency}]'",
98+
file=sys.stderr,
99+
)
100+
sys.exit(1)
101+
102+
return inner
103+
104+
return f
67105

68106

69107
from stactools.cli.registry import Registry

0 commit comments

Comments
 (0)