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.1 to 0.1.0

example/parse.js

4

CHANGELOG.md

@@ -0,1 +1,5 @@

### 0.1.0
- Added support for parsing short arguments (correctly) see bugs #5 & #6 this
was a sort of breaking change (which explains the bump).
### 0.0.1

@@ -2,0 +6,0 @@ - Added support for keys with a dot notation so it's automatically parsed to an

27

index.js

@@ -37,4 +37,6 @@ 'use strict';

if (option === '--') {
//
// By splicing the argv array, we also cancel the reduce as there are no
// more options to parse.
//
argh.argv = argh.argv || [];

@@ -49,9 +51,11 @@ argh.argv = argh.argv.concat(argv.splice(index + 1));

//
insert(argh, data[1], false);
} else if (data = /^--?([^=]+)=\W?([\w\-\.]+)\W?$/.exec(option)) {
insert(argh, data[1], false, option);
} else if (data = /^-(?!-)(.*)/.exec(option)) {
insert(argh, data[1], true, option);
} else if (data = /^--([^=]+)=\W?([\w\-\.]+)\W?$/.exec(option)) {
//
// --foo="bar" and --foo=bar are alternate styles to --foo bar.
//
insert(argh, data[1], data[2]);
} else if (data = /^--?(.*)/.exec(option)) {
insert(argh, data[1], data[2], option);
} else if (data = /^--(.*)/.exec(option)) {
//

@@ -61,6 +65,6 @@ // Check if this was a bool argument

if (!next || next.charAt(0) === '-' || (value = /^true|false$/.test(next))) {
argh[data[1]] = value ? argv.splice(index + 1, 1)[0] === 'true' : true;
insert(argh, data[1], value ? argv.splice(index + 1, 1)[0] : true, option);
} else {
value = argv.splice(index + 1, 1)[0];
insert(argh, data[1], value);
insert(argh, data[1], value, option);
}

@@ -85,5 +89,6 @@ } else {

* @param {String} value The command line flag's value
* @param {String} option The actual option
* @api private
*/
function insert(argh, key, value) {
function insert(argh, key, value, option) {
//

@@ -93,6 +98,12 @@ // Automatic value conversion. This makes sure we store the correct "type"

if ('string' === typeof value && !isNaN(+value)) value = +value;
if (value === 'true' || value === 'false') value = value === 'true';
var properties = key.split('.')
var single = option.charAt(1) !== '-'
, properties = key.split('.')
, position = argh;
if (single && key.length > 1) return key.split('').forEach(function short(char) {
insert(argh, char, value, option);
});
//

@@ -99,0 +110,0 @@ // We don't have any deeply nested properties, so we should just bail out

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

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -40,4 +40,6 @@ # argh!

- `--arg` or `-a` Is transformed to a boolean (true)
- `--no-arg`, `--disable-arg` or `-no-a` Is transformed to a boolean (false)
- `--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

@@ -86,2 +88,10 @@ to key / value pairs. Where `foo` is the key and `bar` the value

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:

@@ -95,2 +105,10 @@

Combining arguments in to an object:
```
$node parse.js --redis.port 8080 --redis.host localhost
{ redis: { port: 8080, host: 'localhost' }
```
Handling rest arguments:

@@ -97,0 +115,0 @@

@@ -22,12 +22,23 @@ describe('argh', function () {

it('transforms `--no-foo`, `--disable-foo`, `-no-foo` in to false', function () {
it('transforms `--no-foo`, `--disable-foo` in to false', function () {
expect(parse('--no-foo').foo).to.equal(false);
expect(parse('-no-foo').foo).to.equal(false);
expect(parse('--disable-foo').foo).to.equal(false);
expect(parse('-disable-foo').foo).to.equal(false);
});
it('transforms `--foo`, `-foo` in to true', function () {
it('transforms `-no-abc` in to multiple (false) booleans', function () {
var args = parse('-no-abcdef');
'abcdef'.split('').forEach(function (char) {
expect(args[char]).to.equal(false);
});
args = parse('-disable-abcdef');
'abcdef'.split('').forEach(function (char) {
expect(args[char]).to.equal(false);
});
});
it('transforms `--foo` in to true', function () {
expect(parse('--foo').foo).to.equal(true);
expect(parse('-foo').foo).to.equal(true);
});

@@ -39,5 +50,2 @@

expect(parse('--foo=stuff').foo).to.equal('stuff');
expect(parse('-foo="stuff"').foo).to.equal('stuff');
expect(parse("-foo='stuff'").foo).to.equal('stuff');
expect(parse('-foo=stuff').foo).to.equal('stuff');
});

@@ -49,11 +57,20 @@

it('transforms short key/values', function () {
expect(parse('-F', 'bar').F).to.equal('bar');
it('transforms short in to booleans', function () {
expect(parse('-F').F).to.equal(true);
expect(parse('-f').f).to.equal(true);
});
it('tranforms true & false in to booleans', function () {
it('explodes a multi char short in to multiple (true) booleans', function () {
var args = parse('-abcdef');
'abcdef'.split('').forEach(function (char) {
expect(args[char]).to.equal(true);
});
});
it('tranforms the values true & false in to booleans', function () {
expect(parse('--foo', 'true').foo).to.equal(true);
expect(parse('-f', 'true').f).to.equal(true);
expect(parse('--foo', 'false').foo).to.equal(false);
expect(parse('-f', 'false').f).to.equal(false);
expect(parse('--foo="false"').foo).to.equal(false);
expect(parse('--foo="true"').foo).to.equal(true);
});

@@ -82,3 +99,3 @@

it('correctly parses multiple arguments', function () {
var args = parse('--foo', 'bar', '-f', 'bar', '--bar', '111', '--bool', '-m', 'false', '--', 'args', 'lol');
var args = parse('--foo', 'bar', '--f', 'bar', '--bar', '111', '--bool', '-m', 'false', '--', 'args', 'lol');

@@ -89,4 +106,4 @@ expect(args.foo).to.equal('bar');

expect(args.bool).to.equal(true);
expect(args.m).to.equal(false);
expect(args.argv).to.deep.equal(['args', 'lol']);
expect(args.m).to.equal(true);
expect(args.argv).to.deep.equal(['false', 'args', 'lol']);
});

@@ -93,0 +110,0 @@

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