Generator-based ES6 CLI options parser.
Synopsis
|
Install
|
Examples
Synopsis
Parsec is a lovingly crafted command line options parser using ES6 generators in around 100 LOC.
Parsec.parse(process.argv)
.options(["T", "terminator"], { default: 800 })
.options("model", { default: 101 })
.options("name", { default: "Schwa" })
{
"T": "850",
"terminator": "850",
"model": "101",
"m": "101",
"name": "Schwa",
"n": "Schwa"
}
Install
npm install parsec
Usage
Parsec automatically slices arguments starting from index 2
, but you can specify the sliceIndex
via the second argument to parse
.
Syntax
Parsec.parse(argv[, {
sliceIndex = 2,
operandsKey = "_",
noFlags = true
}])
.options("option string")
.options([aliases], { default })
...
Parsec uses the first letter of an option string as an alias:
Parsec.parse(["-tla"], { sliceIndex: 0 })
.options("three")
.options("letter")
.options("abbreviation")
{
"t": true,
"l": true,
"a": true,
"three": true,
"letter": true,
"abbreviation": true
}
Pass an array of aliases, or specify default values via { default: value }
Parsec.parse(["-G"], { sliceIndex: 0 })
.options(["G", "g", "great"])
.options(["r", "R"], { default: 8 })
{
"G":true,
"g":true,
"great":true,
"r":8,
"R":8
}
You can negate options using a --no-
prefix before an option.
Parsec.parse(["--no-woman-no-cry", "--no-wonder=false"],
{ sliceIndex: 0 })
{
"woman-no-cry": false,
"wonder": true
}
API
Parsec.parse(args, opts)
The only one method you will probably deal with.
opts
operandsKey = "_"
sliceIndex = 2
noFlags = true
Parsec.prototype.tuples
Returns an iterator that yields objects of the form { prev, curr, next }
for each item in a list.
Parsec.prototype.map
Maps CLI arguments to custom Token objects.
{ isShort, tokens }
{ isLong, key, value, token }
{ token, isBare }
Parsec.prototype.shorts
Token sub-iterator for short options.
Parsec.prototype.tokens
Token iterator for options:
{ curr, next, value }
Parsec.prototype.entries
Iterator for entries:
{ key, value }
Properties
operandsKey = "_"
Use a different key name for the operands list.
noFlags = true
Set to false
to opt out.
Parsec.parse(["--no-love=false", "--no-war", "--no-no", "ok"],
{ sliceIndex: 0 })
{
"love": true,
"war": false,
"no-no": "ok"
}
Roadmap ✈
- Add loose type support.
- Invalid option check.
License
MIT © Jorge Bucaran