Mathias Panzenböck avatar Mathias Panzenböck committed 29d96ef

fixed metavar for enums

Comments (0)

Files changed (1)

 		this.maxargs = params.maxargs;
 		this.program = params.program;
 		this.strings = params.strings;
+
+		if (this.minargs > this.maxargs) {
+			throw new Error('minargs > maxargs');
+		}
 	}
 
 	if (this.strings === undefined) {
 		this.strings = {};
 	}
 
-	defaults(this.strings,  {
+	defaults(this.strings, {
 		help:      'No help available for this option.',
 		usage:     'Usage',
 		options:   'OPTIONS',
 		}
 
 		if (help !== undefined) {
-			out.write('\n');
 			out.write(help);
 			if (help[help.length-1] != '\n') {
 				out.write('\n');
 				}
 		}
 	
-		if (optdef.stringify === undefined) {
-			optdef.stringify = STRINGIFIERS[optdef.type];
-		}
-	
-		if (optdef.stringify === undefined) {
-			optdef.stringify = stringifyAny;
-		}
+		initStringify(optdef);
 		
 		var count = 1;
 		if (optdef.metavar === undefined) {
 	},
 	_initEnum: function (optdef, name) {
 		optdef.argc = 1;
-		var values = {};
 	
 		if (optdef.ignoreCase === undefined) {
 			optdef.ignoreCase = true;
 			throw new Error('no values for enum '+name+' defined');
 		}
 	
-		var labels;
+		initStringify(optdef);
+
+		var labels = [];
+		var values = {};
 		if (optdef.values instanceof Array) {
-			labels = optdef.values;
-			for (i in labels) {
-				var value = labels[i];
+			for (i in optdef.values) {
+				var value = optdef.values[i];
 				values[optdef.ignoreCase ? value.toLowerCase() : value] = value;
+				labels.push(optdef.stringify(value));
 			}
 		}
 		else {
-			labels = [];
 			for (label in optdef.values) {
 				var value = optdef.values[label];
 				values[optdef.ignoreCase ? label.toLowerCase() : label] = value;
-				labels.push(label);
+				labels.push(optdef.stringify(label));
 			}
 			labels.sort();
 		}
 		optdef.values = values;
 		
+		
 		if (optdef.metavar === undefined) {
 			optdef.metavar = '<'+labels.join(', ')+'>';
 		}
 			throw new Error(optdef.type+' '+name+
 				' needs at least a minimum or a maximum');
 		}
+		else if (optdef.min > optdef.max) {
+			throw new Error('min > max for option '+name);
+		}
 		
 		var parse, toStr;
 		if (optdef.type == 'integer-range') {
 	}
 };
 
+function initStringify (optdef) {
+	if (optdef.stringify === undefined) {
+		optdef.stringify = STRINGIFIERS[optdef.type];
+	}
+	
+	if (optdef.stringify === undefined) {
+		optdef.stringify = stringifyAny;
+	}
+}
+
 function defaults (target, defaults) {
 	for (name in defaults) {
 		if (target[name] === undefined) {
 	parser.add('--egg', {type: 'option', default: 'none'});
 	parser.add('--spam', {type: 'option', target: 'egg'});
 	parser.add('--tomato', {type: 'option', target: 'egg', value: 'bla'});
-	parser.add('--sel', {type: 'enum', values: {foo: 42, bar: 23}});
+	parser.add('--sel1', {type: 'enum', values: {foo: 42, bar: 23}});
+	parser.add('--sel2', {type: 'enum', values: ['egg', 'spam spam'], default: ' '});
 	parser.add('--foo-bar', {default: 'bla "bla" \t\n \'bla\' \\'});
 	parser.add('--EGG--SPAM');
 	parser.add('--b64', {type: 'custom', argc: 1, metavar: 'BASE64-STRING',
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.