Interface ParseArgsConfig

The configuration for the arg parser, augmented with descriptions of the arguments.

interface ParseArgsConfig {
    allowPositionals?: boolean;
    args?: string[];
    argumentDescription?: string;
    argumentName?: string;
    description?: string;
    exit?: ((code?: null | string | number) => never);
    options?: ParseArgsOptionsConfig;
    outputStream?: Writable;
    scriptName?: string;
    strict?: boolean;
    tokens?: boolean;
}

Properties

allowPositionals?: boolean

Whether this command accepts positional arguments.

args?: string[]

Array of argument strings.

argumentDescription?: string

Long description for the positional arguments.

minus-h

argumentName?: string

If positiionals are allowed, what name should be used to refer to them in the documentation? Defaults to "arguments".

minus-h

description?: string

Description of the script as a whole, for generating help text.

minus-h

exit?: ((code?: null | string | number) => never)

What to do after writing help to outputStream. Useful for testing. Defaults to process.exit. Called with 64 as the only parameter.

Type declaration

    • (code?): never
    • The process.exit() method instructs Node.js to terminate the process synchronously with an exit status of code. If code is omitted, exit uses either the 'success' code 0 or the value of process.exitCode if it has been set. Node.js will not terminate until all the 'exit' event listeners are called.

      To exit with a 'failure' code:

      import { exit } from 'node:process';

      exit(1);

      The shell that executed Node.js should see the exit code as 1.

      Calling process.exit() will force the process to exit as quickly as possible even if there are still asynchronous operations pending that have not yet completed fully, including I/O operations to process.stdout and process.stderr.

      In most situations, it is not actually necessary to call process.exit() explicitly. The Node.js process will exit on its own if there is no additional work pending in the event loop. The process.exitCode property can be set to tell the process which exit code to use when the process exits gracefully.

      For instance, the following example illustrates a misuse of the process.exit() method that could lead to data printed to stdout being truncated and lost:

      import { exit } from 'node:process';

      // This is an example of what *not* to do:
      if (someConditionNotMet()) {
      printUsageToStdout();
      exit(1);
      }

      The reason this is problematic is because writes to process.stdout in Node.js are sometimes asynchronous and may occur over multiple ticks of the Node.js event loop. Calling process.exit(), however, forces the process to exit before those additional writes to stdout can be performed.

      Rather than calling process.exit() directly, the code should set the process.exitCode and allow the process to exit naturally by avoiding scheduling any additional work for the event loop:

      import process from 'node:process';

      // How to properly set the exit code while letting
      // the process exit gracefully.
      if (someConditionNotMet()) {
      printUsageToStdout();
      process.exitCode = 1;
      }

      If it is necessary to terminate the Node.js process due to an error condition, throwing an uncaught error and allowing the process to terminate accordingly is safer than calling process.exit().

      In Worker threads, this function stops the current thread rather than the current process.

      Parameters

      • Optionalcode: null | string | number

        The exit code. For string type, only integer strings (e.g.,'1') are allowed.

      Returns never

      v0.1.13

Used to describe arguments known to the parser.

outputStream?: Writable

Where to output help? Useful for testing. Defaults to stderr.

scriptName?: string

The name of the script. Defaults to process.argv[1].

minus-h

strict?: boolean

Should an error be thrown when unknown arguments are encountered, or when arguments are passed that do not match the type configured in options.

true
tokens?: boolean

Return the parsed tokens. This is useful for extending the built-in behavior, from adding additional checks through to reprocessing the tokens in different ways.

false