diff --git a/CHANGELOG.md b/CHANGELOG.md index 6061d6f..15cb7ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,13 @@ +# 0.3.1 (02/21/2018) + +- can now handle arguments formatted like `{key}={value}` in any form such as: + - `-{shorthand}={value}` + - `--{shorthand}={value}` + - `--{key}={value}` + - `-{key}={value}` + - `{key}={value}` +- internal mechanism for dealing with arguments has been switched from a access loop, to a stack based approach to retrieving values + # 0.3.0 (01/31/2018) - adds a validate parameter on flags that will let the developer validate content while its being parsed. If there is an error with the parsing it will set the error object on the output. diff --git a/index.js b/index.js index 2035a7c..00c0ae6 100644 --- a/index.js +++ b/index.js @@ -37,7 +37,20 @@ module.exports = function Woof(helpMessage, options={}) { let helpMap = { 'help': true, '--help': true }; // loop through the args, either command line or given - args.forEach((arg, i) => { + let arg = ''; + + while((arg = args.shift()) !== undefined) { + + if(arg.indexOf('=') > -1) { + let parsed = arg.split('='); + + // add the values to the original array + args.push(parsed[0]); + args.push(parsed[1]); + + // continue from the next true argument + continue; + } // The user has requested either of these values and further arguments should not be parsed if(program['error'] || program['version'] || program['help']) return; @@ -63,15 +76,16 @@ module.exports = function Woof(helpMessage, options={}) { } // check the flag maps see if it exists + // since we are working off a stack, the next value is always 0, so we can shift the next value to get it if(flagMap[arg]) { let { type, name } = flagMap[arg]; switch(type) { case 'string': - program[name] = args[i + 1]; + program[name] = args.shift(); break; case 'integer': - program[name] = parseInt(args[i + 1]); + program[name] = parseInt(args.shift()); break; case 'boolean': default: @@ -95,7 +109,7 @@ module.exports = function Woof(helpMessage, options={}) { } if(commandMap[arg]) program[commandMap[arg].name] = true; - }); + } return program; }; diff --git a/package.json b/package.json index bbf02e4..1c8d9f3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "woof", - "version": "0.3.0", + "version": "0.3.1", "description": "🐶 command line applications made as easy as fetch", "main": "index.js", "scripts": { diff --git a/test/index.js b/test/index.js index f23e912..93cb739 100644 --- a/test/index.js +++ b/test/index.js @@ -5,7 +5,7 @@ const test = require('tape'); const woof = require('../'); test('woof', (t) => { - t.plan(7); + t.plan(8); t.test('should be able to take in args argument', (t) => { const cli = woof('', { @@ -247,6 +247,41 @@ test('woof', (t) => { t.end(); }); + t.test('should be able to handle `=` in args', (t) => { + const cli = woof('', { + args: ['start', '--unicorn=blue', '--rainbow', 'amount=10', '-h=150'], + commands: { + start: { + alias: 's' + } + }, + flags: { + rainbow: { + type: 'boolean', + alias: 'r' + }, + amount: { + type: 'integer', + alias: 'a', + amount: 1 + }, + height: { + type: 'integer', + alias: 'h', + amount: 100 + }, + unicorn: { + type: 'string', + alias: 'u', + default: 'rainbow' + } + } + }); + + t.deepEqual(cli, { unicorn: 'blue', start: true, rainbow: true, amount: 10, height: 150 }); + t.end(); + }); + t.test('should respond with a custom validate error', (t) => { const cli = woof('', {