Skip to content

Commit

Permalink
0.3.1
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
gabrielcsapo committed Feb 22, 2018
1 parent c15b632 commit a4c4dc4
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 6 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
22 changes: 18 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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:
Expand All @@ -95,7 +109,7 @@ module.exports = function Woof(helpMessage, options={}) {
}

if(commandMap[arg]) program[commandMap[arg].name] = true;
});
}

return program;
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
37 changes: 36 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('', {
Expand Down Expand Up @@ -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('',
{
Expand Down

0 comments on commit a4c4dc4

Please sign in to comment.