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

> CLI parser


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

CLI parser

npm package

Install | Synopsis | Usage | Example | Hacking | About

Install

npm install parsec

Synopsis

Parsec is a CLI parser in 50 LOC.

// node ./index.js -abc --secret=42
parse()
{
  "a": true,
  "b": true,
  "c": true,
  "secret": 42
}

Customize:

// node ./index.js -s42
parse("secret", ["verbose", "V", { default: true }])
{
  "s": 42,
  "secret": 42,
  "V": true,
  "verbose": true
}

Features

  • Default shorthands
  • Default values / types
  • Handle --no-* options
  • Handle unknown options

Usage

Parse process.argv by default.

import parse from "parsec"
parse(alias1, alias2, ...)
  • Custom aliases

    // node ./index.js --bar
    parse(["foo", "bar", "baz"])
    
    {
      "foo": true,
      "bar": true,
      "baz": true
    }
    
  • Example aliases

    "foo" // → ["f", "foo"]
    ["F", "f", "foo"]
    ["foo", { default: "./" }]
    ["baz", { default: true }]
    
  • Default shorthands

    // node ./index.js -fb
    parse("foo", "bar")
    
    {
      "f": true,
      "foo": true,
      "b": true,
      "bar": true,
    }
    
  • Default values and types

    // node ./index.js --file
    parse(["f", "file", { default: "." }])
    
    {
      "f": ".",
      "file": "."
    }
    
  • Handle --no-flags

    // node ./index.js --no-foo --no-bar=baz
    parse()
    
    {
      "foo": false,
      "no-bar": "baz"
    }
    
  • Handle unknown options

    // node ./index.js --bar
    parse("foo", (option) => {
      throw new RangeError(`unknown option ${option}`) // bar
    })
    
  • 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

// ./node index.js -f bar -bp
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:

Keywords

FAQs

Package last updated on 14 Aug 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