commander 1.3.2 → 2.0.0

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,9 @@
1
1
 
2
+ 2.0.0 / 2013-07-18
3
+ ==================
4
+
5
+ * remove input methods (.prompt, .confirm, etc)
6
+
2
7
  1.3.2 / 2013-07-18
3
8
  ==================
4
9
 
package/Readme.md CHANGED
@@ -153,87 +153,6 @@ Examples:
153
153
 
154
154
  ```
155
155
 
156
- ## .prompt(msg, fn)
157
-
158
- Single-line prompt:
159
-
160
- ```js
161
- program.prompt('name: ', function(name){
162
- console.log('hi %s', name);
163
- });
164
- ```
165
-
166
- Multi-line prompt:
167
-
168
- ```js
169
- program.prompt('description:', function(name){
170
- console.log('hi %s', name);
171
- });
172
- ```
173
-
174
- Coercion:
175
-
176
- ```js
177
- program.prompt('Age: ', Number, function(age){
178
- console.log('age: %j', age);
179
- });
180
- ```
181
-
182
- ```js
183
- program.prompt('Birthdate: ', Date, function(date){
184
- console.log('date: %s', date);
185
- });
186
- ```
187
-
188
- ```js
189
- program.prompt('Email: ', /^.+@.+\..+$/, function(email){
190
- console.log('email: %j', email);
191
- });
192
- ```
193
-
194
- ## .password(msg[, mask], fn)
195
-
196
- Prompt for password without echoing:
197
-
198
- ```js
199
- program.password('Password: ', function(pass){
200
- console.log('got "%s"', pass);
201
- process.stdin.destroy();
202
- });
203
- ```
204
-
205
- Prompt for password with mask char "*":
206
-
207
- ```js
208
- program.password('Password: ', '*', function(pass){
209
- console.log('got "%s"', pass);
210
- process.stdin.destroy();
211
- });
212
- ```
213
-
214
- ## .confirm(msg, fn)
215
-
216
- Confirm with the given `msg`:
217
-
218
- ```js
219
- program.confirm('continue? ', function(ok){
220
- console.log(' got %j', ok);
221
- });
222
- ```
223
-
224
- ## .choose(list, fn)
225
-
226
- Let the user choose from a `list`:
227
-
228
- ```js
229
- var list = ['tobi', 'loki', 'jane', 'manny', 'luna'];
230
-
231
- console.log('Choose the coolest pet:');
232
- program.choose(list, function(i){
233
- console.log('you chose %d "%s"', i, list[i]);
234
- });
235
- ```
236
-
237
156
  ## .outputHelp()
238
157
 
239
158
  Output help information without exiting.
package/index.js CHANGED
@@ -1,22 +1,15 @@
1
- /*!
2
- * commander
3
- * Copyright(c) 2011 TJ Holowaychuk <tj@vision-media.ca>
4
- * MIT Licensed
5
- */
6
1
 
7
2
  /**
8
3
  * Module dependencies.
9
4
  */
10
5
 
11
- var EventEmitter = require('events').EventEmitter
12
- , spawn = require('child_process').spawn
13
- , keypress = require('keypress')
14
- , fs = require('fs')
15
- , exists = fs.existsSync
16
- , path = require('path')
17
- , tty = require('tty')
18
- , dirname = path.dirname
19
- , basename = path.basename;
6
+ var EventEmitter = require('events').EventEmitter;
7
+ var spawn = require('child_process').spawn;
8
+ var fs = require('fs');
9
+ var exists = fs.existsSync;
10
+ var path = require('path');
11
+ var dirname = path.dirname;
12
+ var basename = path.basename;
20
13
 
21
14
  /**
22
15
  * Expose the root command.
@@ -785,300 +778,6 @@ Command.prototype.helpInformation = function(){
785
778
  ].join('\n');
786
779
  };
787
780
 
788
- /**
789
- * Prompt for a `Number`.
790
- *
791
- * @param {String} str
792
- * @param {Function} fn
793
- * @api private
794
- */
795
-
796
- Command.prototype.promptForNumber = function(str, fn){
797
- var self = this;
798
- this.promptSingleLine(str, function parseNumber(val){
799
- val = Number(val);
800
- if (isNaN(val)) return self.promptSingleLine(str + '(must be a number) ', parseNumber);
801
- fn(val);
802
- });
803
- };
804
-
805
- /**
806
- * Prompt for a `Date`.
807
- *
808
- * @param {String} str
809
- * @param {Function} fn
810
- * @api private
811
- */
812
-
813
- Command.prototype.promptForDate = function(str, fn){
814
- var self = this;
815
- this.promptSingleLine(str, function parseDate(val){
816
- val = new Date(val);
817
- if (isNaN(val.getTime())) return self.promptSingleLine(str + '(must be a date) ', parseDate);
818
- fn(val);
819
- });
820
- };
821
-
822
-
823
- /**
824
- * Prompt for a `Regular Expression`.
825
- *
826
- * @param {String} str
827
- * @param {Object} pattern regular expression object to test
828
- * @param {Function} fn
829
- * @api private
830
- */
831
-
832
- Command.prototype.promptForRegexp = function(str, pattern, fn){
833
- var self = this;
834
- this.promptSingleLine(str, function parseRegexp(val){
835
- if(!pattern.test(val)) return self.promptSingleLine(str + '(regular expression mismatch) ', parseRegexp);
836
- fn(val);
837
- });
838
- };
839
-
840
-
841
- /**
842
- * Single-line prompt.
843
- *
844
- * @param {String} str
845
- * @param {Function} fn
846
- * @api private
847
- */
848
-
849
- Command.prototype.promptSingleLine = function(str, fn){
850
- // determine if the 2nd argument is a regular expression
851
- if (arguments[1].global !== undefined && arguments[1].multiline !== undefined) {
852
- return this.promptForRegexp(str, arguments[1], arguments[2]);
853
- } else if ('function' == typeof arguments[2]) {
854
- return this['promptFor' + (fn.name || fn)](str, arguments[2]);
855
- }
856
-
857
- process.stdout.write(str);
858
- process.stdin.setEncoding('utf8');
859
- process.stdin.once('data', function(val){
860
- fn(val.trim());
861
- }).resume();
862
- };
863
-
864
- /**
865
- * Multi-line prompt.
866
- *
867
- * @param {String} str
868
- * @param {Function} fn
869
- * @api private
870
- */
871
-
872
- Command.prototype.promptMultiLine = function(str, fn){
873
- var buf = [];
874
- console.log(str);
875
- process.stdin.setEncoding('utf8');
876
- process.stdin.on('data', function(val){
877
- if ('\n' == val || '\r\n' == val) {
878
- process.stdin.removeAllListeners('data');
879
- fn(buf.join('\n'));
880
- } else {
881
- buf.push(val.trimRight());
882
- }
883
- }).resume();
884
- };
885
-
886
- /**
887
- * Prompt `str` and callback `fn(val)`
888
- *
889
- * Commander supports single-line and multi-line prompts.
890
- * To issue a single-line prompt simply add white-space
891
- * to the end of `str`, something like "name: ", whereas
892
- * for a multi-line prompt omit this "description:".
893
- *
894
- *
895
- * Examples:
896
- *
897
- * program.prompt('Username: ', function(name){
898
- * console.log('hi %s', name);
899
- * });
900
- *
901
- * program.prompt('Description:', function(desc){
902
- * console.log('description was "%s"', desc.trim());
903
- * });
904
- *
905
- * @param {String|Object} str
906
- * @param {Function} fn
907
- * @api public
908
- */
909
-
910
- Command.prototype.prompt = function(str, fn){
911
- var self = this;
912
- if ('string' == typeof str) {
913
- if (/ $/.test(str)) return this.promptSingleLine.apply(this, arguments);
914
- this.promptMultiLine(str, fn);
915
- } else {
916
- var keys = Object.keys(str)
917
- , obj = {};
918
-
919
- function next() {
920
- var key = keys.shift()
921
- , label = str[key];
922
-
923
- if (!key) return fn(obj);
924
- self.prompt(label, function(val){
925
- obj[key] = val;
926
- next();
927
- });
928
- }
929
-
930
- next();
931
- }
932
- };
933
-
934
- /**
935
- * Prompt for password with `str`, `mask` char and callback `fn(val)`.
936
- *
937
- * The mask string defaults to '', aka no output is
938
- * written while typing, you may want to use "*" etc.
939
- *
940
- * Examples:
941
- *
942
- * program.password('Password: ', function(pass){
943
- * console.log('got "%s"', pass);
944
- * process.stdin.destroy();
945
- * });
946
- *
947
- * program.password('Password: ', '*', function(pass){
948
- * console.log('got "%s"', pass);
949
- * process.stdin.destroy();
950
- * });
951
- *
952
- * @param {String} str
953
- * @param {String} mask
954
- * @param {Function} fn
955
- * @api public
956
- */
957
-
958
- Command.prototype.password = function(str, mask, fn){
959
- var self = this
960
- , buf = '';
961
-
962
- // default mask
963
- if ('function' == typeof mask) {
964
- fn = mask;
965
- mask = '';
966
- }
967
-
968
- keypress(process.stdin);
969
-
970
- function setRawMode(mode) {
971
- if (process.stdin.setRawMode) {
972
- process.stdin.setRawMode(mode);
973
- } else {
974
- tty.setRawMode(mode);
975
- }
976
- };
977
- setRawMode(true);
978
- process.stdout.write(str);
979
-
980
- // keypress
981
- process.stdin.on('keypress', function(c, key){
982
- if (key && 'enter' == key.name) {
983
- console.log();
984
- process.stdin.pause();
985
- process.stdin.removeAllListeners('keypress');
986
- setRawMode(false);
987
- if (!buf.trim().length) return self.password(str, mask, fn);
988
- fn(buf);
989
- return;
990
- }
991
-
992
- if (key && key.ctrl && 'c' == key.name) {
993
- console.log('%s', buf);
994
- process.exit();
995
- }
996
-
997
- process.stdout.write(mask);
998
- buf += c;
999
- }).resume();
1000
- };
1001
-
1002
- /**
1003
- * Confirmation prompt with `str` and callback `fn(bool)`
1004
- *
1005
- * Examples:
1006
- *
1007
- * program.confirm('continue? ', function(ok){
1008
- * console.log(' got %j', ok);
1009
- * process.stdin.destroy();
1010
- * });
1011
- *
1012
- * @param {String} str
1013
- * @param {Function} fn
1014
- * @api public
1015
- */
1016
-
1017
-
1018
- Command.prototype.confirm = function(str, fn, verbose){
1019
- var self = this;
1020
- this.prompt(str, function(ok){
1021
- if (!ok.trim()) {
1022
- if (!verbose) str += '(yes or no) ';
1023
- return self.confirm(str, fn, true);
1024
- }
1025
- fn(parseBool(ok));
1026
- });
1027
- };
1028
-
1029
- /**
1030
- * Choice prompt with `list` of items and callback `fn(index, item)`
1031
- *
1032
- * Examples:
1033
- *
1034
- * var list = ['tobi', 'loki', 'jane', 'manny', 'luna'];
1035
- *
1036
- * console.log('Choose the coolest pet:');
1037
- * program.choose(list, function(i){
1038
- * console.log('you chose %d "%s"', i, list[i]);
1039
- * process.stdin.destroy();
1040
- * });
1041
- *
1042
- * @param {Array} list
1043
- * @param {Number|Function} index or fn
1044
- * @param {Function} fn
1045
- * @api public
1046
- */
1047
-
1048
- Command.prototype.choose = function(list, index, fn){
1049
- var self = this
1050
- , hasDefault = 'number' == typeof index;
1051
-
1052
- if (!hasDefault) {
1053
- fn = index;
1054
- index = null;
1055
- }
1056
-
1057
- list.forEach(function(item, i){
1058
- if (hasDefault && i == index) {
1059
- console.log('* %d) %s', i + 1, item);
1060
- } else {
1061
- console.log(' %d) %s', i + 1, item);
1062
- }
1063
- });
1064
-
1065
- function again() {
1066
- self.prompt(' : ', function(val){
1067
- val = parseInt(val, 10) - 1;
1068
- if (hasDefault && isNaN(val)) val = index;
1069
-
1070
- if (null == list[val]) {
1071
- again();
1072
- } else {
1073
- fn(val, list[val]);
1074
- }
1075
- });
1076
- }
1077
-
1078
- again();
1079
- };
1080
-
1081
-
1082
781
  /**
1083
782
  * Output help information for this command
1084
783
  *
@@ -1115,18 +814,6 @@ function camelcase(flag) {
1115
814
  });
1116
815
  }
1117
816
 
1118
- /**
1119
- * Parse a boolean `str`.
1120
- *
1121
- * @param {String} str
1122
- * @return {Boolean}
1123
- * @api private
1124
- */
1125
-
1126
- function parseBool(str) {
1127
- return /^y|yes|ok|true$/i.test(str);
1128
- }
1129
-
1130
817
  /**
1131
818
  * Pad `str` to `width`.
1132
819
  *
package/package.json CHANGED
@@ -1,11 +1,10 @@
1
1
  {
2
2
  "name": "commander"
3
- , "version": "1.3.2"
3
+ , "version": "2.0.0"
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>"
7
7
  , "repository": { "type": "git", "url": "https://github.com/visionmedia/commander.js.git" }
8
- , "dependencies": { "keypress": "0.1.x"}
9
8
  , "devDependencies": { "should": ">= 0.0.1" }
10
9
  , "scripts": { "test": "make test" }
11
10
  , "main": "index"