CLI
Utilitary module to interact with the CLI. This holds the basic implementation that is reused across commands and other interactions.
Example
>>> from command_line_assistant.commands.cli import CommandContext, argument, command
>>> @command("hello", help="A simple command that prints 'Hello, friend')
>>> @argument("-n", "--name", nargs="?", help="Your name goes in here.")
>>> ...
>>> def hello_friend(args: Namespace, context: CommandContext) -> int:
>>> if args.name:
>>> print(f"Hello, {args.name}")
>>> else:
>>> print("Hello, friend")
- class Command(name, func, help=None, description=None)[source]
Represents a single CLI command.
Initialize a new command.
- Parameters:
- _create_wrapper()[source]
Create a wrapper function for the command.
- class CommandContext(username='docs', effective_user_id=1005, os_release=<factory>)[source]
A context for all commands with useful information.
Note
This is meant to be initialized exclusively by the client.
- username
The username of the current user.
- Type:
- effective_user_id
The effective user id.
- Type:
-
effective_user_id:
int= 1005
-
username:
str= 'docs'
- _subcommand_used(args)[source]
Return what subcommand has been used by the user. Return None if no subcommand has been used.
- add_default_command(stdin, argv)[source]
Add the default command when none is given
- argument(*args, **kwargs)[source]
Decorator to add arguments to a command.
- command(name, help=None, description=None)[source]
Decorator to register a command function.
- create_argument_parser()[source]
Create the argument parser for command line assistant.
- Return type:
tuple[ArgumentParser,_SubParsersAction]- Returns:
The parent and subparser created
- Return type:
tuple[ArgumentParser, SubParsersAction]
- create_subparser(parser, name, help)[source]
Create a subparser with some default options
- Return type:
- Parameters:
- Returns:
A new instance of a ArgumentParser to be used as a command.
- Return type:
ArgumentParser
- read_stdin()[source]
Parse the std input when a user give us.
- Return type:
- For example, consider the following scenario:
>>> echo "how to run podman?" | c
- Or a more complex one
>>> cat error-log | c "How to fix this?"
- Returns:
Return the stdin that was read or if there is nothing, return an empty string.
- Return type: