⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 getoopt.cpp

📁 Pegasus is an open-source implementationof the DMTF CIM and WBEM standards. It is designed to be por
💻 CPP
📖 第 1 页 / 共 2 页
字号:
  partsFromLongOpt(arg, name, value);  o.setName(name);  o.setType(Optarg::LONGFLAG);  o.setValue(value);}// Create an Optarg instance from a short flag String like//      -fValue// (The Value part is optional)static voidoptargFromShortOpt(Optarg &o, const char *arg) {  char name[2];  name[0] = arg[0];  name[1] = 0;  o.setName(name);  o.setType(Optarg::FLAG);  const char *p = arg + 1;  o.setValue(p);}// Look at a command line option and determine whether it is a// long flag, a short flag or an unflagged option.static intcatagorize(const char *s) {  if (s[0] != '-')    return 0;  else    if (s[1] == '-')      return 2;  return 1;}// Push an Optarg onto our arraystatic voidaddarg(getoopt::Arg_List&list, const Optarg &o) {  //o.print(cout);  list.append(o);}// Create an Optarg from its members and push it onto the arraystatic voidaddarg(getoopt::Arg_List&list, const String &name, Optarg::opttype type,	      const String &value) {  Optarg *o = new Optarg(name, type, value);  addarg(list, *o);  delete o;}// Take an array of arguments and append it to anotherstatic voidcopyargs(getoopt::Arg_List &out, const getoopt::Arg_List &in) {  Uint32 size = in.size();  for (Uint32 i = 0; i < size; i++) {    addarg(out, in[i]);  }}//------------------------------------// The parse method:  Way too long.// Note that flag args are pushed// onto the stack, then the regular// args are appended, sorting them// to the rear the way getopt() does.//------------------------------------Booleangetoopt::parse(int argc, char **argv) {  Optarg o;  int cat;  const flagspec *fs;  Arg_List nonflagargs;  enum states {START, ARGEXPECTED};  states state = START;  for (unsigned int i = 1; i < (unsigned int)argc; i++) {    unsigned int endsize = static_cast<unsigned int>(strlen(argv[i]));      switch (state) {      case START:        cat = catagorize(argv[i]);	switch (cat) {	case 0: // non-flag command line argument	  addarg(nonflagargs, "", Optarg::REGULAR, argv[i]);	  break;	case 1: // short (1-character) flag	  {	    unsigned int argpos = 1;	    while (argpos < endsize) {	      char c = argv[i][argpos];	      fs = getFlagspec(c);  // Short flag	      String temp = argv[i];	      String name = temp.subString(argpos, 1);	      if (!fs) {  // See if we recognize it	      	//l10n	      	MessageLoaderParms parms("getoopt.getoopt.UNKNOWN_FLAG",	      							 "Unknown flag $0$1",	      							 "-",	      							 name);	      	addError(MessageLoader::getMessage(parms));			//addError("Unknown flag -" + name);			//l10n end			argpos++;	      } else {		if (fs->argtype == NOARG) {  // Should this flag be bound		  addarg(_args, name, Optarg::FLAG,  "");  // NO		  argpos++;		} else { // YES -- the value is here or in the next arg		  optargFromShortOpt(o, &argv[i][argpos]);		  if (o.Value() == "") { // No value yet		    state = ARGEXPECTED;		  } else {		    addarg(_args, o);		  }		  argpos = endsize;		}	      }	    }	  } // end subcase 1	  break;	case 2:  // long (--xyz) flag	  {	    String arg = &(argv[i][2]);	    optargFromLongOpt(o, arg);	    fs = getFlagspec(o.getName());	    if (!fs) { // see if we recognize this flag	    	//l10n	      //String temp = "Unknown flag ";	      //addError(temp + o.getName());	      MessageLoaderParms parms("getoopt.getoopt.UNKNOWN_FLAG",	      						   "Unknown flag $0$1",	      						   "",	      						   o.getName());	      addError(MessageLoader::getMessage(parms));	      //l10n end	    } else {	        // this is a long flag we know about	      if (o.optarg() != ""  || fs->argtype != MUSTHAVEARG) {		addarg(_args, o);		state = START;  // we have a completed long flag	      } else {   // no value yet, and we expect one		if (fs->argtype == MUSTHAVEARG) {		  state = ARGEXPECTED;		}	      }	    }	    break;	  } // end subcase 2	} // end switch catagorize()	break; // end of case START      case ARGEXPECTED:	if (argv[i][0] == '-') {		//l10n	  //addError("Missing required value for flag " + o.getopt());	  MessageLoaderParms parms("getoopt.getoopt.MISSING_VALUE_FOR_FLAG",	      					   "Missing required value for flag $0",	      					   o.getopt());	  addError(MessageLoader::getMessage(parms));	  //l10n end	  i--;	} else {	  o.setValue(argv[i]);	}	addarg(_args, o);	state = START;	break;      } // end switch  } // end for  if (state != START) {  	//l10n    //addError("Missing required value for flag " + o.getName());    MessageLoaderParms parms("getoopt.getoopt.MISSING_VALUE_FOR_FLAG",	      					   "Missing required value for flag $0",	      					   o.getName());	  addError(MessageLoader::getMessage(parms));    //l10n end  }  copyargs(_args, nonflagargs);  return !_errorStrings.size();}//----------------------------------------------------------------------//         Methods to retrieve the command line arguments//----------------------------------------------------------------------//----------------------------------------------// Access the command line arguments by index//----------------------------------------------// Index operatorconst Optarg &getoopt::operator[](unsigned int n) {  unsigned int lim = _args.size();  if (n < lim)    return _args[n];  else    return _emptyopt;}// Return first indexunsigned intgetoopt::first() const { return 0; }// Return one past last indexunsigned intgetoopt::last() const { return _args.size(); }//-----------------------------------------------// Access the command line arguments ad-hoc//-----------------------------------------------// Return the number of times a short flag is set// on the command lineunsigned intgetoopt::isSet(char c) const {  unsigned int cnt = 0;  for (unsigned int i = 0; i < _args.size(); i++) {    const Optarg &o = _args[i];    if (o.getType() == Optarg::FLAG) {      const String &s = o.getopt();      if (s[0] == c) {	cnt++;      }    }  }  return cnt;}// Return the number of times any flag is set// on the command lineunsigned intgetoopt::isSet(const String &s) const {  unsigned int cnt = 0;  for (unsigned int i = 0; i < _args.size(); i++) {    const Optarg &o = _args[i];    if (o.getopt() == s) {      cnt++;    }  }  return cnt;}// Return the String value of the nth instance of// a particular short flag on the command lineconst String &getoopt::value(char opt, unsigned int idx) const {  unsigned int cnt = 0;  for (unsigned int i = 0; i < _args.size(); i++) {    const Optarg &o = _args[i];    if (o.getType() == Optarg::FLAG) {      const String &s = o.getopt();      if (s[0] == opt) {	if (cnt == idx) {	  return o.optarg();	} else {	  cnt++;	}      }    }  }  return(emptystring);}// Return the nth instance of any flag on the command lineconst String &getoopt::value(const String &opt, unsigned int idx) const {  unsigned int cnt = 0;  for (unsigned int i = 0; i < _args.size(); i++) {    const Optarg &o = _args[i];    if (o.optarg() == opt) {      if (cnt == idx) {	return o.getopt();      } else {	cnt++;      }    }  }  return(emptystring);}// Of the command line arguments, how many are flags?unsigned intgetoopt::flagcnt() const {  unsigned int cnt = 0;  for (Uint32 i = 0; i < _args.size(); i++) {    if (_args[i].getType() != Optarg::REGULAR)      cnt++;  }  return cnt;}// How many command line arguments were there?unsigned intgetoopt::size() const {  return _args.size();}// Return the list of command line arguments for use by// the program.const getoopt::Arg_List &getoopt::getArgs() const { return _args; }//-----------------------------------------------------------// Routines dealing with errors during parsing// FIXME:  This needs to be reworked so that the error text// is hidden and provided by the caller//----------------------------------------------------------// Add an error into the listvoidgetoopt::addError(const String &s){  _errorStrings.append(s);}// Return a list of the errorsconst getoopt::Error_List &getoopt::getErrorStrings() const {  return _errorStrings;}// Did any errors occur?Booleangetoopt::hasErrors() const {  return _errorStrings.size() ? true : false;}flagspec *getoopt::getFlagspecForUpdate(const String &s) {  for (unsigned int i = 0; i < _flagspecs.size(); i++) {    flagspec &o = _flagspecs[i];    if (o.islong && s == o.name)      return &_flagspecs[i];  }return 0;}const flagspec *getoopt::getFlagspec(const String &s) {  return (const flagspec *)getFlagspecForUpdate(s);}ostream &getoopt::printErrors(ostream &os) const {  for (Uint32 i = 0; i < _errorStrings.size(); i++) {    os << "> " << _errorStrings[i] << endl;  }  return os;}voidgetoopt::printErrors(String &s) const {  for (Uint32 i = 0; i < _errorStrings.size(); i++) {    s.append("> " + _errorStrings[i] + "\n");  }}//---------------------------------------------------------------//              Private methods//---------------------------------------------------------------flagspec *getoopt::getFlagspecForUpdate(char c) {  for (unsigned int i = 0; i < _flagspecs.size(); i++) {    flagspec &o = _flagspecs[i];    if (!o.islong && c == o.name[0])      return &_flagspecs[i];  }  return 0;}const flagspec *getoopt::getFlagspec(char c) {  return (const flagspec *)getFlagspecForUpdate(c);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -