1 //<script type="text/javascript">
3 XObject = imports.XObject.XObject
6 * Options parsing is needed as GLib's built in GOptionGroup is not feasible to use in an introspected way at present.
11 * help_desription : 'Help about this',
13 * { arg_short : 'a', arg_long: 'test', description : 'a test', flag_array: true , flag_boolean : false, arg_default: 'fred'}
16 * cfg = o.parse(Seed.argv);
18 * Currently only supports simple parsing
19 * eg. --longarg test -b test
21 * Does not support --aaaaa=bbbb
23 Options = XObject.define(
25 options = []; // default.
26 XObject.extend(this,cfg);
27 if (this.help_description.length) {
28 this.options.push({ arg_short : 'h', arg_long: 'help', description : 'Show Help', flag_boolean : true});
35 * @cfg {String} help_description what appears at the start of the help message.
39 * @cfg {Boolean} If extra arguments are allowed.
43 * @param {Object} Resulting key/value of data.
47 * @prop {Array} the arguments
51 * @prop {Array} extra the arguments that are not accounted for.
56 * usage: options.parse(Seed.argv)
58 parse: function (argv)
62 this.options.forEach(function(a) {
64 if (typeof(a.arg_default) != 'undefined') {
65 _this.result[a.arg_long] = a.arg_default;
70 _this.result[a.arg_long] = false;
74 _this.result[a.arg_long] = [];
81 var args = Array.prototype.slice.call(Seed.argv);
83 args.shift(); //script..
85 for(var i =0; i < args.length;i++) {
87 var a= this.findArg(args[i]);
90 if (!this.allow_extra) {
92 name: "ArgumentError",
93 message: "Unknown argument: " + args[i]
96 this.extra.push(args[i]);
99 if (a.arg_long == 'help' ) {
100 print("Help Message Requested");
106 if (a.flag_boolean) {
107 this.result[a.arg_long] = true;
110 var next = args[i+1];
111 if (this.findArg(next)) {
113 name: "ArgumentError",
114 message: "Invalid argument sequence: " + args[i] + ' ' + next
117 // type juggling -- fixme...
120 this.result[a.arg_long].push(next);
125 if (typeof(this.result[a.arg_long]) != 'undefined') {
127 name: "ArgumentError",
128 message: "Invalid argument duplicate: " + args[i] + ' ' + next
132 this.result[a.arg_long] = next;
135 // validate results..
136 this.options.forEach(function(a) {
137 if (typeof(a.arg_default) != 'undefined') {
140 if (a.flag_boolean) {
143 if (typeof(_this.result[a.arg_long]) != 'undefined') {
148 name: "ArgumentError",
149 message: "Missing Argument: --" + a.arg_long
158 findArg : function(str)
161 var type = str.substring(0,1) == '-' ? 'arg_short' : '';
162 type = str.substring(0,2) == '--' ? 'arg_long' : type;
164 return false; // not an arg..
166 var match = str.substring(type =='arg_long' ? 2 : 1);
168 this.options.forEach(function(o) {
170 return; // no more parsing.
172 if (o[type] == match) {
184 print(this.help_description);
185 this.options.forEach(function(o) {
186 // fixme needs tidying up..
187 print(' --' + o.arg_long + ' (-' + o.arg_short + ') ' + o.description);