Skip to content

Reusing Params

As your CLI grows, you may find that you have a lot of commands that share the similar parameters. To help with that, arc provides a couple of different ways to reuse parameters.

Type Annotation

The reccomended way to reuse paramters is to bind your Param defintion to a type annotation

examples/type_annotation_param.py
from typing import Annotated
import pathlib
import arc

ConfigPathParam = Annotated[
    pathlib.Path,
    arc.Option(name="config", short="c", desc="The configuration file path."),
]


@arc.command
def hello(config_path: ConfigPathParam):
    arc.print(config_path, type(config_path))


hello()
$ python type_annotation_param.py --config config.json
config.json <class 'pathlib.PosixPath'>

Now anywhere you use this ConfigPathParam type, the CLI will will have the same param.

Using a group

To use a Parameter Group, all we need to do is add an argument to the argument list of a command, with the

param group as the type hint.

examples/group.py
import arc


@arc.group
class MyGroup:
    firstname: str
    reverse: bool


@arc.command
def hello(group: MyGroup):
    if group.reverse:
        group.firstname = group.firstname[::-1]

    arc.print(f"Hello, {group.firstname}! Hope you have a wonderful day!")


hello()
$ python group.py --help
USAGE
    group.py [-h] [--reverse] firstname

ARGUMENTS
    firstname

OPTIONS
    --help (-h)  Displays this help message
    --reverse

$ python group.py Joseph --reverse
Hello, hpesoJ! Hope you have a wonderful day!

Reference Parameter Groups for more information.