Socket
Socket
Sign inDemoInstall

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

![](https://img.shields.io/badge/---Parsec-05B3E1.svg?style=flat-square) ![](https://img.shields.io/badge/---ES6-FF2C77.svg?style=flat-square) ![](https://img.shields.io/badge/---MIT-36BF91.svg?style=flat-square)


Version published
Weekly downloads
161
decreased by-29.39%
Maintainers
1
Weekly downloads
 
Created
Source

Generator-based ES6 CLI option parser.

Synopsis | Install | Examples

Synopsis Build Status

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


import Parsec from "parsec"

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

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.

  • 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"])
  {
    "love": true,
    "war": false,
    "no-no": "ok"
  }

Roadmap

  • Add loose type support.

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