To avoid redefining the same arguments across multiple subcommands, you can use argument inheritance. Define an argument in a parent Command and use arg(inherited=True) in the child commands. The child commands will automatically inherit the full definition (including help text, short flags, and groups) from the parent.
Note: The parent command must define the argument with its full configuration (help, short flags, etc.) for the child to receive the complete definition.
from clypi import Command, Positional, arg
from typing_extensions import override
class Wave(Command):
"""Wave at someone"""
name: Positional[str]
# Inherits the full definition from the parent 'Cli' command
verbose: bool = arg(inherited=True)
@override
async def run(self) -> None:
print(f"👋 Hey {self.name}")
if self.verbose:
print(f"👋👋👋 HEYYY {self.name}")
class Cli(Command):
"""A very simple CLI"""
subcommand: Wave | None
verbose: bool = arg(
False, short="v", help="Whether to show verbose output", group="global"
)
if __name__ == "__main__":
cmd = Cli.parse()
cmd.start()