Script / CLI
CLI Class Documentation
The CLI
class provides a command-line interface framework for defining and executing commands. It enables registering commands, handling command options, processing user input, and generating help information dynamically. It is accessible on the script global object.
Methods
command
Description Registers a new command in the CLI.
Signature
Parameters
name
(string): The name of the command.description
(string, optional): A brief description of the command.
Returns
Command
: An instance of the Command
class, allowing further configuration.
Example
setPrompt
Description Sets the prompt text that appears before each command input.
Signature
Parameters
prompt
(string): The text to use as the prompt.
Returns
CLI
: The current CLI instance for method chaining.
Example
setWelcomeMessage
Description Sets the welcome message displayed when the CLI starts.
Signature
Parameters
message
(string): The welcome message text.
Returns
CLI
: The current CLI instance for method chaining.
Example
getSettings
Description Retrieves the current CLI settings, including the prompt and welcome message.
Signature
Parameters None.
Returns
{ prompt: string; welcomeMessage: string }
: An object containing the current CLI settings.
Example
exec
Description Executes a command based on user input.
Signature
Parameters
input
(string): The full command input string, including arguments.
Returns
Promise<any>
: Resolves with the command's execution result.
Throws An error if the command is not recognized.
Example
generateHelp
Description Generates a help message displaying available commands and their options. If a command name is provided, it returns detailed help for that command.
Signature
Parameters
commandName
(string, optional): The name of a specific command to show help for.
Returns
string
: The generated help message.
Example
Command Class Documentation
The Command
class represents an individual CLI command. It allows configuring command options and defining actions.
Methods
option
Description Adds an option (flag) to the command.
Signature
Parameters
option
(CommandOption): The option to add, specifying flags, description, and behavior.
Returns
this
: The current Command
instance for method chaining.
Example
action
Description Defines the function to be executed when the command is invoked.
Signature
Parameters
handler
(CommandAction): The function to execute when the command runs.
Returns
this
: The current Command
instance for method chaining.
Example
execute
Description Parses the input arguments and executes the command action.
Signature
Parameters
tokens
(string[]): The arguments passed to the command.
Returns
Promise<any>
: Resolves with the command’s execution result.
Example
How to Build Commands
1️⃣ Creating a Simple Command
Usage:
Output:
2️⃣ Adding Options (Flags)
Usage:
Output:
3️⃣ Handling Missing Required Arguments
If a required argument is missing, the CLI will throw an error:
🚨 Error:
4️⃣ Displaying Help for a Command
All commands automatically support --help
:
Output:
5️⃣ Setting Custom Prompt and Welcome Message
Last updated