Package Exports
- minargs
- minargs/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (minargs) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
minargs
Goals
- no validation* (aka. "bring your own validation" ™️)
- minimal configuration
- minimal assumptions
Package
Installation
npm install minargsUsage
// program.js - --foo=bar
const { minargs } = require('minargs')
const { args, values, positionals } = minargs()
args.foo // true
values.foo // 'bar'
positionals // ['-']Options
known(Array) Default: nonemultiples(Array) Default: nonestrict(Boolean) Default:false- If an argument is found that isn't defined in
known, throw a usage error
- If an argument is found that isn't defined in
positionalValues(Boolean) Default:false
Response Object
{
args: {},
values: {},
positionals: [],
remainder: [],
process: []
}args
values
- Returned values are a string by default
- Returned values are an array of strings if the corresponding arg was defined in
multiples - Examples:
--foo=barwill returnundefinedwith no configuration--foo=barwill return"bar"when'foo'--foo barwill return"bar"when'foo'&positionalValuesistrue- Notably,
baris treated as a positional & returned inpositionalsifpositionalValuesisfalse
- Notably,
positionals
remainder
process
CLI
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.
Installation
# install package globally & call bin...
npm install minargs -g && minargs
# or, use `npx` to install & call bin...
npx minargsUsage
minargs "<args>" [<options>]Options
--known(alias:k)--multiple(alias:m)--alias(alias:a)--positionalValues(alias:p) Default:false--strict(alias:s) Default:false
Examples
Get the # of times a arg was defined (using multiples & alias')...
minargs "--foo -f -f -ffff" -m foo -a f:foo | jq.values.lengthPiping to/reading from stdin...
"--foo --bar baz" | minargs -k bar -v bar -sExtended Use Cases
Handling usage
Handling validation
Handling recursive parsing
F.A.Q.
Are shorts supported?
- Yes.
-a&-aCdeFgare supported-a=bwill capture & return"b"as a value-a bwill capture & return"b"as a value ifpositionalValuesistrue
What is an alias?
- 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-fas{ args: { 'foo': true } })
Is cmd --foo=bar baz the same as cmd baz --foo=bar?
- Yes.
Is value validation or type cohersion supported?
- No.
Are usage errors supported?
- No.
Does --no-foo coerce to --foo=false?
- No.
- It would set
{ args: { 'no-foo': true } }
Is --foo the same as --foo=true?
- No.
Are environment variables supported?
- No.
Do unknown arguments raise an error?
- When
strict=false, no. - When
strict=true, yes.
Does -- signal the end of flags/options?
- Yes.
- Any arguments following a bare
--definition will be returned inremainder.
Is a value stored to represent the existence of --?
- No.
- The only way to determine if
--was present & there were arguments passed afterward is to check the value ofremainder
Is - a positional?
- Yes.
- A bare
-is treated as & returned inpositionals
Is -bar the same as --bar?
- No.
-barwill be parsed as short options, expanding to-b,-a,-r(ref. Utility Syntax Guidelines in POSIX.1-2017)
Is ---foo the same as --foo?
- No.
---fooreturns{ args: '-foo': true }--fooreturns{ args: { 'foo': true }
Is foo=bar a positional?
- Yes.
Notes
* minargs does support "validation" of the existence of known args when strict=true. If you're using strict=true you should likely wrap the call in a try{}catch(e){} or util.promisify() as it will throw when unknown args are passed