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