Class designed to deal with command-line arguments in a fashion similar to what was done in f90 iolib. More...
#include <CmdLine.hh>
Public Member Functions | |
CmdLine () | |
CmdLine (const int argc, char **argv) | |
initialise a CmdLine from a C-style array of command-line arguments | |
CmdLine (const vector< string > &args) | |
initialise a CmdLine from a C++ vector of arguments | |
bool | present (const string &opt) const |
true if the option is present | |
bool | present_and_set (const string &opt) const |
true if the option is present and corresponds to a value | |
const vector< string > & | arguments () const |
return a reference to the vector of command-line arguments (0 is command). | |
template<class T > | |
T | value (const string &opt) const |
returns the value of the argument converted to type T | |
template<class T > | |
T | value (const string &opt, const T &defval) const |
int | int_val (const string &opt) const |
return the integer value corresponding to the given option | |
int | int_val (const string &opt, const int &defval) const |
return the integer value corresponding to the given option or default if option is absent | |
double | double_val (const string &opt) const |
return the double value corresponding to the given option | |
double | double_val (const string &opt, const double &defval) const |
return the double value corresponding to the given option or default if option is absent | |
string | string_val (const string &opt) const |
return the string value corresponding to the given option | |
string | string_val (const string &opt, const string &defval) const |
return the string value corresponding to the given option or default if option is absent | |
string | command_line () const |
return the full command line | |
bool | all_options_used () const |
return true if all options have been asked for at some point or other | |
Private Member Functions | |
void | init () |
builds the internal structures needed to keep track of arguments and options | |
void | _report_conversion_failure (const string &opt, const string &optstring) const |
report failure of conversion | |
Private Attributes | |
map< string, int > | __options |
vector< string > | __arguments |
map< string, bool > | __options_used |
string | __command_line |
Class designed to deal with command-line arguments in a fashion similar to what was done in f90 iolib.
Note that functionality might be slightly different? Currently do not implement access to arguments by index though data structure would in principle allow this quite easily.
GPS 03/01/05 [NB: wonder if some of this might be more efficiently written with templates for different type that can be read in...]
Other question: dealing with list of options is rather common occurrence -- command-line arguments, but also card files; maybe one could somehow use base/derived classes to share common functionality?
Definition at line 50 of file CmdLine.hh.
CmdLine::CmdLine | ( | ) | [inline] |
Definition at line 58 of file CmdLine.hh.
CmdLine::CmdLine | ( | const int | argc, | |
char ** | argv | |||
) |
initialise a CmdLine from a C-style array of command-line arguments
Definition at line 41 of file CmdLine.cc.
References __arguments, and init().
00041 { 00042 __arguments.resize(argc); 00043 for(int iarg = 0; iarg < argc; iarg++){ 00044 __arguments[iarg] = argv[iarg]; 00045 } 00046 this->init(); 00047 }
CmdLine::CmdLine | ( | const vector< string > & | args | ) |
initialise a CmdLine from a C++ vector of arguments
constructor from a vector of strings, one argument per string
Definition at line 50 of file CmdLine.cc.
References __arguments, and init().
00050 { 00051 __arguments = args; 00052 this->init(); 00053 }
void CmdLine::_report_conversion_failure | ( | const string & | opt, | |
const string & | optstring | |||
) | const [private] |
bool CmdLine::all_options_used | ( | ) | const |
return true if all options have been asked for at some point or other
Definition at line 173 of file CmdLine.cc.
References __options_used.
Referenced by main().
00173 { 00174 bool result = true; 00175 for(map<string,bool>::const_iterator opt = __options_used.begin(); 00176 opt != __options_used.end(); opt++) { 00177 bool this_one = opt->second; 00178 if (! this_one) {cerr << "Option "<<opt->first<<" unused/unrecognized"<<endl;} 00179 result = result && this_one; 00180 } 00181 return result; 00182 }
const vector<string>& CmdLine::arguments | ( | ) | const [inline] |
return a reference to the vector of command-line arguments (0 is command).
Definition at line 71 of file CmdLine.hh.
References __arguments.
00071 {return __arguments;}
string CmdLine::command_line | ( | ) | const |
return the full command line
Definition at line 167 of file CmdLine.cc.
References __command_line.
Referenced by print_jets().
00167 { 00168 return __command_line; 00169 }
double CmdLine::double_val | ( | const string & | opt, | |
const double & | defval | |||
) | const |
return the double value corresponding to the given option or default if option is absent
Definition at line 160 of file CmdLine.cc.
References double_val(), and present_and_set().
00160 { 00161 if (this->present_and_set(opt)) {return double_val(opt);} 00162 else {return defval;} 00163 }
double CmdLine::double_val | ( | const string & | opt | ) | const |
return the double value corresponding to the given option
Definition at line 147 of file CmdLine.cc.
References string_val().
Referenced by double_val(), and main().
00147 { 00148 double result; 00149 string optstring = string_val(opt); 00150 istringstream optstream(optstring); 00151 optstream >> result; 00152 if (optstream.fail()) { 00153 cerr << "Error: could not convert option ("<<opt<<") value (" 00154 <<optstring<<") to double"<<endl; 00155 exit(-1);} 00156 return result; 00157 }
void CmdLine::init | ( | ) | [private] |
builds the internal structures needed to keep track of arguments and options
Definition at line 56 of file CmdLine.cc.
References __arguments, __command_line, __options, and __options_used.
Referenced by CmdLine().
00056 { 00057 __command_line = ""; 00058 for(size_t iarg = 0; iarg < __arguments.size(); iarg++){ 00059 __command_line += __arguments[iarg]; 00060 __command_line += " "; 00061 } 00062 00063 // group things into options 00064 bool next_may_be_val = false; 00065 string currentopt; 00066 for(size_t iarg = 1; iarg < __arguments.size(); iarg++){ 00067 // if expecting an option value, then take it (even if 00068 // it is actually next option...) 00069 if (next_may_be_val) {__options[currentopt] = iarg;} 00070 // now see if it might be an option itself 00071 string arg = __arguments[iarg]; 00072 bool thisisopt = (arg.compare(0,1,"-") == 0); 00073 if (thisisopt) { 00074 // set option to a standard undefined value and say that 00075 // we expect (possibly) a value on next round 00076 currentopt = arg; 00077 __options[currentopt] = -1; 00078 __options_used[currentopt] = false; 00079 next_may_be_val = true;} 00080 else { 00081 // otherwise throw away the argument for now... 00082 next_may_be_val = false; 00083 currentopt = ""; 00084 } 00085 } 00086 }
int CmdLine::int_val | ( | const string & | opt, | |
const int & | defval | |||
) | const |
return the integer value corresponding to the given option or default if option is absent
Definition at line 138 of file CmdLine.cc.
References int_val(), and present_and_set().
00138 { 00139 if (this->present_and_set(opt)) {return int_val(opt);} 00140 else {return defval;} 00141 }
int CmdLine::int_val | ( | const string & | opt | ) | const |
return the integer value corresponding to the given option
Definition at line 125 of file CmdLine.cc.
References string_val().
Referenced by int_val(), and main().
00125 { 00126 int result; 00127 string optstring = string_val(opt); 00128 istringstream optstream(optstring); 00129 optstream >> result; 00130 if (optstream.fail()) { 00131 cerr << "Error: could not convert option ("<<opt<<") value (" 00132 <<optstring<<") to int"<<endl; 00133 exit(-1);} 00134 return result; 00135 }
bool CmdLine::present | ( | const string & | opt | ) | const |
true if the option is present
Definition at line 89 of file CmdLine.cc.
References __options, and __options_used.
Referenced by main(), and present_and_set().
00089 { 00090 bool result = (__options.find(opt) != __options.end()); 00091 if (result) __options_used[opt] = true; 00092 return result; 00093 }
bool CmdLine::present_and_set | ( | const string & | opt | ) | const |
true if the option is present and corresponds to a value
Definition at line 96 of file CmdLine.cc.
References __options, and present().
Referenced by double_val(), int_val(), string_val(), and value().
string CmdLine::string_val | ( | const string & | opt, | |
const string & | defval | |||
) | const |
return the string value corresponding to the given option or default if option is absent
Definition at line 117 of file CmdLine.cc.
References present_and_set(), and string_val().
00117 { 00118 if (this->present_and_set(opt)) {return string_val(opt);} 00119 else {return defval;} 00120 }
string CmdLine::string_val | ( | const string & | opt | ) | const |
return the string value corresponding to the given option
Definition at line 103 of file CmdLine.cc.
References __arguments, __options, __options_used, and present_and_set().
Referenced by double_val(), int_val(), string_val(), and value().
00103 { 00104 if (!this->present_and_set(opt)) { 00105 cerr << "Error: Option "<<opt 00106 <<" is needed but is not present_and_set"<<endl; 00107 exit(-1); 00108 } 00109 string arg = __arguments[__options[opt]]; 00110 // this may itself look like an option -- if that is the case 00111 // declare the option to have been used 00112 if (arg.compare(0,1,"-") == 0) {__options_used[arg] = true;} 00113 return arg; 00114 }
T CmdLine::value | ( | const string & | opt, | |
const T & | defval | |||
) | const [inline] |
Definition at line 128 of file CmdLine.hh.
References present_and_set().
00128 { 00129 if (this->present_and_set(opt)) {return value<T>(opt);} 00130 else {return defval;} 00131 }
string CmdLine::value< string > | ( | const string & | opt | ) | const [inline] |
returns the value of the argument converted to type T
for the string case, just copy the string...
Definition at line 113 of file CmdLine.hh.
References _report_conversion_failure(), and string_val().
Referenced by main().
00113 { 00114 T result; 00115 string optstring = string_val(opt); 00116 istringstream optstream(optstring); 00117 optstream >> result; 00118 if (optstream.fail()) _report_conversion_failure(opt, optstring); 00119 return result; 00120 }
vector<string> CmdLine::__arguments [private] |
Definition at line 52 of file CmdLine.hh.
Referenced by arguments(), CmdLine(), init(), and string_val().
string CmdLine::__command_line [private] |
Definition at line 55 of file CmdLine.hh.
Referenced by command_line(), and init().
map<string,int> CmdLine::__options [mutable, private] |
Definition at line 51 of file CmdLine.hh.
Referenced by init(), present(), present_and_set(), and string_val().
map<string,bool> CmdLine::__options_used [mutable, private] |
Definition at line 53 of file CmdLine.hh.
Referenced by all_options_used(), init(), present(), and string_val().