Class CommandPolykey

Base class for all commands

Hierarchy

Constructors

Properties

args: string[]
commands: Command[]
exitHandlers: ExitHandlers
fs: FileSystem
logger: Logger = logger
parent: null | Command
processedArgs: any[]

Methods

  • Parameters

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

          • Rest ...args: any[]

          Returns void | Promise<void>

    Returns CommandPolykey

  • Define argument syntax for command, adding a prepared argument.

    Parameters

    • arg: Argument

    Returns CommandPolykey

    this command for chaining

  • Add a prepared subcommand.

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

    Parameters

    • cmd: Command
    • Optional opts: CommandOptions

    Returns CommandPolykey

    this command for chaining

  • Override default decision whether to add implicit help command.

    Parameters

    • Optional enableOrNameAndArgs: string | boolean
    • Optional description: string

    Returns CommandPolykey

    this command for chaining

    Example

    addHelpCommand() // force on
    addHelpCommand(false); // force off
    addHelpCommand('help [cmd]', 'display help for [cmd]'); // force on with custom details
  • 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.

    Parameters

    • position: AddHelpTextPosition
    • text: string

    Returns CommandPolykey

  • Parameters

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

          • context: AddHelpTextContext

          Returns string

    Returns CommandPolykey

  • Add a prepared Option.

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

    Parameters

    • option: Option

    Returns CommandPolykey

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

    Parameters

    • alias: string

    Returns CommandPolykey

    this command for chaining

  • Get alias for the command.

    Returns string

  • Set aliases for the command.

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

    Parameters

    • aliases: string[]

    Returns CommandPolykey

    this command for chaining

  • Get aliases for the command.

    Returns string[]

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

    Parameters

    • Optional allowExcess: boolean

    Returns CommandPolykey

    this command for chaining

  • Allow unknown options on the command line.

    Parameters

    • Optional allowUnknown: boolean

    Returns CommandPolykey

    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.

    Type Parameters

    • T

    Parameters

    • flags: string
    • description: string
    • fn: ((value, previous) => T)
        • (value, previous): T
        • Parameters

          • value: string
          • previous: T

          Returns T

    • Optional defaultValue: T

    Returns CommandPolykey

    this command for chaining

    Example

    program.argument('<input-file>');
    program.argument('[output-file]');
  • Parameters

    • name: string
    • Optional description: string
    • Optional defaultValue: unknown

    Returns CommandPolykey

  • Define argument syntax for command, adding multiple at once (without descriptions).

    See also .argument().

    Parameters

    • names: string

    Returns CommandPolykey

    this command for chaining

    Example

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

    Parameters

    • Optional combine: boolean

    Returns CommandPolykey

    this command for chaining

    Example

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

    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

    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');
    });
  • Define a command, implemented in a separate executable file.

    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: ExecutableCommandOptions

      configuration options

    Returns CommandPolykey

    this command for chaining

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

    Parameters

    • configuration: Partial<Help>

    Returns CommandPolykey

  • Get configuration

    Returns Partial<Help>

  • 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

    Parameters

    • configuration: OutputConfiguration

    Returns CommandPolykey

  • Get configuration

    Returns 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.)

    Parameters

    • sourceCommand: Command

    Returns CommandPolykey

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

    Parameters

    • name: string
    • Optional description: string

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

    Parameters

    • Optional name: string

    Returns Command

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

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

    Parameters

    • flags: string
    • Optional description: string

    Returns Option

  • Set the description.

    Parameters

    • str: string

    Returns CommandPolykey

    this command for chaining

  • Parameters

    • str: string
    • argsDescription: {
          [argName: string]: string;
      }
      • [argName: string]: string

    Returns CommandPolykey

    Deprecated

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

  • Get the description.

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

    Parameters

    • Optional positional: boolean

    Returns CommandPolykey

    this command for chaining

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

    Parameters

    • Optional callback: ((err) => void)
        • (err): void
        • Parameters

          • err: CommanderError

          Returns void

    Returns CommandPolykey

  • Retrieve option value.

    Parameters

    • key: string

    Returns any

  • Retrieve option value source.

    Parameters

    • key: string

    Returns OptionValueSource

  • Output help information and exit.

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

    Parameters

    • Optional context: HelpContext

    Returns never

  • Parameters

    • Optional cb: ((str) => string)
        • (str): string
        • Parameters

          • str: string

          Returns string

    Returns never

    Deprecated

    since v7

  • Return command help documentation.

    Parameters

    • Optional context: HelpContext

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

    Parameters

    • Optional flags: string | boolean
    • Optional description: string

    Returns CommandPolykey

  • Add hook for life cycle event.

    Parameters

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

          • thisCommand: Command
          • actionCommand: Command

          Returns void | Promise<void>

    Returns CommandPolykey

  • Set the name of the command.

    Parameters

    • str: string

    Returns CommandPolykey

    this command for chaining

  • Get the name of the command.

    Returns string

  • Add a listener (callback) for when events occur. (Implemented using EventEmitter.)

    Parameters

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

          • Rest ...args: any[]

          Returns void

    Returns CommandPolykey

  • Define option with flags, description and optional coercion fn.

    The flags string contains the short and/or 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"

    Parameters

    • flags: string
    • Optional description: string
    • Optional defaultValue: string | boolean

    Returns CommandPolykey

    this command for chaining

    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]');
  • Type Parameters

    • T

    Parameters

    • flags: string
    • description: string
    • fn: ((value, previous) => T)
        • (value, previous): T
        • Parameters

          • value: string
          • previous: T

          Returns T

    • Optional defaultValue: T

    Returns CommandPolykey

  • Parameters

    • flags: string
    • description: string
    • regexp: RegExp
    • Optional defaultValue: string | boolean

    Returns CommandPolykey

    Deprecated

    since v7, instead use choices or a custom function

  • Overrides opts to return all options set in the command hierarchy

    Type Parameters

    • T extends OptionValues

    Returns T

  • Output help information for this command.

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

    Parameters

    • Optional context: HelpContext

    Returns void

  • Parameters

    • Optional cb: ((str) => string)
        • (str): string
        • Parameters

          • str: string

          Returns string

    Returns void

    Deprecated

    since v7

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

    The default expectation is that the arguments are from node and have the application as argv[0] and the script being run in argv[1], with user parameters after that.

    Parameters

    • Optional argv: string[]
    • Optional options: ParseOptions

    Returns CommandPolykey

    this command for chaining

    Example

    program.parse(process.argv);
    program.parse(); // implicitly use process.argv and auto-detect node vs electron conventions
    program.parse(my-args, { from: 'user' }); // just user supplied arguments, nothing special about argv[0]
  • 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.

    The default expectation is that the arguments are from node and have the application as argv[0] and the script being run in argv[1], with user parameters after that.

    Parameters

    • Optional argv: string[]
    • Optional options: ParseOptions

    Returns Promise<CommandPolykey>

    Promise

    Example

    program.parseAsync(process.argv);
    program.parseAsync(); // implicitly use process.argv and auto-detect node vs electron conventions
    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], []

    Parameters

    • argv: string[]

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

    Parameters

    • Optional passThrough: boolean

    Returns CommandPolykey

    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.

    Parameters

    • flags: string
    • Optional description: string
    • Optional defaultValue: string | boolean

    Returns CommandPolykey

  • Type Parameters

    • T

    Parameters

    • flags: string
    • description: string
    • fn: ((value, previous) => T)
        • (value, previous): T
        • Parameters

          • value: string
          • previous: T

          Returns T

    • Optional defaultValue: T

    Returns CommandPolykey

  • Parameters

    • flags: string
    • description: string
    • regexp: RegExp
    • Optional defaultValue: string | boolean

    Returns CommandPolykey

    Deprecated

    since v7, instead use choices or a custom function

  • Store option value.

    Parameters

    • key: string
    • value: unknown

    Returns CommandPolykey

  • Store option value and where the value came from.

    Parameters

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

    Returns CommandPolykey

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

    Parameters

    • Optional displayHelp: string | boolean

    Returns CommandPolykey

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

    Parameters

    • Optional displaySuggestion: boolean

    Returns CommandPolykey

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

    Type Parameters

    • T extends OptionValues

    Returns CommandPolykey & T

    this command for chaining

  • Type Parameters

    • T extends OptionValues

    Parameters

    • storeAsProperties: true

    Returns CommandPolykey & T

  • Parameters

    • Optional storeAsProperties: boolean

    Returns CommandPolykey

  • Set the command usage.

    Parameters

    • str: string

    Returns CommandPolykey

    this command for chaining

  • Get the command usage.

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

    Parameters

    • str: string
    • Optional flags: string
    • Optional description: string

    Returns CommandPolykey

Generated using TypeDoc