Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

argh

Package Overview
Dependencies
Maintainers
2
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

argh - npm Package Compare versions

Comparing version 0.0.0 to 0.0.1

.tern-port

65

index.js
'use strict';
/**
* Argv is a extremely light weight options parse for Node.js it's been build
* for just one single purpose, parsing arguments. It does nothing more then
* Argv is a extremely light weight options parser for Node.js it's been built
* for just one single purpose, parsing arguments. It does nothing more than
* that.
*
* @param {Array} argv The arguments that needs to parse, defaults to process.argv
* @param {Array} argv The arguments that need to be parsed, defaults to process.argv
* @returns {Object} Parsed arguments.
* @api public
*/

@@ -14,2 +15,14 @@ function parse(argv) {

/**
* This is where the actual parsing happens, we can use array#reduce to
* iterate over the arguments and change it to an object. We can easily bail
* out of the loop by destroying `argv` array.
*
* @param {Object} argh The object that stores the parsed arguments
* @param {String} option The command line flag
* @param {Number} index The position of the flag in argv's
* @param {Array} argv The argument variables
* @returns {Object} argh, the parsed commands
* @api private
*/
return argv.reduce(function parser(argh, option, index, argv) {

@@ -36,3 +49,3 @@ var next = argv[index + 1]

//
argh[data[1]] = false;
insert(argh, data[1], false);
} else if (data = /^--?([^=]+)=\W?([\w\-\.]+)\W?$/.exec(option)) {

@@ -42,3 +55,3 @@ //

//
argh[data[1]] = +data[2] || data[2];
insert(argh, data[1], data[2]);
} else if (data = /^--?(.*)/.exec(option)) {

@@ -52,3 +65,3 @@ //

value = argv.splice(index + 1, 1)[0];
argh[data[1]] = +value || value;
insert(argh, data[1], value);
}

@@ -68,2 +81,40 @@ } else {

/**
* Inserts the value in the argh object
*
* @param {Object} argh The object where we store the values
* @param {String} key The received command line flag
* @param {String} value The command line flag's value
* @api private
*/
function insert(argh, key, value) {
//
// Automatic value conversion. This makes sure we store the correct "type"
//
if ('string' === typeof value && !isNaN(+value)) value = +value;
var properties = key.split('.')
, position = argh;
//
// We don't have any deeply nested properties, so we should just bail out
// early enough so we don't have to do any processing
//
if (!properties.length) return argh[key] = value;
while (properties.length) {
var property = properties.shift();
if (properties.length) {
if ('object' !== typeof position[property] && !Array.isArray(position[property])) {
position[property] = Object.create(null);
}
} else {
position[property] = value;
}
position = position[property];
}
}
/**
* Lazy parse the process arguments when `argh.argv` is accessed. This is the

@@ -76,3 +127,3 @@ * same as simply calling `argh()`.

get: function argv() {
return parse();
return argv.parsed || (argv.parsed = parse());
}

@@ -79,0 +130,0 @@ });

5

package.json
{
"name": "argh",
"version": "0.0.0",
"version": "0.0.1",
"description": "light weight option/argv parser for node, it only parses options, nothing more then that.",

@@ -31,4 +31,5 @@ "main": "index.js",

"chai": "1.5.x",
"mocha": "1.9.x"
"mocha": "1.9.x",
"pre-commit": "0.0.x"
}
}
# argh!
`argh` is a extremely light weight options or `process.argv` parser for node.js.
[![Build Status](https://travis-ci.org/observing/argh.png?branch=master)](https://travis-ci.org/observing/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 dependency of a cli library to do all the heavy
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 then
they advertised and added unneeded bloat to what we were trying to achieve.. and
`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

@@ -42,4 +44,4 @@ that one thing really well.

to key / value pairs. Where `foo` is the key and `bar` the value
- `--port 1111` Is automatically transforms the string 1111 in a number
- `--beer true` As you might have guessed it, it's transformed in to a boolean
- `--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.

@@ -49,3 +51,3 @@

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

@@ -52,0 +54,0 @@ ```js

@@ -89,2 +89,16 @@ describe('argh', function () {

it('transforms arguments with a dot notation to a object', function() {
var args = parse('--foo', '--redis.port', '9999', '--redis.host="foo"');
expect(args.foo).to.equal(true);
expect(args.redis).to.be.a('object');
expect(args.redis.port).to.equal(9999);
expect(args.redis.host).to.equal('foo');
});
it('preforms automatic value conversion', function () {
expect(parse('--a', '0').a).to.equal(0);
expect(parse('--a', '242424').a).to.equal(242424);
});
it('lazy parses the process args when .argv is accessed', function () {

@@ -91,0 +105,0 @@ var args = argh.argv;

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