Class Args<TArgTypes>

The root class for the library. Generally, it represents a configured parser which can then be used to parse arbitrary input strings.

It will hold all the state needed to parse inputs. This state is modified through the various helper methods defined on this class.

Type Parameters

Hierarchy

  • Args

Constructors

Properties

_state: ArgsState
positionalIndex: number = 0

Methods

  • Add a flag argument to the configuration. The first long flag is used as the key in the output object so must be given. Any aliases after that are just used as alternatives, and will not appear in the output object

    If the key or any aliases exist in the config, this will throw.

    Returns

    this

    Type Parameters

    • TArg extends CoercedValue

    • TLong extends string

    Parameters

    • param0: [`--${TLong}`, ...(`--${string}` | `-${string}`)[]]

      The key and aliases to register

    • arg: MinimalArgument<TArg>

      The argument to register

    Returns Args<TArgTypes & { [ key in string]: TArg }>

  • Adds a Command to the configuration. This will enable commands / subcommands (declared on the Command itself) to be recognised by the parser.

    If a command is not recognised by the parser, it will refer to the ParserOpts.unrecognisedCommand option to determine what to do.

    This will throw if the command or any of its aliases are already registered, or if it conflicts with the command triggers of a registered builtin.

    Returns

    this

    Type Parameters

    • TName extends string

    • TCommand extends Command

    Parameters

    • param0: [`${TName}`, ...string[]]

      The name and aliases to register

    • command: TCommand

      The command to register with

    • inherit: boolean = false

      Whether to inherit arguments from this configuration into the parser

    Returns Args<TArgTypes>

  • Appends or overwrites the footer lines, which are shown at the bottom of the help string.

    Returns

    this

    Parameters

    • line: string

      The footer line to show

    • append: boolean = true

      Whether to append, or overwrite

    Returns Args<TArgTypes>

  • Appends or overwrites the header lines, which are shown at the top of the help string.

    Returns

    this

    Parameters

    • line: string

      The header line to show

    • append: boolean = true

      Whether to append, or overwrite

    Returns Args<TArgTypes>

  • Generate the help string for this configuration. This is an alias for generateHelp, passing in this.

    Returns

    The generated help string

    Returns string

  • Internal

    Parameters

    • coerced: Map<InternalArgument, CoercedMultiValue | CoercedSingleValue>
    • rest: undefined | string

    Returns TArgTypes

  • Internal

    Parameters

    • args: ParsedArguments

    Returns [Record<string, string[]>, string[]]

  • Parses the provided arguments, printing the problems and exiting if the parse fails. Callers can use parseToResult if control of parse failure behaviour is required.

    Returns

    The result of the parse, never an error

    Parameters

    • argString: string | string[]

      The arguments to parse

    • executeCommands: boolean = false

      Whether to execute discovered commands, or return them

    Returns Promise<ParseSuccess<TArgTypes>>

  • Attempt to parse the provided arguments, returning the result of the operation. This is an alternative to parse, allowing the caller to control what happens in the event of parse failure.

    Returns

    The result of the parse

    Parameters

    • argString: string | string[]

      The arguments to parse

    • executeCommands: boolean = false

      Whether to execute discovered commands, or return them

    Returns Promise<Result<ParseSuccess<TArgTypes>, ParseError | CommandError | CoercionError[]>>

  • Add a positional Argument to the configuration. Conflict behaviour with commands is described in command If a positional exists with the provided key, this will throw.

    Returns

    this

    Type Parameters

    • TArg extends CoercedValue

    • TKey extends string

    Parameters

    • key: `<${TKey}>`

      The key to use for the positional, maps to the output object.

    • arg: MinimalArgument<TArg>

      The argument to register

    Returns Args<TArgTypes & { [ key in string]: TArg }>

  • Adds a Resolver to the configuration. This has no checks for duplicate resolvers.

    Returns

    this

    Parameters

    • resolver: Resolver

      The resolver to add

    Returns Args<TArgTypes>

  • Validate that the schema is okay to use. This detects problems which cannot be detected at a type level. This may be expensive to call, so should not be used in production.

    It is advised to run this in tests, and when your schema changes. If there are problems, and this is not called & handled, the behaviour of the parser is undefined.

    Returns

    SchemaError if there is a problem, this otherwise

    Returns Result<Args<TArgTypes>, SchemaError>

Generated using TypeDoc