commander 0.5.1 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/History.md CHANGED
@@ -1,4 +1,17 @@
1
1
 
2
+ 0.6.1 / 2012-06-01
3
+ ==================
4
+
5
+ * Added: append (yes or no) on confirmation
6
+ * Added: allow node.js v0.7.x
7
+
8
+ 0.6.0 / 2012-04-10
9
+ ==================
10
+
11
+ * Added `.prompt(obj, callback)` support. Closes #49
12
+ * Added default support to .choose(). Closes #41
13
+ * Fixed the choice example
14
+
2
15
  0.5.1 / 2011-12-20
3
16
  ==================
4
17
 
package/Readme.md CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  # Commander.js
3
2
 
4
3
  The complete solution for [node.js](http://nodejs.org) command-line interfaces, inspired by Ruby's [commander](https://github.com/visionmedia/commander).
@@ -50,7 +49,7 @@ console.log(' - %s cheese', program.cheese);
50
49
 
51
50
  Options:
52
51
 
53
- -v, --version output the version number
52
+ -V, --version output the version number
54
53
  -p, --peppers Add peppers
55
54
  -P, --pineapple Add pineappe
56
55
  -b, --bbq Add bbq sauce
@@ -142,7 +141,7 @@ Usage: custom-help [options]
142
141
  Options:
143
142
 
144
143
  -h, --help output usage information
145
- -v, --version output the version number
144
+ -V, --version output the version number
146
145
  -f, --foo enable some foo
147
146
  -b, --bar enable some bar
148
147
  -B, --baz enable some baz
package/lib/commander.js CHANGED
@@ -798,14 +798,34 @@ Command.prototype.promptMultiLine = function(str, fn){
798
798
  * console.log('description was "%s"', desc.trim());
799
799
  * });
800
800
  *
801
- * @param {String} str
801
+ * @param {String|Object} str
802
802
  * @param {Function} fn
803
803
  * @api public
804
804
  */
805
805
 
806
806
  Command.prototype.prompt = function(str, fn){
807
- if (/ $/.test(str)) return this.promptSingleLine.apply(this, arguments);
808
- this.promptMultiLine(str, fn);
807
+ var self = this;
808
+
809
+ if ('string' == typeof str) {
810
+ if (/ $/.test(str)) return this.promptSingleLine.apply(this, arguments);
811
+ this.promptMultiLine(str, fn);
812
+ } else {
813
+ var keys = Object.keys(str)
814
+ , obj = {};
815
+
816
+ function next() {
817
+ var key = keys.shift()
818
+ , label = str[key];
819
+
820
+ if (!key) return fn(obj);
821
+ self.prompt(label, function(val){
822
+ obj[key] = val;
823
+ next();
824
+ });
825
+ }
826
+
827
+ next();
828
+ }
809
829
  };
810
830
 
811
831
  /**
@@ -883,11 +903,12 @@ Command.prototype.password = function(str, mask, fn){
883
903
  */
884
904
 
885
905
 
886
- Command.prototype.confirm = function(str, fn){
906
+ Command.prototype.confirm = function(str, fn, verbose){
887
907
  var self = this;
888
908
  this.prompt(str, function(ok){
889
909
  if (!ok.trim()) {
890
- return self.confirm(str, fn);
910
+ if (!verbose) str += '(yes or no) ';
911
+ return self.confirm(str, fn, true);
891
912
  }
892
913
  fn(parseBool(ok));
893
914
  });
@@ -907,20 +928,33 @@ Command.prototype.confirm = function(str, fn){
907
928
  * });
908
929
  *
909
930
  * @param {Array} list
931
+ * @param {Number|Function} index or fn
910
932
  * @param {Function} fn
911
933
  * @api public
912
934
  */
913
935
 
914
- Command.prototype.choose = function(list, fn){
915
- var self = this;
936
+ Command.prototype.choose = function(list, index, fn){
937
+ var self = this
938
+ , hasDefault = 'number' == typeof index;
939
+
940
+ if (!hasDefault) {
941
+ fn = index;
942
+ index = null;
943
+ }
916
944
 
917
945
  list.forEach(function(item, i){
918
- console.log(' %d) %s', i + 1, item);
946
+ if (hasDefault && i == index) {
947
+ console.log('* %d) %s', i + 1, item);
948
+ } else {
949
+ console.log(' %d) %s', i + 1, item);
950
+ }
919
951
  });
920
952
 
921
953
  function again() {
922
954
  self.prompt(' : ', function(val){
923
955
  val = parseInt(val, 10) - 1;
956
+ if (hasDefault && isNaN(val)) val = index;
957
+
924
958
  if (null == list[val]) {
925
959
  again();
926
960
  } else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "commander"
3
- , "version": "0.5.1"
3
+ , "version": "0.5.2"
4
4
  , "description": "the complete solution for node.js command-line programs"
5
5
  , "keywords": ["command", "option", "parser", "prompt", "stdin"]
6
6
  , "author": "TJ Holowaychuk <tj@vision-media.ca>"
@@ -9,5 +9,5 @@
9
9
  , "devDependencies": { "should": ">= 0.0.1" }
10
10
  , "scripts": { "test": "make test" }
11
11
  , "main": "index"
12
- , "engines": { "node": ">= 0.4.x < 0.7.0" }
12
+ , "engines": { "node": ">= 0.4.x" }
13
13
  }
package/.gitignore DELETED
@@ -1,3 +0,0 @@
1
- .DS_Store
2
- node_modules
3
- *.sock
package/examples/choice DELETED
@@ -1,15 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- /**
4
- * Module dependencies.
5
- */
6
-
7
- var program = require('../');
8
-
9
- var list = ['tobi', 'loki', 'jane', 'manny', 'luna'];
10
-
11
- console.log('Choose the coolest pet:');
12
- program.choose(list, function(i){
13
- console.log('you chose %d "%s"', i, list[i]);
14
- process.stdin.destroy();
15
- });
package/examples/coercion DELETED
@@ -1,33 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- /**
4
- * Module dependencies.
5
- */
6
-
7
- var program = require('../');
8
-
9
- function range(val) {
10
- return val.split('..').map(Number);
11
- }
12
-
13
- function list(val) {
14
- return val.split(',');
15
- }
16
-
17
- program
18
- .version('0.0.1')
19
- .usage('test')
20
- .option('-i, --integer <n>', 'An integer argument', parseInt)
21
- .option('-f, --float <n>', 'A float argument', parseFloat)
22
- .option('-r, --range <a>..<b>', 'A range', range)
23
- .option('-l, --list <items>', 'A list', list)
24
- .option('-o, --optional [value]', 'An optional value')
25
- .parse(process.argv);
26
-
27
- console.log(' int: %j', program.integer);
28
- console.log(' float: %j', program.float);
29
- console.log(' optional: %j', program.optional);
30
- program.range = program.range || [];
31
- console.log(' range: %j..%j', program.range[0], program.range[1]);
32
- console.log(' list: %j', program.list);
33
- console.log(' args: %j', program.args);
package/examples/confirm DELETED
@@ -1,12 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- /**
4
- * Module dependencies.
5
- */
6
-
7
- var program = require('../');
8
-
9
- program.confirm('continue? ', function(ok){
10
- console.log(' got %j', ok);
11
- process.stdin.destroy();
12
- });
@@ -1,32 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- /**
4
- * Module dependencies.
5
- */
6
-
7
- var program = require('../');
8
-
9
- function list(val) {
10
- return val.split(',').map(Number);
11
- }
12
-
13
- program
14
- .version('0.0.1')
15
- .option('-f, --foo', 'enable some foo')
16
- .option('-b, --bar', 'enable some bar')
17
- .option('-B, --baz', 'enable some baz');
18
-
19
- // must be before .parse() since
20
- // node's emit() is immediate
21
-
22
- program.on('--help', function(){
23
- console.log(' Examples:');
24
- console.log('');
25
- console.log(' $ custom-help --help');
26
- console.log(' $ custom-help -h');
27
- console.log('');
28
- });
29
-
30
- program.parse(process.argv);
31
-
32
- console.log('stuff');
package/examples/defaults DELETED
@@ -1,22 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- /**
4
- * Module dependencies.
5
- */
6
-
7
- var program = require('../');
8
-
9
- function list(val) {
10
- return val.split(',').map(Number);
11
- }
12
-
13
- program
14
- .version('0.0.1')
15
- .option('-t, --template-engine [engine]', 'Add template [engine] support', 'jade')
16
- .option('-c, --cheese [type]', 'Add the specified type of cheese [marble]', 'marble')
17
- .option('-l, --list [items]', 'Specify list items defaulting to 1,2,3', list, [1,2,3])
18
- .parse(process.argv);
19
-
20
- console.log(' - %s template engine', program.templateEngine);
21
- console.log(' - %s cheese', program.cheese);
22
- console.log(' - %j', program.list);
package/examples/deploy DELETED
@@ -1,45 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- /**
4
- * Module dependencies.
5
- */
6
-
7
- var program = require('../');
8
-
9
- program
10
- .version('0.0.1')
11
- .option('-C, --chdir <path>', 'change the working directory')
12
- .option('-c, --config <path>', 'set config path. defaults to ./deploy.conf')
13
- .option('-T, --no-tests', 'ignore test hook')
14
-
15
- program
16
- .command('setup [env]')
17
- .description('run setup commands for all envs')
18
- .option("-s, --setup_mode [mode]", "Which setup mode to use")
19
- .action(function(env, options){
20
- var mode = options.setup_mode || "normal";
21
- env = env || 'all';
22
- console.log('setup for %s env(s) with %s mode', env, mode);
23
- });
24
-
25
- program
26
- .command('exec <cmd>')
27
- .description('execute the given remote cmd')
28
- .option("-e, --exec_mode <mode>", "Which exec mode to use")
29
- .action(function(cmd, options){
30
- console.log('exec "%s" using %s mode', cmd, options.exec_mode);
31
- }).on('--help', function() {
32
- console.log(' Examples:');
33
- console.log();
34
- console.log(' $ deploy exec sequential');
35
- console.log(' $ deploy exec async');
36
- console.log();
37
- });
38
-
39
- program
40
- .command('*')
41
- .action(function(env){
42
- console.log('deploying "%s"', env);
43
- });
44
-
45
- program.parse(process.argv);
package/examples/express DELETED
@@ -1,18 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- /**
4
- * Module dependencies.
5
- */
6
-
7
- var program = require('../');
8
-
9
- program
10
- .version('0.0.1')
11
- .option('-s, --sessions', 'add session support')
12
- .option('-t, --template <engine>', 'specify template engine (jade|ejs) [jade]', 'jade')
13
- .option('-c, --css <engine>', 'specify stylesheet engine (stylus|sass|less) [css]', 'css')
14
- .parse(process.argv);
15
-
16
- console.log(' - sessions %j', program.sessions);
17
- console.log(' - template %j', program.template);
18
- console.log(' - css %j', program.css);
package/examples/input DELETED
@@ -1,24 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- /**
4
- * Module dependencies.
5
- */
6
-
7
- var program = require('../');
8
-
9
- program.prompt('Username: ', function(name){
10
- console.log('hi %s\n', name);
11
-
12
- program.prompt('Description:', function(desc){
13
- console.log('description was "%s"', desc.trim());
14
-
15
- program.prompt('Age: ', Number, function(age){
16
- console.log('age: %j\n', age);
17
-
18
- program.prompt('Birthdate: ', Date, function(date){
19
- console.log('date: %s\n', date);
20
- process.stdin.destroy();
21
- });
22
- });
23
- });
24
- });
package/examples/password DELETED
@@ -1,18 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- /**
4
- * Module dependencies.
5
- */
6
-
7
- var program = require('../');
8
-
9
- program.password('Password: ', function(pass){
10
- console.log('got "%s"', pass);
11
- program.password('Password: ', '*', function(pass){
12
- console.log('got "%s"', pass);
13
- program.password('Password: ', '-', function(pass){
14
- console.log('got "%s"', pass);
15
- process.stdin.destroy();
16
- });
17
- });
18
- });
package/examples/pizza DELETED
@@ -1,28 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- /**
4
- * Module dependencies.
5
- */
6
-
7
- var program = require('../');
8
-
9
- program
10
- .version('0.0.1')
11
- .option('-p, --peppers', 'Add peppers')
12
- .option('-P, --pineapple', 'Add pineapple')
13
- .option('-b, --bbq', 'Add bbq sauce')
14
- .option('-c, --cheese <type>', 'Add the specified type of cheese [marble]')
15
- .option('-C, --no-cheese', 'You do not want any cheese')
16
- .parse(process.argv);
17
-
18
- console.log('you ordered a pizza with:');
19
- if (program.peppers) console.log(' - peppers');
20
- if (program.pineappe) console.log(' - pineapple');
21
- if (program.bbq) console.log(' - bbq');
22
-
23
- var cheese = true === program.cheese
24
- ? 'marble'
25
- : program.cheese || 'no';
26
-
27
- console.log(' - %s cheese', cheese);
28
- console.log(program.args);
package/test/run DELETED
@@ -1,16 +0,0 @@
1
- #!/bin/sh
2
-
3
- export NODE_ENV=test
4
-
5
- echo
6
- for file in $@; do
7
- printf "\033[90m ${file#test/}\033[0m "
8
- node $file 2> /tmp/stderr && echo "\033[36m✓\033[0m"
9
- code=$?
10
- if test $code -ne 0; then
11
- echo "\033[31m✖\033[0m"
12
- cat /tmp/stderr >&2
13
- exit $code
14
- fi
15
- done
16
- echo
@@ -1,16 +0,0 @@
1
- /**
2
- * Module dependencies.
3
- */
4
-
5
- var program = require('../')
6
- , should = require('should');
7
-
8
- program
9
- .version('0.0.1')
10
- .option('-f, --foo', 'add some foo')
11
- .option('-b, --bar', 'add some bar');
12
-
13
- program.parse(['node', 'test', '--foo', '--', '--bar', 'baz']);
14
- program.foo.should.be.true;
15
- should.equal(undefined, program.bar);
16
- program.args.should.eql(['--bar', 'baz']);
@@ -1,13 +0,0 @@
1
- /**
2
- * Module dependencies.
3
- */
4
-
5
- var program = require('../')
6
- , should = require('should');
7
-
8
- program
9
- .version('0.0.1')
10
- .option('-c, --cheese [type]', 'optionally specify the type of cheese');
11
-
12
- program.parse(['node', 'test', '--cheese', 'feta']);
13
- program.cheese.should.equal('feta');
@@ -1,13 +0,0 @@
1
- /**
2
- * Module dependencies.
3
- */
4
-
5
- var program = require('../')
6
- , should = require('should');
7
-
8
- program
9
- .version('0.0.1')
10
- .option('-c, --cheese [type]', 'optionally specify the type of cheese');
11
-
12
- program.parse(['node', 'test', '--cheese']);
13
- program.cheese.should.be.true;
@@ -1,16 +0,0 @@
1
-
2
- /**
3
- * Module dependencies.
4
- */
5
-
6
- var program = require('../')
7
- , should = require('should');
8
-
9
- program
10
- .version('0.0.1')
11
- .option('-p, --pepper', 'add pepper')
12
- .option('-c, --no-cheese', 'remove cheese');
13
-
14
- program.parse(['node', 'test', '--pepper']);
15
- program.pepper.should.be.true;
16
- program.cheese.should.be.true;
@@ -1,15 +0,0 @@
1
- /**
2
- * Module dependencies.
3
- */
4
-
5
- var program = require('../')
6
- , should = require('should');
7
-
8
- program
9
- .version('0.0.1')
10
- .option('-p, --pepper', 'add pepper')
11
- .option('-c|--no-cheese', 'remove cheese');
12
-
13
- program.parse(['node', 'test', '--no-cheese']);
14
- should.equal(undefined, program.pepper);
15
- program.cheese.should.be.false;
@@ -1,15 +0,0 @@
1
- /**
2
- * Module dependencies.
3
- */
4
-
5
- var program = require('../')
6
- , should = require('should');
7
-
8
- program
9
- .version('0.0.1')
10
- .option('-p, --pepper', 'add pepper')
11
- .option('-c, --no-cheese', 'remove cheese');
12
-
13
- program.parse(['node', 'test', '-pc']);
14
- program.pepper.should.be.true;
15
- program.cheese.should.be.false;
@@ -1,15 +0,0 @@
1
- /**
2
- * Module dependencies.
3
- */
4
-
5
- var program = require('../')
6
- , should = require('should');
7
-
8
- program
9
- .version('0.0.1')
10
- .option('-p, --pepper', 'add pepper')
11
- .option('-c, --no-cheese', 'remove cheese');
12
-
13
- program.parse(['node', 'test', '-p', '-c']);
14
- program.pepper.should.be.true;
15
- program.cheese.should.be.false;
@@ -1,27 +0,0 @@
1
- /**
2
- * Module dependencies.
3
- */
4
-
5
- var program = require('../')
6
- , should = require('should');
7
-
8
- function parseRange(str) {
9
- return str.split('..').map(Number);
10
- }
11
-
12
- program
13
- .version('0.0.1')
14
- .option('-i, --my-int <n>', 'pass an int', parseInt)
15
- .option('-n, --my-num <n>', 'pass a number', Number)
16
- .option('-f, --my-fLOAT <n>', 'pass a float', parseFloat)
17
- .option('-m, --my-very-long-float <n>', 'pass a float', parseFloat)
18
- .option('-u, --my-URL-count <n>', 'pass a float', parseFloat)
19
- .option('-r, --my-long-range <a..b>', 'pass a range', parseRange);
20
-
21
- program.parse('node test -i 5.5 -f 5.5 -m 6.5 -u 7.5 -n 15.99 -r 1..5'.split(' '));
22
- program.myInt.should.equal(5);
23
- program.myNum.should.equal(15.99);
24
- program.myFLOAT.should.equal(5.5);
25
- program.myVeryLongFloat.should.equal(6.5);
26
- program.myURLCount.should.equal(7.5);
27
- program.myLongRange.should.eql([1,5]);
@@ -1,23 +0,0 @@
1
- /**
2
- * Module dependencies.
3
- */
4
-
5
- var program = require('../')
6
- , should = require('should');
7
-
8
- function parseRange(str) {
9
- return str.split('..').map(Number);
10
- }
11
-
12
- program
13
- .version('0.0.1')
14
- .option('-i, --int <n>', 'pass an int', parseInt)
15
- .option('-n, --num <n>', 'pass a number', Number)
16
- .option('-f, --float <n>', 'pass a float', parseFloat)
17
- .option('-r, --range <a..b>', 'pass a range', parseRange);
18
-
19
- program.parse('node test -i 5.5 -f 5.5 -n 15.99 -r 1..5'.split(' '));
20
- program.int.should.equal(5);
21
- program.num.should.equal(15.99);
22
- program.float.should.equal(5.5);
23
- program.range.should.eql([1,5]);
@@ -1,109 +0,0 @@
1
- /**
2
- * Module dependencies.
3
- */
4
-
5
- var program = require('../')
6
- , should = require('should');
7
-
8
- program
9
- .version('0.0.1')
10
- .option('-C, --chdir <path>', 'change the working directory')
11
- .option('-c, --config <path>', 'set config path. defaults to ./deploy.conf')
12
- .option('-T, --no-tests', 'ignore test hook')
13
-
14
- var envValue = "";
15
- var cmdValue = "";
16
- var customHelp = false;
17
-
18
- program
19
- .command('setup [env]')
20
- .description('run setup commands for all envs')
21
- .option("-s, --setup_mode [mode]", "Which setup mode to use")
22
- .option("-o, --host [host]", "Host to use")
23
- .action(function(env, options){
24
- var mode = options.setup_mode || "normal";
25
- env = env || 'all';
26
-
27
- envValue = env;
28
- });
29
-
30
- program
31
- .command('exec <cmd>')
32
- .description('execute the given remote cmd')
33
- .option("-e, --exec_mode <mode>", "Which exec mode to use")
34
- .option("-t, --target [target]", "Target to use")
35
- .action(function(cmd, options){
36
- cmdValue = cmd;
37
- }).on("--help", function(){
38
- customHelp = true;
39
- });
40
-
41
- program
42
- .command('*')
43
- .action(function(env){
44
- console.log('deploying "%s"', env);
45
- });
46
-
47
- program.parse(['node', 'test', '--config', 'conf']);
48
- program.config.should.equal("conf");
49
- program.commands[0].should.not.have.property.setup_mode;
50
- program.commands[1].should.not.have.property.exec_mode;
51
- envValue.should.be.null;
52
- cmdValue.should.be.null;
53
-
54
- program.parse(['node', 'test', '--config', 'conf1', 'setup', '--setup_mode', 'mode3', 'env1']);
55
- program.config.should.equal("conf1");
56
- program.commands[0].setup_mode.should.equal("mode3");
57
- program.commands[0].should.not.have.property.host;
58
- envValue.should.equal("env1");
59
-
60
- program.parse(['node', 'test', '--config', 'conf2', 'setup', '--setup_mode', 'mode3', '-o', 'host1', 'env2']);
61
- program.config.should.equal("conf2");
62
- program.commands[0].setup_mode.should.equal("mode3");
63
- program.commands[0].host.should.equal("host1");
64
- envValue.should.equal("env2");
65
-
66
- program.parse(['node', 'test', '--config', 'conf3', 'setup', '-s', 'mode4', 'env3']);
67
- program.config.should.equal("conf3");
68
- program.commands[0].setup_mode.should.equal("mode4");
69
- envValue.should.equal("env3");
70
-
71
- program.parse(['node', 'test', '--config', 'conf4', 'exec', '--exec_mode', 'mode1', 'exec1']);
72
- program.config.should.equal("conf4");
73
- program.commands[1].exec_mode.should.equal("mode1");
74
- program.commands[1].should.not.have.property.target;
75
- cmdValue.should.equal("exec1");
76
-
77
- program.parse(['node', 'test', '--config', 'conf5', 'exec', '-e', 'mode2', 'exec2']);
78
- program.config.should.equal("conf5");
79
- program.commands[1].exec_mode.should.equal("mode2");
80
- cmdValue.should.equal("exec2");
81
-
82
- program.parse(['node', 'test', '--config', 'conf6', 'exec', '--target', 'target1', '-e', 'mode2', 'exec3']);
83
- program.config.should.equal("conf6");
84
- program.commands[1].exec_mode.should.equal("mode2");
85
- program.commands[1].target.should.equal("target1");
86
- cmdValue.should.equal("exec3");
87
-
88
- // Make sure we still catch errors with required values for options
89
- var exceptionOccurred = false;
90
- var oldProcessExit = process.exit;
91
- var oldConsoleError = console.error;
92
- process.exit = function() { exceptionOccurred = true; throw new Error(); };
93
- console.error = function() {};
94
-
95
- try {
96
- program.parse(['node', 'test', '--config', 'conf6', 'exec', '--help']);
97
- } catch(ex) {
98
- program.config.should.equal("conf6");
99
- }
100
-
101
- try {
102
- program.parse(['node', 'test', '--config', 'conf', 'exec', '-t', 'target1', 'exec1', '-e']);
103
- }
104
- catch(ex) {
105
- }
106
-
107
- process.exit = oldProcessExit;
108
- exceptionOccurred.should.be.true;
109
- customHelp.should.be.true;
@@ -1,23 +0,0 @@
1
- /**
2
- * Module dependencies.
3
- */
4
-
5
- var program = require('../')
6
- , should = require('should');
7
-
8
- program
9
- .version('0.0.1')
10
- .option('-a, --anchovies', 'Add anchovies?')
11
- .option('-o, --onions', 'Add onions?', true)
12
- .option('-v, --olives', 'Add olives? Sorry we only have black.', 'black')
13
- .option('-s, --no-sauce', 'Uh… okay')
14
- .option('-r, --crust <type>', 'What kind of crust would you like?', 'hand-tossed')
15
- .option('-c, --cheese [type]', 'optionally specify the type of cheese', 'mozzarella');
16
-
17
- program.parse(['node', 'test', '--anchovies', '--onions', '--olives', '--no-sauce', '--crust', 'thin', '--cheese', 'wensleydale']);
18
- program.should.have.property('anchovies', true);
19
- program.should.have.property('onions', true);
20
- program.should.have.property('olives', 'black');
21
- program.should.have.property('sauce', false);
22
- program.should.have.property('crust', 'thin');
23
- program.should.have.property('cheese', 'wensleydale');
@@ -1,23 +0,0 @@
1
- /**
2
- * Module dependencies.
3
- */
4
-
5
- var program = require('../')
6
- , should = require('should');
7
-
8
- program
9
- .version('0.0.1')
10
- .option('-a, --anchovies', 'Add anchovies?')
11
- .option('-o, --onions', 'Add onions?', true)
12
- .option('-v, --olives', 'Add olives? Sorry we only have black.', 'black')
13
- .option('-s, --no-sauce', 'Uh… okay')
14
- .option('-r, --crust <type>', 'What kind of crust would you like?', 'hand-tossed')
15
- .option('-c, --cheese [type]', 'optionally specify the type of cheese', 'mozzarella');
16
-
17
- program.parse(['node', 'test']);
18
- program.should.not.have.property('anchovies');
19
- program.should.not.have.property('onions');
20
- program.should.not.have.property('olives');
21
- program.should.have.property('sauce', true);
22
- program.should.have.property('crust', 'hand-tossed');
23
- program.should.have.property('cheese', 'mozzarella');
@@ -1,13 +0,0 @@
1
- /**
2
- * Module dependencies.
3
- */
4
-
5
- var program = require('../')
6
- , should = require('should');
7
-
8
- program
9
- .version('0.0.1')
10
- .option('--longflag [value]', 'A long only flag with a value');
11
-
12
- program.parse(['node', 'test', '--longflag', 'something']);
13
- program.longflag.should.equal('something');
@@ -1,13 +0,0 @@
1
- /**
2
- * Module dependencies.
3
- */
4
-
5
- var program = require('../')
6
- , should = require('should');
7
-
8
- program
9
- .version('0.0.1')
10
- .option('--verbose', 'do stuff');
11
-
12
- program.parse(['node', 'test', '--verbose']);
13
- program.verbose.should.be.true;
@@ -1,121 +0,0 @@
1
- /**
2
- * Module dependencies.
3
- */
4
-
5
- var events = require('events')
6
- , program = require('../')
7
- , should = require('should');
8
-
9
- //mock stdin on process
10
- var stdin = new events.EventEmitter();
11
- stdin.setEncoding = stdin.resume = function() {};
12
- stdin.write = function(data) { stdin.emit('data', data); };
13
- process.__defineGetter__('stdin', function() { return stdin });
14
-
15
- //mock stdout on process
16
- var stdout = new events.EventEmitter();
17
- stdout.write = function(data) { this.emit('data', data) };
18
- stdout.expect = function(expected, fn) {
19
- var actual = '';
20
- this.once('data', function(data){ actual = data; });
21
- fn();
22
- actual.should.equal(expected);
23
- }
24
- //var realOut = process.stdout;
25
- process.__defineGetter__('stdout', function() { return stdout });
26
-
27
- var count = 0;
28
- var test = function(expected) {
29
- count++;
30
- return function(actual) {
31
- count--;
32
- if(expected instanceof Date) {
33
- //need something better :(
34
- //using `new Date(date.toString())` round trip
35
- //loosing some precision for .getTime()
36
- actual.should.be.an.instanceof(Date);
37
- } else {
38
- actual.should.equal(expected);
39
- }
40
- }
41
- }
42
-
43
- process.on('exit', function(){
44
- count.should.equal(0);
45
- })
46
-
47
- /* Single Line String Prompt Tests */
48
- var prompt = 'hello '
49
- , expected = 'world';
50
-
51
- // simple single line string
52
- stdout.expect(prompt, function(){
53
- program.prompt(prompt, test(expected));
54
- stdin.write(expected);
55
- })
56
- // with trim
57
- stdout.expect(prompt, function(){
58
- program.prompt(prompt, test(expected));
59
- stdin.write(' ' + expected + ' ');
60
- })
61
- // newline
62
- stdout.expect(prompt, function(){
63
- program.prompt(prompt, test(''));
64
- stdin.write('\n');
65
- })
66
-
67
- /* Multiline Tests*/
68
- var prompt = 'A multiline';
69
- stdout.expect(prompt + '\n', function() {
70
- program.prompt(prompt, test('hello world\n and what a world\nit could be'));
71
- stdin.write('hello world\n');
72
- stdin.write(' and what a world\n');
73
- stdin.write('it could be\n');
74
- stdin.write('\n');
75
- })
76
-
77
- /* Date Prompt Tests */
78
- var prompt = 'A date '
79
- , expected = new Date();
80
-
81
- // simple date
82
- stdout.expect(prompt, function(){
83
- program.prompt(prompt, Date, test(expected));
84
- stdin.write(expected.toString());
85
- })
86
- // not a date twice
87
- stdout.expect(prompt, function() {
88
- program.prompt(prompt, Date, test(expected));
89
-
90
- stdout.expect(prompt + '(must be a date) ', function() {
91
- stdin.write('blue');
92
- })
93
- stdout.expect(prompt + '(must be a date) ', function() {
94
- stdin.write('blue');
95
- })
96
- stdin.write(expected.toString());
97
- })
98
-
99
- /* Number Prompt Tests*/
100
- var prompt = 'A number '
101
- , expected = 12;
102
-
103
- //simple number
104
- stdout.expect(prompt, function(){
105
- program.prompt(prompt, Number, test(expected));
106
- stdin.write(expected.toString());
107
- })
108
-
109
- // not a number twice
110
- stdout.expect(prompt, function() {
111
- program.prompt(prompt, Number, test(expected));
112
-
113
- stdout.expect(prompt + '(must be a number) ', function() {
114
- stdin.write('blue');
115
- })
116
- stdout.expect(prompt + '(must be a number) ', function() {
117
- stdin.write('blue');
118
- })
119
- stdin.write(expected.toString());
120
- })
121
-