Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

parsec

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

parsec

> Generator-based ES6 CLI options parser.

  • 0.0.5
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
107
decreased by-52.65%
Maintainers
1
Weekly downloads
 
Created
Source

Generator-based ES6 CLI options parser.

Synopsis | Install | Examples

Synopsis Build Status

Parsec is a lovingly crafted command line options parser using ES6 generators in around 100 LOC.

Parsec.parse(process.argv) // -T850
  .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.

  • Short Options
{ isShort, tokens }
  • Long Options
{ isLong, key, value, token }
  • Operands
{ 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

Keywords

FAQs

Package last updated on 14 May 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc