Base representation of command line option passed to its reader in order to be recognized.

A class implementing this interface along with any interface specific to the parser is created by ZOptionsParser.Config.optionClass method.

The reader uses option methods to recognize and consume command line arguments. Once recognized these arguments are consumed, and option reading continues from the next non-consumed argument.

If multiple readers correspond to the same option key, they all consulted in order. Once one of them recognized the option, the remaining and deferred ones receive an option instance representing already consumed option. The latter can be read by readers, but can no longer be updated.

Hierarchy

  • ZOption

Properties

argIndex: number

An index of the first command line argument the reader should recognized.

args: readonly string[]

Command line arguments the option is read from.

These arguments may be updated by option syntax between the read attempts.

key: string

Option key used to find corresponding option readers.

This may be the same as [name], or may be e.g. a wildcard key when no readers for the option name recognized it. In any case, the option key is syntax-specific.

name: string

Option name.

This is a command line argument at currently processed index.

Methods

  • Defers the option processing until recognized by another reader available for the same option key.

    Calling this method marks arguments as unrecognized. This can be changed by calling any recognition method again. The already registered callback will be unregistered on such call.

    Parameters

    • Optional whenRecognized: Fn<ZOption, unknown>

      Optional callback function that will be called when option recognized by another reader.

    Returns void

  • Marks current option as recognized.

    This method is called by [rest] and [values].

    This method can be called multiple times to register multiple actions to call. These actions won't be called once the option is marked as unrecognized again.

    Parameters

    • Optional action: ((this) => void)

      The action to perform when the option recognized and all readers processed.

        • (this): void
        • Parameters

          • this: void

          Returns void

    Returns void

  • Reads up to the maximum number of the remaining command line arguments and treat them as option values.

    Reads [values] and all command line arguments following them up to the end of command line or their maximum number.

    Calling this method marks all arguments read as recognized. This can be undone by calling any recognition method again.

    When called on already recognized option this method just returns the values previously returned to the reader that recognized them.

    Parameters

    • Optional max: number

      The maximum number of arguments to read.

    Returns readonly string[]

    Command line arguments array.

  • Lists all options supported by the parser.

    Returns readonly string[]

    A read-only array of all supported option keys except hidden ones.

  • Marks current option as unrecognized.

    This method is called by [defer].

    When calling without parameter the previous reason is not updated.

    Does nothing if current option is recognized by another reader.

    Parameters

    • Optional reason: unknown

      An error to throw when all readers processed and option is still unrecognized. When omitted, the ZOptionError will be thrown.

    Returns void

  • Reads up to the maximum number of values of the option.

    Values are command line options following the currently processed one. Their number depend on the option syntax. E.g. a --name=value syntax supports one value, while --name value1 value2 ... syntax supports any number of them.

    Calling this method marks the option and value arguments as recognized. This can be undone by calling any recognition method again.

    When called on already recognized option this method just returns the values previously returned to the reader that recognized them.

    Parameters

    • Optional max: number

      The maximum number of values to read.

    Returns readonly string[]

    Option values array.

  • Allows to await for option recognition.

    Calling this method does not alter option recognition status in any way, unlike recognition. An unlike deferring, the callback registered by this method will be called when the option recognized by any reader, not just the readers registered for the same option key.

    Parameters

    • receiver: ((this, option) => void)

      Callback function that will be called when the option recognized and all readers processed.

        • (this, option): void
        • Parameters

          Returns void

    Returns void

Generated using TypeDoc