Options
All
  • Public
  • Public/Protected
  • All
Menu

A command builder

Hierarchy

  • EventEmitter
    • Command

Index

Constructors

constructor

  • new Command(name?: string): Command

Properties

commands

commands: Command[] = []

rawArgs

rawArgs: Args

runningCommand

runningCommand: ChildProcess

The running node process from running a subcommand executable

Methods

action

  • Register a callback for a command

    Examples:

    program()
      .command('help')
      .description('display verbose help')
      .action(() => {
         // output help here
      });

    Parameters

    • fn: Function

    Returns Command

    Command for chaining

alias

  • Set an alias for the command

    Parameters

    • alias: string

      what to alias the command to

    Returns Command

    Command for chaining

allowUnknownOption

  • allowUnknownOption(arg?: boolean): Command
  • Allow unknown options on the command line.

    Parameters

    • Default value arg: boolean = false

      if false, error will be thrown if unknown option passed. Defaults to false for unknown options.

    Returns Command

arguments

  • Define argument syntax for the top-level command.

    Parameters

    • desc: string

    Returns Command

autocomplete

  • autocomplete(argv: string[]): Command
  • Handle autocomplete if command args starts with special options. It will exit current process after successful processing.

    Parameters

    • argv: string[]

    Returns Command

    Command from chaining

command

  • command(name: string, desc?: string | null, opts?: object): Command
  • Add command name.

    The action callback is invoked when the command name is specified via ARGV, and the remaining arguments are applied to the function for access.

    When the name is "*" an un-matched command will be passed as the first arg, followed by the rest of ARGV remaining.

    Examples:

    const prog = program()
      .version('0.0.1')
      .option('-C, --chdir <path>', 'change the working directory')
      .option('-c, --config <path>', 'set config path. defaults to ./deploy.conf')
      .option('-T, --no-tests', 'ignore test hook')
    
    prog
      .command('setup')
      .description('run remote setup commands')
      .action(() => {
        console.log('setup');
      });
    
    prog
      .command('exec <cmd>')
      .description('run the given remote command')
      .action((cmd) => {
        console.log('exec "%s"', cmd);
      });
    
    prog
      .command('teardown <dir> [otherDirs...]')
      .description('run teardown commands')
      .action((dir, otherDirs) => {
        console.log('dir "%s"', dir);
        if (otherDirs) {
          otherDirs.forEach((oDir) => {
            console.log('dir "%s"', oDir);
          });
        }
      });
    
    prog
      .command('*')
      .description('deploy the given env')
      .action((env) => {
        console.log('deploying "%s"', env);
      });
    
    prog.init();

    Parameters

    • name: string
    • Optional desc: string | null
    • Default value opts: object = {isDefault: false,noHelp: false}

    Returns Command

    Command the new command

complete

  • Define completion rules which will later be used by autocomplete to generate appropriate response

    program()
      .arguments('<a> <b>')
      .option('--verbose', 'verbose')
      .option('-n, --name <name>', 'specify name')
      .option('--description <desc>', 'specify description')
      .complete({
        options: {
          '--name': (typedArgs) => ['kate', 'jim'],
          '--description': ['desc1', 'desc2']
        },
        arguments: {
          a: (typedArgs) => ['a-1', 'a-2'],
          b: ['b-1', 'b-2']
        }
      });

    Parameters

    • rules: object

    Returns Command

description

  • description(str: string, argsDescription?: Record<string, any>): Command
  • Set the command description

    Parameters

    • str: string

      the description for the command

    • Optional argsDescription: Record<string, any>

    Returns Command

    Command for chaining

get

  • get(name: string): boolean | string | number
  • Get the value of an option

    Parameters

    • name: string

      The name of the option you want to get

    Returns boolean | string | number

    the value of the requested option

help

  • help(cb?: undefined | function): void
  • Output help information and exit with code 0

    Parameters

    • Optional cb: undefined | function

    Returns void

init

  • Parse argv, settings options and invoking commands when defined. Arguments to argv come in the following form:

    ['/path/to/node', '/path/to/command', 'help']

    The second argument is the path to the command. This is usually the entrypoint of your dark cli app.

    The third argument is the name of the subcommand you want to call.

    Parameters

    • Default value argv: Args = process.argv

      the argments to parse and send to your cli app

    Returns Command

    Command for chaining

name

option

  • option(flags: string, description: string, fn?: Function | RegExp, defaultValue?: any): Command
  • Define option with flags, description and optional coercion fn.

    The flags string should contain both the short and long flags, separated by comma, a pipe or space. The following are all valid all will output this way when --help is used.

    • "-p, --pepper"
    • "-p|--pepper"
    • "-p --pepper"

    Examples:

    // simple boolean defaulting to false
    program.option('-p, --pepper', 'add pepper');
    
    // --pepper
    program.pepper
    // => Boolean
    
    // simple boolean defaulting to true
    program.option('-C, --no-cheese', 'remove cheese');
    
    program.cheese
    // => true
    
    // --no-cheese
    program.cheese
    // => false
    
    // required argument
    program.option('-C, --chdir <path>', 'change the working directory');
    
    // --chdir /tmp
    program.chdir
    // => "/tmp"
    
    // optional argument
    program.option('-c, --cheese [type]', 'add cheese [marble]');

    Parameters

    • flags: string
    • description: string
    • Optional fn: Function | RegExp
    • Optional defaultValue: any

    Returns Command

    Command for chaining

opts

  • opts(): Record<string, any>
  • Get an object containing options as key-value pairs

    Returns Record<string, any>

    an object containing options as key-value pairs

outputHelp

  • outputHelp(cb?: undefined | function): void
  • Output help information for this command

    Parameters

    • Optional cb: undefined | function

    Returns void

usage

  • usage(usage: string): string | Command

version

  • version(version: string, flags?: undefined | string): Command
  • Set the program version

    This method auto-registers the "-V, --version" flag which will print the version number when passed.

    Parameters

    • version: string
    • Optional flags: undefined | string

    Returns Command

    Command for chaining

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc