A CLI tool to build beautiful command-line interfaces with type validation.
It is as simple as
from piou import Cli, Option
cli = Cli(description='A CLI tool')
@cli.command(cmd='foo', help='Run foo command')deffoo_main(
bar: int = Option(..., help='Bar positional argument (required)'),
baz: str = Option(..., '-b', '--baz', help='Baz keyword argument (required)'),
foo: str = Option(None, '--foo', help='Foo keyword argument'),
):
"""
A longer description on what the function is doing.
You can run it with:
```bash
poetry run python -m piou.test.simple foo 1 -b baz
```
And you are good to go!
"""passif __name__ == '__main__':
cli.run()
customization of the interface (to build a CLI similar to the one of Poetry)
type validation / casting
Typer is the closest alternative in terms of experience but lacks the possibility
to format the output is a custom way using external libraries (like Rich).
Piou provides all these possibilities and lets you define your own Formatter.
So when running python run.py sub -h it will output the following:
Options processor
Sometimes, you want to run a function using the global arguments before running the actual command (for instance
initialize a logger based on the verbose level).
To do so, you use set_options_processor that will receive all the current global options of the CLI.
By default, when a processor is set, the global arguments will not be passed downstream.
If you still want them to be passed to the functions by setting
cli.add_sub_parser(cmd='sub', help='A sub command', propagate_options=True)
Derived Options
Sometimes, you want to reuse the options in multiple command and group them into a single output to pass to
the command. For instance, you might want to group a connection string parameter to connect to a database. Here is a
full example:
You can also pass dynamic derived functions to avoid duplicating the derived logic:
import os
from typing importLiteralfrom piou import Cli, Option, Derived
cli = Cli(description='A CLI tool')
defget_pg_url_dynamic(source: Literal['db1', 'db2']):
_source_upper = source.upper()
_host_arg = f'--host-{source}'
_db_arg = f'--{source}'def_derived(# We need to specify the `arg_name` here
pg_host: str = Option(os.getenv(f'PG_HOST_{_source_upper}', 'localhost'),
_host_arg, arg_name=_host_arg),
pg_db: str = Option(os.getenv(f'PG_DB_{_source_upper}', source),
_db_arg, arg_name=_db_arg),
):
returnf'postgresql://postgres:postgres@{pg_host}:5432/{pg_db}'return _derived
@cli.command(help='Run dynamic command')defdynamic(url_1: str = Derived(get_pg_url_dynamic('db1')),
url_2: str = Derived(get_pg_url_dynamic('db2'))):
...
So that the output will look like this:
On Command Run
If you want to get the command name and arguments information that are passed to it (in case of general purpose
debugging for instance), you can pass on_cmd_run to the CLI.
from piou import Cli, Option, CommandMeta, Derived
defon_cmd_run(meta: CommandMeta):
pass
cli = Cli(description='A CLI tool',
on_cmd_run=on_cmd_run)
defprocessor(a: int = Option(1, '-a'),
b: int = Option(2, '-b')):
return a + b
@cli.command()deftest(
value: int = Derived(processor),
bar: str = Option(None, '--bar')
):
pass
You can customize the help and the different errors displayed by the CLI by passing a Formatter.
The default one is the Rich formatter based on the Rich package:
cmd_color: set the color of the command in the help
option_color: set the color of the positional / keyword arguments in the help
default_color: set the color of the default values in the help
show_default: show the default values if the keyword arguments (if available)
You can create your own Formatter by subclassing the Formatter class (see
the Rich formatter
for example).
The Rich Formatter supports the Password type that will hide the default value when printing help.
For instance:
We found that piou demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.