Skip to content

parseArgv()

function parseArgv(argv: string[]): parseArgv.Result

Tokenizes a process.argv into positionals and raw option values. This is the first stage of what parse() does — it knows nothing about your commands, schemas, or types. You rarely need it directly; it is exported because it is useful on its own.

import { parseArgv } from 'clibuilder'
parseArgv(['node', 'app', 'build', '--watch', '--out', 'dist'])
// { _: ['build'], watch: ['true'], out: ['dist'] }
namespace parseArgv {
type Result = { _: string[]; __?: string[] } & Record<string, string[]>
}
Key Contents
_ Positional values, in order
(option name) Every value given for that option, as raw strings — always an array
__ Present when a bare -- was seen; holds everything after it

Every option value is an array because an option may legitimately appear more than once. Whether that is allowed, and what the strings mean, is decided later against the command’s declared types.

Input Result
value Appended to _
--flag { flag: ['true'] }
--key=value { key: ['value'] }
--key value { key: ['value'] }
--key a b { key: ['a', 'b'] }
-abc { a: ['true'], b: ['true'], c: ['true'] }
-abc=value { a: ['true'], b: ['true'], c: ['value'] }
-- Everything after it lands in __

The first two entries of argv are skipped — it expects a full process.argv.