|
1 | 1 | # flake8: noqa |
2 | 2 |
|
| 3 | +import sys |
| 4 | +from typing import Callable |
| 5 | + |
| 6 | +from click import Group, Command |
| 7 | + |
3 | 8 | import stactools.core |
4 | 9 |
|
5 | 10 | try: |
@@ -59,11 +64,44 @@ def register_plugin(registry: "Registry") -> None: |
59 | 64 | from stactools.cli.commands import validate |
60 | 65 |
|
61 | 66 | registry.register_subcommand(validate.create_validate_command) |
| 67 | + else: |
| 68 | + registry.register_subcommand( |
| 69 | + _missing_optional_dependency(command="validate", dependency="validate") |
| 70 | + ) |
62 | 71 |
|
63 | 72 | if HAS_STAC_CHECK: |
64 | 73 | from stactools.cli.commands import lint |
65 | 74 |
|
66 | 75 | 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 |
67 | 105 |
|
68 | 106 |
|
69 | 107 | from stactools.cli.registry import Registry |
|
0 commit comments