e927a37c95f997b7d3421884598385f909aadd5d
1 #include "argument_reader.hh"
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.
11 argument_reader::argument_reader(string app_name
){
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>
30 void argument_reader::add_param (string shortname
, string longname
,
31 string description
, int * status
,
32 string
* target
, string placeholder
){
34 if(status
!=NULL
) arguments
.insert(arguments
.end(),
35 parameter(shortname
,longname
, description
,
36 status
,target
,placeholder
)
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.
49 *\note Arguments are filled in the order of adding them to the
51 * There would be no other way to determine the order.
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
));
60 * Read in the args passed to main().
61 *\returns empty vector on success or the error messages to be output.
63 vector
<string
> argument_reader::read_args(int argc
, char ** args
){
64 vector
<string
> result
;
66 for (char ** akt
=args
; *akt
;akt
++) argv
.insert(argv
.end(),string(*akt
));
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
71 // Look for long argument
72 if ((akt_p
->substr(0,2)=="--")&&(free_parms_count
==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
){
79 if (akt_p
->length()>2+parm_p
->longname
.length()){
80 *(parm_p
->target
)=akt_p
->substr(2+parm_p
->longname
.length());
82 } else // Word not long enough
83 if (akt_p
+1<argv
.end()) { // If next word existend
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!");
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)){
97 for (unsigned int pos
=1; pos
<akt_p
->length()&& !stop_char_loop
;pos
++){
99 for (vector
<parameter
>::iterator parm_p
=arguments
.begin();parm_p
<arguments
.end();parm_p
++){
100 if (parm_p
->shortname
[0]==(*akt_p
)[pos
]){
103 if (parm_p
->target
){ // Need argument
104 if (akt_p
->length()>pos
+1){
105 *(parm_p
->target
)=akt_p
->substr(pos
+1);
107 } else { // Word not long enough
108 if (akt_p
+1<argv
.end()) { // If next word existend
109 *(parm_p
->target
)=*(++akt_p
);
111 } else { // No next word :-(
112 result
.insert(result
.end(),
113 "Parameter --"+parm_p
->longname
+parm_p
->placeholder
+" needs an Argument!");
119 if (!found
) result
.insert(result
.end(),"Unknown switch: "+akt_p
->substr(pos
,1));
122 else{ // no switch found
123 if (free_parms_count
<free_arguments
.size()){
124 *(free_arguments
[free_parms_count
].target
)=*akt_p
;
128 } //looking for not -- args
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(),"");
135 // if (!result.empty()){
142 *\brief Generate help.
143 *\arg target Reference to a vector<string> to which lots of helpful
144 * strings are appended.
146 void argument_reader::get_help(vector
<string
> & result
){
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
;
154 if (line
.length()+addstr
.length()>79){
155 result
.insert(result
.end(),line
);
156 line
=string(7+progname
.length(),' ');
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(),' ');
166 line
+=" ["+parm_p
->placeholder
+"]";
168 result
.insert(result
.end(),line
);
170 /*******************************/
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
);
183 for (vector
<free_parameter
>::iterator parm_p
=free_arguments
.begin();parm_p
<free_arguments
.end();parm_p
++){
185 line
=parm_p
->description
;
186 st2
+=" "+parm_p
->placeholder
;
187 left
.insert(left
.end(),st2
);
188 right
.insert(right
.end(),line
);
191 if (arguments
.size()){
192 result
.insert(result
.end(),"");
193 result
.insert(result
.end(),"Options:");
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
]);
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);
209 result
.insert(result
.end(),nl
);
212 if (free_arguments
.size()){
213 result
.insert(result
.end(),"");
214 result
.insert(result
.end(),"Arguments:");
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
]);
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);
232 result
.insert(result
.end(),nl
);
234 result
.insert(result
.end(),"");
238 *\brief Generate help.
239 *\return A vector containing many helpful strings for the user.
241 vector
<string
> argument_reader::get_help(){
242 vector
<string
> result
;
248 /**************************************************/
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
;
254 description
=n_description
;
257 placeholder
=n_placeholder
;
258 if (status
) *status
=0;
261 argument_reader::free_parameter::free_parameter( string n_placeholder
, string n_description
,
262 int * n_status
, string
* n_target
){
263 description
=n_description
;
266 placeholder
=n_placeholder
;
267 if (status
) *status
=0;