JSDOC/Options.js
[gnome.introspection-doc-generator] / JSDOC / Options.js
1 //<script type="text/javascript">
2 /**
3  * Options management...
4  */
5 XObject = imports.XObject.XObject;
6 File = imports.File.File;
7
8 Options = {
9
10     // generic stuff...
11     "--help"          : 'Show help',   // was h
12     "help": false,   // was h;se
13     
14     
15     // configurable settings.. - 
16     "usage" : "Usage seed jsdocbuild.js OPTIONS \n",
17     
18     
19     // options get defined like this..
20     "--src"           :  "source directory (either absolute - starts with "/" or relative " + 
21                         "- without, in which case it's added to baseDir",
22     "--baseDir"       :  'Base Directory (root directory of code)',
23     "--target"        :  'Target Directory (where html files go)',
24     "--cacheDirectory": 'Cached Files Directory',
25     "--conf"          : 'Read From a Configuration file',       // was c. - configuration file.. - parsed with JSON.parse
26     "--template"      : 'Template Directory',   // was t.
27     // "recurse": false,   // was r. - not supported..
28     "--ext"           :  'Extension of code files to read (normally js)',   // was x.
29     "--publishExt"    : 'Extension of html files to write (normally html)',
30     //"private": '',   // was p
31     //"allfunctions": '',   // was a
32     //"encoding": '',   // was e.
33     //"nocode": '',  // was n
34     //"out": '',   // was o.
35     //"suppress": '',  // was s ??? used?
36     "--outputSource" : 'Output the Source code to symbols/src/* (boolean)',
37     //"testmode": '',  // was t
38     
39     "--verbose"       : 'Show verbose messages',   // was v
40     //"disablecache": '',   // was C -- not needed? - see if cacheDirectory was set..
41     //"define" : [],   // was D.
42     //"handler" : [],  // was H -- not supported..
43
44     
45     // and now the defaults.. (which type can be infered from..)
46     "src" : [],
47     "baseDir" :  '',  // base directory - 
48     "target" : '',   // was d. ?? source directory (needed to put temporary files..)
49     "cacheDirectory" : '',
50     "conf" : '',       // was c. - configuration file.. - parsed with JSON.parse
51     "template": '',   // was t.
52     // "recurse": false,   // was r. - not supported..
53     "ext": 'js',   // was x.
54     "publishExt" : 'html',
55     "private": '',   // was p
56     "allfunctions": '',   // was a
57     "encoding": '',   // was e.
58     "nocode": '',  // was n
59     "out": '',   // was o.
60     "suppress": '',  // was s ??? used?
61     "outputSource" : true,
62     "testmode": '',  // was t
63     
64     "verbose": '',   // was v
65     "disablecache": '',   // was C
66     "define" : [],   // was D.
67     "handler" : [],  // was H -- not supported..
68     LOG : {
69         warn : function(str) {
70             print("Warn: " +str );
71         },
72         inform : function(str) {
73             print("Inform: " +str );
74         },
75         close : function() { },
76         flush : function() { },
77         out: false,
78         warnings : [],
79         verbose : false    
80     },
81     init : function()
82     {
83         
84         if (this.help) {
85             this.showHelp();
86           
87         }
88         
89         // the reset of this is specific to JSDOC - and can not be moved to a generic handler..
90         
91          
92         this.LOG.verbose = this.verbose;
93         
94         if (!this.baseDir) { // should we set this to cwd?
95             throw {
96                 name: "ArgumentError", 
97                 message: "No baseDir specified" 
98             };
99         }
100         
101         // this is most likely to come from the command line..
102         if (this.conf) {
103             XObject.extend(this, JSON.parse(File.read(this.conf)));;
104         }
105         // help ?? -- usage..
106        
107         if (!this.src.length) {
108             throw {
109                 name: "ArgumentError", 
110                 message: "No source directories specified" 
111             };
112         }
113         // append full path to source directories.
114         var _this= this;
115         this.src.forEach(function(v, i) {
116             if (v[0] != '/') {
117                 this.src[i] = _this.baseDir + '/' + v;
118             }
119             if (File.isDirectory(this.src[i])) {
120                 throw {
121                     name: "ArgumentError", 
122                     message: "invalid Source Directory : " +  v;
123                 };
124             }
125         }
126         
127         
128         if (!this.template) {
129             throw {
130                 name: "ArgumentError", 
131                 message: "No template Directory specified" 
132             };
133         }
134          
135         if (!this.target) {
136             throw {
137                 name: "ArgumentError", 
138                 message: "No directory specified" 
139             };
140         }
141       
142         
143         // should cacheDirectory be a subdirectory of target??
144         // if not set..
145         //if (!this.cacheDirectory) {
146         //    throw {
147         //        name: "ArgumentError", 
148         //        message: "No cacheDirectory specified" 
149         //    };
150         // }
151         
152     },
153     /** 
154      *  this might be nice as a standard bit of code..
155      */
156        
157     parseArgv : function() 
158     {
159         
160         var args = Array.prototype.slice.call(Seed.argv);
161         args.shift(); //seed
162         args.shift(); // pack.js
163         
164         for(var i =0; i < args.length;i++) {
165             if (args[i].substring(0,2) != '--') {
166                 
167                 throw {
168                     name: "ArgumentError", 
169                     message: "Unknown argument: " + args[i] 
170                 };
171             }
172             var a = arg[i].substring(2);
173             if (typeof(argTypes[arg[i]]) == 'undefined') {
174                 throw {
175                     name: "ArgumentError", 
176                     message: "Unknown argument: " + args[i] 
177                 };
178             }
179             // type!!?!?
180             if (typeof(this[a]) == 'string') {
181                 this[a] = args[i+1];
182                 i++;
183                 continue;
184             }
185             if (typeof(this[a]) == 'boolean') {
186                 if (['false', 'true'].indexOf(args[i+1]) < 0) {
187                     throw {
188                         name: "ArgumentError", 
189                         message: "Unknown value for : " + args[i] + ' : ' +  args[i+1] 
190                     };
191                 }
192                 this[a] = args[i+1] == 'true';
193                 i++;
194                 continue;
195             }
196             if (typeof(this[a]) == 'object') { // tecnically an array.
197                 i++;
198                 while(i < args.length)
199                 {
200                     if (args[i].substring(0,2) != '--'){
201                         
202                         break;
203                     }
204                     this[a].push(args[i]);
205                 }
206                 i--;
207             }
208             throw {
209                 name: "ArgumentError", 
210                 message: "Do not know how to handle: " + a
211             };  
212         }
213         
214         
215     },
216     
217     
218     function showHelp()
219     {
220         print(this.usage);
221         for(var i in this) {
222             if (i.substring(0,2) != '--') {
223                 continue;
224             }
225             print( i + '  ARG  : ' + this[i]);
226             throw "DONE";
227         }
228     }
229 }