string-argv parses a string into an argument array to mimic process.argv. This is useful when testing Command Line Utilities that you want to pass arguments to.
string-argv parses a string into an argument array to mimic process.argv.
This is useful when testing Command Line Utilities that you want to pass arguments to and is the opposite of what the other argv utilities do.
Installation
npm install string-argv --save
Requirements
Requires Node.js >=16.20.0.
This package is ESM-only ("type": "module"). Use import, not require():
import stringArgv from 'string-argv';
TypeScript 7 requires Node.js >=16.20, and the single ESM build targets
ES2022, so 16.20.0 is the minimum supported version.
required: arguments String: arguments that you would normally pass to the command line.
optional: environment String: Adds to the environment position in the argv array. If ommitted then there is no need to call argv.split(2) to remove the environment/file values. However if your cli.parse method expects a valid argv value then you should include this value.
optional: file String: file that called the arguments. If omitted then there is no need to call argv.split(2) to remove the environment/file values. However if your cli.parse method expects a valid argv value then you should include this value.
Parsing behavior (bash-compatible quoting)
Quoting follows bash word-splitting rules: ' and " group text into a single
argument, adjacent quoted and unquoted segments are concatenated, and the quote
characters themselves are removed. Whitespace separates arguments only when it
appears outside of quotes.
A quote of the opposite kind inside quotes is kept literally
(--name='Phil "The Power" Taylor' -> --name=Phil "The Power" Taylor),
and empty quotes produce an empty argument ("" -> [""]).
Known differences from bash:
backslash escapes are not processed
$variables, globs and other expansions are left literal
an unclosed quote consumes the rest of the input instead of raising a syntax error