Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fixes #37 #56

Merged
merged 2 commits into from

2 participants

@pifantastic

Set defaults for aliases.

Aaron Forsander added some commits
Aaron Forsander Fixes #37. 541bac8
Aaron Forsander Test for #37. 90ff6a1
@pifantastic

Sorry for all the whitespace changes. My editor strips trailing whitespace. If you want whitespace there, I can redo the pull request.

@substack substack merged commit 90ff6a1 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 26, 2012
  1. Fixes #37.

    Aaron Forsander authored
  2. Test for #37.

    Aaron Forsander authored
This page is out of date. Refresh to see the latest.
Showing with 36 additions and 16 deletions.
  1. +3 −0  index.js
  2. +33 −16 test/usage.js
View
3  index.js
@@ -393,6 +393,9 @@ function Argv (args, cwd) {
Object.keys(defaults).forEach(function (key) {
if (!(key in argv)) {
argv[key] = defaults[key];
+ if (key in aliases) {
+ argv[aliases[key]] = defaults[key];
+ }
}
});
View
49 test/usage.js
@@ -13,7 +13,7 @@ test('usageFail', function (t) {
r.result,
{ x : 10, z : 20, _ : [], $0 : './usage' }
);
-
+
t.same(
r.errors.join('\n').split(/\n+/),
[
@@ -75,12 +75,12 @@ test('checkFail', function (t) {
})
.argv;
});
-
+
t.same(
r.result,
{ x : 10, z : 20, _ : [], $0 : './usage' }
);
-
+
t.same(
r.errors.join('\n').split(/\n+/),
[
@@ -88,7 +88,7 @@ test('checkFail', function (t) {
'You forgot about -y'
]
);
-
+
t.same(r.logs, []);
t.ok(r.exit);
t.end();
@@ -98,7 +98,7 @@ test('checkCondPass', function (t) {
function checker (argv) {
return 'x' in argv && 'y' in argv;
}
-
+
var r = checkUsage(function () {
return optimist('-x 10 -y 20'.split(' '))
.usage('Usage: $0 -x NUM -y NUM')
@@ -118,25 +118,25 @@ test('checkCondFail', function (t) {
function checker (argv) {
return 'x' in argv && 'y' in argv;
}
-
+
var r = checkUsage(function () {
return optimist('-x 10 -z 20'.split(' '))
.usage('Usage: $0 -x NUM -y NUM')
.check(checker)
.argv;
});
-
+
t.same(
r.result,
{ x : 10, z : 20, _ : [], $0 : './usage' }
);
-
+
t.same(
r.errors.join('\n').split(/\n+/).join('\n'),
'Usage: ./usage -x NUM -y NUM\n'
+ 'Argument check failed: ' + checker.toString()
);
-
+
t.same(r.logs, []);
t.ok(r.exit);
t.end();
@@ -169,7 +169,7 @@ test('countFail', function (t) {
r.result,
{ _ : [ '1', '2' ], moo : true, $0 : './usage' }
);
-
+
t.same(
r.errors.join('\n').split(/\n+/),
[
@@ -177,7 +177,7 @@ test('countFail', function (t) {
'Not enough non-option arguments: got 2, need at least 3',
]
);
-
+
t.same(r.logs, []);
t.ok(r.exit);
t.end();
@@ -203,6 +203,23 @@ test('defaultSingles', function (t) {
t.end();
});
+test('defaultAliases', function (t) {
+ var r = checkUsage(function () {
+ return optimist('')
+ .alias('f', 'foo')
+ .default('f', 5)
+ .argv
+ ;
+ });
+ t.same(r.result, {
+ f : '5',
+ foo : '5',
+ _ : [],
+ $0 : './usage',
+ });
+ t.end();
+});
+
test('defaultHash', function (t) {
var r = checkUsage(function () {
return optimist('--foo 50 --baz 70'.split(' '))
@@ -248,24 +265,24 @@ function checkUsage (f) {
process.exit = function (t) { exit = true };
process.env = Hash.merge(process.env, { _ : 'node' });
process.argv = [ './usage' ];
-
+
var errors = [];
var logs = [];
-
+
console._error = console.error;
console.error = function (msg) { errors.push(msg) };
console._log = console.log;
console.log = function (msg) { logs.push(msg) };
-
+
var result = f();
-
+
process.exit = process._exit;
process.env = process._env;
process.argv = process._argv;
console.error = console._error;
console.log = console._log;
-
+
return {
errors : errors,
logs : logs,
Something went wrong with that request. Please try again.