1 |
16 |
gryzor |
/*
|
2 |
|
|
(c) Jose Tejada Gomez, 9th May 2013
|
3 |
|
|
You can use this file following the GNU GENERAL PUBLIC LICENSE version 3
|
4 |
|
|
Read the details of the license in:
|
5 |
|
|
http://www.gnu.org/licenses/gpl.txt
|
6 |
|
|
|
7 |
|
|
Send comments to: jose.tejada at ieee.org
|
8 |
|
|
|
9 |
|
|
*/
|
10 |
|
|
|
11 |
|
|
#include <string>
|
12 |
|
|
#include <vector>
|
13 |
|
|
#include <algorithm>
|
14 |
|
|
#include <cstdlib>
|
15 |
|
|
#include <iostream>
|
16 |
|
|
|
17 |
|
|
typedef std::vector<struct argument_t*> arg_vector_t;
|
18 |
|
|
|
19 |
|
|
struct argument_t {
|
20 |
|
|
std::string short_name, long_name;
|
21 |
17 |
gryzor |
typedef enum { integer, text, flag, real } arg_type;
|
22 |
16 |
gryzor |
arg_type type;
|
23 |
|
|
std::string description;
|
24 |
|
|
// possible states
|
25 |
|
|
int integer_value;
|
26 |
|
|
std::string string_value;
|
27 |
17 |
gryzor |
float real_value;
|
28 |
16 |
gryzor |
bool state, req;
|
29 |
|
|
|
30 |
|
|
argument_t( arg_vector_t& av, const char* long_name, arg_type type,
|
31 |
17 |
gryzor |
const char* desc="", bool required=false )
|
32 |
16 |
gryzor |
: long_name(long_name), type( type ), description( desc ),
|
33 |
17 |
gryzor |
req(required), state(false), integer_value(0), real_value(0)
|
34 |
16 |
gryzor |
{
|
35 |
|
|
if( !this->long_name.empty() ) {
|
36 |
|
|
this->short_name = "-" + this->long_name.substr(0,1);
|
37 |
|
|
this->long_name = "--" + this->long_name;
|
38 |
|
|
}
|
39 |
|
|
av.push_back(this);
|
40 |
|
|
}
|
41 |
|
|
void set() { state=true; }
|
42 |
|
|
bool is_set() { return state; }
|
43 |
|
|
};
|
44 |
|
|
|
45 |
|
|
class Args {
|
46 |
|
|
arg_vector_t& legal_args;
|
47 |
|
|
argument_t* def_arg;
|
48 |
|
|
std::string program_name;
|
49 |
|
|
argument_t help_arg;
|
50 |
18 |
gryzor |
void clean_args() {
|
51 |
|
|
for( int j=0; j<legal_args.size(); j++ ) {
|
52 |
|
|
argument_t& a = *legal_args[j];
|
53 |
|
|
if( a.short_name=="-h" && a.long_name!="help" )
|
54 |
|
|
{ help_arg.short_name.clear(); break; } // remove -h for help if already used
|
55 |
|
|
}
|
56 |
|
|
}
|
57 |
|
|
void throw_error( std::string x ) /*throw const char**/ { throw x.c_str(); }
|
58 |
16 |
gryzor |
public:
|
59 |
|
|
Args( int argc, char *argv[], arg_vector_t& legal_args ) //throw const char *
|
60 |
|
|
: legal_args( legal_args ),
|
61 |
|
|
help_arg( legal_args, "help", argument_t::flag, "Display usage information")
|
62 |
18 |
gryzor |
{
|
63 |
|
|
clean_args(); // eliminate duplicated values
|
64 |
16 |
gryzor |
// look for default argument
|
65 |
|
|
def_arg=NULL;
|
66 |
|
|
for( int j=0; j<legal_args.size(); j++ ) {
|
67 |
|
|
if ( legal_args[j]->short_name.empty() && legal_args[j]->long_name.empty() )
|
68 |
|
|
if( def_arg==NULL ) def_arg = legal_args[j];
|
69 |
|
|
else throw "Cannot set more than one default argument.";
|
70 |
|
|
}
|
71 |
|
|
if( def_arg && def_arg->type!=argument_t::integer && def_arg->type!=argument_t::text )
|
72 |
|
|
throw "Default arguments can only be of type integer or text";
|
73 |
|
|
|
74 |
|
|
program_name = argv[0];
|
75 |
|
|
for( int k=1; k<argc; k++ ) {
|
76 |
|
|
bool matched=false;
|
77 |
|
|
for( int j=0; j<legal_args.size(); j++ ) {
|
78 |
|
|
argument_t& a = *legal_args[j];
|
79 |
|
|
if( a.long_name==argv[k] || a.short_name==argv[k] ) {
|
80 |
|
|
if( a.type == argument_t::flag ) { a.set(); matched=true; continue; }
|
81 |
|
|
if( a.type == argument_t::text ) {
|
82 |
|
|
k++;
|
83 |
|
|
if( k>=argc ) throw_error("Expecting input after "+a.long_name+" param");
|
84 |
|
|
a.string_value = argv[k];
|
85 |
|
|
a.set();
|
86 |
|
|
matched=true;
|
87 |
|
|
continue;
|
88 |
|
|
}
|
89 |
|
|
if( a.type == argument_t::integer ) {
|
90 |
|
|
k++;
|
91 |
|
|
if( k>=argc ) throw_error("Expecting input after "+a.long_name+" param");
|
92 |
|
|
a.integer_value = atoi(argv[k]);
|
93 |
|
|
a.set();
|
94 |
|
|
matched=true;
|
95 |
|
|
continue;
|
96 |
|
|
}
|
97 |
17 |
gryzor |
if( a.type == argument_t::real ) {
|
98 |
|
|
k++;
|
99 |
|
|
if( k>=argc ) throw_error("Expecting input after "+a.long_name+" param");
|
100 |
|
|
a.real_value = atof(argv[k]);
|
101 |
|
|
a.set();
|
102 |
|
|
matched=true;
|
103 |
|
|
continue;
|
104 |
|
|
}
|
105 |
16 |
gryzor |
}
|
106 |
|
|
}
|
107 |
|
|
if( !matched && def_arg!=NULL )
|
108 |
|
|
if( def_arg->state )
|
109 |
|
|
throw_error( "Unknown parameter " + std::string(argv[k] ));
|
110 |
|
|
else {
|
111 |
|
|
if( def_arg->type==argument_t::integer ) def_arg->integer_value=atoi(argv[k]);
|
112 |
|
|
if( def_arg->type==argument_t::text ) def_arg->string_value=argv[k];
|
113 |
|
|
def_arg->set();
|
114 |
|
|
}
|
115 |
|
|
}
|
116 |
|
|
if( help_arg.is_set() ) return; // do not perform more checks
|
117 |
|
|
// check that all required parameters are present
|
118 |
|
|
for( int j=0; j<legal_args.size(); j++ ) {
|
119 |
|
|
argument_t& a = *legal_args[j];
|
120 |
|
|
if( a.req && !a.state ) {
|
121 |
|
|
std::string pname;
|
122 |
|
|
if( !a.long_name.empty() ) pname=a.long_name;
|
123 |
|
|
else if( !a.short_name.empty() ) pname=a.short_name;
|
124 |
|
|
throw_error("Parameter "+pname+" is required.");
|
125 |
|
|
}
|
126 |
|
|
}
|
127 |
|
|
}
|
128 |
21 |
gryzor |
void check_ilegal_combinations( arg_vector_t& ilegal ) {
|
129 |
|
|
int count=0;
|
130 |
|
|
std::string names;
|
131 |
|
|
for( int k=0; k < ilegal.size(); k++ )
|
132 |
|
|
if( ilegal[k]->is_set() ) { count++; names += " " + ilegal[k]->long_name; }
|
133 |
|
|
if( count>1 ) throw_error( "Parameters" + names + " cannot be used together" );
|
134 |
|
|
}
|
135 |
17 |
gryzor |
bool help_request() { if( help_arg.is_set() ) show_help(); return help_arg.is_set(); }
|
136 |
16 |
gryzor |
std::string brackets( const argument_t& a, std::string s ) {
|
137 |
|
|
return a.req ? "<"+s+">" : "["+s+"]";
|
138 |
|
|
}
|
139 |
|
|
void show_help() {
|
140 |
|
|
std::cout << "Usage: " << program_name << " ";
|
141 |
|
|
if( def_arg!=NULL )
|
142 |
|
|
std::cout << brackets( *def_arg, def_arg->description.empty() ? "parameter " : def_arg->description);
|
143 |
|
|
std::cout << "\n";
|
144 |
|
|
for( int j=0; j<legal_args.size(); j++ ) {
|
145 |
|
|
argument_t& a = *legal_args[j];
|
146 |
|
|
if( a.long_name.empty() && a.long_name.empty() ) continue;
|
147 |
|
|
std::cout << "\t";
|
148 |
|
|
std::string aux;
|
149 |
|
|
if( !a.long_name.empty() ) aux = a.long_name;
|
150 |
18 |
gryzor |
if( !a.long_name.empty() && !a.short_name.empty() ) aux += " | ";
|
151 |
16 |
gryzor |
if( !a.short_name.empty() ) aux+= a.short_name;
|
152 |
|
|
std::cout << brackets( a, aux );
|
153 |
17 |
gryzor |
switch( a.type ) {
|
154 |
|
|
case argument_t::integer: std::cout << " followed by integer number. "; break;
|
155 |
|
|
case argument_t::real : std::cout << " followed by real number. "; break;
|
156 |
|
|
case argument_t::text : std::cout << " followed by string. "; break;
|
157 |
|
|
}
|
158 |
16 |
gryzor |
if( !a.description.empty() ) std::cout << ": " << a.description;
|
159 |
|
|
std::cout << "\n";
|
160 |
|
|
}
|
161 |
|
|
}
|
162 |
|
|
};
|