Skip to content

Commit 41bdd93

Browse files
committedJan 9, 2020
Refactoring
1 parent 5484e17 commit 41bdd93

File tree

3 files changed

+22
-23
lines changed

3 files changed

+22
-23
lines changed
 

‎lib/command.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ module.exports = class Command {
198198
}
199199

200200
const oldValue = obj[option.name];
201-
const newValue = option.params.maxCount ? option.normalize(value, oldValue) : Boolean(value);
201+
const newValue = option.normalize(value, oldValue);
202202
const retValue = Reflect.set(obj, option.name, newValue);
203203

204204
if (option.shortcut) {

‎lib/option.js

+19-20
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = class Option {
1212
: { default: opt1 };
1313

1414
return {
15-
default: !ensureFunction(raw.action) ? raw.default : undefined,
15+
default: raw.default,
1616
normalize: ensureFunction(raw.normalize, self),
1717
shortcut: ensureFunction(raw.shortcut),
1818
action: ensureFunction(raw.action),
@@ -22,44 +22,43 @@ module.exports = class Option {
2222

2323
static parseUsage(usage) {
2424
const [m, short, long = ''] = usage.trim()
25-
.match(/^(?:(-[a-z\d])(?:\s*,\s*|\s+))?(--[a-z][a-z\d\-\_]*)?\s*/i) || [];
25+
.match(/^(?:(-[a-z\d])(?:\s*,\s*|\s+))?(--[a-z][a-z\d\-\_]*)\s*/i) || [];
2626

27-
if (!long) {
27+
if (!m) {
2828
throw new Error(`Usage has no long name: ${usage}`);
2929
}
3030

31-
let name = long.replace(/^--(no-)?/, ''); // --no-flag - invert value if flag is boolean
32-
let defValue = /--no-/.test(long);
3331
let params = new Params(usage.slice(m.length), `option usage: ${usage}`);
3432

35-
if (params.maxCount > 0) {
36-
name = long.slice(2);
37-
defValue = undefined;
38-
}
39-
40-
return { short, long, name, params, defValue };
33+
return { short, long, params };
4134
}
4235

43-
constructor(usage, description, ...options) {
44-
const { short, long, name, params, defValue } = Option.parseUsage(usage);
36+
constructor(usage, description, ...rawOptions) {
37+
const { short, long, params } = Option.parseUsage(usage);
38+
const options = Option.normalizeOptions(...rawOptions);
39+
40+
const isBool = params.maxCount === 0 && !options.action;
41+
let name = camelcase(long.replace(isBool ? /^--(no-)?/ : /^--/, '')); // --no-flag - invert value if flag is boolean
42+
43+
if (options.action) {
44+
options.default = undefined;
45+
} else if (isBool) {
46+
options.normalize = Boolean;
47+
options.default = long.startsWith('--no-');
48+
}
4549

4650
// names
4751
this.short = short;
4852
this.long = long;
49-
this.name = camelcase(name);
53+
this.name = name;
5054

5155
// meta
5256
this.usage = usage.trim();
5357
this.description = description || '';
5458

5559
// attributes
5660
this.params = params;
57-
Object.assign(this, Option.normalizeOptions(...options));
58-
59-
// ignore defValue from config for boolean options
60-
if (typeof defValue === 'boolean' && !this.action) {
61-
this.default = defValue;
62-
}
61+
Object.assign(this, options);
6362
}
6463

6564
messageRef() {

‎test/option-bool.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('boolean options', function() {
3838
);
3939
});
4040

41-
it('process function result should be ignored', function() {
41+
it('normalize function result should be ignored', function() {
4242
const command = cli.command()
4343
.option('--bool', 'description', () => false);
4444

@@ -84,7 +84,7 @@ describe('boolean options', function() {
8484
);
8585
});
8686

87-
it('process function result should be ignored', function() {
87+
it('normalize function result should be ignored', function() {
8888
const command = cli.command()
8989
.option('--no-bool', 'description', () => true);
9090

0 commit comments

Comments
 (0)
Please sign in to comment.