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 129 template <
typename T>
138 CmdLine_shared_ptr<std::ostringstream>
os_;
141 template <
typename T>
147 inline void OneShotExtract<std::string>(std::istringstream& is,
std::string&
obj) {
158 inline operator void*()
const {
return valid_ && !
readout_ ? (
void*)
this : (
void*)
nullptr; }
160 template <
typename T>
163 throw CmdLineError() <<
"can't reuse command line argument \"" <<
str_ <<
'"';
166 std::istringstream is(
str_);
168 if (is.bad() || is.fail())
169 throw CmdLineError() <<
"failed to parse command line argument \"" <<
str_ <<
'"' 171 <<
", " << demangle(
obj) <<
" expected" 174 if (is.peek() != EOF)
175 throw CmdLineError() <<
"extra characters in command line argument \"" <<
str_ <<
'"' 177 <<
", " << demangle(
obj) <<
" expected" 192 template <
typename T>
195 const std::type_info& ti =
typeid(
obj);
196 char* realname = abi::__cxa_demangle(ti.name(),
nullptr,
nullptr, &
status);
210 typedef std::pair<std::string, int>
Pair;
213 inline Optlist::iterator
find(
const char* shortOpt,
const char* longOpt) {
214 Optlist::iterator iend =
args_.end();
215 for (Optlist::iterator it =
args_.begin(); it != iend; ++it) {
216 if (shortOpt && it->second == 1 && it->first == shortOpt)
218 if (longOpt && it->second == 2 && it->first == longOpt)
234 if (strncmp(
progname,
"lt-", 3) == 0)
240 bool previousIsOpt =
false;
241 bool nextIsArg =
false;
242 for (
unsigned i = 1;
i <
argc; ++
i) {
245 previousIsOpt =
false;
248 }
else if (strcmp(
argv[
i],
"-") == 0)
250 else if (strcmp(
argv[
i],
"--") == 0) {
252 for (
unsigned k =
i + 1;
k <
argc; ++
k) {
257 }
else if (strncmp(
argv[
i],
"--", 2) == 0) {
259 previousIsOpt =
true;
260 }
else if (
argv[
i][0] ==
'-') {
261 const unsigned len = strlen(
argv[
i]);
262 for (
unsigned k = 1;
k < len; ++
k) {
266 previousIsOpt =
true;
270 previousIsOpt =
false;
278 inline bool has(
const char* shortOpt,
const char* longOpt =
nullptr) {
280 for (Optlist::iterator it =
find(shortOpt, longOpt); it !=
args_.end(); it =
find(shortOpt, longOpt)) {
282 Optlist::iterator it0(it);
283 if (++it !=
args_.end())
293 for (Optlist::iterator it =
find(shortOpt, longOpt); it !=
args_.end(); it =
find(shortOpt, longOpt)) {
294 Optlist::iterator it0(it);
295 if (++it !=
args_.end())
296 if (it->second == 0) {
298 args_.erase(it0, ++it);
302 throw CmdLineError() <<
"missing command line argument for option \"" << it0->first <<
'"';
310 const char empty[] =
"";
311 const char*
s = shortOpt ? shortOpt : (longOpt ? longOpt :
empty);
312 throw CmdLineError() <<
"required command line option \"" <<
s <<
"\" is missing";
318 for (Optlist::const_iterator it =
args_.begin(); it !=
args_.end(); ++it)
319 if (it->second == 1 || it->second == 2)
320 throw CmdLineError(
"invalid command line option \"") << it->first <<
'"';
323 inline operator void*()
const {
return (
void*)(
static_cast<unsigned long>(
nprogargs_)); }
327 template <
typename T>
330 throw CmdLineError(
"no more input available on the command line");
331 Optlist::iterator it =
args_.begin();
332 for (; it !=
args_.end(); ++it)
333 if (it->second == 0 || it->second == 3)
352 #endif // CONDTOOLS_HCAL_CMDLINE_H_
OneShotIStream option(const char *shortOpt, const char *longOpt=nullptr)
std::list< Pair > Optlist
std::pair< std::string, int > Pair
CmdLineError(const char *msg=nullptr)
OneShotIStream(const std::string &s)
std::tr1::shared_ptr< std::ostringstream > os_
bool has(const char *shortOpt, const char *longOpt=nullptr)
void OneShotExtract(std::istringstream &is, T &obj)
const char * progname() const
CmdLine & operator>>(T &obj)
CmdLineError & operator<<(const T &obj)
OneShotIStream require(const char *shortOpt, const char *longOpt=nullptr)
CmdLine(const unsigned argc, const char *const argv[])
Optlist::iterator find(const char *shortOpt, const char *longOpt)