1 //<script type="text/javascript">
3 XObject = import 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 * { short : 'a', long: 'test', description : 'a test', flag_array: true , flag_boolean : false}
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);
31 * @cfg {String} help_description what appears at the start of the help message.
35 * @cfg {Boolean} If extra arguments are allowed.
39 * @param {Object} Resulting key/value of data.
43 * @prop {Array} the arguments
47 * @prop {Array} extra the arguments that are not accounted for.
52 * usage: options.parse(Seed.argv)
54 parse: function (argv)
57 this.options.forEach(function(a) {
59 _this.result[a.name] = false;
63 _this.result[a.name] = [];
69 var args = Array.prototype.slice.call(Seed.argv);
71 args.shift(); //script..
73 for(var i =0; i < args.length;i++) {
75 var a= this.findArg(args[i]);
78 if (!this.allow_extra) {
80 name: "ArgumentError",
81 message: "Unknown argument: " + args[i]
84 this.extra.push(args[i]);
87 if (a.long == 'help' ) {
93 this.result[a.long] = true;
97 if (this.findArg(next)) {
99 name: "ArgumentError",
100 message: "Invalid argument sequence: " + args[i] + ' ' + next;
103 // type juggling -- fixme...
106 this.result[a.long].push(next);
111 if (typeof(this.result[a.long]) != 'undefined') {
113 name: "ArgumentError",
114 message: "Invalid argument duplicate: " + args[i] + ' ' + next;
118 this.result[a.long] = next;
127 findArg : function(str)
130 var type = str.substring(0,1) == '-' ? 'short' : '';
131 type = str.substring(0,2) == '--' ? 'long' : type;
133 return false; // not an arg..
135 var match = str.substring(type =='long' ? 2 , 1);
136 this.options.forEach(function(o)) {
138 return; // no more parsing.
140 if (o[type] == 'match') {
152 print(this.help_description);
153 this.options.forEach(function(o) {
154 // fixme needs tidying up..
155 print(' --' + o.long + ' (-' + o.short + ') ' + o.description);