Skip to content

Commit

Permalink
be more forgiving about alias ordering when using chaining API see #134
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Coe committed Apr 4, 2015
1 parent 2e5d2c2 commit 5126304
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 5 deletions.
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ function Argv (processArgs, cwd) {
self.resetOptions();

self.boolean = function (bools) {
([].concat(bools)).forEach(function(key) {
options.key[key] = true;
});
options.boolean.push.apply(options.boolean, [].concat(bools));
return self;
};
Expand Down Expand Up @@ -170,7 +173,7 @@ function Argv (processArgs, cwd) {
demanded[keys] = { msg: msg };
}
else if (msg === true || typeof msg === 'undefined') {
demanded[keys] = { msg: null };
demanded[keys] = { msg: undefined };
}
}

Expand Down Expand Up @@ -221,6 +224,7 @@ function Argv (processArgs, cwd) {
self.defaults = self.default;

self.describe = function (key, desc) {
options.key[key] = true;
usage.describe(key, desc);
return self;
};
Expand Down
34 changes: 30 additions & 4 deletions lib/usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ module.exports = function (yargs) {
};

self.help = function () {
normalizeAliases();

var demanded = yargs.getDemanded(),
options = yargs.getOptions(),
keys = Object.keys(
Expand Down Expand Up @@ -156,10 +158,11 @@ module.exports = function (yargs) {
var desc = descriptions[key] || '';
var type = null;

if (options.boolean[key]) type = '[boolean]';
if (options.count[key]) type = '[count]';
if (options.string[key]) type = '[string]';
if (options.normalize[key]) type = '[string]';
if (~options.boolean.indexOf(key)) type = '[boolean]';
if (~options.count.indexOf(key)) type = '[count]';
if (~options.string.indexOf(key)) type = '[string]';
if (~options.normalize.indexOf(key)) type = '[string]';
if (~options.array.indexOf(key)) type = '[array]';

var extra = [
type,
Expand Down Expand Up @@ -206,6 +209,29 @@ module.exports = function (yargs) {
return help.join('\n');
};

// make sure any options set for aliases,
// are copied to the keys being aliased.
function normalizeAliases () {
var options = yargs.getOptions(),
demanded = yargs.getDemanded();

(Object.keys(options.alias) || []).forEach(function(key) {
options.alias[key].forEach(function(alias) {
// copy descriptions.
if (descriptions[alias]) self.describe(key, descriptions[alias]);
// copy demanded.
if (demanded[alias]) yargs.demand(key, demanded[alias].msg);

// type messages.
if (~options.boolean.indexOf(alias)) yargs.boolean(key);
if (~options.count.indexOf(alias)) yargs.count(key);
if (~options.string.indexOf(alias)) yargs.string(key);
if (~options.normalize.indexOf(alias)) yargs.normalize(key);
if (~options.array.indexOf(alias)) yargs.array(key);
});
});
};

self.showHelp = function (level) {
level = level || 'error';
console[level](self.help());
Expand Down
61 changes: 61 additions & 0 deletions test/usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -1001,4 +1001,65 @@ describe('usage tests', function () {
r.logs[0].should.include('default: foo-description');
});
});

describe('normalizeAliases', function() {
// see #128
it("should display 'description' string in help message if set for alias", function() {
var r = checkUsage(function() {
return yargs(['-h'])
.describe('foo', 'foo option')
.alias('f', 'foo')
.help('h')
.wrap(null)
.argv
;
});

r.logs.join('\n').split(/\n+/).should.deep.equal([
'Options:',
' -h Show help ',
' -f, --foo foo option',
''
]);
});

it("should display 'required' string in help message if set for alias", function() {
var r = checkUsage(function() {
return yargs(['-h'])
.demand('foo')
.alias('f', 'foo')
.help('h')
.wrap(null)
.argv
;
});

r.logs.join('\n').split(/\n+/).should.deep.equal([
'Options:',
' -h Show help',
' -f, --foo [required]',
''
]);
});

it("should display 'type' string in help message if set for alias", function() {
var r = checkUsage(function() {
return yargs(['-h'])
.string('foo')
.describe('foo', 'bar')
.alias('f', 'foo')
.help('h')
.wrap(null)
.argv
;
});

r.logs.join('\n').split(/\n+/).should.deep.equal([
'Options:',
' -h Show help',
' -f, --foo bar [string]',
''
]);
});
})
});

0 comments on commit 5126304

Please sign in to comment.