CLI parser
Install
|
Synopsis
|
Usage
|
Example
|
Hacking
|
About
Install
npm install parsec
Synopsis
Parsec is a CLI parser in 60 LOC.
parse()
{
"a": true,
"b": true,
"c": true,
"secret": 42
}
Customize:
parse("secret", ["verbose", "V", { default: true }])
{
"s": 42,
"secret": 42,
"V": true,
"verbose": true
}
Features
- Default values / types
- Handle
--no-*
options - Handle unknown options
Usage
Parse process.argv
by default.
import parse from "parsec"
parse(alias1, alias2, ...)
-
Custom aliases
parse(["foo", "bar", "baz"])
{
"foo": true,
"bar": true,
"baz": true
}
-
Example aliases
"foo"
["F", "f", "foo"]
["foo", { default: "./" }]
["baz", { default: true }]
-
Default shorthands
parse("foo", "bar")
{
"f": true,
"foo": true,
"b": true,
"bar": true,
}
-
Default values and types
parse(["f", "file", { default: "." }])
{
"f": ".",
"file": "."
}
-
Handle --no-flags
parse()
{
"foo": false,
"no-bar": "baz"
}
-
Handle unknown options
parse("foo", (option) => {
throw new RangeError(`unknown option ${option}`)
})
-
Bare operands and arguments after --
are added to ._
. To override:
parse(["_", "alias"])
-
Bind parse
to a different source of arguments:
parse.call(["--foo", "--bar"], [alias1, alias2, ...])
Example
parse()
{
"f": "bar",
"b": true,
"p": "./"
}
with custom aliases:
parse("foo", "bar", ["path", { default: "./" }])
{
"f": "bar",
"foo": "bar",
"b": true,
"bar": true,
"p": "./",
"path": "./"
}
About
I also found the following projects useful and inspiring while writing Parsec:
Hacking
git clone https://github.com/bucaran/parsec
cd parsec
npm run setup
License
MIT © Jorge Bucaran et al
:heart: