17 std::string::size_type
x;
19 x = line.find(
"<description>");
20 if(x != std::string::npos){
24 x = line.find(
"<options>");
25 if(x == std::string::npos){
26 cl.
_error_messages.push_back(
"Config file format error - options section misplaced?.");
29 std::string options_content;
31 std::istringstream Istr(options_content);
32 while(std::getline(Istr,line)){
33 std::istringstream OpIn(line);
40 cl.push_back(std::make_pair(opchar,token));
41 cl.
_type[opchar] = optype;
47 std::getline(Istr,line);
48 x = line.find(
"<help>");
49 if(x == std::string::npos){
50 cl.
_error_messages.push_back(
"Configuration input format error in option help section.");
53 cl.
_help[opchar] =
"";
59 x = line.find(
"<arguments>");
60 if(x == std::string::npos){
61 cl.
_error_messages.push_back(
"Configuration intput format error in argument section.");
64 std::string argsection;
67 while(std::getline(Istr,line)){
68 std::istringstream ArgIn(line);
71 ArgIn >> argname >> argtype;
75 std::string::size_type x = tag.find(
"<help>");
76 if(x == std::string::npos){
77 cl.
_error_messages.push_back(
"Configuration input format error in arghelp section.");
89 Out <<
"<description>\n" << cl.
_description <<
"</description>\n";
91 ComLineObject::const_iterator cli = cl.begin();
92 while(cli != cl.end()){
93 char opchar = cli->first;
94 std::map<char,int>::const_iterator ti = cl.
_type.find(opchar);
95 Out << cli->first <<
" " << cli->second <<
" " 96 << (ti == cl.
_type.end() ? 0 : ti->second);
97 if(ti != cl.
_type.end()){
98 std::map<char,std::string>::const_iterator ai = cl.
_argname.find(opchar);
99 Out <<
" " << (ai == cl.
_argname.end() ?
"(unspecified)" : ai->second);
101 std::map<char,std::string>::const_iterator hi = cl.
_help.find(opchar);
102 Out <<
"\n<help>\n" << (hi == cl.
_help.end() ?
"(unspecified)" : hi->second)
106 Out <<
"</options>\n";
107 if(!cl.
_args.empty()){
108 Out <<
"<arguments>\n";
109 std::vector<std::pair<std::string,int> >::const_iterator ai = cl.
_args.begin();
110 while(ai != cl.
_args.end()){
111 std::map<std::string,std::string>::const_iterator ahi = cl.
_arghelp.find(ai->first);
112 Out << ai->first <<
" " << ai->second
113 <<
"\n<help>\n" << (ahi == cl.
_arghelp.end() ?
"(unspecified)" : ahi->second)
117 Out <<
"</arguments>\n";
148 std::vector<std::string>::iterator ici = incom.
_error_messages.begin();
152 while(ici != incom.
_nonops.end())
154 std::map<char,std::string>::iterator icmi = incom.
_options.begin();
155 while(icmi != incom.
_options.end()){
156 _options[icmi->first] = icmi->second;
159 icmi = incom.
_help.begin();
160 while(icmi != incom.
_help.end()){
161 _help[icmi->first] = icmi->second;
165 while(icmi != incom.
_argname.end()){
166 _argname[icmi->first] = icmi->second;
169 std::map<char,int>::iterator icti = incom.
_type.begin();
170 while(icti != incom.
_type.end()){
171 _type[icti->first] = icti->second;
174 std::vector<std::pair<std::string,int> >::iterator icai = incom.
_args.begin();
175 while(icai != incom.
_args.end())
176 _args.push_back(*icai++);
178 std::map<std::string,std::string>::iterator icahi = incom.
_arghelp.begin();
179 while(icahi != incom.
_arghelp.end()){
180 _arghelp[icahi->first] = icahi->second;
183 ComLineObject::iterator cli = incom.begin();
184 while(cli != incom.end())
185 this->push_back(*cli++);
190 std::ostringstream Ostr;
193 Ostr <<
"Usage: " << std::endl
194 << std::endl <<
ShortUsage() << std::endl << std::endl;
195 std::vector<std::pair<char,std::string> >::const_iterator ti = this->begin();
196 while(ti != this->end()){
197 Ostr <<
"\t" <<
"-" << ti->first <<
",--" << ti->second
198 << (
_type[ti->first] > 0 ? (
_type[ti->first]==1 ?
" [" :
" <") :
"")
200 << (_type[ti->first] > 0 ? (_type[ti->first]==1 ?
"]" :
">") :
"")
201 << std::endl <<
"\t\t" <<
_help[ti->first];
203 if(ti != this->end())
204 Ostr << std::endl << std::endl;
206 Ostr << std::endl << std::endl;
207 std::vector<std::pair<std::string,int> >::const_iterator ai =
_args.begin();
208 while(ai !=
_args.end()){
209 Ostr <<
"\t" << (ai->second ?
"<" :
"[") << ai->first
210 << (ai->second ?
">" :
"]") << std::endl <<
"\t\t" 213 if(ai !=
_args.end())
214 Ostr << std::endl << std::endl;
217 Ostr << std::endl <<
_notes;
223 std::vector<std::pair<char,std::string> >::iterator ti = this->begin();
224 while(ti != this->end()){
234 std::ostringstream Ostr;
235 std::vector<std::pair<char,std::string> >::iterator oi = this->begin();
236 while(oi != this->end()){
237 if(
_type[oi->first] >= mintype &&
_type[oi->first] <= maxtype)
246 std::ostringstream Ostr;
249 if(!flagstring.empty())
250 Ostr <<
"[-" << flagstring <<
"] ";
252 if(!optionals.empty()){
253 std::string::iterator oi = optionals.begin();
255 while(oi != optionals.end()){
257 << (
_type[*oi] == 1 ?
" [" :
" <")
259 << (_type[*oi] == 1 ?
"] " :
"> ");
266 std::string::iterator oi = reqd.begin();
268 while(oi != reqd.end()){
269 Ostr <<
"-" << *oi <<
" <" <<
_argname[*oi]
275 std::vector<std::pair<std::string,int> >::iterator ai =
_args.begin();
276 while(ai !=
_args.end()){
277 Ostr << (ai->second > 0 ?
"<" :
"[") << ai->first
278 << (ai->second > 0 ?
"> ":
"] ");
286 std::vector<std::pair<char,std::string> >::iterator ti = this->begin();
287 while(ti != this->end()){
298 this->push_back(std::make_pair(s,l));
303 _help[s] = std::string(
"");
309 this->push_back(std::make_pair(s,l));
313 _help[s] = std::string(
"");
319 std::ostringstream Ostr;
322 _toks.push_back(std::string(args[i]));
327 _line.assign(Ostr.str());
332 bool end_of_ops =
false;
334 std::vector<std::string> &args(
_toks);
336 std::vector<std::string>::iterator ai = args.begin();
337 while(ai != args.end()){
338 std::string this_argument = *ai++;
339 std::string next_argument;
341 next_argument = *ai--;
342 std::string::iterator si = this_argument.begin();
343 if(*si ==
'-' && !end_of_ops){
345 if(si == this_argument.end())
348 while(si != this_argument.end()){
349 char flag_char = *si++;
350 std::vector<std::pair<char,std::string> >::iterator oi = this->begin();
352 while((oi != this->end()) && !found){
353 if(flag_char == oi->first){
355 if(si == this_argument.end()){
356 if(
_type[flag_char] != 0){
357 if(!next_argument.empty() && next_argument[0] !=
'-'){
358 _options[flag_char] = next_argument;
361 else if(
_type[flag_char] > 1){
363 std::ostringstream Ostr;
364 Ostr <<
"Option -" << flag_char <<
" requires an argument.";
374 if(
_type[flag_char] > 1){
376 std::ostringstream Ostr;
377 Ostr <<
"Option -" << flag_char <<
" requires an argument.";
388 std::ostringstream Ostr;
389 Ostr <<
"Option -" << flag_char <<
" is unrecognized.";
395 std::string opstring = this_argument.substr(2);
396 char flag_char =
GetShort(opstring);
397 if(flag_char !=
'\0'){
398 if(
_type[flag_char] != 0){
399 if(!next_argument.empty() && next_argument[0] !=
'-'){
400 _options[flag_char] = next_argument;
403 else if(
_type[flag_char] > 1){
405 std::ostringstream Ostr;
406 Ostr <<
"Option --" <<
GetLong(flag_char)
407 <<
" requires an argument.";
418 std::ostringstream Ostr;
419 Ostr <<
"Option --" << opstring <<
" is unrecognized.";
425 _nonops.push_back(this_argument);
434 std::vector<std::pair<char,std::string> >::iterator oi = this->begin();
435 while(oi != this->end()){
438 std::ostringstream Ostr;
439 Ostr <<
"Required option: -" << oi->first <<
",--" << oi->second
440 <<
" <" <<
_argname[oi->first] <<
"> was not specified.";
446 unsigned int nreqd_args = 0;
447 std::vector<std::pair<std::string,int> >::iterator aai =
_args.begin();
448 while(aai !=
_args.end()){
453 if(nreqd_args >
_nonops.size()){
455 std::ostringstream Ostr;
456 Ostr <<
"Missing required arguments.";
464 std::istringstream Istr(incl);
469 _line += (std::string(
" ")+token);
475 std::ostringstream Ostr;
476 Ostr <<
_program_name <<
" command line errors: " << std::endl;
void ReadRC(const std::string &RCString)
Read a config from RC string.
int ProcessOptions()
Processes all command line tokens.
std::string ErrorReport()
Error reporting.
std::string GetOption(const char &s)
Get the value of an option.
std::string GetLong(const char &s)
Obtain the long word option from the short char version.
std::vector< std::pair< std::string, int > > _args
application arguments;
std::map< char, int > _type
stores the type of option
std::vector< std::string > _error_messages
stores error messages
void Copy(ComLineObject &incom)
Copy method.
void DumpContents(std::ostream &Ostr, const ContainerType &c, std::string del="\)
Dump container contents.
std::map< std::string, std::string > _arghelp
help string for args
std::string GetOpStringByType(int mintype, int maxtype)
Obtains option strings by type.
std::string _line
unformatted command line
std::string GetNextContent(std::istream &In)
Defines MPI-specific parallel global and program classes.
std::map< char, std::string > _argname
stores a name for arguments
std::ostream & operator<<(std::ostream &Ostr, const util::ConfigurationObject &cob)
Stream output operator for util::ConfigurationObject.
std::istream & operator>>(std::istream &Istr, util::ConfigurationObject &cob)
Stream input operator for util::ConfigurationObject.
std::map< char, std::string > _help
stores the help string for each op
void const size_t const size_t const size_t const double const double * x
std::string _description
application description.
void Record(const char *args[])
Minimal recording of command line.
void SetRawComLine(const std::string &incl)
Raw Command Line Access.
std::string _program_name
the name of the program
std::vector< std::string > _nonops
stores non-option arguments
void AddOption(char s, const std::string &l, int=0)
User interface to describe simple option.
std::vector< std::string > _toks
commandline tokens
const std::string stripdirs(const std::string &pname)
Strip absolute path.
std::map< char, std::string > _options
stores the value of each option
void GetContentUntil(std::istream &In, std::string ret, const std::string &etag)
std::string LongUsage()
Generate long usage string.
void WriteRC(std::ostream &Ostr) const
Write an RC string that can be used for config.
std::string _notes
Notes to be displayed at the end of LongUsage().
char GetShort(const std::string &l)
Obtain the short one char option from the long word version.
ComLineObject()
Default constructor.
std::string ShortUsage()
Generate short usage string.