GithubHelp home page GithubHelp logo

chinory / pargv-lite Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 48 KB

Simple and reliable high performance command-line options parser.

License: MIT License

JavaScript 100.00%
parse argv cli options arguments cli-parser json simple reliable performance

pargv-lite's Introduction

pargv-lite

Simple and reliable high performance command-line options parser.

Using a readable JSON object to define what you want, pargv-lite will ensure you get the right data.

Installation

$ npm i pargv-lite

Usage

let opts = {}, path = "", model = {
  bool: { set: ["-b", "--bool"], reset: ["-B", "--nega"], def: false },
  str: { set: ["--str"], reset: ["--reset"], def: "string" },
  array: { set: ["--", "-a"], reset: ["--clear"], def: ["default"] },
  module: { set: ["m", "-m", "--mod"], def: {
    x: { def: "1", set: ["x", "-x"] },
    y: { def: "2", set: ["y", "-y"] }
  } }
};
require("pargv-lite")(process.argv, 2, opts, "", model, (err, arg, opts, name) => {
  err ? console.error("[error] %s %s -- %s", path + name + "/", err, arg) 
    : console.log("[module] %s %j", path += name + "/", opts);
  return true; // continue parsing
});
console.log(`[result] ${path}\n`, opts);
$ node demo --what -what --bool=str --nega=str --mod=str --str='new string' -- --str
[error] / invaild option -- -w
[error] / invaild option -- -h
[error] / can not set value of boolean option -- --bool
[error] / can not set value of reset option -- --nega
[error] / can not set value of module option -- --mod
[module] / {"bool":false,"str":"new string","array":["default","--what","t","--str"],"module":null}
[result] /
 { bool: false,
  str: 'new string',
  array: [ 'default', '--what', 't', '--str' ],
  module: null }
$ node demo --str 'new string' --reset -a "by -a" "by --" -bmx 100 no-more-arg
[module] / {"bool":true,"str":"string","array":["default","by -a","by --"],"module":null}
[error] /module/ uncaptured argument -- no-more-arg
[module] /module/ {"x":"100","y":"2"}
[result] /module/
 { bool: true,
  str: 'string',
  array: [ 'default', 'by -a', 'by --' ],
  module: { x: '100', y: '2' } }
$ node demo first --clear second -a third
[module] / {"bool":false,"str":"string","array":["default","second","third"],"module":null}
[result] /
 { bool: false,
  str: 'string',
  array: [ 'default', 'second', 'third' ],
  module: null }

For detailed, see index.d.ts

Benchmarks

Intel Core i7 CPU @ 2.60GHz, node v11.6.0

getopts x 1,218,598 ops/sec ±1.44% (93 runs sampled)
pargv-lite x 1,093,233 ops/sec ±0.27% (93 runs sampled)
mri x 542,287 ops/sec ±2.07% (89 runs sampled)
minimist x 324,039 ops/sec ±0.11% (96 runs sampled)
yargs x 34,837 ops/sec ±2.44% (89 runs sampled)

License

MIT © Chinory

pargv-lite's People

Contributors

chinory avatar

Stargazers

 avatar

Watchers

 avatar

pargv-lite's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.