Comparing version 0.2.0 to 0.2.1
@@ -5,2 +5,7 @@ # commandos Change Log | ||
## [0.2.1] - Mar 27th, 2018 | ||
* Fixed the bug on `commandos.parse(Object)` which leads to ambiguity about what it is? Options or definitions? | ||
* `commandos.parse.onlyArgs()` added. | ||
## [0.2.0] - Mar 23nd, 2018 | ||
@@ -7,0 +12,0 @@ |
@@ -15,3 +15,3 @@ { | ||
"name": "commandos", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"main": "index.js", | ||
@@ -18,0 +18,0 @@ "keywords": [ |
47
parse.js
@@ -465,10 +465,14 @@ 'use strict'; | ||
* @param {Object} def | ||
* @param {boolean} onlyArgs - PRIVATE | ||
*/ | ||
function parseCommand(cmd, def) { | ||
function parseCommand(cmd, def, onlyArgs) { | ||
// --------------------------- | ||
// Argument Validation | ||
let argv = null, readyOptions = null; | ||
// Because `onlyArgs` is a private argument which may only be passed in by | ||
// `parseCommand.onlyArgs()`. It always occupies the third position in the | ||
// argument list. | ||
let argv = null, readyMadeOptions = null; | ||
if (typeof arguments[0] == 'string') { | ||
@@ -478,2 +482,5 @@ argv = split(arguments[0], /\s+/, ['"', "'"], '\\'); | ||
} | ||
// If arguments look like (Array), the only array will be regarded as | ||
// command line args. | ||
else if (arguments[0] instanceof Array) { | ||
@@ -483,4 +490,11 @@ argv = arguments[0].slice(0); | ||
} | ||
else if (typeof arguments[0] == 'object') { | ||
// If the first argument is an object, it will be regarded as made-ready | ||
// command line args ONLY IF the second argument exists and is *def*. | ||
else if (typeof arguments[0] == 'object' | ||
&& (typeof arguments[1] == 'object' || arguments[1] instanceof Array)) | ||
{ | ||
let cmd = arguments[0]; | ||
def = arguments[1]; | ||
let options = [], $ = []; | ||
@@ -495,3 +509,4 @@ for (let name in cmd) { | ||
} | ||
readyOptions = { options, $ }; | ||
readyMadeOptions = { options, $ }; | ||
} | ||
@@ -501,9 +516,6 @@ else { | ||
def = arguments[0]; | ||
onlyArgs = false; | ||
} | ||
if (def instanceof Array) { | ||
def = { | ||
options: def | ||
} | ||
} | ||
if (def instanceof Array) def = { options: def }; | ||
def = Object.assign({ | ||
@@ -518,3 +530,4 @@ overwrite: true, | ||
let args = argv ? argv.slice(1) : null; | ||
let args = null; | ||
if (argv) args = onlyArgs ? argv : argv.slice(1); | ||
// let [name, ...args] = argv; | ||
@@ -527,3 +540,3 @@ | ||
try { | ||
let raw = readyOptions ? readyOptions : parseRaw(args, def); | ||
let raw = readyMadeOptions ? readyMadeOptions : parseRaw(args, def); | ||
// let raw = parseRaw(args, def); | ||
@@ -572,2 +585,12 @@ if (def.groups && def.groups.length) { | ||
/** | ||
* Same as parseCommand() but the command name itself is absent in `args`. | ||
* E.g. | ||
* parseCommand('foo --name ching --age 18', def); | ||
* parseCommand.onlyArgs('--name ching --age 18', def); | ||
*/ | ||
parseCommand.onlyArgs = function(args, def) { | ||
return parseCommand(args, def, true); | ||
}; | ||
module.exports = parseCommand; |
@@ -196,2 +196,6 @@ # commandos | ||
### commandos.parse.onlyArgs() | ||
Same as `commandos.parse()` but first part of *cmdline* will be regarded as option or non-option value instead of name of command. | ||
## Go Advanced | ||
@@ -198,0 +202,0 @@ |
@@ -14,3 +14,3 @@ 'use strict'; | ||
describe('parse, simple usage', () => { | ||
describe('parse, basic usage', () => { | ||
it('basic usage', () => { | ||
@@ -17,0 +17,0 @@ let cmdtext = 'foo -v 1.0 --male'; |
59726
979
290