API 参考
    正在准备搜索索引...

    层级

    • Command
      • VipCommander
    索引

    构造函数

    属性

    args: string[]
    commands: readonly Command[]
    options: readonly Option[]
    parent: null | Command
    processedArgs: any[]
    registeredArguments: readonly Argument[]

    方法

    • Register callback fn for the command.

      参数

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

      返回 this

      this command for chaining

      program
      .command('serve')
      .description('start service')
      .action(function() {
      // do work here
      });
    • Define argument syntax for command, adding a prepared argument.

      参数

      • arg: Argument

      返回 this

      this command for chaining

    • Add a prepared subcommand.

      See .command() for creating an attached subcommand which inherits settings from its parent.

      参数

      • cmd: Command
      • 可选opts: CommandOptions

      返回 this

      this command for chaining

    • Add prepared custom help command.

      参数

      • cmd: Command

      返回 this

    • 参数

      • nameAndArgs: string
      • 可选description: string

      返回 this

      since v12, instead use helpCommand

    • 参数

      • 可选enable: boolean

      返回 this

      since v12, instead use helpCommand

    • Supply your own option to use for the built-in help option. This is an alternative to using helpOption() to customise the flags and description etc.

      参数

      • option: Option

      返回 this

    • Add additional text to be displayed with the built-in help.

      Position is 'before' or 'after' to affect just this command, and 'beforeAll' or 'afterAll' to affect this command and all its subcommands.

      参数

      • position: AddHelpTextPosition
      • text: string

      返回 this

    • Add additional text to be displayed with the built-in help.

      Position is 'before' or 'after' to affect just this command, and 'beforeAll' or 'afterAll' to affect this command and all its subcommands.

      参数

      • position: AddHelpTextPosition
      • text: (context: AddHelpTextContext) => string

      返回 this

    • Add a prepared Option.

      See .option() and .requiredOption() for creating and attaching an option in a single call.

      参数

      • option: Option

      返回 this

    • Set an alias for the command.

      You may call more than once to add multiple aliases. Only the first alias is shown in the auto-generated help.

      参数

      • alias: string

      返回 this

      this command for chaining

    • Get alias for the command.

      返回 string

    • Set aliases for the command.

      Only the first alias is shown in the auto-generated help.

      参数

      • aliases: readonly string[]

      返回 this

      this command for chaining

    • Get aliases for the command.

      返回 string[]

    • Allow excess command-arguments on the command line. Pass false to make excess arguments an error.

      参数

      • 可选allowExcess: boolean

      返回 this

      this command for chaining

    • Allow unknown options on the command line.

      参数

      • 可选allowUnknown: boolean

      返回 this

      this command for chaining

    • Define argument syntax for command.

      The default is that the argument is required, and you can explicitly indicate this with <> around the name. Put [] around the name for an optional argument.

      类型参数

      • T

      参数

      • flags: string
      • description: string
      • fn: (value: string, previous: T) => T
      • 可选defaultValue: T

      返回 this

      this command for chaining

      program.argument('<input-file>');
      program.argument('[output-file]');
    • Define argument syntax for command.

      The default is that the argument is required, and you can explicitly indicate this with <> around the name. Put [] around the name for an optional argument.

      参数

      • name: string
      • 可选description: string
      • 可选defaultValue: unknown

      返回 this

      this command for chaining

      program.argument('<input-file>');
      program.argument('[output-file]');
    • Define argument syntax for command, adding multiple at once (without descriptions).

      See also .argument().

      参数

      • names: string

      返回 this

      this command for chaining

      program.arguments('<cmd> [env]');
      
    • Alter parsing of short flags with optional values.

      参数

      • 可选combine: boolean

      返回 this

      this command for chaining

      // for `.option('-f,--flag [value]'):
      .combineFlagAndOptionalValue(true) // `-f80` is treated like `--flag=80`, this is the default behaviour
      .combineFlagAndOptionalValue(false) // `-fb` is treated like `-f -b`
    • Define a command, implemented using an action handler.

      参数

      • nameAndArgs: string

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

      • 可选opts: CommandOptions

        configuration options

      返回 Command

      new command

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

      program
      .command('clone <source> [destination]')
      .description('clone a repository into a newly created directory')
      .action((source, destination) => {
      console.log('clone command called');
      });
    • Define a command, implemented in a separate executable file.

      参数

      • nameAndArgs: string

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

      • description: string

        description of executable command

      • 可选opts: ExecutableCommandOptions

        configuration options

      返回 this

      this command for chaining

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

       program
      .command('start <service>', 'start named service')
      .command('stop [service]', 'stop named service, or all if no name supplied');
    • You can customise the help by overriding Help properties using configureHelp(), or with a subclass of Help by overriding createHelp().

      参数

      • configuration: HelpConfiguration

      返回 this

    • Get configuration

      返回 HelpConfiguration

    • The default output goes to stdout and stderr. You can customise this for special applications. You can also customise the display of errors by overriding outputError.

      The configuration properties are all functions:

      // functions to change where being written, stdout and stderr
      writeOut(str)
      writeErr(str)
      // matching functions to specify width for wrapping help
      getOutHelpWidth()
      getErrHelpWidth()
      // functions based on what is being written out
      outputError(str, write) // used for displaying errors, and not used for displaying help

      参数

      • configuration: OutputConfiguration

      返回 this

    • Get configuration

      返回 OutputConfiguration

    • Copy settings that are useful to have in common across root command and subcommands.

      (Used internally when adding a command using .command() so subcommands inherit parent settings.)

      参数

      • sourceCommand: Command

      返回 this

    • Factory routine to create a new unattached argument.

      See .argument() for creating an attached argument, which uses this routine to create the argument. You can override createArgument to return a custom argument.

      参数

      • name: string
      • 可选description: string

      返回 Argument

    • Factory routine to create a new unattached command.

      See .command() for creating an attached subcommand, which uses this routine to create the command. You can override createCommand to customise subcommands.

      参数

      • 可选name: string

      返回 Command

    • You can customise the help with a subclass of Help by overriding createHelp, or by overriding Help properties using configureHelp().

      返回 Help

    • Factory routine to create a new unattached option.

      See .option() for creating an attached option, which uses this routine to create the option. You can override createOption to return a custom option.

      参数

      • flags: string
      • 可选description: string

      返回 Option

    • Set the description.

      参数

      • str: string

      返回 this

      this command for chaining

    • 参数

      • str: string
      • argsDescription: Record<string, string>

      返回 this

      since v8, instead use .argument to add command argument with description

    • Get the description.

      返回 string

    • Enable positional options. Positional means global options are specified before subcommands which lets subcommands reuse the same option names, and also enables subcommands to turn on passThroughOptions.

      The default behaviour is non-positional and global options may appear anywhere on the command line.

      参数

      • 可选positional: boolean

      返回 this

      this command for chaining

    • Display error message and exit (or call exitOverride).

      参数

      • message: string
      • 可选errorOptions: ErrorOptions

      返回 never

    • Set the directory for searching for executable subcommands of this command.

      参数

      • path: string

      返回 this

      this command for chaining

      program.executableDir(__dirname);
      // or
      program.executableDir('subcommands');
    • Get the executable search directory.

      返回 null | string

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

      参数

      • 可选callback: (err: CommanderError) => void

      返回 this

    • Retrieve option value.

      参数

      • key: string

      返回 any

    • Get source of option value.

      参数

      • key: string

      返回 OptionValueSource

    • Get source of option value. See also .optsWithGlobals().

      参数

      • key: string

      返回 OptionValueSource

    • Output help information and exit.

      Outputs built-in help, and custom text added using .addHelpText().

      参数

      • 可选context: HelpContext

      返回 never

    • 参数

      • 可选cb: (str: string) => string

      返回 never

      since v7

    • Customise or override default help command. By default a help command is automatically added if your command has subcommands.

      参数

      • nameAndArgs: string
      • 可选description: string

      返回 this

      program.helpCommand('help [cmd]');
      program.helpCommand('help [cmd]', 'show help');
      program.helpCommand(false); // suppress default help command
      program.helpCommand(true); // add help command even if no subcommands
    • Customise or override default help command. By default a help command is automatically added if your command has subcommands.

      参数

      • enable: boolean

      返回 this

      program.helpCommand('help [cmd]');
      program.helpCommand('help [cmd]', 'show help');
      program.helpCommand(false); // suppress default help command
      program.helpCommand(true); // add help command even if no subcommands
    • Return command help documentation.

      参数

      • 可选context: HelpContext

      返回 string

    • You can pass in flags and a description to override the help flags and help description for your command. Pass in false to disable the built-in help option.

      参数

      • 可选flags: string | boolean
      • 可选description: string

      返回 this

    • Add hook for life cycle event.

      参数

      • event: HookEvent
      • listener: (thisCommand: Command, actionCommand: Command) => void | Promise<void>

      返回 this

    • Set the name of the command.

      参数

      • str: string

      返回 this

      this command for chaining

    • Get the name of the command.

      返回 string

    • Set the name of the command from script filename, such as process.argv[1], or require.main.filename, or __filename.

      (Used internally and public although not documented in README.)

      参数

      • filename: string

      返回 this

      this command for chaining

      program.nameFromFilename(require.main.filename);
      
    • Add a listener (callback) for when events occur. (Implemented using EventEmitter.)

      参数

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

      返回 this

    • Define option with flags, description, and optional argument parsing function or defaultValue or both.

      The flags string contains the short and/or long flags, separated by comma, a pipe or space. A required option-argument is indicated by <> and an optional option-argument by [].

      See the README for more details, and see also addOption() and requiredOption().

      参数

      • flags: string
      • 可选description: string
      • 可选defaultValue: string | boolean | string[]

      返回 this

      this command for chaining

      program
      .option('-p, --pepper', 'add pepper')
      .option('-p, --pizza-type <TYPE>', 'type of pizza') // required option-argument
      .option('-c, --cheese [CHEESE]', 'add extra cheese', 'mozzarella') // optional option-argument with default
      .option('-t, --tip <VALUE>', 'add tip to purchase cost', parseFloat) // custom parse function
    • Define option with flags, description, and optional argument parsing function or defaultValue or both.

      The flags string contains the short and/or long flags, separated by comma, a pipe or space. A required option-argument is indicated by <> and an optional option-argument by [].

      See the README for more details, and see also addOption() and requiredOption().

      类型参数

      • T

      参数

      • flags: string
      • description: string
      • parseArg: (value: string, previous: T) => T
      • 可选defaultValue: T

      返回 this

      this command for chaining

      program
      .option('-p, --pepper', 'add pepper')
      .option('-p, --pizza-type <TYPE>', 'type of pizza') // required option-argument
      .option('-c, --cheese [CHEESE]', 'add extra cheese', 'mozzarella') // optional option-argument with default
      .option('-t, --tip <VALUE>', 'add tip to purchase cost', parseFloat) // custom parse function
    • 参数

      • flags: string
      • description: string
      • regexp: RegExp
      • 可选defaultValue: string | boolean | string[]

      返回 this

      since v7, instead use choices or a custom function

    • Return an object containing local option values as key-value pairs

      类型参数

      • T extends OptionValues

      返回 T

    • Return an object containing merged local and global option values as key-value pairs.

      类型参数

      • T extends OptionValues

      返回 T

    • Output help information for this command.

      Outputs built-in help, and custom text added using .addHelpText().

      参数

      • 可选context: HelpContext

      返回 void

    • 参数

      • 可选cb: (str: string) => string

      返回 void

      since v7

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

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

      Call with no parameters to parse process.argv. Detects Electron and special node options like node --eval. Easy mode!

      Or call with an array of strings to parse, and optionally where the user arguments start by specifying where the arguments are from:

      • 'node': default, argv[0] is the application and argv[1] is the script being run, with user arguments after that
      • 'electron': argv[0] is the application and argv[1] varies depending on whether the electron application is packaged
      • 'user': just user arguments

      参数

      • 可选argv: readonly string[]
      • 可选parseOptions: ParseOptions

      返回 this

      this command for chaining

      program.parse(); // parse process.argv and auto-detect electron and special node flags
      program.parse(process.argv); // assume argv[0] is app and argv[1] is script
      program.parse(my-args, { from: 'user' }); // just user supplied arguments, nothing special about argv[0]
    • Parse argv, setting options and invoking commands when defined.

      Call with no parameters to parse process.argv. Detects Electron and special node options like node --eval. Easy mode!

      Or call with an array of strings to parse, and optionally where the user arguments start by specifying where the arguments are from:

      • 'node': default, argv[0] is the application and argv[1] is the script being run, with user arguments after that
      • 'electron': argv[0] is the application and argv[1] varies depending on whether the electron application is packaged
      • 'user': just user arguments

      参数

      • 可选argv: readonly string[]
      • 可选parseOptions: ParseOptions

      返回 Promise<VipCommander>

      Promise

      await program.parseAsync(); // parse process.argv and auto-detect electron and special node flags
      await program.parseAsync(process.argv); // assume argv[0] is app and argv[1] is script
      await program.parseAsync(my-args, { from: 'user' }); // just user supplied arguments, nothing special about argv[0]
    • Parse options from argv removing known options, and return argv split into operands and unknown arguments.

      argv => operands, unknown
      --known kkk op => [op], []
      op --known kkk => [op], []
      sub --unknown uuu op => [sub], [--unknown uuu op]
      sub -- --unknown uuu op => [sub --unknown uuu op], []
      

      参数

      • argv: string[]

      返回 ParseOptionsResult

    • Pass through options that come after command-arguments rather than treat them as command-options, so actual command-options come before command-arguments. Turning this on for a subcommand requires positional options to have been enabled on the program (parent commands).

      The default behaviour is non-positional and options may appear before or after command-arguments.

      参数

      • 可选passThrough: boolean

      返回 this

      this command for chaining

    • 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 contains the short and/or long flags, separated by comma, a pipe or space.

      参数

      • flags: string
      • 可选description: string
      • 可选defaultValue: string | boolean | string[]

      返回 this

    • 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 contains the short and/or long flags, separated by comma, a pipe or space.

      类型参数

      • T

      参数

      • flags: string
      • description: string
      • parseArg: (value: string, previous: T) => T
      • 可选defaultValue: T

      返回 this

    • 参数

      • flags: string
      • description: string
      • regexp: RegExp
      • 可选defaultValue: string | boolean | string[]

      返回 this

      since v7, instead use choices or a custom function

    • Store option value.

      参数

      • key: string
      • value: unknown

      返回 this

    • Store option value and where the value came from.

      参数

      • key: string
      • value: unknown
      • source: OptionValueSource

      返回 this

    • Display the help or a custom message after an error occurs.

      参数

      • 可选displayHelp: string | boolean

      返回 this

    • Display suggestion of similar commands for unknown commands, or options for unknown options.

      参数

      • 可选displaySuggestion: boolean

      返回 this

    • 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().

      类型参数

      • T extends OptionValues

      返回 VipCommander & T

      this command for chaining

    • 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().

      类型参数

      • T extends OptionValues

      参数

      • storeAsProperties: true

      返回 VipCommander & T

      this command for chaining

    • 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().

      参数

      • 可选storeAsProperties: boolean

      返回 this

      this command for chaining

    • Set the summary. Used when listed as subcommand of parent.

      参数

      • str: string

      返回 this

      this command for chaining

    • Get the summary.

      返回 string

    • Set the command usage.

      参数

      • str: string

      返回 this

      this command for chaining

    • Get the command usage.

      返回 string

    • 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.

      参数

      • str: string
      • 可选flags: string
      • 可选description: string

      返回 this

    • Get the program version.

      返回 undefined | string