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