Giter Site home page Giter Site logo

3rd-eden / argh Goto Github PK

View Code? Open in Web Editor NEW
45.0 7.0 3.0 44 KB

argh is a extremely light weight option/argument/process.argv parser for Node.js. It only parses options, nothing more than that.

License: MIT License

JavaScript 100.00%
argument-parsing argh parse-options cli javascript

argh's Introduction

argh!

argh is an extremely light weight options or process.argv parser for node.js. It only includes the bare minimal to parse options. It's not a full blown cli library, but it can be used as a dependency of a cli library to do all the heavy lifting.

argh was born out of rage, every cli library that we've found did more than they advertised and added unneeded bloat to what we were trying to achieve... and that was argument parsing. Tiny modules should only focus on one thing and do that one thing really well.

Installation

npm install argh --save

Usage

argh has two functions:

  1. A simple parser interface for custom option parsing using argh(..)
  2. A lazy loaded parsed results for the process.argv using argh.argv
var argh = require('argh');

// You can directly access the parsed arguments of the node process through
console.log(argh.argv);

// This the same result as running
console.log(argh(process.argv));

So what is supported?

  • --arg or -a Is transformed to a boolean (true) if no value is given
  • -abc Is transformed to multiple booleans.
  • --no-arg, --disable-arg Is transformed to a boolean (false)
  • -no-abc, --disable-abc Is transformed to multiple booleans (false)
  • --foo bar, --foo="bar", --foo='bar' or --foo=bar Is all transformed to key / value pairs. Where foo is the key and bar the value
  • --port 1111 Automatically transforms the string 1111 in a number
  • --beer true As you might have guessed it, it's transformed into a boolean
  • -- Can be used as an indicator to stop parsing arguments.

Examples

Everybody likes examples, let's assume that the following code is stored as parse.js:

var argv = require('argh').argv;

console.log(argv);

Parsing a single argument:

$ node parse.js --foo

{ foo: true }

Parsing multiple arguments:

$ node parse.js --foo bar --bar='baz'

{ foo: 'bar', bar: 'baz' }

Parsing multiple boolean arguments:

$ node parse.js --foo --no-bar -s --no-f

{ foo: true,
  bar: false,
  s: true,
  f: false }

Parsing multiple short arguments:

$ node parse.js -abc -no-def

{ a: true, b: true, c: true, d: false, e: false, f: false }

Parsing different values:

$ node parse.js --awesome true --port 1111

{ awesome: true, port: 1111 }

Combining arguments in to an object:

$node parse.js --redis.port 8080 --redis.host localhost

{ redis: { port: 8080, host: 'localhost' }

Handling rest arguments:

$ node parse.js --argh --is --awesome -- 1111 --pewpew aaarrgghh

{ argh: true,
  is: true,
  awesome: true,
  argv: [ '1111', '--pewpew', 'aaarrgghh' ] }

All unknown arguments are also directly pushed in to the argv property:

$ node parse.js --foo 111 bar unkown --hello world BUUURRRRRNN

{ foo: 111,
  argv: [ 'bar', 'unkown', 'BUUURRRRRNN' ],
  hello: 'world' }

Parsing duplicate flags:

$ node parse.js --item foo --item bar --item baz

{ item: [ 'foo', 'bar', 'baz' ] }

License

MIT

argh's People

Contributors

3rd-eden avatar greenkeeper[bot] avatar mathiasbynens avatar tomgco avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

argh's Issues

`-abc` should map to `{a: true, b: true, c: true}`

I have the following in test.js (same example as in the README):

var argv = require('argh').argv;
console.log(argv);

Now, this happens:

$ node test.js -abc
{ abc: true }

However, what should happen is the following:

$ node test.js -abc
{ a: true,
  b: true,
  c: true
 }

Leading dot is stripped in some cases

Using the parse.js example from the README:

$ node parse.js --foo=./foo.ext
{ foo: '/ext' }

$ node parse.js --foo ./foo.ext
{ foo: './foo.ext' }

I’d expect { foo: './foo.ext' } in both cases.

Version 10 of node.js has been released

Version 10 of Node.js (code name Dubnium) has been released! 🎊

To see what happens to your code in Node.js 10, Greenkeeper has created a branch with the following changes:

  • Added the new Node.js version to your .travis.yml

If you’re interested in upgrading this repo to Node.js 10, you can open a PR with these changes. Please note that this issue is just intended as a friendly reminder and the PR as a possible starting point for getting your code running on Node.js 10.

More information on this issue

Greenkeeper has checked the engines key in any package.json file, the .nvmrc file, and the .travis.yml file, if present.

  • engines was only updated if it defined a single version, not a range.
  • .nvmrc was updated to Node.js 10
  • .travis.yml was only changed if there was a root-level node_js that didn’t already include Node.js 10, such as node or lts/*. In this case, the new version was appended to the list. We didn’t touch job or matrix configurations because these tend to be quite specific and complex, and it’s difficult to infer what the intentions were.

For many simpler .travis.yml configurations, this PR should suffice as-is, but depending on what you’re doing it may require additional work or may not be applicable at all. We’re also aware that you may have good reasons to not update to Node.js 10, which is why this was sent as an issue and not a pull request. Feel free to delete it without comment, I’m a humble robot and won’t feel rejected 🤖


FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Parsing goes wrong

console.log(process.argv);
/* [ 'node',
  '/Some/path/to/bin',
  '--realFilePath=some/path/file.js',
  'some/path/file2.js' ] */

console.log(require('argh').argv);
// { 'realFilePath=some/path/file': { js: 'some/path/file2.js' } }

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.