Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/3rd-Eden/argh
argh is a extremely light weight option/argument/process.argv parser for Node.js. It only parses options, nothing more than that.
https://github.com/3rd-Eden/argh
argh argument-parsing cli javascript parse-options
Last synced: about 1 month ago
JSON representation
argh is a extremely light weight option/argument/process.argv parser for Node.js. It only parses options, nothing more than that.
- Host: GitHub
- URL: https://github.com/3rd-Eden/argh
- Owner: 3rd-Eden
- License: mit
- Created: 2013-04-12T12:56:31.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2019-05-15T21:43:40.000Z (over 5 years ago)
- Last Synced: 2024-10-17T05:36:26.004Z (about 2 months ago)
- Topics: argh, argument-parsing, cli, javascript, parse-options
- Language: JavaScript
- Size: 43 KB
- Stars: 45
- Watchers: 7
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- my-awesome-list - argh - Eden | 45 | (JavaScript)
README
# 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````js
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`:
```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](LICENSE)