CMS 3D CMS Logo

List of all members | Public Member Functions | Private Types | Private Member Functions | Private Attributes
cmdline::CmdLine Class Reference

#include <CmdLine.h>

Public Member Functions

unsigned argc () const
 
 CmdLine (const unsigned argc, const char *const argv[])
 
bool has (const char *shortOpt, const char *longOpt=nullptr)
 
 operator void * () const
 
template<typename T >
CmdLineoperator>> (T &obj)
 
void optend () const
 
OneShotIStream option (const char *shortOpt, const char *longOpt=nullptr)
 
const char * progname () const
 
OneShotIStream require (const char *shortOpt, const char *longOpt=nullptr)
 

Private Types

typedef std::list< PairOptlist
 
typedef std::pair< std::string, int > Pair
 

Private Member Functions

 CmdLine ()=delete
 
Optlist::iterator find (const char *shortOpt, const char *longOpt)
 

Private Attributes

Optlist args_
 
unsigned nprogargs_
 
std::string progname_
 

Detailed Description

Definition at line 204 of file CmdLine.h.

Member Typedef Documentation

◆ Optlist

typedef std::list<Pair> cmdline::CmdLine::Optlist
private

Definition at line 211 of file CmdLine.h.

◆ Pair

typedef std::pair<std::string, int> cmdline::CmdLine::Pair
private

Definition at line 210 of file CmdLine.h.

Constructor & Destructor Documentation

◆ CmdLine() [1/2]

cmdline::CmdLine::CmdLine ( const unsigned  argc,
const char *const  argv[] 
)
inline

Definition at line 225 of file CmdLine.h.

References argc(), args_, GCPpyPlots::argv, mps_fire::i, dqmdumpme::k, nprogargs_, progname(), progname_, and AlCaHLTBitMon_QueryRunRegistry::string.

225  : nprogargs_(0) {
226  // Parse the program name
227  const char* progname = std::strrchr(argv[0], '/');
228  if (progname)
229  ++progname;
230  else
231  progname = argv[0];
232 
233  // Take into account program name mangling by GNU autotools
234  if (strncmp(progname, "lt-", 3) == 0)
235  progname += 3;
237 
238  // Make a list of arguments noting on the way if this is
239  // a short option, long option, or possible option argument
240  bool previousIsOpt = false;
241  bool nextIsArg = false;
242  for (unsigned i = 1; i < argc; ++i) {
243  if (nextIsArg) {
244  args_.push_back(Pair(argv[i], previousIsOpt ? 0 : 3));
245  previousIsOpt = false;
246  ++nprogargs_;
247  nextIsArg = false;
248  } else if (strcmp(argv[i], "-") == 0)
249  nextIsArg = true;
250  else if (strcmp(argv[i], "--") == 0) {
251  // End of options
252  for (unsigned k = i + 1; k < argc; ++k) {
253  args_.push_back(Pair(argv[k], 3));
254  ++nprogargs_;
255  }
256  return;
257  } else if (strncmp(argv[i], "--", 2) == 0) {
258  args_.push_back(Pair(argv[i], 2));
259  previousIsOpt = true;
260  } else if (argv[i][0] == '-') {
261  const unsigned len = strlen(argv[i]);
262  for (unsigned k = 1; k < len; ++k) {
263  std::string dummy("-");
264  dummy += argv[i][k];
265  args_.push_back(Pair(dummy, 1));
266  previousIsOpt = true;
267  }
268  } else {
269  args_.push_back(Pair(argv[i], previousIsOpt ? 0 : 3));
270  previousIsOpt = false;
271  ++nprogargs_;
272  }
273  }
274  }
std::pair< std::string, int > Pair
Definition: CmdLine.h:210
unsigned argc() const
Definition: CmdLine.h:325
const char * progname() const
Definition: CmdLine.h:276
unsigned nprogargs_
Definition: CmdLine.h:347
std::string progname_
Definition: CmdLine.h:345
Optlist args_
Definition: CmdLine.h:346

◆ CmdLine() [2/2]

cmdline::CmdLine::CmdLine ( )
privatedelete

Member Function Documentation

◆ argc()

unsigned cmdline::CmdLine::argc ( ) const
inline

Definition at line 325 of file CmdLine.h.

References nprogargs_.

Referenced by CmdLine().

325 { return nprogargs_; }
unsigned nprogargs_
Definition: CmdLine.h:347

◆ find()

Optlist::iterator cmdline::CmdLine::find ( const char *  shortOpt,
const char *  longOpt 
)
inlineprivate

Definition at line 213 of file CmdLine.h.

References args_.

Referenced by BeautifulSoup.Tag::__getattr__(), BeautifulSoup.Tag::firstText(), has(), and option().

213  {
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)
217  return it;
218  if (longOpt && it->second == 2 && it->first == longOpt)
219  return it;
220  }
221  return iend;
222  }
Optlist args_
Definition: CmdLine.h:346

◆ has()

bool cmdline::CmdLine::has ( const char *  shortOpt,
const char *  longOpt = nullptr 
)
inline

Definition at line 278 of file CmdLine.h.

References args_, find(), and newFWLiteAna::found.

278  {
279  bool found = false;
280  for (Optlist::iterator it = find(shortOpt, longOpt); it != args_.end(); it = find(shortOpt, longOpt)) {
281  found = true;
282  Optlist::iterator it0(it);
283  if (++it != args_.end())
284  if (it->second == 0)
285  it->second = 3;
286  args_.erase(it0);
287  }
288  return found;
289  }
Optlist::iterator find(const char *shortOpt, const char *longOpt)
Definition: CmdLine.h:213
Optlist args_
Definition: CmdLine.h:346

◆ operator void *()

cmdline::CmdLine::operator void * ( ) const
inline

Definition at line 323 of file CmdLine.h.

References nprogargs_.

323 { return (void*)(static_cast<unsigned long>(nprogargs_)); }
unsigned nprogargs_
Definition: CmdLine.h:347

◆ operator>>()

template<typename T >
CmdLine& cmdline::CmdLine::operator>> ( T obj)
inline

Definition at line 328 of file CmdLine.h.

References args_, nprogargs_, and getGTfromDQMFile::obj.

328  {
329  if (!nprogargs_)
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)
334  break;
335  OneShotIStream is(it->first);
336  args_.erase(it);
337  --nprogargs_;
338  is >> obj;
339  return *this;
340  }
unsigned nprogargs_
Definition: CmdLine.h:347
Optlist args_
Definition: CmdLine.h:346

◆ optend()

void cmdline::CmdLine::optend ( ) const
inline

Definition at line 317 of file CmdLine.h.

References args_.

317  {
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 << '"';
321  }
Optlist args_
Definition: CmdLine.h:346

◆ option()

OneShotIStream cmdline::CmdLine::option ( const char *  shortOpt,
const char *  longOpt = nullptr 
)
inline

Definition at line 291 of file CmdLine.h.

References args_, find(), nprogargs_, and mps_fire::result.

Referenced by require().

291  {
292  OneShotIStream result;
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) {
297  result = OneShotIStream(it->first);
298  args_.erase(it0, ++it);
299  --nprogargs_;
300  continue;
301  }
302  throw CmdLineError() << "missing command line argument for option \"" << it0->first << '"';
303  }
304  return result;
305  }
unsigned nprogargs_
Definition: CmdLine.h:347
Optlist::iterator find(const char *shortOpt, const char *longOpt)
Definition: CmdLine.h:213
Optlist args_
Definition: CmdLine.h:346

◆ progname()

const char* cmdline::CmdLine::progname ( ) const
inline

Definition at line 276 of file CmdLine.h.

References progname_.

Referenced by CmdLine().

276 { return progname_.c_str(); }
std::string progname_
Definition: CmdLine.h:345

◆ require()

OneShotIStream cmdline::CmdLine::require ( const char *  shortOpt,
const char *  longOpt = nullptr 
)
inline

Definition at line 307 of file CmdLine.h.

References relativeConstraints::empty, cmdline::OneShotIStream::isValid(), option(), and alignCSCRings::s.

307  {
308  const OneShotIStream& is(option(shortOpt, longOpt));
309  if (!is.isValid()) {
310  const char empty[] = "";
311  const char* s = shortOpt ? shortOpt : (longOpt ? longOpt : empty);
312  throw CmdLineError() << "required command line option \"" << s << "\" is missing";
313  }
314  return is;
315  }
OneShotIStream option(const char *shortOpt, const char *longOpt=nullptr)
Definition: CmdLine.h:291

Member Data Documentation

◆ args_

Optlist cmdline::CmdLine::args_
private

◆ nprogargs_

unsigned cmdline::CmdLine::nprogargs_
private

Definition at line 347 of file CmdLine.h.

Referenced by argc(), CmdLine(), operator void *(), operator>>(), and option().

◆ progname_

std::string cmdline::CmdLine::progname_
private

Definition at line 345 of file CmdLine.h.

Referenced by CmdLine(), and progname().