CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-vorpal

Node's first framework for building immersive CLI apps.

Pending
Overview
Eval results
Files

commands.mddocs/

Command Management

Complete command registration and management system with support for arguments, options, validation, and advanced behaviors like modes and catch-all commands.

Capabilities

Command Registration

Registers a new command in the Vorpal API with optional description and configuration options.

/**
 * Registers a new command in the vorpal API
 * @param name - Command name with optional arguments <required> or [optional]
 * @param desc - Optional command description
 * @param opts - Optional options object with noHelp, mode, catch properties
 * @returns Command instance for further configuration
 */
function command(name: string, desc?: string, opts?: CommandOptions): Command;

interface CommandOptions {
  noHelp?: boolean;
  mode?: boolean;
  catch?: boolean;
}

Usage Examples:

const vorpal = require('vorpal')();

// Simple command
vorpal
  .command('hello', 'Says hello')
  .action(function(args, callback) {
    this.log('Hello World!');
    callback();
  });

// Command with required argument
vorpal
  .command('greet <name>', 'Greets a person')
  .action(function(args, callback) {
    this.log(`Hello, ${args.name}!`);
    callback();
  });

// Command with optional argument
vorpal
  .command('say [message]', 'Says a message')
  .action(function(args, callback) {
    this.log(args.message || 'Nothing to say');
    callback();
  });

Mode Command Registration

Registers a new 'mode' command that creates an interactive mode with its own command set.

/**
 * Registers a new 'mode' command (interactive mode)
 * @param name - Mode command name
 * @param desc - Optional mode description
 * @param opts - Optional mode options
 * @returns Command instance for mode configuration
 */
function mode(name: string, desc?: string, opts?: object): Command;

Usage Example:

const vorpal = require('vorpal')();

vorpal
  .mode('config', 'Enters configuration mode')
  .delimiter('config:')
  .init(function(args, callback) {
    this.log('Entering configuration mode...');
    callback();
  })
  .action(function(command, callback) {
    // Handle commands within this mode
    if (command === 'set param value') {
      this.log('Parameter set!');
    }
    callback();
  });

Catch-All Command Registration

Registers a 'catch' command executed when no command matches are found.

/**
 * Registers a 'catch' command executed when no command matches
 * @param name - Catch command pattern
 * @param desc - Optional catch description
 * @param opts - Optional catch options
 * @returns Command instance for catch configuration
 */
function catch(name: string, desc?: string, opts?: object): Command;

Usage Example:

const vorpal = require('vorpal')();

vorpal
  .catch('[words...]', 'Handles unmatched commands')
  .action(function(args, callback) {
    this.log(`Command not found: ${args.words.join(' ')}`);
    this.log('Type "help" for available commands');
    callback();
  });

Default Command Registration

Alias to the catch command for registering default handlers.

/**
 * Alias to the catch command
 * @param name - Default command pattern
 * @param desc - Optional default description
 * @param opts - Optional default options
 * @returns Command instance for default configuration
 */
function default(name: string, desc?: string, opts?: object): Command;

Command Lookup

Returns the instance of a given command by name.

/**
 * Returns the instance of given command
 * @param name - Command name to find
 * @returns Command instance or undefined if not found
 */
function find(name: string): Command | undefined;

Usage Example:

const vorpal = require('vorpal')();

vorpal.command('test', 'Test command');

const testCommand = vorpal.find('test');
if (testCommand) {
  // Modify the existing command
  testCommand.description('Updated test command');
}

Command Configuration

Command Description

Sets or gets the description for a command.

/**
 * Defines description for given command
 * @param str - Description string (optional for getter)
 * @returns Description string (getter) or Command instance (setter)
 */
function description(str?: string): string | Command;

Command Options

Registers an option for the command.

/**
 * Registers an option for given command
 * @param flags - Option flags (e.g., '-v, --verbose')
 * @param description - Option description
 * @param autocomplete - Optional autocomplete configuration
 * @returns Command instance for chaining
 */
function option(flags: string, description: string, autocomplete?: any): Command;

Usage Example:

vorpal
  .command('deploy <environment>')
  .option('-f, --force', 'Force deployment without confirmation')
  .option('-v, --verbose', 'Show detailed deployment logs')
  .action(function(args, callback) {
    if (args.options.force) {
      this.log('Forcing deployment...');
    }
    if (args.options.verbose) {
      this.log('Verbose mode enabled');
    }
    callback();
  });

Command Aliases

Defines aliases for a command.

/**
 * Defines an alias for a given command
 * @param aliases - Single alias string or array of aliases
 * @returns Command instance for chaining
 */
function alias(...aliases: string[] | string[][]): Command;

Usage Example:

vorpal
  .command('list', 'Lists items')
  .alias('ls', 'dir')
  .action(function(args, callback) {
    this.log('Listing items...');
    callback();
  });

Command Arguments

Sets the arguments description for a command.

/**
 * Returns the commands arguments as string
 * @param desc - Arguments description
 * @returns Command instance for chaining
 */
function arguments(desc: string): Command;

Command Usage

Sets or gets the usage string for help display.

/**
 * Returns the command usage string for help
 * @param str - Usage string (optional for getter)
 * @returns Usage string (getter) or Command instance (setter)
 */
function usage(str?: string): string | Command;

Command Behavior

Command Action

Defines the action function to execute when the command is called.

/**
 * Defines an action for a given command
 * @param fn - Action function receiving args and callback
 * @returns Command instance for chaining
 */
function action(fn: (args: CommandArgs, callback: function) => void): Command;

interface CommandArgs {
  [key: string]: any;
  options: { [key: string]: any };
}

Command Validation

Defines a function to validate arguments before action is performed.

/**
 * Defines a function to validate arguments before action
 * @param fn - Validation function returning true/false or error message
 * @returns Command instance for chaining
 */
function validate(fn: (args: CommandArgs) => boolean | string): Command;

Command Cancellation

Defines a function to be called when the command is canceled.

/**
 * Defines a function to be called when command is canceled
 * @param fn - Cancellation handler function
 * @returns Command instance for chaining
 */
function cancel(fn: () => void): Command;

Command Completion

Defines a method to be called when the command set has completed.

/**
 * Defines a method to be called when command set has completed
 * @param fn - Completion handler function
 * @returns Command instance for chaining
 */
function done(fn: () => void): Command;

Post-Command Handler

Adds a command to be executed after command completion.

/**
 * Adds a command to be executed after command completion
 * @param fn - Post-command handler function
 * @returns Command instance for chaining
 */
function after(fn: () => void): Command;

Command Parser

Edits the raw command string before it is executed.

/**
 * Edits the raw command string before execution
 * @param fn - Parser function that modifies command string
 * @returns Command instance for chaining
 */
function parse(fn: (command: string, args: CommandArgs) => string): Command;

Mode-Specific Methods

Mode Initialization

Defines an init action for a mode command.

/**
 * Defines an init action for a mode command
 * @param fn - Initialization function for mode entry
 * @returns Command instance for chaining
 */
function init(fn: (args: CommandArgs, callback: function) => void): Command;

Mode Delimiter

Defines a prompt delimiter for a mode once entered.

/**
 * Defines a prompt delimiter for a mode once entered
 * @param delimiter - Delimiter string for mode prompt
 * @returns Command instance for chaining
 */
function delimiter(delimiter: string): Command;

Autocomplete

Autocomplete Configuration

Defines tabbed auto-completion for the given command.

/**
 * Defines tabbed auto-completion for the given command
 * @param obj - Autocomplete configuration object
 * @returns Command instance for chaining
 */
function autocomplete(obj: AutocompleteConfig): Command;

interface AutocompleteConfig {
  [key: string]: string[] | function;
}

Usage Example:

vorpal
  .command('connect <server>')
  .autocomplete({
    server: ['production', 'staging', 'development', 'local']
  })
  .action(function(args, callback) {
    this.log(`Connecting to ${args.server}...`);
    callback();
  });

Advanced Configuration

Static Typing

Sets arguments for static typing of options using minimist.

/**
 * Sets args for static typing of options using minimist
 * @param types - Type configuration object
 * @returns Command instance for chaining
 */
function types(types: TypesConfig): Command;

interface TypesConfig {
  string?: string[];
  boolean?: string[];
  number?: string[];
}

Hidden Commands

Hides command from the help menu.

/**
 * Doesn't show command in the help menu
 * @returns Command instance for chaining
 */
function hidden(): Command;

Unknown Options

Allows undeclared options to be passed in with the command.

/**
 * Allows undeclared options to be passed in with the command
 * @param allow - Whether to allow unknown options (default true)
 * @returns Command instance for chaining
 */
function allowUnknownOptions(allow?: boolean): Command;

Custom Help

Adds a custom handling for the --help flag.

/**
 * Adds a custom handling for the --help flag
 * @param fn - Custom help function (optional)
 * @returns Command instance for chaining
 */
function help(fn?: (cmd: Command) => string): Command;

Command Extensions

Allows composing other functions to extend the command.

/**
 * Let's you compose other functions to extend the command
 * @param fn - Extension function
 * @returns Result of extension function
 */
function use(fn: (command: Command) => any): any;

Command Removal

Removes command from parent Vorpal instance.

/**
 * Removes self from Vorpal instance
 * @returns Command instance
 */
function remove(): Command;

Help and Information

Help Information

Returns the help info for given command.

/**
 * Returns the help info for given command
 * @returns Help information string
 */
function helpInformation(): string;

Option Help

Returns the help string for the command's options.

/**
 * Returns the help string for the command's options
 * @returns Options help string
 */
function optionHelp(): string;

Complete Command Example

const vorpal = require('vorpal')();

vorpal
  .command('deploy <environment> [version]', 'Deploy application to environment')
  .alias('d')
  .option('-f, --force', 'Force deployment without confirmation')
  .option('-v, --verbose', 'Show detailed deployment logs')
  .option('-t, --timeout <seconds>', 'Deployment timeout in seconds')
  .types({
    string: ['environment', 'version'],
    number: ['timeout'],
    boolean: ['force', 'verbose']
  })
  .autocomplete({
    environment: ['production', 'staging', 'development']
  })
  .validate(function(args) {
    if (!['production', 'staging', 'development'].includes(args.environment)) {
      return 'Environment must be production, staging, or development';
    }
    return true;
  })
  .action(function(args, callback) {
    const env = args.environment;
    const version = args.version || 'latest';
    const force = args.options.force;
    const verbose = args.options.verbose;
    const timeout = args.options.timeout || 300;
    
    if (verbose) {
      this.log(`Deploying version ${version} to ${env}...`);
      this.log(`Timeout: ${timeout} seconds`);
      this.log(`Force mode: ${force ? 'enabled' : 'disabled'}`);
    }
    
    // Deploy logic here
    this.log(`Successfully deployed to ${env}!`);
    callback();
  });

Install with Tessl CLI

npx tessl i tessl/npm-vorpal

docs

commands.md

configuration.md

events.md

execution.md

extensions.md

index.md

storage.md

ui.md

tile.json