minargs
is meant to be a primitive library which expands the current process.argv
information for developers that want to quickly write CLI tools with minimal configuration, assumptions & dependencies. Argument parsing can take many shapes but the explicit goals of this library are as follows:
- no usage
- no validation
- no types or type cohersion
- no regular expressions
- no strictness
- no dependencies
- minimal assumptions
- minimal configuration
- minimal information loss
npm install minargs
// program.js - --foo=bar
const { minargs } = require('minargs')
const { args, values, positionals } = minargs()
args.foo // true
values.foo // 'bar'
positionals // ['-']
known
(Array
) Default: nonemultiples
(Array
) Default: nonepositionalValues
(Array
) Default: none
{
args: {},
values: {},
positionals: [],
remainder: [],
process: []
}
- Returned values are a string by default
- Returned values are an array of strings if the corresponding arg was defined in
multiples
- Examples:
--foo=bar
will returnundefined
with no configuration--foo=bar
will return"bar"
when'foo'
--foo bar
will return"bar"
when'foo'
&positionalValues
istrue
- Notably,
bar
is treated as a positional & returned inpositionals
ifpositionalValues
isfalse
- Notably,
minargs
comes with a CLI out-of-the-box to make it a little easier to try/use the parser & test any assumptions about input.
# install package globally & call bin...
npm install minargs -g && minargs
# or, use `npx` to install & call bin...
npx minargs
minargs "<args>" [<options>]
--known
(alias:k
)--multiple
(alias:m
)--alias
(alias:a
)--positionalValues
(alias:p
) Default:false
--strict
(alias:s
) Default:false
minargs "--foo -f -f -ffff" -m foo -a f:foo | jq.values.length
"--foo --bar baz" | minargs -k bar -v bar -s
- Yes.
-a
&-aCdeFg
are supported-a=b
will capture & return"b"
as a value-a b
will capture & return"b"
as a value ifpositionalValues
istrue
- An alias can be any other string that maps to the canonical option; this includes single characters which will map shorts to a long-form (ex.
alias: { f: foo }
will parse-f
as{ args: { 'foo': true } }
)
- Yes.
- No.
- No.
- No.
- It would set
{ args: { 'no-foo': true } }
- No.
- No.
- When
strict=false
, no. - When
strict=true
, yes.
- Yes.
- Any arguments following a bare
--
definition will be returned inremainder
.
- No.
- The only way to determine if
--
was present & there were arguments passed afterward is to check the value ofremainder
- Yes.
- A bare
-
is treated as & returned inpositionals
- No.
-bar
will be parsed as short options, expanding to-b
,-a
,-r
(ref. Utility Syntax Guidelines in POSIX.1-2017)
- No.
---foo
returns{ args: '-foo': true }
--foo
returns{ args: { 'foo': true }
- Yes.