parseArgs
The util/parseArgs module parses command-line style argument arrays.
It is exposed both as require('util/parseArgs') and as require('util').parseArgs.
parseArgs()
Parses an argument array using one or more configuration objects.
Syntax
parseArgs(args, ...configs)Parameters
argsString[]: Argument array to parse....configsObject: One or more parser configurations.
When multiple configs are passed and some of them include command, the parser tries to match args[0] to the command name and uses the matching configuration.
Configuration fields
| Field | Type | Default | Description |
|---|---|---|---|
command | String | Sub-command name matched against args[0]. | |
options | Object | {} | Option definitions. |
strict | Boolean | true | Throws on unknown options and unexpected positionals. |
allowNegative | Boolean | false | Enables --no- form for boolean long options. |
tokens | Boolean | false | Includes token details in the result. |
allowPositionals | Boolean | derived from positionals | Allows positional arguments. |
positionals | Array | Positional definition list. | |
usage | String | Used by formatHelp(). | |
description | String | Used by formatHelp(). | |
longDescription | String | Used by formatHelp() in multi-command help. |
Option definitions
Each entry in config.options is keyed by the JavaScript property name.
The parser automatically converts camelCase names to kebab-case flags.
For example, maxRetryCount maps to --max-retry-count.
| Field | Type | Description |
|---|---|---|
type | String | One of boolean, string, integer, float. |
short | String | One-letter short flag such as v for -v. |
multiple | Boolean | Collects repeated values into an array. |
default | any | Default value applied before parsing. |
description | String | Help text used by formatHelp(). |
Supported forms include:
- Long options such as
--output file.txt - Inline long values such as
--output=file.txt - Short options such as
-o file.txt - Inline short values such as
-o=file.txt - Short boolean groups such as
-abc - Option terminator
--
Positional definitions
positionals can contain simple strings or detailed objects.
Simple form:
positionals: ['inputFile', 'outputFile']Detailed form:
positionals: [
{ name: 'input-file' },
{ name: 'output-file', optional: true, default: 'stdout' },
{ name: 'files', variadic: true }
]Rules:
- Variadic positionals must be the last positional.
- Missing required positionals raise
TypeError. - Named positional keys are converted from kebab-case to camelCase in
result.namedPositionals.
Return value
parseArgs() returns an object with these fields.
| Field | Type | Description |
|---|---|---|
values | Object | Parsed option values. |
positionals | String[] | Positional values in order. |
namedPositionals | Object | Present when positionals are configured. |
tokens | Object[] | Present when tokens: true. |
command | String | Present when a sub-command config matched. |
Usage example
| |
Number parsing
Use integer for whole-number arguments and float for decimal numbers.
integerrejects values that contain a decimal point.- Both
integerandfloatreturn JavaScript numbers.
| |
Negative booleans
With allowNegative: true, boolean long options accept --no-....
| |
Sub-command parsing
When multiple configs are supplied, the first argument can select a command-specific config.
| |
parseArgs.formatHelp()
Generates human-readable help text from the same config structure used by parseArgs().
Syntax
parseArgs.formatHelp(...configs)It supports both:
- single-command help output
- multi-command help output with command summaries and per-command sections
| |
parseArgs.toKebabCase()
Converts a JavaScript camelCase option name to its CLI kebab-case form.
Syntax
parseArgs.toKebabCase(name)Usage example
| |
Behavior notes
- The first argument must be an array, otherwise
TypeErroris thrown. - In
strictmode, unknown options and unexpected positionals raiseTypeError. multiple: truecollects repeated values into arrays.- Defaults are applied before parsing explicit option values.
- The parser accepts both
require('util').parseArgsandrequire('util/parseArgs')usage.