Comparing version 0.2.1 to 0.3.1
@@ -5,2 +5,6 @@ # commandos Change Log | ||
## [0.3.0] - Apr 23rd, 2018 | ||
* Objected options supported. | ||
## [0.2.1] - Mar 27th, 2018 | ||
@@ -7,0 +11,0 @@ |
{ | ||
"bin": {}, | ||
"dependencies": { | ||
"jinang": "^0.5.1" | ||
"jinang": "^0.21.0" | ||
}, | ||
@@ -15,3 +15,3 @@ "devDependencies": { | ||
"name": "commandos", | ||
"version": "0.2.1", | ||
"version": "0.3.1", | ||
"main": "index.js", | ||
@@ -18,0 +18,0 @@ "keywords": [ |
53
parse.js
@@ -183,2 +183,3 @@ 'use strict'; | ||
case 'overwrite': | ||
case 'objected': | ||
case 'caseSensitive': // actually, "caseSensitive" will not really occur because deco has been lowercased before. | ||
@@ -264,2 +265,7 @@ column[deco] = !notdeco; | ||
if (column.name.endsWith('-*')) { | ||
column.name = column.name.slice(0, -2); | ||
column.objected = true; | ||
} | ||
return column; | ||
@@ -296,19 +302,49 @@ } | ||
const names_lc = caseSensitive ? null : names.map(name => name.toLowerCase()); | ||
const names_notation = names.map(name => (name.length > 1 ? '--' : '-') + name).join(', '); | ||
const names_matching = (names, name) => { | ||
let matched = false; | ||
for (let i = 0; i < names.length && !matched; i++) { | ||
if (column.objected) { | ||
let prefix = names[i]; | ||
if (name.startsWith(prefix) && name[prefix.length] == '-') { | ||
matched = name.slice(prefix.length + 1); | ||
} | ||
} | ||
else { | ||
matched = (name == names[i]); | ||
} | ||
} | ||
return matched; | ||
}; | ||
// names_notation 变量仅用于在遭遇异常时,生成错误信息。 | ||
const names_notation = names.map(name => | ||
(name.length > 1 ? '--' : '-') + name + (column.objected ? '-*' : '')).join(', '); | ||
names_notation_cache[column.name] = names_notation; | ||
let found = false; | ||
let value = column.multiple ? [] : null; | ||
let value = null; | ||
if (column.multiple) value = []; | ||
if (column.objected) value = {}; | ||
for (let i = 0; i < raw.options.length; i++) { | ||
let option = raw.options[i]; | ||
let matched = caseSensitive ? names.includes(option.name) : names_lc.includes(option.name.toLowerCase()); | ||
let matched = caseSensitive ? names_matching(names, option.name) : names_matching(names_lc, option.name.toLowerCase()); | ||
if (matched) { | ||
if (column.multiple) { | ||
if (column.objected) { | ||
let v = consumeOption(i, !column.assignable); | ||
if (!column.nullable && typeof v == 'boolean') { | ||
throw new Error(`option need to be valued: ${names_notation}`); | ||
} | ||
value[matched] = v; | ||
} | ||
else if (column.multiple) { | ||
let v = consumeOption(i); | ||
if (typeof v == 'boolean') { | ||
throw new Error(`option need to be value: ${names_notation}`); | ||
throw new Error(`option need to be valued: ${names_notation}`); | ||
} | ||
value.push(v); | ||
i--; | ||
} else { | ||
} | ||
else { | ||
if (found && !overwrite) { | ||
@@ -318,4 +354,4 @@ throw new Error(`option not allowed to be duplicated: ${names_notation}`); | ||
value = consumeOption(i, !column.assignable); | ||
i--; | ||
} | ||
i--; | ||
found = true; | ||
@@ -342,2 +378,3 @@ } | ||
// 余项指没有归属于任何显式选项的命令行参数。 | ||
// 在依据选项定义的 nonOption 属性消费余项之前,需要先删除已被其他选项显式占用的余项。 | ||
@@ -344,0 +381,0 @@ raw.$ = raw.$.filter(v => v !== null); |
@@ -17,16 +17,22 @@ # commandos | ||
## ToC, Table of Contents | ||
## Table of Contents | ||
* [Get Started](#get-started) | ||
* [API](#api) | ||
* [Go Advanced](#go-advanced) | ||
* [ODL, Option Definition Language](#odl-option-definition-language) | ||
* [Take Non-option Argument As Option Value](#take-non-option-argument-as-option-value) | ||
* [Examples](#examples) | ||
* [Why commandos](#why-commandos) | ||
* [Honorable Dependents](#honorable-dependents) | ||
* [About](#about) | ||
* [References](#references) | ||
* [CHANGE LOG](./CHANGELOG.md) | ||
* [Homepage](https://github.com/YounGoat/nodejs.commandos) | ||
* [Get Started](#get-started) | ||
* [Generally Returned](#generally-returned) | ||
* [Exceptions](#exceptions) | ||
* [Options Group](#options-group) | ||
* [Objected Option](#objected-option) | ||
* [Ready-Made Object Passed In](#ready-made-object-passed-in) | ||
* [API](#api) | ||
* [commandos.parse()](#commandosparse) | ||
* [commandos.parse.onlyArgs()](#commandosparseonlyargs) | ||
* [Go Advanced](#go-advanced) | ||
* [ODL, Option Definition Language](#odl-option-definition-language) | ||
* [Take Non-option Argument As Option Value](#take-non-option-argument-as-option-value) | ||
* [Examples](#examples) | ||
* [Why *commandos*](#why-commandos) | ||
* [Honorable Dependents](#honorable-dependents) | ||
* [About](#about) | ||
* [References](#references) | ||
* [Links](#links) | ||
@@ -129,2 +135,10 @@ ## Get Started | ||
### Objected Option | ||
```javascript | ||
const options = [ '--meta-*' ]; | ||
commandos.parse('foo --meta-name Charley --meta-gender male', options); | ||
// RETURN { meta: { name, gender } } | ||
``` | ||
### Ready-Made Object Passed In | ||
@@ -292,1 +306,6 @@ | ||
* [cli-argparse](https://www.npmjs.com/package/cli-argparse) | ||
## Links | ||
* [CHANGE LOG](./CHANGELOG.md) | ||
* [Homepage](https://github.com/YounGoat/nodejs.commandos) |
63399
14
1063
309
+ Addedjinang@0.21.0(transitive)
- Removedjinang@0.5.2(transitive)
Updatedjinang@^0.21.0