PageRenderTime 13ms CodeModel.GetById 7ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

/branches/jsdoc_tk_gui/setup/jsDoc/Util.js

http://jsdoc-toolkit.googlecode.com/
JavaScript | 97 lines | 66 code | 5 blank | 26 comment | 24 complexity | 44553a824d74563235470b5e1b8c5fbb MD5 | raw file
 1/**
 2 * @fileOverview
 3 * @name Util
 4 * @author Michael Mathews micmath@gmail.com
 5 * @url $HeadURL: https://jsdoc-toolkit.googlecode.com/svn/branches/jsdoc_tk_gui/setup/app/Util.js $
 6 * @revision $Id: Util.js 313 2007-11-11 22:01:03Z sebastien.bordes $
 7 * @license <a href="http://en.wikipedia.org/wiki/MIT_License">X11/MIT License</a>
 8 *          (See the accompanying README file for full details.)
 9 */
10
11/**
12 * @class Various utility methods used by JsDoc.
13 * @static
14 */
15Util = {
16	/**
17	 * Turn a path into just the name of the file.
18	 * @static
19	 * @param {string} path
20	 * @return {string} The fileName portion of the path.
21	 */
22	fileName: function(path) {
23		var nameStart = Math.max(path.lastIndexOf("/")+1, path.lastIndexOf("\\")+1, 0);
24		return path.substring(nameStart);
25	},
26	
27	/**
28	 * Turn a path into just the directory part.
29	 * @static
30	 * @param {string} path
31	 * @return {string} The directory part of the path.
32	 */
33	dir: function(path) {
34		var nameStart = Math.max(path.lastIndexOf("/")+1, path.lastIndexOf("\\")+1, 0);
35		return path.substring(0, nameStart-1);
36	},
37	
38	/**
39	 * Get commandline option values.
40	 * @static
41	 * @param {Array} args Commandline arguments. Like ["-a=xml", "-b", "--class=new", "--debug"]
42	 * @param {object} optNames Map short names to long names. Like {a:"accept", b:"backtrace", c:"class", d:"debug"}.
43	 * @return {object} Short names and values. Like {a:"xml", b:true, c:"new", d:true}
44	 */
45	getOptions: function(args, optNames) {
46		var opt = {"_": []}; // the unnamed option allows multiple values
47		for (var i = 0; i < args.length; i++) {
48			var arg = new String(args[i]);
49			var name;
50			var value;
51			if (arg.charAt(0) == "-") {
52				if (arg.charAt(1) == "-") { // it's a longname like --foo
53					arg = arg.substring(2);
54					var m = arg.split("=");
55					name = m.shift();
56					value = m.shift();
57					if (typeof value == "undefined") value = true;
58					
59					for (var n in optNames) { // convert it to a shortname
60						if (name == optNames[n]) {
61							name = n;
62						}
63					}
64				}
65				else { // it's a shortname like -f
66					arg = arg.substring(1);
67					var m = arg.split("=");
68					name = m.shift();
69					value = m.shift();
70					if (typeof value == "undefined") value = true;
71					
72					for (var n in optNames) { // find the matching key
73						if (name == n || name+'[]' == n) {
74							name = n;
75							break;
76						}
77					}
78				}
79				if (name.match(/(.+)\[\]$/)) { // it's an array type like n[]
80					name = RegExp.$1;
81					if (!opt[name]) opt[name] = [];
82				}
83				
84				if (opt[name] && opt[name].push) {
85					opt[name].push(value);
86				}
87				else {
88					opt[name] = value;
89				}
90			}
91			else { // not associated with any optname
92				opt._.push(args[i]);
93			}
94		}
95		return opt;
96	}
97}