Commits

Mathias Panzenböck committed 751f0bd

merged integer with integer-range and float with float-range

Comments (0)

Files changed (1)

  *      help:        string, optional
  *      details:     array, optional
  *                   short list of details shown in braces after the option name
- *                   integer and integer-range type options add e.g. 'base: 16'
- *                   if base !== undefined
+ *                   e.g. integer type options add 'base: '+base if base !== undefined
  *      metavar:     string or array, per deflault inferred from type
  *      onOption:    function (value, ...) -> boolean, optional
  *                   Returning true canceles any further option parsing
  *      // Type: object  (alias: obj)
  *
  *      // Type: integer (alias: int)
+ *      min:         integer, optional
+ *      max:         integer, optional
  *      NaN:         boolean, default: false
  *      base:        integer, optional
  *
  *      // Type: float   (alias: number)
+ *      min:         float, optional
+ *      max:         float, optional
  *      NaN:         boolean, default: false
  *
  *      // Type: flag
  *      values:      array or object where the user enteres the field name of
  *                   the object and you get the value of the field
  *
- *      // Type: integer-range (alias: range, int-range)
- *      min:         integer, optional
- *      max:         integer, optional (at least one of the two musst be set)
- *      base:        integer, optional
- *      
- *      // Type: float-range
- *      min:         float, optional
- *      max:         float, optional (at least one of the two musst be set)
- *
  *      // Type: record
  *      args:        array of type definitions (type part of option definitions)
  *
  *
  * ===== Option-Arguments =====
  * For the following types exactly one argument is required:
- *   integer, float, integer-range, float-range, string, boolean, object, enum
+ *   integer, float, string, boolean, object, enum
  *
  * The following types have optional arguments:
  *   flag
 				this._initEnum(optdef, name);
 				break;
 	
-			case 'integer-range':
-			case 'float-range':
-				this._initRange(optdef, name);
-				break;
-	
 			case 'integer':
-				optdef.argc = 1;
-	
-				if (optdef.NaN === undefined) {
-					optdef.NaN = false;
-				}
-	
-				if (optdef.base !== undefined) {
-					var details = optdef.details;
-					if (details === undefined) {
-						details = optdef.details = [];
-					}
-					details.push(this.strings.base+': '+optdef.base);
-				}
-					
-				optdef.parse = function (s) {
-					var i = parseInt(s,this.base);
-					if (!this.NaN && (isNaN(i) || s.indexOf('.') != -1)) {
-						throw new Error('illegal value for option '+name+': '+s);
-					}
-					return i;
-				};
-				break;
-	
 			case 'float':
-				optdef.argc = 1;
-	
-				if (optdef.NaN === undefined) {
-					optdef.NaN = false;
-				}
-					
-				optdef.parse = function (s) {
-					var f = parseFloat(s);
-					if (!this.NaN && isNaN(f)) {
-						throw new Error('illegal value for option '+name+': '+s);
-					}
-					return f;
-				};
+				this._initNumber(optdef, name);
 				break;
 	
 			case 'record':
 			throw new Error('illegal value for option '+name+': '+s);
 		};
 	},
-	_initRange: function (optdef, name) {
+	_initNumber: function (optdef, name) {
 		optdef.argc = 1;
 	
-		if (optdef.min === undefined && optdef.max === undefined) {
-			throw new Error(optdef.type+' '+name+
-				' needs at least a minimum or a maximum');
+		if (optdef.NaN === undefined) {
+			optdef.NaN = false;
 		}
-		else if (optdef.min > optdef.max) {
+
+		if (optdef.min > optdef.max) {
 			throw new Error('min > max for option '+name);
 		}
 		
 		var parse, toStr;
-		if (optdef.type == 'integer-range') {
+		if (optdef.type == 'integer') {
 			parse = function (s) {
 				var i = NaN;
 				if (s.indexOf('.') == -1) {
 					i = parseInt(s, optdef.base)
 				}
-				if (isNaN(i)) {
-					throw new Error('illegal value for option '+name+': '+s);
-				}
 				return i;
 			};
 			if (optdef.base === undefined) {
 			}
 		}
 		else {
-			parse = function (s) {
-				var f = parseFloat(s);
-				if (isNaN(f)) {
-					throw new Error('illegal value for option '+name+': '+s);
-				}
-				return f;
-			};
+			parse = parseFloat;
 			toStr = dec;
 		}
 	
 		if (optdef.metavar === undefined) {
-			if (optdef.min === undefined) {
+			if (optdef.min === undefined && optdef.max === undefined) {
+				optdef.metavar = this.strings.metavars[optdef.type];
+			}
+			else if (optdef.min === undefined) {
 				optdef.metavar = '...'+toStr(optdef.max);
 			}
 			else if (optdef.max === undefined) {
 		optdef.parse = function (s) {
 			var n = parse(s);
 					
-			if (isNaN(n)
+			if ((!this.NaN && isNaN(n))
 					|| (optdef.min !== undefined && n < optdef.min)
 					|| (optdef.max !== undefined && n > optdef.max)) {
 				throw new Error('illegal value for option '+name+': '+s);
 	number: 'float',
 	bool:   'boolean',
 	str:    'string',
-	obj:    'object',
-	range:       'integer-range',
-	'int-range': 'integer-range'
+	obj:    'object'
 };
 
 const METAVARS = {
 	boolean: 'BOOLEAN',
 	object:  'OBJECT',
 	enum:    'VALUE',
-	custom:  'VALUE',
-	'integer-range': 'INTEGER',
-	'float-range':   'FLOAT'
+	custom:  'VALUE'
 };
 
 function stringifyString(s) {
-	if (/[\s'"\\]/.test(s)) {
+	if (/[\s'"\\<>,]/.test(s)) {
 //		s = "'"+s.replace(/\\/g,'\\\\').replace(/'/g, "'\\''")+"'";
 		s = JSON.stringify(s);
 	}
 	boolean:         stringifyPrimitive,
 	float:           stringifyPrimitive,
 	object:          JSON.stringify,
-	'integer-range': stringifyInteger,
-	'float-range':   stringifyPrimitive,
 	enum:            stringifyAny,
 	custom:          stringifyAny,
 	record:          stringifyRecord
 	parser.add('-s');
 	parser.add('--s2', {default: 'blub'});
 	parser.add('-i', {type: 'int', required: true, redefinable: false});
+	parser.add('--int32', {type: 'integer',
+		min: -2147483648, max: 2147483647, metavar: 'INT32'});
 	parser.add('--hex', {type: 'integer', base: 16});
 	parser.add(['-n','-d','--float'], {type: 'float', NaN: true});
 	parser.add('-o', {type: 'object', default: {foo: 'bar', egg: null, spam: true}});
 	parser.add('-b', {type: 'boolean', default: true});
 	parser.add('-x', {type: 'flag', redefinable: false});
-	parser.add('--r1', {type: 'range', default: 1, min: 0, base: 8});
-	parser.add('--r2', {type: 'range', default: 3, max: 20, base: 16});
-	parser.add('--r3', {type: 'range', default: 4, min: -10, max: 10, base: 2});
-
+	parser.add('--r1', {type: 'integer', default: 1, min: 0, base: 8});
+	parser.add('--r2', {type: 'integer', default: 3, max: 20, base: 16});
+	parser.add('--r3', {type: 'integer', default: 4, min: -10, max: 10, base: 2});
 	parser.add('--egg', {type: 'option', default: 'none'});
 	parser.add('--spam', {type: 'option', target: 'egg'});
 	parser.add('--tomato', {type: 'option', target: 'egg', value: 'bla'});
 		default: [11., 5, 'foo'],
 		args: [
 			{type: 'float'},
-			{type: 'range', min: 1, max: 22},
+			{type: 'integer', min: 1, max: 22},
 			{type: 'string'}
 		]});
 	parser.add(['--record','-T'], {type: 'record',