Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
lahmatiy committed Jan 9, 2020
1 parent 5484e17 commit 41bdd93
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lib/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ module.exports = class Command {
}

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

if (option.shortcut) {
Expand Down
39 changes: 19 additions & 20 deletions lib/option.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = class Option {
: { default: opt1 };

return {
default: !ensureFunction(raw.action) ? raw.default : undefined,
default: raw.default,
normalize: ensureFunction(raw.normalize, self),
shortcut: ensureFunction(raw.shortcut),
action: ensureFunction(raw.action),
Expand All @@ -22,44 +22,43 @@ module.exports = class Option {

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

if (!long) {
if (!m) {
throw new Error(`Usage has no long name: ${usage}`);
}

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

if (params.maxCount > 0) {
name = long.slice(2);
defValue = undefined;
}

return { short, long, name, params, defValue };
return { short, long, params };
}

constructor(usage, description, ...options) {
const { short, long, name, params, defValue } = Option.parseUsage(usage);
constructor(usage, description, ...rawOptions) {
const { short, long, params } = Option.parseUsage(usage);
const options = Option.normalizeOptions(...rawOptions);

const isBool = params.maxCount === 0 && !options.action;
let name = camelcase(long.replace(isBool ? /^--(no-)?/ : /^--/, '')); // --no-flag - invert value if flag is boolean

if (options.action) {
options.default = undefined;
} else if (isBool) {
options.normalize = Boolean;
options.default = long.startsWith('--no-');
}

// names
this.short = short;
this.long = long;
this.name = camelcase(name);
this.name = name;

// meta
this.usage = usage.trim();
this.description = description || '';

// attributes
this.params = params;
Object.assign(this, Option.normalizeOptions(...options));

// ignore defValue from config for boolean options
if (typeof defValue === 'boolean' && !this.action) {
this.default = defValue;
}
Object.assign(this, options);
}

messageRef() {
Expand Down
4 changes: 2 additions & 2 deletions test/option-bool.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('boolean options', function() {
);
});

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

Expand Down Expand Up @@ -84,7 +84,7 @@ describe('boolean options', function() {
);
});

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

Expand Down

0 comments on commit 41bdd93

Please sign in to comment.