Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Args

A class that represents a set of arguments.

Hierarchy

  • Args
    • Args

Index

Constructors

  • new Args(parserOutput: ParserOutput): Args
  • Parameters

    • parserOutput: ParserOutput

      The parser output.

    Returns Args

Properties

parserOutput: ParserOutput

The parser output.

state: ArgsState

The state of this instance.

Accessors

  • get finished(): boolean
  • Whether all ordered tokens have been used.

    Returns boolean

  • get length(): number
  • The amount of ordered tokens.

    Returns number

  • get remaining(): number
  • The amount of remaining ordered tokens.

    Returns number

Methods

  • Returns Args

  • filterMap<T>(f: (x: string) => Option<T>, limit?: number, from?: number): T[]
  • Filters and retrieves all unused tokens that could be transformed. Those tokens will now be consider used.

    // Suppose args are from '1 2 3'.
    const parse = (x: string) => {
    const n = Number(x);
    return isNaN(n) || n === 1 ? none() : some(n);
    };

    console.log(args.filterMap(parse));
    >>> [2, 3]

    Type parameters

    • T

      Output type.

    Parameters

    • f: (x: string) => Option<T>

      Gives an option of either the resulting value, or nothing if failed.

        • (x: string): Option<T>
        • Parameters

          • x: string

          Returns Option<T>

    • Optional limit: number

      The limit on the amount of tokens to retrieve; defaults to infinite.

    • Optional from: number

      Where to start looking for tokens; defaults to current position.

    Returns T[]

    The resulting values.

  • filterMapAsync<T>(f: (x: string) => Promise<Option<T>>, limit?: number, from?: number): Promise<T[]>
  • Filters and retrieves all unused tokens that could be transformed. This variant of the function is asynchronous using Promise. Those tokens will now be consider used.

    Type parameters

    • T

      Output type.

    Parameters

    • f: (x: string) => Promise<Option<T>>

      Gives an option of either the resulting value, or nothing if failed.

        • Parameters

          • x: string

          Returns Promise<Option<T>>

    • Optional limit: number

      The limit on the amount of tokens to retrieve; defaults to infinite.

    • Optional from: number

      Where to start looking for tokens; defaults to current position.

    Returns Promise<T[]>

    The resulting values.

  • findMap<T>(f: (x: string) => Option<T>, from?: number): Option<T>
  • Finds and retrieves the first unused token that could be transformed. That token will now be consider used.

    // Suppose args are from '1 2 3'.
    const parse = (x: string) => {
    const n = Number(x);
    return isNaN(n) || n === 1 ? none() : some(n);
    };

    console.log(args.findMap(parse));
    >>> { exists: true, value: 2 }

    Type parameters

    • T

      Output type.

    Parameters

    • f: (x: string) => Option<T>

      Gives an option of either the resulting value, or nothing if failed.

        • (x: string): Option<T>
        • Parameters

          • x: string

          Returns Option<T>

    • Optional from: number

      Where to start looking for tokens; defaults to current position.

    Returns Option<T>

    The resulting value if it was found.

  • findMapAsync<T>(f: (x: string) => Promise<Option<T>>, from?: number): Promise<Option<T>>
  • Finds and retrieves the first unused token that could be transformed. This variant of the function is asynchronous using Promise. That token will now be consider used.

    Type parameters

    • T

      Output type.

    Parameters

    • f: (x: string) => Promise<Option<T>>

      Gives an option of either the resulting value, or nothing if failed.

        • Parameters

          • x: string

          Returns Promise<Option<T>>

    • Optional from: number

      Where to start looking for tokens; defaults to current position.

    Returns Promise<Option<T>>

    The resulting value if it was found.

  • findParse<T, E>(f: (x: string) => Result<T, E>, from?: number): Result<T, E[]>
  • Finds and retrieves the first unused token that could be transformed. That token will now be consider used. This is a variant of {@linkcode Args#findMap} that allows for a Result to be returned.

    Type parameters

    • T

      Output type.

    • E

      Error type.

    Parameters

    • f: (x: string) => Result<T, E>

      Gives a result of either the resulting value, or an error.

        • (x: string): Result<T, E>
        • Parameters

          • x: string

          Returns Result<T, E>

    • Optional from: number

      Where to start looking for tokens; defaults to current position.

    Returns Result<T, E[]>

    The resulting value if it was found or a list of errors during parsing.

  • findParseAsync<T, E>(f: (x: string) => Promise<Result<T, E>>, from?: number): Promise<Result<T, E[]>>
  • Finds and retrieves the first unused token that could be transformed. That token will now be consider used. This variant of the function is asynchronous using Promise. This is a variant of {@linkcode Args#findMapAsync} that allows for a Result to be returned.

    Type parameters

    • T

      Output type.

    • E

      Error type.

    Parameters

    • f: (x: string) => Promise<Result<T, E>>

      Gives a result of either the resulting value, or an error.

        • Parameters

          • x: string

          Returns Promise<Result<T, E>>

    • Optional from: number

      Where to start looking for tokens; defaults to current position.

    Returns Promise<Result<T, E[]>>

    The resulting value if it was found or a list of errors during parsing.

  • flag(...keys: string[]): boolean
  • Checks if a flag was given.

    // Suppose args are from '--f --g'.
    console.log(args.flag('f'));
    >>> true

    console.log(args.flag('g', 'h'));
    >>> true

    console.log(args.flag('h'));
    >>> false

    Parameters

    • Rest ...keys: string[]

      The name(s) of the flag.

    Returns boolean

    Whether the flag was given.

  • many(limit?: number, from?: number): Token[]
  • Retrieves many unused tokens.

    // Suppose args are from '1 2 3'.
    const xs = args.many();
    console.log(joinTokens(xs));
    >>> '1 2 3'

    // Suppose args are from '1 2 3'.
    const xs = args.many(2);
    console.log(joinTokens(xs));
    >>> '1 2'

    Parameters

    • Optional limit: number

      The limit on the amount of tokens to retrieve; defaults to infinite.

    • Optional from: number

      Where to start looking for tokens; defaults to current position.

    Returns Token[]

    The tokens.

  • manyFromEnd(limit?: number, from?: number): Token[]
  • Retrieves many unused tokens from the end. Note that the order of retrieved tokens will be the same order as in the ordered tokens list.

    // Suppose args are from '1 2 3'.
    const xs = args.manyFromEnd();
    console.log(joinTokens(xs));
    >>> '1 2 3'

    // Suppose args are from '1 2 3'.
    const xs = args.manyFromEnd(2);
    console.log(joinTokens(xs));
    >>> '2 3'

    Parameters

    • Optional limit: number

      The limit on the amount of tokens to retrieve; defaults to infinite.

    • Optional from: number

      Where to start looking for tokens; defaults to current position from end.

    Returns Token[]

    The tokens.

  • next(): IteratorResult<string, any>
  • Gets the next ordered argument.

    Returns IteratorResult<string, any>

    An iterator result containing a string.

  • option(...keys: string[]): null | string
  • Gets the last value of an option.

    // Suppose args are from '--a=1 --b=2 --c=3'.
    console.log(args.option('a'));
    >>> '1'

    console.log(args.option('b', 'c'));
    >>> '2'

    console.log(args.option('d'));
    >>> null

    Parameters

    • Rest ...keys: string[]

      The name(s) of the option.

    Returns null | string

    The last value of the option if it was given. When there are multiple names, the last value of the first found name is given.

  • options(...keys: string[]): null | string[]
  • Gets all the values of an option.

    // Suppose args are from '--a=1 --a=1 --b=2 --c=3'.
    console.log(args.options('a'));
    >>> ['1', '1']

    console.log(args.option('b', 'c'));
    >>> ['2', '3']

    console.log(args.option('d'));
    >>> null

    Parameters

    • Rest ...keys: string[]

      The name(s) of the option.

    Returns null | string[]

    The values of the option if it was given.

  • rest(): string
  • restore(state: ArgsState): void
  • Sets the current state to the given state from {@linkcode Args#save}. Use this to backtrack after a series of retrievals.

    Parameters

    • state: ArgsState

      State to restore to.

    Returns void

  • save(): ArgsState
  • Saves the current state that can then be restored later by using {@linkcode Args#restore}.

    Returns ArgsState

    The current state.

  • single(): null | string
  • Retrieves the value of the next unused ordered token. That token will now be consider used.

    // Suppose args are from '1 2 3'.
    console.log(args.single());
    >>> '1'

    console.log(args.single());
    >>> '2'

    console.log(args.single());
    >>> '3'

    console.log(args.single());
    >>> null

    Returns null | string

    The value if there are tokens left.

  • singleFromEnd(): null | string
  • Retrieves the value of the next unused ordered token from the end. That token will now be consider used.

    // Suppose args are from '1 2 3'.
    console.log(args.singleFromEnd());
    >>> '3'

    console.log(args.singleFromEnd());
    >>> '2'

    console.log(args.singleFromEnd());
    >>> '1'

    console.log(args.singleFromEnd());
    >>> null

    Returns null | string

    The value if there are tokens left.

  • singleMap<T>(f: (x: string) => Option<T>, useAnyways?: boolean): null | Option<T>
  • Retrieves the value of the next unused ordered token, but only if it could be transformed. That token will now be consider used if the transformation succeeds.

    // Suppose args are from '1 2 3'.
    const parse = (x: string) => {
    const n = Number(x);
    return isNaN(n) ? none() : some(n);
    };

    console.log(args.singleMap(parse));
    >>> { exists: true, value: 1 }

    Type parameters

    • T

      Output type.

    Parameters

    • f: (x: string) => Option<T>

      Gives an option of either the resulting value, or nothing if failed.

        • (x: string): Option<T>
        • Parameters

          • x: string

          Returns Option<T>

    • Optional useAnyways: boolean

      Whether to consider the token used even if the transformation fails; defaults to false.

    Returns null | Option<T>

    The value if the transformation succeeds. If there are no tokens left, null is returned.

  • singleMapAsync<T>(f: (x: string) => Promise<Option<T>>, useAnyways?: boolean): Promise<null | Option<T>>
  • Retrieves the value of the next unused ordered token, but only if it could be transformed. This variant of the function is asynchronous using Promise. That token will now be consider used if the transformation succeeds.

    Type parameters

    • T

      Output type.

    Parameters

    • f: (x: string) => Promise<Option<T>>

      Gives an option of either the resulting value, or nothing if failed.

        • Parameters

          • x: string

          Returns Promise<Option<T>>

    • Optional useAnyways: boolean

      Whether to consider the token used even if the transformation fails; defaults to false.

    Returns Promise<null | Option<T>>

    The value if the transformation succeeds. If there are no tokens left, null is returned.

  • singleParse<T, E>(f: (x: string) => Result<T, E>, useAnyways?: boolean): null | Result<T, E>
  • Retrieves the value of the next unused ordered token, but only if it could be transformed. That token will now be consider used if the transformation succeeds. This is a variant of {@linkcode Args#singleMap} that allows for a Result to be returned.

    // Suppose args are from '1 a'.
    const parse = (x: string) => {
    const n = Number(x);
    return isNaN(n) ? err(x + ' is not a number') : ok(n);
    };

    console.log(args.singleParse(parse));
    >>> { success: true, value: 1 }

    console.log(args.singleParse(parse));
    >>> { success: false, error: 'a is not a number' }

    console.log(args.singleParse(parse));
    >>> null

    Type parameters

    • T

      Output type.

    • E

      Error type.

    Parameters

    • f: (x: string) => Result<T, E>

      Gives a result of either the resulting value, or an error.

        • (x: string): Result<T, E>
        • Parameters

          • x: string

          Returns Result<T, E>

    • Optional useAnyways: boolean

      Whether to consider the token used even if the transformation fails; defaults to false.

    Returns null | Result<T, E>

    The result which succeeds if the transformation succeeds. If there are no tokens left, null is returned.

  • singleParseAsync<T, E>(f: (x: string) => Promise<Result<T, E>>, useAnyways?: boolean): Promise<null | Result<T, E>>
  • Retrieves the value of the next unused ordered token, but only if it could be transformed. That token will now be consider used if the transformation succeeds. This variant of the function is asynchronous using Promise. This is a variant of {@linkcode Args#singleMapAsync} that allows for a Result to be returned.

    Type parameters

    • T

      Output type.

    • E

      Error type.

    Parameters

    • f: (x: string) => Promise<Result<T, E>>

      Gives a result of either the resulting value, or an error.

        • Parameters

          • x: string

          Returns Promise<Result<T, E>>

    • Optional useAnyways: boolean

      Whether to consider the token used even if the transformation fails; defaults to false.

    Returns Promise<null | Result<T, E>>

    The result which succeeds if the transformation succeeds. If there are no tokens left, null is returned.