sbin/res/jsdoc/app/frame/Opt.js
author hamidouk
Fri, 23 Dec 2011 11:38:47 +0100
branchjsdoc
changeset 520 fe008e95a716
permissions -rw-r--r--
added jsdoc support, and a script to generate the docs. Also added sample documentation for the IriSP.widget base class.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
520
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
     1
/** @namespace */
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
     2
Opt = {
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
     3
	/**
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
     4
	 * Get commandline option values.
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
     5
	 * @param {Array} args Commandline arguments. Like ["-a=xml", "-b", "--class=new", "--debug"]
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
     6
	 * @param {object} optNames Map short names to long names. Like {a:"accept", b:"backtrace", c:"class", d:"debug"}.
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
     7
	 * @return {object} Short names and values. Like {a:"xml", b:true, c:"new", d:true}
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
     8
	 */
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
     9
	get: function(args, optNames) {
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    10
		var opt = {"_": []}; // the unnamed option allows multiple values
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    11
		for (var i = 0; i < args.length; i++) {
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    12
			var arg = new String(args[i]);
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    13
			var name;
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    14
			var value;
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    15
			if (arg.charAt(0) == "-") {
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    16
				if (arg.charAt(1) == "-") { // it's a longname like --foo
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    17
					arg = arg.substring(2);
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    18
					var m = arg.split("=");
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    19
					name = m.shift();
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    20
					value = m.shift();
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    21
					if (typeof value == "undefined") value = true;
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    22
					
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    23
					for (var n in optNames) { // convert it to a shortname
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    24
						if (name == optNames[n]) {
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    25
							name = n;
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    26
						}
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    27
					}
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    28
				}
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    29
				else { // it's a shortname like -f
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    30
					arg = arg.substring(1);
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    31
					var m = arg.split("=");
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    32
					name = m.shift();
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    33
					value = m.shift();
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    34
					if (typeof value == "undefined") value = true;
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    35
					
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    36
					for (var n in optNames) { // find the matching key
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    37
						if (name == n || name+'[]' == n) {
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    38
							name = n;
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    39
							break;
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    40
						}
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    41
					}
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    42
				}
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    43
				if (name.match(/(.+)\[\]$/)) { // it's an array type like n[]
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    44
					name = RegExp.$1;
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    45
					if (!opt[name]) opt[name] = [];
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    46
				}
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    47
				
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    48
				if (opt[name] && opt[name].push) {
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    49
					opt[name].push(value);
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    50
				}
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    51
				else {
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    52
					opt[name] = value;
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    53
				}
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    54
			}
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    55
			else { // not associated with any optname
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    56
				opt._.push(args[i]);
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    57
			}
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    58
		}
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    59
		return opt;
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    60
	}
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    61
}
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    62
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    63
/*t:
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    64
	plan(11, "Testing Opt.");
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    65
	
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    66
	is(
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    67
		typeof Opt,
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    68
		"object",
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    69
		"Opt is an object."
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    70
	);
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    71
	
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    72
	is(
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    73
		typeof Opt.get,
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    74
		"function",
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    75
		"Opt.get is a function."
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    76
	);
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    77
	
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    78
	var optNames = {a:"accept", b:"backtrace", c:"class", d:"debug", "e[]":"exceptions"};
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    79
	var t_options = Opt.get(["-a=xml", "-b", "--class=new", "--debug", "-e=one", "-e=two", "foo", "bar"], optNames);
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    80
	
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    81
	is(
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    82
		t_options.a,
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    83
		"xml",
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    84
		"an option defined with a short name can be accessed by its short name."
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    85
	);
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    86
	
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    87
	is(
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    88
		t_options.b,
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    89
		true,
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    90
		"an option defined with a short name and no value are true."
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    91
	);
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    92
	
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    93
	is(
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    94
		t_options.c,
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    95
		"new",
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    96
		"an option defined with a long name can be accessed by its short name."
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    97
	);
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    98
	
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
    99
	is(
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   100
		t_options.d,
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   101
		true,
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   102
		"an option defined with a long name and no value are true."
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   103
	);
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   104
	
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   105
	is(
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   106
		typeof t_options.e,
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   107
		"object",
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   108
		"an option that can accept multiple values is defined."
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   109
	);
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   110
	
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   111
	is(
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   112
		t_options.e.length,
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   113
		2,
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   114
		"an option that can accept multiple values can have more than one value."
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   115
	);
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   116
	
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   117
	is(
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   118
		t_options.e[1],
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   119
		"two",
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   120
		"an option that can accept multiple values can be accessed as an array."
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   121
	);
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   122
	
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   123
	is(
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   124
		typeof t_options._,
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   125
		"object",
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   126
		"the property '_' is defined for unnamed options."
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   127
	);
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   128
	
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   129
	is(
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   130
		t_options._[0],
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   131
		"foo",
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   132
		"the property '_' can be accessed as an array."
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   133
	);
fe008e95a716 added jsdoc support, and a script to generate the docs.
hamidouk
parents:
diff changeset
   134
 */