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
public command(name: string, description?: string): Command
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
script.command("greet", "Greets the user")
.action(() => console.log("Hello, welcome to roboqo-cli!"));
setPrompt
Description Sets the prompt text that appears before each command input.
Signature
public setPrompt(prompt: string): CLI
Parameters
prompt
(string): The text to use as the prompt.
Returns
CLI
: The current CLI instance for method chaining.
Example
script.setPrompt("roboqo> ");
setWelcomeMessage
Description Sets the welcome message displayed when the CLI starts.
Signature
public setWelcomeMessage(message: string): CLI
Parameters
message
(string): The welcome message text.
Returns
CLI
: The current CLI instance for method chaining.
Example
script.setWelcomeMessage("Welcome to the Roboqo Command Line Interface!");
getSettings
Description Retrieves the current CLI settings, including the prompt and welcome message.
Signature
public getSettings(): { prompt: string; welcomeMessage: string }
Parameters None.
Returns
{ prompt: string; welcomeMessage: string }
: An object containing the current CLI settings.
Example
const settings = script.getSettings();
console.log(settings.prompt); // Outputs the current prompt
console.log(settings.welcomeMessage); // Outputs the welcome message
exec
Description Executes a command based on user input.
Signature
public async exec(input: string): Promise<any>
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
await script.exec("help"); // Executes the 'help' command
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
private generateHelp(commandName?: string): string
Parameters
commandName
(string, optional): The name of a specific command to show help for.
Returns
string
: The generated help message.
Example
console.log(script.exec("help")); // Prints all available commands
console.log(script.exec("buy --help")); // Prints help for the "buy" command
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
public option(option: CommandOption): this
Parameters
option
(CommandOption): The option to add, specifying flags, description, and behavior.
Returns
this
: The current Command
instance for method chaining.
Example
script.command("buy", "Buy USDC on Jupiter")
.option({
short: "-a",
long: "--amount",
argument: "amount",
required: true,
description: "Dollar amount to buy",
});
action
Description Defines the function to be executed when the command is invoked.
Signature
public action(handler: CommandAction): this
Parameters
handler
(CommandAction): The function to execute when the command runs.
Returns
this
: The current Command
instance for method chaining.
Example
script.command("buy", "Buy USDC on Jupiter")
.action((args) => {
console.log(`Buying ${args.amount} USDC...`);
});
execute
Description Parses the input arguments and executes the command action.
Signature
public execute(tokens: string[]): Promise<any> | any
Parameters
tokens
(string[]): The arguments passed to the command.
Returns
Promise<any>
: Resolves with the command’s execution result.
Example
await script.exec("buy --amount 10");
How to Build Commands
1️⃣ Creating a Simple Command
script.command("hello", "Prints a greeting message")
.action(() => console.log("Hello, welcome to roboqo-cli!"));
Usage:
hello
Output:
Hello, welcome to roboqo-cli!
2️⃣ Adding Options (Flags)
script.command("buy", "Buy USDC on Jupiter")
.option({
short: "-a",
long: "--amount",
argument: "amount",
required: true,
description: "Dollar amount to buy",
})
.action((args) => {
console.log(`Buying ${args.amount} USDC...`);
});
Usage:
buy -a 10
buy --amount 10
Output:
Buying 10 USDC...
3️⃣ Handling Missing Required Arguments
If a required argument is missing, the CLI will throw an error:
buy --amount
🚨 Error:
Error: Missing value for required option: --amount
4️⃣ Displaying Help for a Command
All commands automatically support --help
:
buy --help
Output:
buy - Buy USDC on Jupiter
-a, --amount - Dollar amount to buy
5️⃣ Setting Custom Prompt and Welcome Message
script.setPrompt("amazebot>");
script.setWelcomeMessage("Welcome to amazebot. Type help for more information.");
Last updated