Options
Modules
  • Command
  • Localization
  • Logger
All
  • Public
  • Public/Protected
  • All
Menu

Class CommandArgumentSpec

Used for defining the specification for what a Command's given arguments should look like so the input parser can know how to parse the input given for a Command

Hierarchy

  • CommandArgumentSpec

Index

Constructors

constructor

Properties

bindings

bindings: Map<string, CommandArgumentSpecEntry> = ...

flags

flags: Map<string, CommandArgumentSpecFlag> = ...

operands

operands: CommandArgumentSpecOperand[] = []

options

options: Map<string, CommandArgumentSpecOption> = ...

parsingStrategy

parsingStrategy: CommandArgumentParsingStrategy = ...

Methods

clone

defineFlag

  • defineFlag(ident: string, options?: { bindTo?: string; long?: string }): void
  • Defines a flag (like -h or --help) for your Command's arguments specification.

    If given an identifier, it may be a single character or a long identifier. If given an optional long identifier in the options object parameter in addition to the identifier as the first parameter, the first paramater identifier may only be a single character. Example:

    <Command>.arguments.defineFlag('f');
    <Command>.arguments.defineFlag('bar');
    <Command>.arguments.defineFlag('b', { long: 'baz' });
    

    NOTE: If a flag is found and the parsing strategy is not set to Advanced (2) then it will be treated as an operand

    Parameters

    • ident: string
    • options: { bindTo?: string; long?: string } = {}
      • Optional bindTo?: string

        The identifier of the argument this argument should bind to. Arguments that are bound to another will fill that argument's value with theirs in the event that that argument has no value

        <Command>.arguments.defineOperand('A', 'String', { bindTo: 'B' });
        <Command>.arguments.defineOption('B', 'String')
        

        In the above example, argument A is bound to argument B. Whenever a value is passed for A (a positional operand in this case), that value will be mirrored to argument B, provided argument B's value is undefined

      • Optional long?: string

        A long-identifier for this Option (to be used like --long foo)

    Returns void

defineOperand

  • defineOperand(ident: string, type: string, options?: { bindTo?: string; required?: boolean; rest?: boolean }): void
  • Defines a positional operand argument declaration for your Command's arguments specification. Must be given an identifier and a type, and can be declared required (true by default). Optional operands must be defined last and may not be followed by a non-optional operand.

    May also be declared as a rest operand (false by default), which will consume the remainder of the input as a single operand when parsing for that operand begins. Rest operands must be defined last and may not be followed by any additional operands.

    Operand identifiers must be at least 2 characters to avoid confusion and avoid conflict with flags and options

    Examples:

    <Command>.arguments.defineOperand('foo', 'String');
    <Command>.arguments.defineOperand('bar', 'String', { required: false });
    

    NOTE: If the parsing strategy is set to AllowQuoting (1) or higher and a quoted operand is going to be parsed but the spec says it should be a rest argument then the quotes will be preserved in the operand value.

    NOTE: Rest operands will supercede any other kind of argument when parsing for that operand begins. If the parsing strategy is Advanced (2) but the parser encounters something that would otherwise be parsed as a flag or option it will still be parsed as part of the rest operand. Note this is only the case when the parser is currently consuming a rest operand.

    NOTE: If the parsing strategy is not set to Advanced (2) then every argument encountered will be treated as an operand. If an argument follows a flag and that flag is not defined as an Option via defineOption() then the argument will be parsed as an operand and the flag will be parsed as a flag as expected

    Parameters

    • ident: string
    • type: string
    • options: { bindTo?: string; required?: boolean; rest?: boolean } = {}
      • Optional bindTo?: string

        The identifier of the argument this argument should bind to. Arguments that are bound to another will fill that argument's value with theirs in the event that that argument has no value

        <Command>.arguments.defineOperand('A', 'String', { bindTo: 'B' });
        <Command>.arguments.defineOption('B', 'String')
        

        In the above example, argument A is bound to argument B. Whenever a value is passed for A (a positional operand in this case), that value will be mirrored to argument B, provided argument B's value is undefined

      • Optional required?: boolean

        Whether or not this operand is a required argument (Defaults to true)

      • Optional rest?: boolean

        Whether or not this operand is a rest operand (Defaults to false)

    Returns void

defineOption

  • defineOption(ident: string, type: string, options?: { bindTo?: string; long?: string; required?: boolean }): void
  • Defines an option that takes an argument (like -f value) for your Command's arguments specification.

    If given an identifier, it may be a single character or a long identifier. If given an optional long identifier in the options object parameter in addition to the identifier as the first parameter, the first paramater may only be a single character. Examples:

    <Command>.arguments.defineOption('f', 'String');
    <Command>.arguments.defineOption('bar', 'String');
    <Command>.arguments.defineOption('b', 'String', { long: 'baz' });
    

    NOTE: If an option is found and the parsing strategy is not set to Advanced (2) then it will be treated as an operand. If an argument that can be parsed as an option is found in a Command's input but the option is not declared then it will be treated as a long flag and the argument passed to it will be treated as an operand

    Parameters

    • ident: string
    • type: string
    • options: { bindTo?: string; long?: string; required?: boolean } = {}
      • Optional bindTo?: string

        The identifier of the argument this argument should bind to. Arguments that are bound to another will fill that argument's value with theirs in the event that that argument has no value

        <Command>.arguments.defineOperand('A', 'String', { bindTo: 'B' });
        <Command>.arguments.defineOption('B', 'String')
        

        In the above example, argument A is bound to argument B. Whenever a value is passed for A (a positional operand in this case), that value will be mirrored to argument B, provided argument B's value is undefined

      • Optional long?: string

        A long-identifier for this Option (to be used like --long foo or --long=foo)

      • Optional required?: boolean

        Whether or not this option is required (Defaults to false)

    Returns void

finalizeBindings

  • finalizeBindings(): void
  • Matches arguments to their bindings, erroring if the bound argument does not exist. This should be called before commands are allowed to be run

    Returns void

get

  • get<T>(ident: string): undefined | T
  • Returns a flag or option spec for the given identifier.

    NOTE: Operand specs should be retrieved by shifting the operands array of a cloned spec.

    WARNING: Be sure to only operate on cloned specs. We do not want to shift the operand specs out of the original specification

    Type parameters

    • T: CommandArgumentSpecEntry

    Parameters

    • ident: string

    Returns undefined | T

setParsingStrategy

  • setParsingStrategy(strategy: CommandArgumentParsingStrategy): void
  • Sets the argument parsing strategy. Can be set to one of the following:

    0 -> Basic. All words are counted as separate operands
    1 -> AllowQuoting. Same as basic, but multiple arguments can be quoted
         to create a single operand
    2 -> Advanced. Includes basic and quoting, adds flags and options
         with arguments
    

    Examples:

           Basic -> `foo bar baz`
    AllowQuoting -> `foo "bar baz" boo`
        Advanced -> `foo -b --bar --baz="boo far faz"`
    

    NOTE: The default strategy is Basic (0). This is set per command, so setting one command's argument spec parsing strategy to AllowQuoting or Advanced will not affect any other commands. This can also be changed at any time, so you can feasibly iterate over all commands at runtime and forcibly change their parsing strategy (for example, to allow quoted arguments on a loaded command from a plugin that you did not write). For example:

    CommandModule.commands.get('foo').arguments.setParsingStrategy(1);
    

    Parameters

    • strategy: CommandArgumentParsingStrategy

    Returns void

Generated using TypeDoc