Skip to content

Commands API Documentation


Command-Line Interface — Examples


zmag.CLI

Interface for creating click groups. Instead of using the CLI class directly, utilize it through the cli instance for consistent command-line interface management.

Parameters:

  • obj (Callable) –

    A callable to be turned into a click.Group.

  • register (bool | None) –

    Whether to register the group as a component to be loaded.

Returns:

  • group

    A click group object.

Examples:

# Simple
@zmag.cli
def cli(): pass

@cli.command()
def hello_world():
    zmag.cli.echo("Hello World (Command)")
# Groups
@zmag.cli
def cli(): pass

@zmag.cli(group=True)
def database():  pass

@database.command()
def hello_world():
    zmag.cli.echo("Hello World (Command)")

# Register Group
cli.add_command(database)

command(obj=None, *, register=True) classmethod

Decorate a function as a click command.

Parameters:

  • obj (Callable, default: None ) –

    A callable to be turned into a click.Command.

  • register (bool | None, default: True ) –

    Whether to register the command as a component to be loaded.

Returns:

  • command ( Command | Any ) –

    A click command object.

echo(*args, **kwargs) staticmethod

Wrapper around click.secho.

clear() staticmethod

Wrapper around click.clear.

range(min=0, max=1, min_open=False, max_open=False, clamp=False) staticmethod

Wrapper around click.IntRange and click.FloatRange.

Returns:

  • range ( IntRange | FloatRange ) –

    range based on the min type.

argument(parameter, type=None, default=None, nargs=None, help=None, **kwargs) staticmethod

Wrapper around click.argument.

option(*parameters, type=None, default=None, nargs=1, is_flag=False, help=None, prompt=None, confirmation_prompt=False, multiple=False, count=False, hide_input=False, **kwargs) staticmethod

Wrapper around click.option.


Async Commands


zmag.coro(function)

Transform an asynchronous function into a synchronous function.

Parameters:

  • function (Coroutine) –

    The regular function to be transformed.

Returns:

  • function ( Callable ) –

    An async function that runs the original function using asyncio.run().

CLI

Wrap your commands with coro to run async commands with click.

Example:

@zmag.coro
async def async_function(x, y):
        return x + y

result = async_function(3, 4)
print(result)