Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface CommanderStatic

Hierarchy

Indexable

[key: string]: any

Index

Properties

Command

CommanderError

Option

args

args: string[]

Methods

action

  • Register callback fn for the command.

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

    Parameters

    • fn: (...args: any[]) => void | Promise<void>
        • (...args: any[]): void | Promise<void>
        • Parameters

          • Rest ...args: any[]

          Returns void | Promise<void>

    Returns Command

    Command for chaining

addListener

  • addListener(event: string | symbol, listener: (...args: any[]) => void): this
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

alias

  • alias(alias: string): Command
  • alias(): string
  • Set an alias for the command.

    Parameters

    • alias: string

    Returns Command

    Command for chaining

  • Get alias for the command.

    Returns string

allowUnknownOption

  • allowUnknownOption(arg?: undefined | false | true): Command
  • Allow unknown options on the command line.

    Parameters

    • Optional arg: undefined | false | true

    Returns Command

    Command for chaining

arguments

  • Define argument syntax for the top-level command.

    Parameters

    • desc: string

    Returns Command

    Command for chaining

command

  • Define a command, implemented using an action handler.

    remarks

    The command description is supplied using .description, not as a parameter to .command.

    example
     program
       .command('clone <source> [destination]')
       .description('clone a repository into a newly created directory')
       .action((source, destination) => {
         console.log('clone command called');
       });

    Parameters

    • nameAndArgs: string

      command name and arguments, args are <required> or [optional] and last may also be variadic...

    • Optional opts: CommandOptions

      configuration options

    Returns Command

    new command

  • Define a command, implemented in a separate executable file.

    remarks

    The command description is supplied as the second parameter to .command.

    example
     program
       .command('start <service>', 'start named service')
       .command('stop [service]', 'stop named serice, or all if no name supplied');

    Parameters

    • nameAndArgs: string

      command name and arguments, args are <required> or [optional] and last may also be variadic...

    • description: string

      description of executable command

    • Optional opts: CommandOptions

      configuration options

    Returns Command

    top level command for chaining more command definitions

description

  • description(str: string, argsDescription?: undefined | {}): Command
  • description(): string
  • Set the description.

    Parameters

    • str: string
    • Optional argsDescription: undefined | {}

    Returns Command

    Command for chaining

  • Get the description.

    Returns string

emit

  • emit(event: string | symbol, ...args: any[]): boolean
  • Parameters

    • event: string | symbol
    • Rest ...args: any[]

    Returns boolean

eventNames

  • eventNames(): Array<string | symbol>

exitOverride

  • Register callback to use as replacement for calling process.exit.

    Parameters

    • Optional callback: undefined | ((err: CommanderError) => never | void)

    Returns Command

getMaxListeners

  • getMaxListeners(): number

help

  • help(cb?: undefined | ((str: string) => string)): never
  • Output help information and exit.

    Parameters

    • Optional cb: undefined | ((str: string) => string)

    Returns never

helpOption

  • helpOption(flags?: undefined | string, description?: undefined | string): Command
  • You can pass in flags and a description to override the help flags and help description for your command.

    Parameters

    • Optional flags: undefined | string
    • Optional description: undefined | string

    Returns Command

listenerCount

  • listenerCount(type: string | symbol): number
  • Parameters

    • type: string | symbol

    Returns number

listeners

  • listeners(event: string | symbol): Function[]
  • Parameters

    • event: string | symbol

    Returns Function[]

name

  • name(str: string): Command
  • name(): string
  • Set the name of the command.

    Parameters

    • str: string

    Returns Command

    Command for chaining

  • Get the name of the command.

    Returns string

off

  • off(event: string | symbol, listener: (...args: any[]) => void): this
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

on

  • on(event: string | symbol, listener: (...args: any[]) => void): this
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

once

  • once(event: string | symbol, listener: (...args: any[]) => void): this
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

option

  • option(flags: string, description?: undefined | string, fn?: ((arg1: any, arg2: any) => void) | RegExp, defaultValue?: any): Command
  • option(flags: string, description?: undefined | string, 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"

    example
    // 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
    • Optional description: undefined | string
    • Optional fn: ((arg1: any, arg2: any) => void) | RegExp
    • Optional defaultValue: any

    Returns Command

    Command for chaining

  • Parameters

    • flags: string
    • Optional description: undefined | string
    • Optional defaultValue: any

    Returns Command

opts

  • opts(): {}
  • Return an object containing options as key-value pairs

    Returns {}

    • [key: string]: any

outputHelp

  • outputHelp(cb?: undefined | ((str: string) => string)): void
  • Output help information for this command.

    When listener(s) are available for the helpLongFlag those callbacks are invoked.

    Parameters

    • Optional cb: undefined | ((str: string) => string)

    Returns void

parse

  • Parse argv, setting options and invoking commands when defined.

    Parameters

    • argv: string[]

    Returns Command

    Command for chaining

parseAsync

  • parseAsync(argv: string[]): Promise<any>
  • Parse argv, setting options and invoking commands when defined.

    Use parseAsync instead of parse if any of your action handlers are async. Returns a Promise.

    Parameters

    • argv: string[]

    Returns Promise<any>

    Promise

parseExpectedArgs

  • parseExpectedArgs(args: string[]): Command
  • Parse expected args.

    For example ["[type]"] becomes [{ required: false, name: 'type' }].

    Parameters

    • args: string[]

    Returns Command

    Command for chaining

parseOptions

  • Parse options from argv returning argv void of these options.

    Parameters

    • argv: string[]

    Returns ParseOptionsResult

passCommandToAction

  • passCommandToAction(value?: undefined | false | true): Command
  • Whether to pass command to action handler, or just the options (specify false).

    Parameters

    • Optional value: undefined | false | true

    Returns Command

    Command for chaining

prependListener

  • prependListener(event: string | symbol, listener: (...args: any[]) => void): this
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

prependOnceListener

  • prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

rawListeners

  • rawListeners(event: string | symbol): Function[]

removeAllListeners

  • removeAllListeners(event?: string | symbol): this
  • Parameters

    • Optional event: string | symbol

    Returns this

removeListener

  • removeListener(event: string | symbol, listener: (...args: any[]) => void): this
  • Parameters

    • event: string | symbol
    • listener: (...args: any[]) => void
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns this

requiredOption

  • requiredOption(flags: string, description?: undefined | string, fn?: ((arg1: any, arg2: any) => void) | RegExp, defaultValue?: any): Command
  • requiredOption(flags: string, description?: undefined | string, defaultValue?: any): Command
  • Define a required option, which must have a value after parsing. This usually means the option must be specified on the command line. (Otherwise the same as .option().)

    The flags string should contain both the short and long flags, separated by comma, a pipe or space.

    Parameters

    • flags: string
    • Optional description: undefined | string
    • Optional fn: ((arg1: any, arg2: any) => void) | RegExp
    • Optional defaultValue: any

    Returns Command

  • Parameters

    • flags: string
    • Optional description: undefined | string
    • Optional defaultValue: any

    Returns Command

setMaxListeners

  • setMaxListeners(n: number): this

storeOptionsAsProperties

  • storeOptionsAsProperties(value?: undefined | false | true): Command
  • Whether to store option values as properties on command object, or store separately (specify false). In both cases the option values can be accessed using .opts().

    Parameters

    • Optional value: undefined | false | true

    Returns Command

    Command for chaining

usage

  • usage(str: string): Command
  • usage(): string
  • Set the command usage.

    Parameters

    • str: string

    Returns Command

    Command for chaining

  • Get the command usage.

    Returns string

version

  • version(str: string, flags?: undefined | string, description?: undefined | string): Command
  • Set the program version to str.

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

    You can optionally supply the flags and description to override the defaults.

    Parameters

    • str: string
    • Optional flags: undefined | string
    • Optional description: undefined | string

    Returns Command