-
Notifications
You must be signed in to change notification settings - Fork 1
/
Argument_helper.h
873 lines (754 loc) · 27.1 KB
/
Argument_helper.h
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
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
283
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
/*
*
* Argument Helper
*
* Daniel Russel drussel@alumni.princeton.edu
* Stanford University
*
*
* This software is not subject to copyright protection and is in the
* public domain. Neither Stanford nor the author assume any
* responsibility whatsoever for its use by other parties, and makes no
* guarantees, expressed or implied, about its quality, reliability, or
* any other characteristic.
*
*/
#ifndef _DSR_ARGS_H_
#define _DSR_ARGS_H_
#include <vector>
#include <hash_map>
#include <list>
#include <string>
#include <iostream>
#include <cstdlib>
#include <cstdio>
//#include <limits>
#include <cassert>
/*!
\mainpage A Simple C++ Argument Parser
This is a class to aid handling of command line arguments in a C++
program. It follows (and enforces) the unsual conventions.
New arguments are added by calling functions of the form of new_[optional_/named_]type.
- type is the type of the value for the argument (int, double, string, vector of strings...)
- optional means that the use doesn't have to supply it.
- named means that it is identified by following an "-c" or
"--long-name" identifier. All named arguments are optional.
Unnamed arguments are expected to appear in order of addition on
the command line. Named arguments can be passed in any order (and
mixed with the unnamed arguments).
The special argument "--" means that all remaining arguments are
treated as unnamed (so you can pass file names that begin with -).
When calling a new_foo function to create an argument the
following can/must be passed in
- For all arguments
+ The place to put the value.
+ A description of the value of the argument (i.e. it is a filename)
+ A description of the argument as a whole (i.e. it is the input file).
- For named arguments
+ A character for the short name.
+ A string for the long name.
When the program is called if "--help" is passed as an argument
the useage information is printed and the program exits.
There is always an implicit "-v" flag for verbose which sets the
dsr::verbose variable and a "-V" which sets the VERBOSE variable.
Any extra arguments or arguments with unexpected types are treated
as errors and cause the program to abort. Extra arguments can be
allowed for by adding a std::vector<std::string> to store them
using the "set_string_vector" function. All extra (unnamed)
arguments are placed there.
This software is not subject to copyright protection and is in the
public domain. Neither Stanford nor the author assume any
responsibility whatsoever for its use by other parties, and makes no
guarantees, expressed or implied, about its quality, reliability, or
any other characteristic.
An example using the class is:
\include argument_helper_example.cc
*/
namespace dsr{
extern bool verbose, VERBOSE;
//! A helper class for parsing command line arguments.
/*!
This is the only class you need to look at in order to use it.
*/
class Argument_helper{
private:
class Argument_target;
class FlagTarget;
class DoubleTarget;
class IntTarget;
class UIntTarget;
class StringTarget;
class CharTarget;
class StringVectorTarget;
public:
Argument_helper();
//! Toggle a boolean
void new_flag(const char *key, const char *long_name, const char *description,bool &dest);
//! add a string argument
void new_string( const char *arg_description, const char *description, std::string &dest);
//! add a string which must have a key.
void new_named_string(const char *key, const char *long_name,
const char *arg_description,
const char *description, std::string &dest);
//! Add an optional string-- any extra arguments are put in these.
void new_optional_string( const char *arg_description, const char *description, std::string &dest);
//! add an int
void new_int( const char *arg_description, const char *description, int &dest);
//! Add an int.
void new_named_int(const char *key, const char *long_name,const char *value_name,
const char *description,
int &dest);
//! Add an optional named int.
void new_optional_int(const char *value_name,
const char *description,
int &dest);
//! Add a named double.
void new_double(const char *value_name,
const char *description,
double &dest);
//! Add a named double.
void new_named_double(const char *key, const char *long_name,const char *value_name,
const char *description,
double &dest);
//! Add a named double.
void new_optional_double(const char *value_name,
const char *description,
double &dest);
//! Add an char.
void new_char(const char *value_name,
const char *description,
char &dest);
//! Add an optional char.
void new_named_char(const char *key, const char *long_name,const char *value_name,
const char *description,
char &dest);
//! Add an named char.
void new_optional_char(const char *value_name,
const char *description,
char &dest);
//! Add an unsigned int.
void new_unsigned_int(const char *value_name, const char *description,
unsigned int &dest);
//! Add an named unsigned int.
void new_optional_unsigned_int(const char *value_name, const char *description,
unsigned int &dest);
//! Add an optional named unsigned int.
void new_named_unsigned_int(const char *key, const char *long_name,
const char *value_name, const char *description,
unsigned int &dest);
//! add a target which takes a list of strings
/*!
Only named makes sense as the string vector default handles unnamed and optional.
*/
void new_named_string_vector(const char *key, const char *long_name,
const char *value_name, const char *description,
std::vector<std::string> &dest);
//! add a vector of strings.
/*! Any arguments which are not claimed by earlier unnamed
arguments or which are named are put here. This means you cannot
have a string vector followed by a string.
*/
void set_string_vector(const char *arg_description, const char *description, std::vector<std::string> &dest);
//! Set who wrote the program.
void set_author(const char *author);
//! Set what the program does.
void set_description(const char *descr);
//! Set what the version is.
void set_version(float v);
void set_version(const char *str);
//! Set the name of the program.
void set_name(const char *name);
//! Set when the program was built.
void set_build_date(const char *date);
//! Process the list of arguments and parse them.
/*!
This returns true if all the required arguments are there.
*/
void process(int argc, const char **argv);
void process(int argc, char **argv){
process(argc, const_cast<const char **>(argv));
}
//! Write how to call the program.
void write_usage(std::ostream &out) const;
//! Write the values of all the possible arguments.
void write_values(std::ostream &out) const;
~Argument_helper();
protected:
typedef hash_map<std::string, Argument_target*> SMap;
typedef hash_map<std::string, Argument_target*> LMap;
typedef std::vector<Argument_target*> UVect;
// A hash_map from short names to arguments.
SMap short_names_;
// A hash_map from long names to arguments.
LMap long_names_;
std::string author_;
std::string name_;
std::string description_;
std::string date_;
float version_;
bool seen_end_named_;
// List of unnamed arguments
std::vector<Argument_target*> unnamed_arguments_;
std::vector<Argument_target*> optional_unnamed_arguments_;
std::vector<Argument_target*> all_arguments_;
std::string extra_arguments_descr_;
std::string extra_arguments_arg_descr_;
std::vector<std::string> *extra_arguments_;
std::vector<Argument_target*>::iterator current_unnamed_;
std::vector<Argument_target*>::iterator current_optional_unnamed_;
void new_argument_target(Argument_target*);
void handle_error() const;
private:
Argument_helper(const Argument_helper &){};
const Argument_helper& operator=(const Argument_helper &){return *this;}
};
bool verbose=false, VERBOSE=false;
//////////////////////////////////////////////// Argument Targets
// This is a base class for representing one argument value.
/*
This is inherited by many classes and which represent the different types.
*/
class Argument_helper::Argument_target {
public:
std::string key;
std::string long_name;
std::string description;
std::string arg_description;
Argument_target(const std::string& k, const std::string& lname,
const std::string& descr,
const std::string& arg_descr) {
key=k;
long_name=lname;
description=descr;
arg_description=arg_descr;
}
Argument_target(const std::string& descr,
const std::string& arg_descr) {
key="";
long_name="";
description=descr;
arg_description=arg_descr;
}
virtual bool process(int &, const char **&)=0;
virtual void write_name(std::ostream &out) const;
virtual void write_value(std::ostream &out) const=0;
virtual void write_usage(std::ostream &out) const;
virtual ~Argument_target(){}
};
void Argument_helper::Argument_target::write_name(std::ostream &out) const {
if (key != "") out << '-' << key;
else if (!long_name.empty()) out << "--" << long_name;
else out << arg_description;
}
void Argument_helper::Argument_target::write_usage(std::ostream &out) const {
if (key != "") {
out << '-' << key;
out << "/--" << long_name;
}
out << ' ' << arg_description;
out << "\t" << description;
out << " Value: ";
write_value(out);
out << std::endl;
}
class Argument_helper::FlagTarget: public Argument_helper::Argument_target{
public:
bool &val;
FlagTarget(const char *k, const char *lname,
const char *descr,
bool &b): Argument_target(std::string(k), std::string(lname), std::string(descr),
std::string()), val(b){}
virtual bool process(int &, const char **&){
val= !val;
return true;
}
virtual void write_value(std::ostream &out) const {
out << val;
}
virtual void write_usage(std::ostream &out) const {
if (key != "") {
out << '-' << key;
out << "/--" << long_name;
}
out << "\t" << description;
out << " Value: ";
write_value(out);
out << std::endl;
}
virtual ~FlagTarget(){}
};
class Argument_helper::DoubleTarget: public Argument_target{
public:
double &val;
DoubleTarget(const char *k, const char *lname,
const char *arg_descr,
const char *descr, double &b): Argument_target(std::string(k), std::string(lname),
std::string(descr),
std::string(arg_descr)), val(b){}
DoubleTarget(const char *arg_descr,
const char *descr, double &b): Argument_target(std::string(descr),
std::string(arg_descr)), val(b){}
virtual bool process(int &argc, const char **&argv){
if (argc==0){
std::cerr << "Missing value for argument." << std::endl;
return false;
}
if (sscanf(argv[0], "%le", &val) ==1){
--argc;
++argv;
return true;
} else {
std::cerr << "Double not found at " << argv[0] << std::endl;
return false;
}
}
virtual void write_value(std::ostream &out) const {
out << val;
}
virtual ~DoubleTarget(){}
};
class Argument_helper::IntTarget: public Argument_target{
public:
int &val;
IntTarget(const char *arg_descr,
const char *descr, int &b): Argument_target(std::string(descr), std::string(arg_descr)),
val(b){}
IntTarget(const char *k, const char *lname,
const char *arg_descr,
const char *descr, int &b): Argument_target(std::string(k), std::string(lname),
std::string(descr),
std::string(arg_descr)),
val(b){}
virtual bool process(int &argc, const char **&argv){
if (argc==0){
std::cerr << "Missing value for argument." << std::endl;
return false;
}
if (sscanf(argv[0], "%d", &val) ==1){
--argc;
++argv;
return true;
} else {
std::cerr << "Integer not found at " << argv[0] << std::endl;
return false;
}
}
virtual void write_value(std::ostream &out) const {
out << val;
}
virtual ~IntTarget(){}
};
class Argument_helper::UIntTarget: public Argument_target{
public:
unsigned int &val;
UIntTarget(const char *arg_descr,
const char *descr, unsigned int &b): Argument_target(std::string(descr), std::string(arg_descr)),
val(b){}
UIntTarget(const char *k, const char *lname,
const char *arg_descr,
const char *descr, unsigned int &b): Argument_target(std::string(k), std::string(lname),
std::string(descr),
std::string(arg_descr)),
val(b){}
virtual bool process(int &argc, const char **&argv){
if (argc==0){
std::cerr << "Missing value for argument." << std::endl;
return false;
}
if (sscanf(argv[0], "%ud", &val) ==1){
--argc;
++argv;
return true;
} else {
std::cerr << "Unsigned integer not found at " << argv[0] << std::endl;
return false;
}
}
virtual void write_value(std::ostream &out) const {
out << val;
}
virtual ~UIntTarget(){}
};
class Argument_helper::CharTarget: public Argument_target{
public:
char &val;
CharTarget(const char *k, const char *lname,
const char *arg_descr,
const char *descr, char &b): Argument_target(std::string(k), std::string(lname),
std::string(descr),
std::string(arg_descr)), val(b){}
CharTarget(const char *arg_descr,
const char *descr, char &b): Argument_target(std::string(descr),
std::string(arg_descr)), val(b){}
virtual bool process(int &argc, const char **&argv){
if (argc==0){
std::cerr << "Missing value for argument." << std::endl;
return false;
}
if (sscanf(argv[0], "%c", &val) ==1){
--argc;
++argv;
return true;
} else {
std::cerr << "Character not found at " << argv[0] << std::endl;
return false;
}
}
virtual void write_value(std::ostream &out) const {
out << val;
}
virtual ~CharTarget(){}
};
class Argument_helper::StringTarget: public Argument_target{
public:
std::string &val;
StringTarget(const char *arg_descr,
const char *descr, std::string &b): Argument_target(std::string(descr), std::string(arg_descr)),
val(b){}
StringTarget(const char *k, const char *lname, const char *arg_descr,
const char *descr, std::string &b): Argument_target(std::string(k), std::string(lname),
std::string(descr), std::string(arg_descr)),
val(b){}
virtual bool process(int &argc, const char **&argv){
if (argc==0){
std::cerr << "Missing string argument." << std::endl;
return false;
}
val= argv[0];
--argc;
++argv;
return true;
}
virtual void write_value(std::ostream &out) const {
out << val;
}
virtual ~StringTarget(){}
};
class Argument_helper::StringVectorTarget: public Argument_target{
public:
std::vector<std::string> &val;
StringVectorTarget(const char *k, const char *lname, const char *arg_descr,
const char *descr, std::vector<std::string> &b): Argument_target(std::string(k), std::string(lname),
std::string(descr), std::string(arg_descr)),
val(b){}
virtual bool process(int &argc, const char **&argv){
while (argc >0 && argv[0][0] != '-'){
val.push_back(argv[0]);
--argc;
++argv;
}
return true;
}
virtual void write_value(std::ostream &out) const {
for (unsigned int i=0; i< val.size(); ++i){
out << val[i] << " ";
}
}
virtual ~StringVectorTarget(){}
};
//////////////////////////////////////////////// Argument_helper functions
Argument_helper::Argument_helper(){
author_="a programmer";
description_= "This program produces output.";
date_= "some day a long, long time ago.";
version_=-1;
extra_arguments_=NULL;
seen_end_named_=false;
new_flag("v", "verbose", "Whether to print extra information", verbose);
new_flag("V", "VERBOSE", "Whether to print lots of extra information", VERBOSE);
}
void Argument_helper::set_string_vector(const char *arg_description,
const char *description,
std::vector<std::string> &dest){
assert(extra_arguments_==NULL);
extra_arguments_descr_= description;
extra_arguments_arg_descr_= arg_description;
extra_arguments_= &dest;
}
void Argument_helper::set_author(const char *author){
author_=author;
}
void Argument_helper::set_description(const char *descr){
description_= descr;
}
void Argument_helper::set_name(const char *descr){
name_= descr;
}
void Argument_helper::set_version(float v){
version_=v;
}
void Argument_helper::set_version(const char *s){
version_=atof(s);
}
void Argument_helper::set_build_date(const char *date){
date_=date;
}
void Argument_helper::new_argument_target(Argument_target *t) {
assert(t!= NULL);
if (t->key != ""){
if (short_names_.find(t->key) != short_names_.end()){
std::cerr << "Two arguments are defined with the same string key, namely" << std::endl;
short_names_[t->key]->write_usage(std::cerr);
std::cerr << "\n and \n";
t->write_usage(std::cerr);
std::cerr << std::endl;
}
short_names_[t->key]= t;
}
if (!t->long_name.empty()){
if (long_names_.find(t->long_name) != long_names_.end()){
std::cerr << "Two arguments are defined with the same long key, namely" << std::endl;
long_names_[t->long_name]->write_usage(std::cerr);
std::cerr << "\n and \n";
t->write_usage(std::cerr);
std::cerr << std::endl;
}
long_names_[t->long_name]= t;
}
all_arguments_.push_back(t);
}
void Argument_helper::new_flag(const char *key, const char *long_name, const char *description,bool &dest){
Argument_target *t= new FlagTarget(key, long_name, description, dest);
new_argument_target(t);
};
void Argument_helper::new_string(const char *arg_description, const char *description,
std::string &dest){
Argument_target *t= new StringTarget(arg_description, description, dest);
unnamed_arguments_.push_back(t);
all_arguments_.push_back(t);
};
void Argument_helper::new_optional_string(const char *arg_description, const char *description,
std::string &dest){
Argument_target *t= new StringTarget(arg_description, description, dest);
optional_unnamed_arguments_.push_back(t);
};
void Argument_helper::new_named_string(const char *key, const char *long_name,
const char *arg_description, const char *description,
std::string &dest){
Argument_target *t= new StringTarget(key, long_name, arg_description, description, dest);
new_argument_target(t);
};
void Argument_helper::new_named_string_vector(const char *key, const char *long_name,
const char *arg_description, const char *description,
std::vector<std::string> &dest){
Argument_target *t= new StringVectorTarget(key, long_name, arg_description, description, dest);
new_argument_target(t);
};
void Argument_helper::new_int(const char *arg_description, const char *description,
int &dest){
Argument_target *t= new IntTarget(arg_description, description, dest);
unnamed_arguments_.push_back(t);
all_arguments_.push_back(t);
};
void Argument_helper::new_optional_int(const char *arg_description, const char *description,
int &dest){
Argument_target *t= new IntTarget(arg_description, description, dest);
optional_unnamed_arguments_.push_back(t);
};
void Argument_helper::new_named_int(const char *key, const char *long_name,
const char *arg_description, const char *description,
int &dest){
Argument_target *t= new IntTarget(key, long_name, arg_description, description, dest);
new_argument_target(t);
};
void Argument_helper::new_unsigned_int(const char *arg_description, const char *description,
unsigned int &dest){
Argument_target *t= new UIntTarget(arg_description, description, dest);
unnamed_arguments_.push_back(t);
all_arguments_.push_back(t);
};
void Argument_helper::new_optional_unsigned_int(const char *arg_description, const char *description,
unsigned int &dest){
Argument_target *t= new UIntTarget(arg_description, description, dest);
optional_unnamed_arguments_.push_back(t);
};
void Argument_helper::new_named_unsigned_int(const char *key, const char *long_name,
const char *arg_description, const char *description,
unsigned int &dest){
Argument_target *t= new UIntTarget(key, long_name, arg_description, description, dest);
new_argument_target(t);
};
void Argument_helper::new_double(const char *arg_description, const char *description,
double &dest){
Argument_target *t= new DoubleTarget(arg_description, description, dest);
unnamed_arguments_.push_back(t);
all_arguments_.push_back(t);
};
void Argument_helper::new_optional_double(const char *arg_description, const char *description,
double &dest){
Argument_target *t= new DoubleTarget(arg_description, description, dest);
optional_unnamed_arguments_.push_back(t);
};
void Argument_helper::new_named_double(const char *key, const char *long_name,
const char *arg_description, const char *description,
double &dest){
Argument_target *t= new DoubleTarget(key, long_name, arg_description, description, dest);
new_argument_target(t);
};
void Argument_helper::new_char(const char *arg_description, const char *description,
char &dest){
Argument_target *t= new CharTarget(arg_description, description, dest);
unnamed_arguments_.push_back(t);
all_arguments_.push_back(t);
};
void Argument_helper::new_optional_char(const char *arg_description, const char *description,
char &dest){
Argument_target *t= new CharTarget(arg_description, description, dest);
optional_unnamed_arguments_.push_back(t);
};
void Argument_helper::new_named_char(const char *key, const char *long_name,
const char *arg_description, const char *description,
char &dest){
Argument_target *t= new CharTarget(key, long_name, arg_description, description, dest);
new_argument_target(t);
};
void Argument_helper::write_usage(std::ostream &out) const {
out << name_ << " version " << version_ << ", by " << author_ << std::endl;
out << description_ << std::endl;
out << "Compiled on " << date_ << std::endl << std::endl;
out << "Usage: " << name_ << " ";
for (UVect::const_iterator it= unnamed_arguments_.begin(); it != unnamed_arguments_.end(); ++it){
(*it)->write_name(out);
out << " ";
}
for (UVect::const_iterator it= optional_unnamed_arguments_.begin();
it != optional_unnamed_arguments_.end(); ++it){
out << "[";
(*it)->write_name(out);
out << "] ";
}
if (extra_arguments_ != NULL) {
out << "[" << extra_arguments_arg_descr_ << "]";
}
out << std::endl << std::endl;
out << "All arguments:\n";
for (UVect::const_iterator it= unnamed_arguments_.begin(); it != unnamed_arguments_.end(); ++it){
(*it)->write_usage(out);
}
for (UVect::const_iterator it= optional_unnamed_arguments_.begin();
it != optional_unnamed_arguments_.end(); ++it){
(*it)->write_usage(out);
}
if (!extra_arguments_arg_descr_.empty()) {
out << extra_arguments_arg_descr_ << ": " << extra_arguments_descr_ << std::endl;
}
for (SMap::const_iterator it= short_names_.begin(); it != short_names_.end(); ++it){
(it->second)->write_usage(out);
}
}
void Argument_helper::write_values(std::ostream &out) const {
for (UVect::const_iterator it= unnamed_arguments_.begin(); it != unnamed_arguments_.end(); ++it){
out << (*it)->description;
out << ": ";
(*it)->write_value(out);
out << std::endl;
}
for (UVect::const_iterator it= optional_unnamed_arguments_.begin();
it != optional_unnamed_arguments_.end(); ++it){
out << (*it)->description;
out << ": ";
(*it)->write_value(out);
out << std::endl;
}
if (extra_arguments_!=NULL){
for (std::vector<std::string>::const_iterator it= extra_arguments_->begin();
it != extra_arguments_->end(); ++it){
out << *it << " ";
}
}
for (SMap::const_iterator it= short_names_.begin(); it != short_names_.end(); ++it){
out << it->second->description;
out << ": ";
it->second->write_value(out);
out << std::endl;
}
}
Argument_helper::~Argument_helper(){
for (std::vector<Argument_target*>::iterator it= all_arguments_.begin();
it != all_arguments_.end(); ++it){
delete *it;
}
}
void Argument_helper::process(int argc, const char **argv){
name_= argv[0];
++argv;
--argc;
current_unnamed_= unnamed_arguments_.begin();
current_optional_unnamed_= optional_unnamed_arguments_.begin();
for ( int i=0; i< argc; ++i){
if (strcmp(argv[i], "--help") == 0){
write_usage(std::cout);
exit(0);
}
}
while (argc != 0){
const char* cur_arg= argv[0];
if (cur_arg[0]=='-' && !seen_end_named_){
--argc; ++argv;
if (cur_arg[1]=='-'){
if (cur_arg[2] == '\0') {
//std::cout << "Ending flags " << std::endl;
seen_end_named_=true;
} else {
// long argument
LMap::iterator f= long_names_.find(cur_arg+2);
if ( f != long_names_.end()){
if (!f->second->process(argc, argv)) {
handle_error();
}
} else {
std::cerr<< "Invalid long argument "<< cur_arg << ".\n";
handle_error();
}
}
} else {
if (cur_arg[1]=='\0') {
std::cerr << "Invalid argument " << cur_arg << ".\n";
handle_error();
}
SMap::iterator f= short_names_.find(cur_arg+1);
if ( f != short_names_.end()){
if (!f->second->process(argc, argv)) {
handle_error();
}
} else {
std::cerr<< "Invalid short argument "<< cur_arg << ".\n";
handle_error();
}
}
} else {
if (current_unnamed_ != unnamed_arguments_.end()){
Argument_target *t= *current_unnamed_;
t->process(argc, argv);
++current_unnamed_;
} else if (current_optional_unnamed_ != optional_unnamed_arguments_.end()){
Argument_target *t= *current_optional_unnamed_;
t->process(argc, argv);
++current_optional_unnamed_;
} else if (extra_arguments_!= NULL){
extra_arguments_->push_back(cur_arg);
--argc;
++argv;
} else {
std::cerr << "Invalid extra argument " << argv[0] << std::endl;
handle_error();
}
}
}
if (current_unnamed_ != unnamed_arguments_.end()){
std::cerr << "Missing required arguments:" << std::endl;
for (; current_unnamed_ != unnamed_arguments_.end(); ++current_unnamed_){
(*current_unnamed_)->write_name(std::cerr);
std::cerr << std::endl;
}
std::cerr << std::endl;
handle_error();
}
if (VERBOSE) verbose=true;
}
void Argument_helper::handle_error() const {
write_usage(std::cerr);
exit(1);
}
}
#endif