Skip to content

Commit 5126304

Browse files
author
Benjamin Coe
committedApr 4, 2015
be more forgiving about alias ordering when using chaining API see #134 #128
1 parent 2e5d2c2 commit 5126304

File tree

3 files changed

+96
-5
lines changed

3 files changed

+96
-5
lines changed
 

‎index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ function Argv (processArgs, cwd) {
7474
self.resetOptions();
7575

7676
self.boolean = function (bools) {
77+
([].concat(bools)).forEach(function(key) {
78+
options.key[key] = true;
79+
});
7780
options.boolean.push.apply(options.boolean, [].concat(bools));
7881
return self;
7982
};
@@ -170,7 +173,7 @@ function Argv (processArgs, cwd) {
170173
demanded[keys] = { msg: msg };
171174
}
172175
else if (msg === true || typeof msg === 'undefined') {
173-
demanded[keys] = { msg: null };
176+
demanded[keys] = { msg: undefined };
174177
}
175178
}
176179

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

223226
self.describe = function (key, desc) {
227+
options.key[key] = true;
224228
usage.describe(key, desc);
225229
return self;
226230
};

‎lib/usage.js

+30-4
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ module.exports = function (yargs) {
9393
};
9494

9595
self.help = function () {
96+
normalizeAliases();
97+
9698
var demanded = yargs.getDemanded(),
9799
options = yargs.getOptions(),
98100
keys = Object.keys(
@@ -156,10 +158,11 @@ module.exports = function (yargs) {
156158
var desc = descriptions[key] || '';
157159
var type = null;
158160

159-
if (options.boolean[key]) type = '[boolean]';
160-
if (options.count[key]) type = '[count]';
161-
if (options.string[key]) type = '[string]';
162-
if (options.normalize[key]) type = '[string]';
161+
if (~options.boolean.indexOf(key)) type = '[boolean]';
162+
if (~options.count.indexOf(key)) type = '[count]';
163+
if (~options.string.indexOf(key)) type = '[string]';
164+
if (~options.normalize.indexOf(key)) type = '[string]';
165+
if (~options.array.indexOf(key)) type = '[array]';
163166

164167
var extra = [
165168
type,
@@ -206,6 +209,29 @@ module.exports = function (yargs) {
206209
return help.join('\n');
207210
};
208211

212+
// make sure any options set for aliases,
213+
// are copied to the keys being aliased.
214+
function normalizeAliases () {
215+
var options = yargs.getOptions(),
216+
demanded = yargs.getDemanded();
217+
218+
(Object.keys(options.alias) || []).forEach(function(key) {
219+
options.alias[key].forEach(function(alias) {
220+
// copy descriptions.
221+
if (descriptions[alias]) self.describe(key, descriptions[alias]);
222+
// copy demanded.
223+
if (demanded[alias]) yargs.demand(key, demanded[alias].msg);
224+
225+
// type messages.
226+
if (~options.boolean.indexOf(alias)) yargs.boolean(key);
227+
if (~options.count.indexOf(alias)) yargs.count(key);
228+
if (~options.string.indexOf(alias)) yargs.string(key);
229+
if (~options.normalize.indexOf(alias)) yargs.normalize(key);
230+
if (~options.array.indexOf(alias)) yargs.array(key);
231+
});
232+
});
233+
};
234+
209235
self.showHelp = function (level) {
210236
level = level || 'error';
211237
console[level](self.help());

‎test/usage.js

+61
Original file line numberDiff line numberDiff line change
@@ -1001,4 +1001,65 @@ describe('usage tests', function () {
10011001
r.logs[0].should.include('default: foo-description');
10021002
});
10031003
});
1004+
1005+
describe('normalizeAliases', function() {
1006+
// see #128
1007+
it("should display 'description' string in help message if set for alias", function() {
1008+
var r = checkUsage(function() {
1009+
return yargs(['-h'])
1010+
.describe('foo', 'foo option')
1011+
.alias('f', 'foo')
1012+
.help('h')
1013+
.wrap(null)
1014+
.argv
1015+
;
1016+
});
1017+
1018+
r.logs.join('\n').split(/\n+/).should.deep.equal([
1019+
'Options:',
1020+
' -h Show help ',
1021+
' -f, --foo foo option',
1022+
''
1023+
]);
1024+
});
1025+
1026+
it("should display 'required' string in help message if set for alias", function() {
1027+
var r = checkUsage(function() {
1028+
return yargs(['-h'])
1029+
.demand('foo')
1030+
.alias('f', 'foo')
1031+
.help('h')
1032+
.wrap(null)
1033+
.argv
1034+
;
1035+
});
1036+
1037+
r.logs.join('\n').split(/\n+/).should.deep.equal([
1038+
'Options:',
1039+
' -h Show help',
1040+
' -f, --foo [required]',
1041+
''
1042+
]);
1043+
});
1044+
1045+
it("should display 'type' string in help message if set for alias", function() {
1046+
var r = checkUsage(function() {
1047+
return yargs(['-h'])
1048+
.string('foo')
1049+
.describe('foo', 'bar')
1050+
.alias('f', 'foo')
1051+
.help('h')
1052+
.wrap(null)
1053+
.argv
1054+
;
1055+
});
1056+
1057+
r.logs.join('\n').split(/\n+/).should.deep.equal([
1058+
'Options:',
1059+
' -h Show help',
1060+
' -f, --foo bar [string]',
1061+
''
1062+
]);
1063+
});
1064+
})
10041065
});

0 commit comments

Comments
 (0)
Please sign in to comment.