Blame view

ui/arguments.h 9.08 KB
7a2d0012   David Mayerich   mirst1d updates
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  #ifndef RTS_ARGUMENTS
  #define RTS_ARGUMENTS
  
  #include <string>
  #include <vector>
  #include <iostream>
  #include <iomanip>
  #include <sstream>
  #include <iterator>
  #include <algorithm>
  
  #ifdef _WIN32
  #include <Windows.h>
  #endif
  
  namespace rts{
  
  	class argument
  	{
  	private:
  		bool ansi;
  
  		//argument name
  		std::string name;
  
  		//description of the argument
  		std::vector<std::string> desc;
  
  		//argument values
3b012a80   David Mayerich   added code for co...
30
31
32
33
34
35
  		std::vector<std::string> vals;
  
  		//range or example
  		std::string range;
  
  		//flag is true when the argument is user-specified
7a2d0012   David Mayerich   mirst1d updates
36
37
38
39
  		bool flag;
  
  		void parse_val(const std::string &s){
  
3b012a80   David Mayerich   added code for co...
40
41
  			vals.clear();
  
7a2d0012   David Mayerich   mirst1d updates
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  			std::stringstream ss(s);
  			std::string item;
  			while (std::getline(ss, item, ' ')) {
  				vals.push_back(item);
  			}
  		}
  
  		void parse_desc(const std::string &s){
  
  			desc.clear();
  
  			std::stringstream ss(s);
  			std::string item;
  			while (std::getline(ss, item, '\n')) {
  				desc.push_back(item);
  			}
  		}
  
3b012a80   David Mayerich   added code for co...
60
61
  	public:
  		void set_ansi(bool b){ ansi = b; }
7a2d0012   David Mayerich   mirst1d updates
62
63
64
65
66
          //create an argument with a given name, description, and default value
  		argument(std::string _name, std::string _desc, std::string _default = "", std::string _range = "")
  		{
  			name = _name;
  			parse_desc(_desc);
3b012a80   David Mayerich   added code for co...
67
68
69
70
71
72
73
74
75
  			parse_val(_default);
  
  			//if a default value is provided, set the flag
  			if(_default != "")
                  flag = true;
              else flag = false;
  
  			range = _range;
  
7a2d0012   David Mayerich   mirst1d updates
76
  
3b012a80   David Mayerich   added code for co...
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
  		}
  
  		int nargs()
  		{
              return vals.size();
          }
  
  		//return the value of a text argument
  		std::string as_text(int n = 0)
  		{
              if(!flag)
              {
                  std::cout<<"ERROR - Argument requested without being set: "<<name<<std::endl;
                  exit(1);
              }
  
              if(vals.size() > n)
                  return vals[n];
  
              else return "";
  		}
  
          //return the value of a floating point argument
  		float as_float(int n = 0)
  		{
              if(!flag)
              {
                  std::cout<<"ERROR - Argument requested without being set: "<<name<<std::endl;
                  exit(1);
              }
  
              if(vals.size() > n)
              {
                  float r;
                  if ( ! (istringstream(vals[n]) >> r) ) r = 0;
                  return r;
              }
  
              else return 0;
  		}
  
  		//return the value of an integer argument
  		int as_int(int n = 0)
  		{
              if(!flag)
              {
                  std::cout<<"ERROR - Argument requested without being set: "<<name<<std::endl;
                  exit(1);
              }
  
              if(vals.size() > n)
              {
                  int r;
                  if ( ! (istringstream(vals[n]) >> r) ) r = 0;
                  return r;
              }
  
              else return 0;
  		}
  
  		//get the width of the left column
  		int col_width()
  		{
              int n = 3;
              //add the length of the argument name
              n += name.size();
  
              //if there are any default parameters
              if(vals.size() > 0)
              {
                  //padding (parenthesis, =, etc.)
                  n += 6;
  
                  //for each default argument value
                  for(int v=0; v<vals.size(); v++)
                      n += vals[v].size() + 1;
              }
  
              //add a buffer of 4 characters
              n += 4;
  
              return n;
7a2d0012   David Mayerich   mirst1d updates
159
160
161
162
163
164
  		}
  
  
  		//string output
  		std::string toStr(int width = 0)
  		{
3b012a80   David Mayerich   added code for co...
165
166
167
168
  			std::stringstream ss;
  
  			int color_size = 0;
  
7a2d0012   David Mayerich   mirst1d updates
169
170
171
172
  
  			//create the left column
  			std::string left_part = std::string(" --") + name;
  			if(vals.size() != 0)
3b012a80   David Mayerich   added code for co...
173
174
175
  			{
  				if(ansi)
  					left_part += "\033[1;32m";
7a2d0012   David Mayerich   mirst1d updates
176
177
178
  				left_part += " ( = ";
  				for(int v=0; v<vals.size(); v++)
  					left_part += vals[v] + std::string(" ");
3b012a80   David Mayerich   added code for co...
179
180
181
182
183
184
185
  				left_part += ")";
  				if(ansi)
  					left_part += "\033[0m";
  				if(ansi)
  					color_size = 11;
  			}
  			else
7a2d0012   David Mayerich   mirst1d updates
186
187
188
                  color_size = 0;
  
  			//if no width is passed, put 4 spaces between left and right columns
3b012a80   David Mayerich   added code for co...
189
  			if(width == 0) width = col_width();
7a2d0012   David Mayerich   mirst1d updates
190
191
192
193
194
195
196
197
  
  			ss<<std::left<<std::setw(width + color_size)<<left_part;
  
  			//output right column
  			for(int d=0; d<desc.size(); d++)
  			{
  				if(d == 0)
  					ss<<desc[0];
3b012a80   David Mayerich   added code for co...
198
199
  				else
  					ss<<std::endl<<setfill(' ')<<setw(width)<<" "<<desc[d];
7a2d0012   David Mayerich   mirst1d updates
200
  
3b012a80   David Mayerich   added code for co...
201
202
203
204
  			}
  
  			//output the range in the right column
  			if(range != "" && ansi)
7a2d0012   David Mayerich   mirst1d updates
205
206
207
208
209
                  ss<<std::endl<<setfill(' ')<<setw(width)<<" "<<"    "<<std::string("\033[1;36m") + range + "\033[0m";
  			else if(range != "")
  				ss<<std::endl<<setfill(' ')<<setw(width)<<" "<<"    "<<range;
  
  			return ss.str();
3b012a80   David Mayerich   added code for co...
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
  		}
  
  		//compare the name of the argument to a string
  		bool operator==(std::string rhs)
  		{
              return (name == rhs);
  		}
  
  		//set the argument to a given value
  		void set(std::string _value)
  		{
              parse_val(_value);
  
              //set the flag
              flag = true;
  		}
  
  		bool is_set()
  		{
              return flag;
7a2d0012   David Mayerich   mirst1d updates
230
231
          }
  
3b012a80   David Mayerich   added code for co...
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
  	};
  
  	struct argsection
  	{
  		std::string name;
  		unsigned int index;
  	};
  
  	class arglist
  	{
      private:
  		bool ansi;
  
  		//vector of arguments
          std::vector<argument> args;
  
  		//column width of the longest argument
          int col_width;
  
  		//list of sections
  		std::vector<argsection> sections;
  
      public:
  
          arglist(){ col_width = 0; }
  
  		void set_ansi(bool b)
  		{
  			ansi = b;
  			for(int i=0; i<args.size(); i++)
  				args[i].set_ansi(ansi);
  		}
  
          void add(std::string _name, std::string _desc, std::string _default = "", std::string _range = "")
          {
              argument arg(_name, _desc, _default, _range);
  			arg.set_ansi(ansi);
              args.push_back(arg);
  
              col_width = std::max<int>(col_width, arg.col_width());
          }
  
  		void section(std::string _name)
  		{
  			argsection s;
  			s.name = _name;
  			s.index = args.size();
  			sections.push_back(s);
  		}
  
          //output the arguments (generally in response to --help)
7a2d0012   David Mayerich   mirst1d updates
283
          std::string str()
3b012a80   David Mayerich   added code for co...
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
          {
              std::stringstream ss;
  
              int si = -1;
  
              if(sections.size() > 0)
                  si = 0;
  
              //for each argument
              for(int a=0; a<args.size(); a++)
              {
                  if(si != -1 && a == sections[si].index)
                  {
  					if(ansi)
  						ss<<std::endl<<std::left<<setfill('=')<<setw(col_width)<<std::string("\033[1;31m") + sections[si].name<<"\033[0m"<<std::endl;
  					else
  						ss<<std::endl<<std::left<<setfill('=')<<setw(col_width)<<sections[si].name<<std::endl;
                      si++;
                      if(si == sections.size()) si = -1;
                  }
  
                  ss<<args[a].toStr(col_width)<<std::endl;
              }
  
              return ss.str();
          }
  
          int index(std::string _name)
          {
              int i = find(args.begin(), args.end(), _name) - args.begin();
  
              if(i >= args.size())
                  i = -1;
  
              return i;
          }
  
          void set(std::string _name, std::string _value)
          {
              int i = index(_name);
  
              if(i != -1)
              {
                  args[i].set(_value);
                  //adjust the column width if necessary
                  col_width = max(col_width, args[i].col_width());
              }
              else
                  std::cout<<"ERROR - Argument not recognized: "<<_name<<std::endl;
          }
  
          //parse a parameter string
          void parse(int argc, char* argv[])
          {
              //if the number of arguments is 1, we're done
              if(argc <= 1) return;
  
              std::string name;
              std::string params;
  
              for(int i=1; i<argc; i++)
              {
                  //if the argument is a parameter name
                  if(argv[i][0] == '-' && argv[i][1] == '-')
                  {
                      //add any previous arguments
                      if(name != "")
                          set(name, params);
                      //set the current argument to this name
                      name = argv[i]+2;
                      //clear the parameters list
                      params = "";
                  }
                  else
                  {
  					if(params != "")
  						params += " ";
                      params += argv[i];
                  }
              }
  
              //set the last argument
              set(name, params);
          }
  
          //determine if a parameter has been set (either specified by the user or with a default value)
          bool operator()(std::string _name)
          {
              int i = find(args.begin(), args.end(), _name) - args.begin();
  
              if(i < 0)
              {
                  std::cout<<"ERROR - Unspecified parameter name: "<<_name<<std::endl;
                  exit(1);
              }
  
              return args[i].is_set();
          }
  
          int nargs(std::string _name)
          {
              int i = find(args.begin(), args.end(), _name) - args.begin();
  
              if(i < 0)
              {
                  std::cout<<"ERROR - Unspecified parameter name: "<<_name<<std::endl;
                  exit(1);
              }
  
              return args[i].nargs();
          }
  
          argument operator[](std::string _name)
          {
              int i = find(args.begin(), args.end(), _name) - args.begin();
  
              if(i < 0)
              {
                  std::cout<<"ERROR - Unspecified parameter name: "<<_name<<std::endl;
                  exit(1);
              }
  
              return args[i];
          }
  
  
7a2d0012   David Mayerich   mirst1d updates
410
411
412
413
  	};
  
  
  
3b012a80   David Mayerich   added code for co...
414
415
416
  }	//end namespace rts
  
  #endif