![](https://img.shields.io/badge/--- MIT-FF9933.svg?style=flat-square)
Generator-based ES6 CLI option parser.
Synopsis
|
Install
|
Examples
Synopsis
Parsec is a lovingly crafted command line options parser using ES6 generators in around 100 LOC.
import Parsec from "parsec"
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
Syntax
Parsec.parse(argv)
...
.options("option string")
.options([aliases], { default })
...
Parsec uses the first letter of an option string as an alias:
Parsec.parse(["-tla"])
.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"])
.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"])
{
"woman-no-cry": false,
"wonder": true
}
API
Parsec.parse(args)
The one method to rule them all.
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"])
{
"love": true,
"war": false,
"no-no": "ok"
}
Roadmap
License
MIT © Jorge Bucaran