1 #ifndef CONDTOOLS_HCAL_CMDLINE_H_
2 #define CONDTOOLS_HCAL_CMDLINE_H_
111 #ifdef __GXX_EXPERIMENTAL_CXX0X__
113 #define CmdLine_shared_ptr std::shared_ptr
115 #include <tr1/memory>
116 #define CmdLine_shared_ptr std::tr1::shared_ptr
138 CmdLine_shared_ptr<std::ostringstream>
os_;
146 inline void OneShotExtract<std::string>(std::istringstream& is,
158 inline operator void*()
const
168 <<
"can't reuse command line argument \"" <<
str_ <<
'"';
172 std::istringstream is(
str_);
174 if (is.bad() || is.fail())
176 <<
"failed to parse command line argument \""
179 <<
", " << demangle(obj) <<
" expected"
182 if (is.peek() != EOF)
184 <<
"extra characters in command line argument \""
187 <<
", " << demangle(obj) <<
" expected"
202 template <
typename T>
206 const std::type_info &ti =
typeid(
obj);
207 char *realname = abi::__cxa_demangle(ti.name(), 0, 0, &
status);
222 typedef std::pair<std::string,int>
Pair;
225 inline Optlist::iterator
find(
const char* shortOpt,
const char* longOpt)
227 Optlist::iterator iend =
args_.end();
228 for (Optlist::iterator it =
args_.begin(); it != iend; ++it)
230 if (shortOpt && it->second == 1 && it->first == shortOpt)
232 if (longOpt && it->second == 2 && it->first == longOpt)
243 const char *
progname = std::strrchr(argv[0],
'/');
250 if (strncmp(progname,
"lt-", 3) == 0)
256 bool previousIsOpt =
false;
257 bool nextIsArg =
false;
258 for (
unsigned i=1;
i<
argc; ++
i)
262 args_.push_back(
Pair(argv[
i], previousIsOpt ? 0 : 3));
263 previousIsOpt =
false;
267 else if (strcmp(argv[
i],
"-") == 0)
269 else if (strcmp(argv[i],
"--") == 0)
272 for (
unsigned k=i+1;
k<
argc; ++
k)
279 else if (strncmp(argv[i],
"--", 2) == 0)
282 previousIsOpt =
true;
284 else if (argv[i][0] ==
'-')
286 const unsigned len = strlen(argv[i]);
287 for (
unsigned k=1;
k<len; ++
k)
292 previousIsOpt =
true;
297 args_.push_back(
Pair(argv[i], previousIsOpt ? 0 : 3));
298 previousIsOpt =
false;
306 inline bool has(
const char* shortOpt,
const char* longOpt=0)
309 for (Optlist::iterator it =
find(shortOpt, longOpt);
310 it !=
args_.end(); it =
find(shortOpt, longOpt))
313 Optlist::iterator it0(it);
314 if (++it !=
args_.end())
325 for (Optlist::iterator it =
find(shortOpt, longOpt);
326 it !=
args_.end(); it =
find(shortOpt, longOpt))
328 Optlist::iterator it0(it);
329 if (++it !=
args_.end())
333 args_.erase(it0, ++it);
338 <<
"missing command line argument for option \""
339 << it0->first <<
'"';
349 const char empty[] =
"";
350 const char*
s = shortOpt ? shortOpt : (longOpt ? longOpt :
empty);
352 <<
"required command line option \"" << s <<
"\" is missing";
359 for (Optlist::const_iterator it =
args_.begin();
360 it !=
args_.end(); ++it)
361 if (it->second == 1 || it->second == 2)
366 inline operator void*()
const
368 return (
void*)(
static_cast<unsigned long>(
nprogargs_));
380 throw CmdLineError(
"no more input available on the command line");
381 Optlist::iterator it =
args_.begin();
382 for (; it !=
args_.end(); ++it)
383 if (it->second == 0 || it->second == 3)
402 #endif // CONDTOOLS_HCAL_CMDLINE_H_
bool has(const char *shortOpt, const char *longOpt=0)
std::list< Pair > Optlist
CmdLineError(const char *msg=0)
std::pair< std::string, int > Pair
OneShotIStream(const std::string &s)
std::tr1::shared_ptr< std::ostringstream > os_
void OneShotExtract(std::istringstream &is, T &obj)
CmdLine & operator>>(T &obj)
OneShotIStream require(const char *shortOpt, const char *longOpt=0)
const char * progname() const
CmdLineError & operator<<(const T &obj)
CmdLine(const unsigned argc, const char *const argv[])
OneShotIStream option(const char *shortOpt, const char *longOpt=0)
volatile std::atomic< bool > shutdown_flag false
Optlist::iterator find(const char *shortOpt, const char *longOpt)