Options
All
  • Public
  • Public/Protected
  • All
Menu

Hierarchy

Indexable

[key: string]: any

Index

Constructors

constructor

  • Initialize a new Command.

    Parameters

    • Optional name: undefined | string

    Returns CommanderStatic

Properties

Command

Command: Command

CommandOptions

CommandOptions: CommandOptions

Option

Option: Option

ParseOptionsResult

ParseOptionsResult: ParseOptionsResult

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: function
        • (...args: any[]): void
        • Parameters

          • Rest ...args: any[]

          Returns void

    Returns Command

    for chaining

addListener

  • addListener(event: string | symbol, listener: function): this
  • Parameters

    • event: string | symbol
    • listener: function
        • (...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

  • Returns string

allowUnknownOption

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

    Parameters

    • Optional arg: undefined | false | true

    Returns Command

    for chaining

arguments

  • Define argument syntax for the top-level command.

    Parameters

    • desc: string

    Returns Command

    for chaining

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.

    example
     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')
    
     program
       .command('setup')
       .description('run remote setup commands')
       .action(function() {
         console.log('setup');
       });
    
     program
       .command('exec <cmd>')
       .description('run the given remote command')
       .action(function(cmd) {
         console.log('exec "%s"', cmd);
       });
    
     program
       .command('teardown <dir> [otherDirs...]')
       .description('run teardown commands')
       .action(function(dir, otherDirs) {
         console.log('dir "%s"', dir);
         if (otherDirs) {
           otherDirs.forEach(function (oDir) {
             console.log('dir "%s"', oDir);
           });
         }
       });
    
     program
       .command('*')
       .description('deploy the given env')
       .action(function(env) {
         console.log('deploying "%s"', env);
       });
    
     program.parse(process.argv);

    Parameters

    • name: string
    • Optional desc: undefined | string
    • Optional opts: CommandOptions

    Returns Command

    the new command

description

  • description(str: string): Command
  • description(): string
  • Set the description to str.

    Parameters

    • str: string

    Returns Command

  • Returns string

emit

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

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

    Returns boolean

eventNames

  • eventNames(): Array<string | symbol>
  • Returns Array<string | symbol>

getMaxListeners

  • getMaxListeners(): number

help

  • help(cb?: undefined | function): never
  • Output help information and exit.

    Parameters

    • Optional cb: undefined | function

    Returns never

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

  • Get the name of the command.

    Returns string

off

  • off(event: string | symbol, listener: function): this
  • Parameters

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

          • Rest ...args: any[]

          Returns void

    Returns this

on

  • on(event: string | symbol, listener: function): this
  • Parameters

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

          • Rest ...args: any[]

          Returns void

    Returns this

once

  • once(event: string | symbol, listener: function): this
  • Parameters

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

          • Rest ...args: any[]

          Returns void

    Returns this

option

  • option(flags: string, description?: undefined | string, fn?: function | 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: function | RegExp
    • Optional defaultValue: any

    Returns Command

    for chaining

  • Parameters

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

    Returns Command

opts

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

    Returns object

    • [key: string]: any

outputHelp

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

    Parameters

    • Optional cb: undefined | function

    Returns void

parse

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

    Parameters

    • argv: string[]

    Returns Command

    for chaining

parseExpectedArgs

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

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

    Parameters

    • args: string[]

    Returns Command

    for chaining

parseOptions

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

    Parameters

    • argv: string[]

    Returns ParseOptionsResult

prependListener

  • prependListener(event: string | symbol, listener: function): this
  • Parameters

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

          • Rest ...args: any[]

          Returns void

    Returns this

prependOnceListener

  • prependOnceListener(event: string | symbol, listener: function): this
  • Parameters

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

          • Rest ...args: any[]

          Returns void

    Returns this

rawListeners

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

    • event: string | symbol

    Returns Function[]

removeAllListeners

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

    • Optional event: string | symbol

    Returns this

removeListener

  • removeListener(event: string | symbol, listener: function): this
  • Parameters

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

          • Rest ...args: any[]

          Returns void

    Returns this

setMaxListeners

  • setMaxListeners(n: number): this

usage

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

    Parameters

    • str: string

    Returns Command

  • Returns string

version

  • version(str: string, flags?: 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.

    Parameters

    • str: string
    • Optional flags: undefined | string

    Returns Command

    for chaining

Generated using TypeDoc