Process command line arguments with complete control over how they are interpreted by defining a function to handle each parsed value.
- Based on minimist
- 100% unit test coverage
- Zero dependencies (124 LOC)
- Supports node 6+
npm install argv-walk
or
yarn add argv-walk
const walkArgv = require('argv-walk');
const args = { _: [] };
walkArgv(process.argv.slice(2), {
onArg: (arg) => {
if (arg.key) {
args[arg.key] = arg.value;
} else {
args._.push(arg.item);
}
}
});
argv-walk is a lower level package than minimist so it has no concept of abstractions like aliases or types other than string and boolean.
This package is most useful when you would like to to provide your own implementations of such abstractions (or don't need them).
▸ walkArgv(argv
: string[], options
: Options): void
Iterate over argv
array and call options.onArg
for each parsed argument.
Uses minimist parsing conventions with the following differences:
- number-like values are NOT converted to numbers (they remain strings)
- dot separated arguments (ex.
--foo.bar
) are NOT treated differently than other arguments (when processing the preceding example, the argumentkey
would be"foo.bar"
)
Returns: void
▸ boolean?: true | string | string[]
Optional: Key or array of keys to always treat as booleans, or true
If true, all double hyphenated arguments without equal signs are treated as boolean (e.g. affects --foo
, not -f
or --foo=bar
).
▸ onArg(arg: Arg): boolean | undefined | void
Called with each argument
Returns: If false is returned, the walk will stop (no further args will be processed)
▸ item: string
Current
argv
item
▸ index: number
Current
argv
index
▸ indexOffset: number
1 if
value
is based on the nextargv
item, otherwise 0
▸ compoundIndex: number | undefined
Current compound index if argument is a compound argument (ex.
-abc
), otherwise undefined
Example: When processing -abc
, a
would have compoundIndex
0, b
would have compoundIndex
1 and so on. The index
value for all three keys would be the same (in this case 0).
▸ isShort: boolean
true if argument used short syntax (ex.
-k
or-abc
), otherwise false
▸ isStrict: boolean
true if argument used strict syntax (ex.
--key=value
or-k=value
), otherwise false
▸ key: null | string
Parsed argument name
- null for positional arguments
"--"
for all arguments after--
is encountered
▸ value: boolean | string
Parsed value for key
- git
- node 8+ (argv-walk supports node 6+, but 8+ is required to run all package scripts except
test
) - yarn
git clone https://github.com/adamjarret/argv-walk.git
cd argv-walk
yarn
If you use VS Code, see .vscode/settings.sample.json for recommended project settings.
yarn test
Runs all the scripts required to format and check the module.
Runs eslint
(see eslint) on all javascript files not ignored in the .eslintignore file.
See .eslintrc.js for configuration.
Runs prettier
(see prettier) to check source code file format. Files with the extensions ts, js, json or md that are not ignored in the .eslintignore file are processed.
See .prettierrc.js for configuration.
Runs cspell
(see cspell) to spell-check source code files.
See .vscode/cSpell.json for configuration.
Note: This configuration path is used so the settings can also be honored by the Code Spell Checker plugin for VS Code.
Runs ncu
(see npm-check-updates) to check for dependency updates.
Use the -u
flag to update version numbers in package.json file.
Any additional arguments will be passed to the ncu
command.
Runs all unit tests with spooning.
Same as yarn test
but also collects and outputs test coverage information.