632a71a2 |
1 | #include "argument_reader.hh" |
2 | #include <stdio.h> |
3 | |
4 | /*! |
5 | *\brief Constructor. |
6 | * |
7 | * This constructor makes a new argument_reader ready to use. |
8 | *\arg app_name Name of the application as mentioned in the |
9 | * "Use: <appname> ..." help message. |
10 | */ |
11 | argument_reader::argument_reader(string app_name){ |
12 | progname=app_name; |
13 | } |
14 | |
15 | |
16 | /*! |
17 | *\brief Add a new parameter to be searched for. |
18 | *\param shortname A character for the short form. |
19 | * For example the 'h' in -h |
20 | *\param longname The double dash longname. For example |
21 | * "input_file=" in --input_file= or |
22 | * "ignore_errors" in --ignore--errors |
23 | *\param description A detailed parameter description. |
24 | *\param status Pointer to an integer. Will be set to 1 if arg found. |
25 | *\target Pointer to a value should be read. |
26 | * If no value is needed, pass NULL. |
27 | *\placeholder A placeholder for the documentation. |
28 | * For example "<filename>" in -f<filename> |
29 | */ |
30 | void argument_reader::add_param (string shortname, string longname, |
31 | string description, int * status, |
32 | string * target, string placeholder){ |
33 | |
34 | if(status!=NULL) arguments.insert(arguments.end(), |
35 | parameter(shortname,longname, description, |
36 | status,target,placeholder) |
37 | ); |
38 | } |
39 | |
40 | |
41 | /*! |
42 | *\brief Add an accepted argument to the argument reader. |
43 | *\param placeholder Something like "<input-file>". |
44 | *\param description Text describing the argument. |
45 | *\param status A pointer to a status variable. Will be set to 0 immediately. |
46 | * If the argument is filled in, it will be set to 1. |
47 | *\param target A pointer to a c++ string where the argument goes to. |
48 | * |
49 | *\note Arguments are filled in the order of adding them to the |
50 | * argument reader. |
51 | * There would be no other way to determine the order. |
52 | */ |
53 | void argument_reader::add_argument(string placeholder, string description, int * status, string * target){ |
54 | if (target!=NULL) if(status!=NULL) |
55 | free_arguments.insert(free_arguments.end(),free_parameter(placeholder,description,status,target)); |
56 | } |
57 | |
58 | |
59 | /*! |
60 | * Read in the args passed to main(). |
61 | *\returns empty vector on success or the error messages to be output. |
62 | */ |
63 | vector<string> argument_reader::read_args(int argc, char ** args){ |
64 | vector<string> result; |
65 | vector<string> argv; |
66 | for (char ** akt=args; *akt ;akt++) argv.insert(argv.end(),string(*akt)); |
67 | |
68 | unsigned int free_parms_count=0; |
69 | for (vector<string>::iterator akt_p=argv.begin()+1;akt_p<argv.end();akt_p++){ //Argument loop |
70 | |
71 | // Look for long argument |
72 | if ((akt_p->substr(0,2)=="--")&&(free_parms_count==0)){ |
73 | int found=0; |
74 | for (vector<parameter>::iterator parm_p=arguments.begin();parm_p<arguments.end();parm_p++){ |
75 | if (akt_p->substr(2,parm_p->longname.length())==parm_p->longname){ |
76 | found=1; |
77 | *(parm_p->status)=1; |
78 | if (parm_p->target){ |
79 | if (akt_p->length()>2+parm_p->longname.length()){ |
80 | *(parm_p->target)=akt_p->substr(2+parm_p->longname.length()); |
81 | |
82 | } else // Word not long enough |
83 | if (akt_p+1<argv.end()) { // If next word existend |
84 | *(parm_p->status)=1; |
85 | *(parm_p->target)=*(++akt_p); |
86 | } else { // No next word :-( |
87 | result.insert(result.end(), |
88 | "Parameter --"+parm_p->longname+parm_p->placeholder+" needs an Argument!"); |
89 | } |
90 | } // arg needed |
91 | } |
92 | } // search for loop |
93 | if (!found) result.insert(result.end(),"Unknown parameter: "+*akt_p); |
94 | } else { // No -- param, now look for switches |
95 | if (((*akt_p)[0]=='-')&&(free_parms_count==0)){ |
96 | int stop_char_loop=0; |
97 | for (unsigned int pos=1; pos<akt_p->length()&& !stop_char_loop ;pos++){ |
98 | int found=0; |
99 | for (vector<parameter>::iterator parm_p=arguments.begin();parm_p<arguments.end();parm_p++){ |
100 | if (parm_p->shortname[0]==(*akt_p)[pos]){ |
101 | found=1; |
102 | (*parm_p->status)=1; |
103 | if (parm_p->target){ // Need argument |
104 | if (akt_p->length()>pos+1){ |
105 | *(parm_p->target)=akt_p->substr(pos+1); |
106 | stop_char_loop=1; |
107 | } else { // Word not long enough |
108 | if (akt_p+1<argv.end()) { // If next word existend |
109 | *(parm_p->target)=*(++akt_p); |
110 | stop_char_loop=1; |
111 | } else { // No next word :-( |
112 | result.insert(result.end(), |
113 | "Parameter --"+parm_p->longname+parm_p->placeholder+" needs an Argument!"); |
114 | } |
115 | } |
116 | } // arg needed |
117 | } //if match |
118 | } //args loop |
119 | if (!found) result.insert(result.end(),"Unknown switch: "+akt_p->substr(pos,1)); |
120 | } // char loop |
121 | }// switch found |
122 | else{ // no switch found |
123 | if (free_parms_count<free_arguments.size()){ |
124 | *(free_arguments[free_parms_count].target)=*akt_p; |
125 | } |
126 | free_parms_count++; |
127 | } |
128 | } //looking for not -- args |
129 | } // argv loop |
130 | if (free_parms_count>free_arguments.size()) result.insert(result.begin(),"Too many arguments!"); |
131 | if (!result.empty()){ |
132 | result.insert(result.begin(),"Error!"); |
133 | result.insert(result.begin(),""); |
134 | } |
135 | // if (!result.empty()){ |
136 | // get_help(result); |
137 | // } |
138 | return result; |
139 | } |
140 | |
141 | /*! |
142 | *\brief Generate help. |
143 | *\arg target Reference to a vector<string> to which lots of helpful |
144 | * strings are appended. |
145 | */ |
146 | void argument_reader::get_help(vector<string> & result){ |
147 | |
148 | result.insert(result.end(),""); |
149 | string line="Usage: "+progname; |
150 | for (vector<parameter>::iterator parm_p=arguments.begin();parm_p<arguments.end();parm_p++){ |
151 | string addstr=" [-"+parm_p->shortname; |
152 | if (parm_p->target!=0) addstr+=parm_p->placeholder; |
153 | addstr+="]"; |
154 | if (line.length()+addstr.length()>79){ |
155 | result.insert(result.end(),line); |
156 | line=string(7+progname.length(),' '); |
157 | } |
158 | line+=addstr; |
159 | } |
160 | |
161 | for (vector<free_parameter>::iterator parm_p=free_arguments.begin();parm_p<free_arguments.end();parm_p++){ |
162 | if (line.length()+parm_p->placeholder.length()>79){ |
163 | result.insert(result.end(),line); |
164 | line=string(7+progname.length(),' '); |
165 | } |
166 | line+=" ["+parm_p->placeholder+"]"; |
167 | } |
168 | result.insert(result.end(),line); |
169 | |
170 | /*******************************/ |
171 | |
172 | vector<string> left,right; |
173 | for (vector<parameter>::iterator parm_p=arguments.begin();parm_p<arguments.end();parm_p++){ |
174 | line=parm_p->description; |
175 | string st2=" -"+parm_p->shortname; |
176 | if (parm_p->target)st2+=" "+parm_p->placeholder; |
177 | st2+=", --"+parm_p->longname; |
178 | if (parm_p->target)st2+=parm_p->placeholder; |
179 | left.insert(left.end(),st2); |
180 | right.insert(right.end(),line); |
181 | } |
182 | |
183 | for (vector<free_parameter>::iterator parm_p=free_arguments.begin();parm_p<free_arguments.end();parm_p++){ |
184 | string st2; |
185 | line=parm_p->description; |
186 | st2+=" "+parm_p->placeholder; |
187 | left.insert(left.end(),st2); |
188 | right.insert(right.end(),line); |
189 | } |
190 | |
191 | if (arguments.size()){ |
192 | result.insert(result.end(),""); |
193 | result.insert(result.end(),"Options:"); |
194 | } |
195 | |
196 | unsigned int max_width=0; |
197 | for (unsigned int c=0; c<arguments.size();c++) |
198 | if(left[c].length()>max_width) max_width=left[c].length(); |
199 | for (unsigned int c=0; c<arguments.size();c++){ |
200 | string nl(max_width,' '); |
201 | nl.replace(0,left[c].length(),left[c]); |
202 | nl+=" "+right[c]; |
203 | while (nl.length()>80){ // Too long??? |
204 | int limit=nl.find_last_of(' ',80); |
205 | printf("limit:%i\n",limit); |
206 | result.insert(result.end(),nl.substr(0,limit)); |
207 | nl=string(max_width+2,' ')+nl.substr(limit+1); |
208 | } |
209 | result.insert(result.end(),nl); |
210 | } |
211 | |
212 | if (free_arguments.size()){ |
213 | result.insert(result.end(),""); |
214 | result.insert(result.end(),"Arguments:"); |
215 | } |
216 | |
217 | max_width=0; |
218 | for (unsigned int c=arguments.size(); c<arguments.size()+free_arguments.size();c++) |
219 | if(left[c].length()>max_width) max_width=left[c].length(); |
220 | for (unsigned int c=arguments.size(); c<arguments.size()+free_arguments.size();c++){ |
221 | string nl(max_width,' '); |
222 | nl.replace(0,left[c].length(),left[c]); |
223 | nl+=" "+right[c]; |
224 | |
225 | while (nl.length()>80){ // Too long??? |
226 | int limit=nl.find_last_of(' ',80); |
227 | printf("limit:%i\n",limit); |
228 | result.insert(result.end(),nl.substr(0,limit)); |
229 | nl=string(max_width+2,' ')+nl.substr(limit+1); |
230 | } |
231 | |
232 | result.insert(result.end(),nl); |
233 | } |
234 | result.insert(result.end(),""); |
235 | } |
236 | |
237 | /*! |
238 | *\brief Generate help. |
239 | *\return A vector containing many helpful strings for the user. |
240 | */ |
241 | vector<string> argument_reader::get_help(){ |
242 | vector<string> result; |
243 | get_help(result); |
244 | return result; |
245 | } |
246 | |
247 | |
248 | /**************************************************/ |
249 | |
250 | argument_reader::parameter::parameter(string n_shortname, string n_longname,string n_description, int * n_status, |
251 | string * n_target, string n_placeholder){ |
252 | shortname=n_shortname; |
253 | longname=n_longname; |
254 | description=n_description; |
255 | status=n_status; |
256 | target=n_target; |
257 | placeholder=n_placeholder; |
258 | if (status) *status=0; |
259 | } |
260 | |
261 | argument_reader::free_parameter::free_parameter( string n_placeholder, string n_description, |
262 | int * n_status, string * n_target){ |
263 | description=n_description; |
264 | status=n_status; |
265 | target=n_target; |
266 | placeholder=n_placeholder; |
267 | if (status) *status=0; |
268 | } |