Skip to content

Commit 52cf580

Browse files
committed
📝 Extend the manual w/ subcommand guide
1 parent 7dde1df commit 52cf580

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/pre_commit_terraform/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,37 @@ POSIX-inspired CLI app.
5555
Since it's an app that can be executed outside the [`pre-commit` framework],
5656
it is useful to check out and follow these [CLI guidelines][clig].
5757

58+
## Subcommand development
59+
60+
`populate_argument_parser()` accepts a regular instance of
61+
[`argparse.ArgumentParser`]. Call its methods to extend the CLI arguments that
62+
would be specific for the subcommand you are creating. Those arguments will be
63+
available later, as an argument to the `invoke_cli_app()` function — through an
64+
instance of [`argparse.Namespace`]. For the `CLI_SUBCOMMAND_NAME` constant,
65+
choose `kebab-space-sub-command-style`, it does not need to be `snake_case`.
66+
67+
Make sure to return a `ReturnCode` instance or an integer from
68+
`invoke_cli_app()`. Returning a non-zero value will result in the CLI app
69+
exiting with a return code typically interpreted as an error while zero means
70+
success. You can `import errno` to use typical POSIX error codes through their
71+
human-readable identifiers.
72+
73+
Another way to interrupt the CLI app control flow is by raising an instance of
74+
one of the in-app errors. `raise PreCommitTerraformExit` for a successful exit,
75+
but it can be turned into an error outcome via
76+
`raise PreCommitTerraformExit(1)`.
77+
`raise PreCommitTerraformRuntimeError('The world is broken')` to indicate
78+
problems within the runtime. The framework will intercept any exceptions
79+
inheriting `PreCommitTerraformBaseError`, so they won't be presented to the
80+
end-users.
81+
5882
[`.pre-commit-hooks.yaml`]: ../../.pre-commit-hooks.yaml
5983
[`_cli_parsing.py`]: ./_cli_parsing.py
6084
[`_cli_subcommands.py`]: ./_cli_subcommands.py
85+
[`argparse.ArgumentParser`]:
86+
https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser
87+
[`argparse.Namespace`]:
88+
https://docs.python.org/3/library/argparse.html#argparse.Namespace
6189
[clig]: https://clig.dev
6290
[importable package]: https://docs.python.org/3/tutorial/modules.html#packages
6391
[import package]: https://packaging.python.org/en/latest/glossary/#term-Import-Package

0 commit comments

Comments
 (0)