You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`radicli` is a small, zero-dependency Python package for creating command line interfaces, built on top of Python's [`argparse`](https://docs.python.org/3/library/argparse.html) module. It introduces minimal overhead, preserves your original Python functions and uses type hints to parse values provided on the CLI. It supports all common types out-of-the-box, including complex ones like `List[str]`, `Literal` and `Enum`, and allows registering custom types with custom converters.
5
+
`radicli` is a small, zero-dependency Python package for creating command line interfaces, built on top of Python's [`argparse`](https://docs.python.org/3/library/argparse.html) module. It introduces minimal overhead, preserves your original Python functions and uses type hints to parse values provided on the CLI. It supports all common types out-of-the-box, including complex ones like `List[str]`, `Literal` and `Enum`, and allows registering custom types with custom converters, as well as custom CLI-only error handling.
6
6
7
7
> **Important note:** This package aims to be a simple option based on the requirements of our libraries. If you're looking for a more full-featured CLI toolkit, check out [`typer`](https://typer.tiangolo.com), [`click`](https://click.palletsprojects.com) or [`plac`](https://plac.readthedocs.io/en/latest/).
One common problem when adding CLIs to a code base is error handling. When called in a CLI context, you typically want to pretty-print any errors and avoid long tracebacks. However, you don't want to use those errors and plain `SystemExit`s with no traceback in helper functions that are used in other places, or when the CLI functions are called directly from Python or during testing.
240
+
241
+
To solve this, `radicli` lets you provide an error map via the `errors` argument on initialization. It maps `Exception` types like `ValueError` or fully custom error subclasses to handler functions. If an error of that type is raised, the handler is called and will receive the error. The handler can optionally return an exit code – in this case, `radicli` will perform a `sys.exit` using that code. If no error code is returned, no exit is performed and the handler can either take care of the exiting itself or choose to not exit.
242
+
243
+
```python
244
+
from radicli import Radicli
245
+
from termcolor import colored
246
+
247
+
defpretty_print_error(error: Exception) -> int:
248
+
print(colored(f"🚨 {error}", "red"))
249
+
return1
250
+
251
+
cli = Radicli(errors={ValueError: handle_error})
252
+
253
+
@cli.command("hello", name=Arg("--name"))
254
+
defhello(name: str):
255
+
if name =="Alex":
256
+
raiseValueError("Invalid name")
257
+
```
258
+
259
+
```
260
+
$ python cli.py hello --name Alex
261
+
🚨 Invalid name
262
+
```
263
+
264
+
```bash
265
+
>>> hello("Alex")
266
+
Traceback (most recent call last):
267
+
File "<stdin>", line 1, in<module>
268
+
ValueError: Invalid name
269
+
```
270
+
271
+
This approach is especially powerful with custom error subclasses. Here you can decide which arguments the error should take and how this information should be displayed on the CLI vs. in a regular non-CLI context.
272
+
273
+
```python
274
+
classCustomError(Exception):
275
+
def__init__(self, text: str, additional_info: Any ="") -> None:
|`prog`|`Optional[str]`| Program name displayed in `--help` prompt usage examples, e.g. `"python -m spacy"`. |
322
+
|`help`|`str`| Help text for the CLI, displayed in top-level `--help`. Defaults to `""`. |
323
+
|`version`|`Optional[str]`| Version available via `--version`, if set. |
324
+
|`converters`|`Dict[Type, Callable[[str], Any]]`| Dict mapping types to global converter functions. |
325
+
|`errors`|`Dict[Type[Exception], Callable[[Exception], Optional[int]]]`| Dict mapping errors types to global error handlers. If the handler returns an exit code, a `sys.exit` will be raised using that code. See [error handling](#error-handling) for details. |
326
+
|`commands`|`Dict[str, Command]`| The commands added to the CLI, keyed by name. |
327
+
|`subcommands`|`Dict[str, Dict[str, Command]]`| The subcommands added to the CLI, keyed by parent name, then keyed by subcommand name. |
|`prog`|`Optional[str]`| Program name displayed in `--help` prompt usage examples, e.g. `"python -m spacy"`. |
291
-
|`help`|`str`| Help text for the CLI, displayed in top-level `--help`. Defaults to `""`. |
292
-
|`version`|`Optional[str]`| Version available via `--version`, if set. |
293
-
|`converters`|`Dict[Type, Callable[[str], Any]]`| Dict mapping types to converter functions. All arguments with these types will then be passed to the respective converter. |
294
-
|`extra_key`|`str`| Name of function argument that receives extra arguments if the `command_with_extra` or `subcommand_with_extra` decorator is used. Defaults to `"_extra"`. |
|`prog`|`Optional[str]`| Program name displayed in `--help` prompt usage examples, e.g. `"python -m spacy"`. |
342
+
|`help`|`str`| Help text for the CLI, displayed in top-level `--help`. Defaults to `""`. |
343
+
|`version`|`Optional[str]`| Version available via `--version`, if set. |
344
+
|`converters`|`Dict[Type, Callable[[str], Any]]`| Dict mapping types to converter functions. All arguments with these types will then be passed to the respective converter. |
345
+
|`errors`|`Dict[Type[Exception], Callable[[Exception], Optional[int]]]`| Dict mapping errors types to global error handlers. If the handler returns an exit code, a `sys.exit` will be raised using that code. See [error handling](#error-handling) for details. |
346
+
|`extra_key`|`str`| Name of function argument that receives extra arguments if the `command_with_extra` or `subcommand_with_extra` decorator is used. Defaults to `"_extra"`. |
0 commit comments