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=0)
 
 operator void * () const
 
template<typename T >
CmdLineoperator>> (T &obj)
 
void optend () const
 
OneShotIStream option (const char *shortOpt, const char *longOpt=0)
 
const char * progname () const
 
OneShotIStream require (const char *shortOpt, const char *longOpt=0)
 

Private Types

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

Private Member Functions

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

Private Attributes

Optlist args_
 
unsigned nprogargs_
 
std::string progname_
 

Detailed Description

Definition at line 215 of file CmdLine.h.

Member Typedef Documentation

Definition at line 223 of file CmdLine.h.

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

Definition at line 222 of file CmdLine.h.

Constructor & Destructor Documentation

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

Definition at line 239 of file CmdLine.h.

References dir2webdir::argc, mps_fire::i, gen::k, and AlCaHLTBitMon_QueryRunRegistry::string.

240  : nprogargs_(0)
241  {
242  // Parse the program name
243  const char *progname = std::strrchr(argv[0], '/');
244  if (progname)
245  ++progname;
246  else
247  progname = argv[0];
248 
249  // Take into account program name mangling by GNU autotools
250  if (strncmp(progname, "lt-", 3) == 0)
251  progname += 3;
253 
254  // Make a list of arguments noting on the way if this is
255  // a short option, long option, or possible option argument
256  bool previousIsOpt = false;
257  bool nextIsArg = false;
258  for (unsigned i=1; i<argc; ++i)
259  {
260  if (nextIsArg)
261  {
262  args_.push_back(Pair(argv[i], previousIsOpt ? 0 : 3));
263  previousIsOpt = false;
264  ++nprogargs_;
265  nextIsArg = false;
266  }
267  else if (strcmp(argv[i], "-") == 0)
268  nextIsArg = true;
269  else if (strcmp(argv[i], "--") == 0)
270  {
271  // End of options
272  for (unsigned k=i+1; k<argc; ++k)
273  {
274  args_.push_back(Pair(argv[k], 3));
275  ++nprogargs_;
276  }
277  return;
278  }
279  else if (strncmp(argv[i], "--", 2) == 0)
280  {
281  args_.push_back(Pair(argv[i], 2));
282  previousIsOpt = true;
283  }
284  else if (argv[i][0] == '-')
285  {
286  const unsigned len = strlen(argv[i]);
287  for (unsigned k=1; k<len; ++k)
288  {
289  std::string dummy("-");
290  dummy += argv[i][k];
291  args_.push_back(Pair(dummy, 1));
292  previousIsOpt = true;
293  }
294  }
295  else
296  {
297  args_.push_back(Pair(argv[i], previousIsOpt ? 0 : 3));
298  previousIsOpt = false;
299  ++nprogargs_;
300  }
301  }
302  }
std::pair< std::string, int > Pair
Definition: CmdLine.h:222
unsigned nprogargs_
Definition: CmdLine.h:397
const char * progname() const
Definition: CmdLine.h:304
int k[5][pyjets_maxn]
unsigned argc() const
Definition: CmdLine.h:371
std::string progname_
Definition: CmdLine.h:395
Optlist args_
Definition: CmdLine.h:396
cmdline::CmdLine::CmdLine ( )
private

Member Function Documentation

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

Definition at line 371 of file CmdLine.h.

372  {
373  return nprogargs_;
374  }
unsigned nprogargs_
Definition: CmdLine.h:397
Optlist::iterator cmdline::CmdLine::find ( const char *  shortOpt,
const char *  longOpt 
)
inlineprivate

Definition at line 225 of file CmdLine.h.

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

226  {
227  Optlist::iterator iend = args_.end();
228  for (Optlist::iterator it = args_.begin(); it != iend; ++it)
229  {
230  if (shortOpt && it->second == 1 && it->first == shortOpt)
231  return it;
232  if (longOpt && it->second == 2 && it->first == longOpt)
233  return it;
234  }
235  return iend;
236  }
Optlist args_
Definition: CmdLine.h:396
bool cmdline::CmdLine::has ( const char *  shortOpt,
const char *  longOpt = 0 
)
inline

Definition at line 306 of file CmdLine.h.

References spr::find(), and runEdmFileComparison::found.

Referenced by VisualizationOptions::load().

307  {
308  bool found = false;
309  for (Optlist::iterator it = find(shortOpt, longOpt);
310  it != args_.end(); it = find(shortOpt, longOpt))
311  {
312  found = true;
313  Optlist::iterator it0(it);
314  if (++it != args_.end())
315  if (it->second == 0)
316  it->second = 3;
317  args_.erase(it0);
318  }
319  return found;
320  }
Optlist::iterator find(const char *shortOpt, const char *longOpt)
Definition: CmdLine.h:225
Optlist args_
Definition: CmdLine.h:396
cmdline::CmdLine::operator void * ( ) const
inline

Definition at line 366 of file CmdLine.h.

367  {
368  return (void*)(static_cast<unsigned long>(nprogargs_));
369  }
unsigned nprogargs_
Definition: CmdLine.h:397
template<typename T >
CmdLine& cmdline::CmdLine::operator>> ( T obj)
inline

Definition at line 377 of file CmdLine.h.

References cmdline::CmdLineError::CmdLineError(), and haddnano::obj.

378  {
379  if (!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)
384  break;
385  OneShotIStream is(it->first);
386  args_.erase(it);
387  --nprogargs_;
388  is >> obj;
389  return *this;
390  }
unsigned nprogargs_
Definition: CmdLine.h:397
Optlist args_
Definition: CmdLine.h:396
void cmdline::CmdLine::optend ( ) const
inline

Definition at line 357 of file CmdLine.h.

References cmdline::CmdLineError::CmdLineError().

358  {
359  for (Optlist::const_iterator it = args_.begin();
360  it != args_.end(); ++it)
361  if (it->second == 1 || it->second == 2)
362  throw CmdLineError("invalid command line option \"")
363  << it->first << '"';
364  }
Optlist args_
Definition: CmdLine.h:396
OneShotIStream cmdline::CmdLine::option ( const char *  shortOpt,
const char *  longOpt = 0 
)
inline

Definition at line 322 of file CmdLine.h.

References cmdline::CmdLineError::CmdLineError(), spr::find(), and mps_fire::result.

Referenced by VisualizationOptions::load().

323  {
324  OneShotIStream result;
325  for (Optlist::iterator it = find(shortOpt, longOpt);
326  it != args_.end(); it = find(shortOpt, longOpt))
327  {
328  Optlist::iterator it0(it);
329  if (++it != args_.end())
330  if (it->second == 0)
331  {
332  result = OneShotIStream(it->first);
333  args_.erase(it0, ++it);
334  --nprogargs_;
335  continue;
336  }
337  throw CmdLineError()
338  << "missing command line argument for option \""
339  << it0->first << '"';
340  }
341  return result;
342  }
unsigned nprogargs_
Definition: CmdLine.h:397
Optlist::iterator find(const char *shortOpt, const char *longOpt)
Definition: CmdLine.h:225
Optlist args_
Definition: CmdLine.h:396
const char* cmdline::CmdLine::progname ( ) const
inline

Definition at line 304 of file CmdLine.h.

304 {return progname_.c_str();}
std::string progname_
Definition: CmdLine.h:395
OneShotIStream cmdline::CmdLine::require ( const char *  shortOpt,
const char *  longOpt = 0 
)
inline

Definition at line 344 of file CmdLine.h.

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

345  {
346  const OneShotIStream& is(option(shortOpt, longOpt));
347  if (!is.isValid())
348  {
349  const char empty[] = "";
350  const char* s = shortOpt ? shortOpt : (longOpt ? longOpt : empty);
351  throw CmdLineError()
352  << "required command line option \"" << s << "\" is missing";
353  }
354  return is;
355  }
OneShotIStream option(const char *shortOpt, const char *longOpt=0)
Definition: CmdLine.h:322

Member Data Documentation

Optlist cmdline::CmdLine::args_
private
unsigned cmdline::CmdLine::nprogargs_
private

Definition at line 397 of file CmdLine.h.

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

Definition at line 395 of file CmdLine.h.