version 1.32, 2006/01/30 16:18:39
|
version 1.33, 2007/06/19 17:58:04
|
|
|
// implementation of getoopt | // implementation of getoopt |
| |
#include <Pegasus/Common/PegasusVersion.h> | #include <Pegasus/Common/PegasusVersion.h> |
#include <Pegasus/Common/MessageLoader.h> //l10n |
#include <Pegasus/Common/MessageLoader.h> |
| |
#include "getoopt.h" | #include "getoopt.h" |
#include <cctype> | #include <cctype> |
|
|
| |
// Constructors | // Constructors |
// Default Constructor | // Default Constructor |
Optarg::Optarg() : _name(""), _opttype(REGULAR), _value("") { |
Optarg::Optarg() |
|
: _name(""), |
|
_opttype(REGULAR), |
|
_value("") |
|
{ |
} | } |
| |
// All-in-one | // All-in-one |
Optarg::Optarg(const String &name, opttype type, const String &value) : |
Optarg::Optarg( |
_name(name), _opttype(type), _value(value) { |
const String& name, |
|
opttype type, |
|
const String& value) |
|
: _name(name), |
|
_opttype(type), |
|
_value(value) |
|
{ |
} | } |
| |
// Destructor | // Destructor |
Optarg::~Optarg() {}; |
Optarg::~Optarg() |
|
{ |
|
} |
| |
//----------------------------------------------------------------------- | //----------------------------------------------------------------------- |
// Set the class members | // Set the class members |
//----------------------------------------------------------------------- | //----------------------------------------------------------------------- |
| |
// Set the _name member | // Set the _name member |
void |
void Optarg::setName(const String& name) |
Optarg::setName(const String &name) { |
{ |
_name = name; | _name = name; |
} | } |
| |
// Set the _opttype member | // Set the _opttype member |
void |
void Optarg::setType(opttype type) |
Optarg::setType(opttype type) { |
{ |
_opttype = type; | _opttype = type; |
} | } |
| |
// Set the _value member | // Set the _value member |
void |
void Optarg::setValue(const String& value) |
Optarg::setValue(const String &value) { |
{ |
_value = value; | _value = value; |
} | } |
| |
|
|
Optarg::getName() const { return _name; } | Optarg::getName() const { return _name; } |
| |
// Get the _name member using getopt() terminology | // Get the _name member using getopt() terminology |
const String & |
const String& Optarg::getopt() const |
Optarg::getopt() const { return _name; } |
{ |
|
return _name; |
|
} |
| |
// Get the _type member | // Get the _type member |
Optarg::opttype |
Optarg::opttype Optarg::getType() const |
Optarg::getType() const { return _opttype; } |
{ |
|
return _opttype; |
|
} |
| |
//------------------------------------------------------------------- | //------------------------------------------------------------------- |
// Ways to get the _value member | // Ways to get the _value member |
//------------------------------------------------------------------- | //------------------------------------------------------------------- |
| |
// Return _value as const String ref | // Return _value as const String ref |
const String & |
const String& Optarg::Value() const |
Optarg::Value() const { return _value; } |
{ |
|
return _value; |
|
} |
| |
// Same thing as Value(), but using getopt() terminology | // Same thing as Value(), but using getopt() terminology |
const String & |
const String& Optarg::optarg() const |
Optarg::optarg() const { return _value; } |
{ |
|
return _value; |
|
} |
| |
// Fill in a caller-provided String | // Fill in a caller-provided String |
void |
void Optarg::Value(String& s) const |
Optarg::Value(String &s) const { s = _value; } |
{ |
|
s = _value; |
|
} |
| |
// Fill in a caller-provided int with the integer conversion of the value. | // Fill in a caller-provided int with the integer conversion of the value. |
void Optarg::Value (int &i) const | void Optarg::Value (int &i) const |
|
|
} | } |
| |
// Fill in a caller-provided long | // Fill in a caller-provided long |
void |
void Optarg::Value(long& l) const |
Optarg::Value(long &l) const { |
{ |
l = (long)atoi(_value.getCString()); | l = (long)atoi(_value.getCString()); |
} | } |
| |
// Fill in a caller-provided unsigned long | // Fill in a caller-provided unsigned long |
void |
void Optarg::Value(unsigned long& l) const |
Optarg::Value(unsigned long &l) const { |
{ |
l = (unsigned long)atoi(_value.getCString()); | l = (unsigned long)atoi(_value.getCString()); |
} | } |
| |
// Fill in a caller-provided double | // Fill in a caller-provided double |
void |
void Optarg::Value(double& d) const |
Optarg::Value(double &d) const { |
{ |
d = (double)atof(_value.getCString()); | d = (double)atof(_value.getCString()); |
} | } |
| |
|
|
//-------------------------------------------------------------------- | //-------------------------------------------------------------------- |
| |
// Is the option value is bound to a flag? | // Is the option value is bound to a flag? |
Boolean |
Boolean Optarg::isFlag() const |
Optarg::isFlag() const { return (_opttype == FLAG || _opttype == LONGFLAG); } |
{ |
|
return (_opttype == FLAG || _opttype == LONGFLAG); |
|
} |
| |
// Is it bound to a long-named flag? | // Is it bound to a long-named flag? |
Boolean |
Boolean Optarg::isLongFlag() const |
Optarg::isLongFlag() const { return (_opttype == LONGFLAG); } |
{ |
|
return (_opttype == LONGFLAG); |
|
} |
| |
//----------------------------------------------------------------------- | //----------------------------------------------------------------------- |
// print the members as a formatted String | // print the members as a formatted String |
//----------------------------------------------------------------------- | //----------------------------------------------------------------------- |
ostream & |
ostream& Optarg::print(ostream& os) const |
Optarg::print(ostream &os) const { |
{ |
os << "{name:(" << getName(); | os << "{name:(" << getName(); |
os << ") type:("; | os << ") type:("; |
switch (getType()) { |
switch (getType()) |
|
{ |
case FLAG: os << "FLAG"; break; | case FLAG: os << "FLAG"; break; |
case LONGFLAG: os << "LONGFLAG"; break; | case LONGFLAG: os << "LONGFLAG"; break; |
case REGULAR: os << "REGULAR"; break; | case REGULAR: os << "REGULAR"; break; |
|
|
// a getopt() optstring | // a getopt() optstring |
getoopt::getoopt(const char *optstring) | getoopt::getoopt(const char *optstring) |
{ | { |
if (optstring) { |
if (optstring) |
|
{ |
addFlagspec(optstring); | addFlagspec(optstring); |
} | } |
} | } |
| |
getoopt::~getoopt() {;} |
getoopt::~getoopt() |
|
{ |
|
} |
| |
//---------------------------------------------------------------------- | //---------------------------------------------------------------------- |
// methods to register program-defined flags and their characteristics | // methods to register program-defined flags and their characteristics |
|
|
| |
// Parse through a getopt() optstring and create flagspecs from each | // Parse through a getopt() optstring and create flagspecs from each |
// short flag. | // short flag. |
Boolean |
Boolean getoopt::addFlagspec(const String& opt) |
getoopt::addFlagspec(const String &opt) { |
{ |
unsigned int size = opt.size(); | unsigned int size = opt.size(); |
if (size == 0) | if (size == 0) |
return false; | return false; |
for (unsigned int i = 0; i < size; i++) { |
for (unsigned int i = 0; i < size; i++) |
|
{ |
char c = static_cast<char>(opt[i]); | char c = static_cast<char>(opt[i]); |
if ( ((i + 1) < size) && (opt[i+1] == ':') ) { |
if ( ((i + 1) < size) && (opt[i+1] == ':') ) |
if (!(addFlagspec(c, true))) { |
{ |
|
if (!(addFlagspec(c, true))) |
|
{ |
return false; | return false; |
} | } |
++i; | ++i; |
} else { |
} |
|
else |
|
{ |
if (!(addFlagspec(c, false))) | if (!(addFlagspec(c, false))) |
return false; | return false; |
} | } |
|
|
} | } |
| |
// Create a filespec from a single short flag and push it onto the array | // Create a filespec from a single short flag and push it onto the array |
Boolean |
Boolean getoopt::addFlagspec(char flag, Boolean hasarg) |
getoopt::addFlagspec(char flag, Boolean hasarg) { |
{ |
if (flag == '*') { |
if (flag == '*') |
//l10n |
{ |
MessageLoaderParms parms("getoopt.getoopt.CANT_NAME_FLAG", |
MessageLoaderParms parms( |
|
"getoopt.getoopt.CANT_NAME_FLAG", |
"You can't have a flag named '$0'", | "You can't have a flag named '$0'", |
flag); | flag); |
addError(MessageLoader::getMessage(parms)); | addError(MessageLoader::getMessage(parms)); |
//addError("You can't have a flag named '*'"); |
|
//l10n end |
|
return false; | return false; |
} | } |
flagspec fs; | flagspec fs; |
|
|
} | } |
| |
// Create a flagspec from a single long flag and push it onto the array | // Create a flagspec from a single long flag and push it onto the array |
Boolean |
Boolean getoopt::addLongFlagspec( |
getoopt::addLongFlagspec(const String &name, argtype type) { |
const String& name, |
|
argtype type) |
|
{ |
flagspec fs; | flagspec fs; |
| |
// Changing "fs.name = name" to the following line masks an ugly crash | // Changing "fs.name = name" to the following line masks an ugly crash |
|
|
} | } |
| |
// Unregister a flagspec | // Unregister a flagspec |
Boolean |
Boolean getoopt::removeFlagspec(char opt) |
getoopt::removeFlagspec(char opt) { |
{ |
flagspec *fs = getFlagspecForUpdate(opt); | flagspec *fs = getFlagspecForUpdate(opt); |
if (fs) { |
if (fs) |
|
{ |
fs->active = false; | fs->active = false; |
return true; | return true; |
} | } |
|
|
// Parse out the flagname and the value from a long flag option that | // Parse out the flagname and the value from a long flag option that |
// may be in the form | // may be in the form |
// --longflag=value | // --longflag=value |
static void |
static void partsFromLongOpt( |
partsFromLongOpt (const String &s, String &name, String &value) { |
const String& s, |
for (unsigned int i = 0; i < s.size(); i++) { |
String& name, |
if (s[i] == '=') { |
String& value) |
|
{ |
|
for (unsigned int i = 0; i < s.size(); i++) |
|
{ |
|
if (s[i] == '=') |
|
{ |
name = s.subString(0, i); | name = s.subString(0, i); |
value = s.subString(i+1); | value = s.subString(i+1); |
return; | return; |
|
|
// Create an Optarg instance from a long flag String like | // Create an Optarg instance from a long flag String like |
// --longflag=value | // --longflag=value |
// (The =value is optional). | // (The =value is optional). |
static void |
static void optargFromLongOpt( |
optargFromLongOpt(Optarg &o, const String &arg) { |
Optarg& o, |
|
const String& arg) |
|
{ |
String name; | String name; |
String value; | String value; |
partsFromLongOpt(arg, name, value); | partsFromLongOpt(arg, name, value); |
|
|
// Create an Optarg instance from a short flag String like | // Create an Optarg instance from a short flag String like |
// -fValue | // -fValue |
// (The Value part is optional) | // (The Value part is optional) |
static void |
static void optargFromShortOpt( |
optargFromShortOpt(Optarg &o, const char *arg) { |
Optarg& o, |
|
const char* arg) |
|
{ |
char name[2]; | char name[2]; |
name[0] = arg[0]; | name[0] = arg[0]; |
name[1] = 0; | name[1] = 0; |
|
|
| |
// Look at a command line option and determine whether it is a | // Look at a command line option and determine whether it is a |
// long flag, a short flag or an unflagged option. | // long flag, a short flag or an unflagged option. |
static int |
static int catagorize(const char* s) |
catagorize(const char *s) { |
{ |
if (s[0] != '-') | if (s[0] != '-') |
return 0; | return 0; |
else | else |
|
|
} | } |
| |
// Push an Optarg onto our array | // Push an Optarg onto our array |
static void |
static void addarg( |
addarg(getoopt::Arg_List&list, const Optarg &o) { |
getoopt::Arg_List&list, |
|
const Optarg& o) |
|
{ |
//o.print(cout); | //o.print(cout); |
list.append(o); | list.append(o); |
} | } |
| |
// Create an Optarg from its members and push it onto the array | // Create an Optarg from its members and push it onto the array |
static void |
static void addarg( |
addarg(getoopt::Arg_List&list, const String &name, Optarg::opttype type, |
getoopt::Arg_List& list, |
const String &value) { |
const String& name, |
|
Optarg::opttype type, |
|
const String& value) |
|
{ |
Optarg *o = new Optarg(name, type, value); | Optarg *o = new Optarg(name, type, value); |
addarg(list, *o); | addarg(list, *o); |
delete o; | delete o; |
} | } |
| |
// Take an array of arguments and append it to another | // Take an array of arguments and append it to another |
static void |
static void copyargs( |
copyargs(getoopt::Arg_List &out, const getoopt::Arg_List &in) { |
getoopt::Arg_List& out, |
|
const getoopt::Arg_List& in) |
|
{ |
Uint32 size = in.size(); | Uint32 size = in.size(); |
for (Uint32 i = 0; i < size; i++) { |
for (Uint32 i = 0; i < size; i++) |
|
{ |
addarg(out, in[i]); | addarg(out, in[i]); |
} | } |
} | } |
|
|
// args are appended, sorting them | // args are appended, sorting them |
// to the rear the way getopt() does. | // to the rear the way getopt() does. |
//------------------------------------ | //------------------------------------ |
Boolean |
Boolean getoopt::parse( |
getoopt::parse(int argc, char **argv) { |
int argc, |
|
char** argv) |
|
{ |
Optarg o; | Optarg o; |
int cat; | int cat; |
const flagspec *fs; | const flagspec *fs; |
Arg_List nonflagargs; | Arg_List nonflagargs; |
enum states {START, ARGEXPECTED}; | enum states {START, ARGEXPECTED}; |
states state = START; | states state = START; |
for (unsigned int i = 1; i < (unsigned int)argc; i++) { |
for (unsigned int i = 1; i < (unsigned int)argc; i++) |
|
{ |
unsigned int endsize = static_cast<unsigned int>(strlen(argv[i])); | unsigned int endsize = static_cast<unsigned int>(strlen(argv[i])); |
switch (state) { |
switch (state) |
|
{ |
case START: | case START: |
cat = catagorize(argv[i]); | cat = catagorize(argv[i]); |
switch (cat) { |
switch (cat) |
|
{ |
case 0: // non-flag command line argument | case 0: // non-flag command line argument |
addarg(nonflagargs, "", Optarg::REGULAR, argv[i]); | addarg(nonflagargs, "", Optarg::REGULAR, argv[i]); |
break; | break; |
case 1: // short (1-character) flag | case 1: // short (1-character) flag |
{ | { |
unsigned int argpos = 1; | unsigned int argpos = 1; |
while (argpos < endsize) { |
while (argpos < endsize) |
|
{ |
char c = argv[i][argpos]; | char c = argv[i][argpos]; |
fs = getFlagspec(c); // Short flag | fs = getFlagspec(c); // Short flag |
String temp = argv[i]; | String temp = argv[i]; |
String name = temp.subString(argpos, 1); | String name = temp.subString(argpos, 1); |
if (!fs) { // See if we recognize it |
// See if we recognize it |
//l10n |
if (!fs) |
MessageLoaderParms parms("getoopt.getoopt.UNKNOWN_FLAG", |
{ |
|
MessageLoaderParms parms( |
|
"getoopt.getoopt.UNKNOWN_FLAG", |
"Unknown flag $0$1", | "Unknown flag $0$1", |
"-", | "-", |
name); | name); |
addError(MessageLoader::getMessage(parms)); | addError(MessageLoader::getMessage(parms)); |
//addError("Unknown flag -" + name); |
|
//l10n end |
|
argpos++; | argpos++; |
} else { |
} |
if (fs->argtype == NOARG) { // Should this flag be bound |
else |
addarg(_args, name, Optarg::FLAG, ""); // NO |
{ |
|
// Should this flag be bound? |
|
if (fs->argtype == NOARG) |
|
{ |
|
// NO |
|
addarg(_args, name, Optarg::FLAG, ""); |
argpos++; | argpos++; |
} else { // YES -- the value is here or in the next arg |
} |
|
else |
|
{ |
|
// YES -- the value is here or in the next arg |
optargFromShortOpt(o, &argv[i][argpos]); | optargFromShortOpt(o, &argv[i][argpos]); |
if (o.Value() == "") { // No value yet |
if (o.Value() == "") |
|
{ |
|
// No value yet |
state = ARGEXPECTED; | state = ARGEXPECTED; |
} else { |
} |
|
else |
|
{ |
addarg(_args, o); | addarg(_args, o); |
} | } |
argpos = endsize; | argpos = endsize; |
} | } |
} | } |
} | } |
} // end subcase 1 |
|
break; | break; |
|
} // end subcase 1 |
case 2: // long (--xyz) flag | case 2: // long (--xyz) flag |
{ | { |
String arg = &(argv[i][2]); | String arg = &(argv[i][2]); |
optargFromLongOpt(o, arg); | optargFromLongOpt(o, arg); |
fs = getFlagspec(o.getName()); | fs = getFlagspec(o.getName()); |
if (!fs) { // see if we recognize this flag |
// see if we recognize this flag |
//l10n |
if (!fs) |
//String temp = "Unknown flag "; |
{ |
//addError(temp + o.getName()); |
MessageLoaderParms parms( |
MessageLoaderParms parms("getoopt.getoopt.UNKNOWN_FLAG", |
"getoopt.getoopt.UNKNOWN_FLAG", |
"Unknown flag $0$1", | "Unknown flag $0$1", |
"", | "", |
o.getName()); | o.getName()); |
addError(MessageLoader::getMessage(parms)); | addError(MessageLoader::getMessage(parms)); |
//l10n end |
} |
} else { |
else |
|
{ |
// this is a long flag we know about | // this is a long flag we know about |
if (o.optarg() != "" || fs->argtype != MUSTHAVEARG) { |
if (o.optarg() != "" || fs->argtype != MUSTHAVEARG) |
|
{ |
addarg(_args, o); | addarg(_args, o); |
state = START; // we have a completed long flag | state = START; // we have a completed long flag |
} else { // no value yet, and we expect one |
} |
if (fs->argtype == MUSTHAVEARG) { |
else |
|
{ |
|
// no value yet, and we expect one |
|
if (fs->argtype == MUSTHAVEARG) |
|
{ |
state = ARGEXPECTED; | state = ARGEXPECTED; |
} | } |
} | } |
|
|
break; // end of case START | break; // end of case START |
| |
case ARGEXPECTED: | case ARGEXPECTED: |
if (argv[i][0] == '-') { |
if (argv[i][0] == '-') |
//l10n |
{ |
//addError("Missing required value for flag " + o.getopt()); |
MessageLoaderParms parms( |
MessageLoaderParms parms("getoopt.getoopt.MISSING_VALUE_FOR_FLAG", |
"getoopt.getoopt.MISSING_VALUE_FOR_FLAG", |
"Missing required value for flag $0", | "Missing required value for flag $0", |
o.getopt()); | o.getopt()); |
addError(MessageLoader::getMessage(parms)); | addError(MessageLoader::getMessage(parms)); |
//l10n end |
|
i--; | i--; |
} else { |
} |
|
else |
|
{ |
o.setValue(argv[i]); | o.setValue(argv[i]); |
} | } |
addarg(_args, o); | addarg(_args, o); |
|
|
break; | break; |
} // end switch | } // end switch |
} // end for | } // end for |
if (state != START) { |
if (state != START) |
//l10n |
{ |
//addError("Missing required value for flag " + o.getName()); |
MessageLoaderParms parms( |
MessageLoaderParms parms("getoopt.getoopt.MISSING_VALUE_FOR_FLAG", |
"getoopt.getoopt.MISSING_VALUE_FOR_FLAG", |
"Missing required value for flag $0", | "Missing required value for flag $0", |
o.getName()); | o.getName()); |
addError(MessageLoader::getMessage(parms)); | addError(MessageLoader::getMessage(parms)); |
//l10n end |
|
} | } |
copyargs(_args, nonflagargs); | copyargs(_args, nonflagargs); |
return !_errorStrings.size(); | return !_errorStrings.size(); |
|
|
//---------------------------------------------- | //---------------------------------------------- |
| |
// Index operator | // Index operator |
const Optarg & |
const Optarg& getoopt::operator[](unsigned int n) |
getoopt::operator[](unsigned int n) { |
{ |
unsigned int lim = _args.size(); | unsigned int lim = _args.size(); |
if (n < lim) | if (n < lim) |
return _args[n]; | return _args[n]; |
|
|
} | } |
| |
// Return first index | // Return first index |
unsigned int |
unsigned int getoopt::first() const |
getoopt::first() const { return 0; } |
{ |
|
return 0; |
|
} |
| |
// Return one past last index | // Return one past last index |
unsigned int |
unsigned int getoopt::last() const |
getoopt::last() const { return _args.size(); } |
{ |
|
return _args.size(); |
|
} |
| |
//----------------------------------------------- | //----------------------------------------------- |
// Access the command line arguments ad-hoc | // Access the command line arguments ad-hoc |
|
|
| |
// Return the number of times a short flag is set | // Return the number of times a short flag is set |
// on the command line | // on the command line |
unsigned int |
unsigned int getoopt::isSet(char c) const |
getoopt::isSet(char c) const { |
{ |
unsigned int cnt = 0; | unsigned int cnt = 0; |
for (unsigned int i = 0; i < _args.size(); i++) { |
for (unsigned int i = 0; i < _args.size(); i++) |
|
{ |
const Optarg &o = _args[i]; | const Optarg &o = _args[i]; |
if (o.getType() == Optarg::FLAG) { |
if (o.getType() == Optarg::FLAG) |
|
{ |
const String &s = o.getopt(); | const String &s = o.getopt(); |
if (s[0] == c) { |
if (s[0] == c) |
|
{ |
cnt++; | cnt++; |
} | } |
} | } |
|
|
| |
// Return the number of times any flag is set | // Return the number of times any flag is set |
// on the command line | // on the command line |
unsigned int |
unsigned int getoopt::isSet(const String& s) const |
getoopt::isSet(const String &s) const { |
{ |
unsigned int cnt = 0; | unsigned int cnt = 0; |
for (unsigned int i = 0; i < _args.size(); i++) { |
for (unsigned int i = 0; i < _args.size(); i++) |
|
{ |
const Optarg &o = _args[i]; | const Optarg &o = _args[i]; |
if (o.getopt() == s) { |
if (o.getopt() == s) |
|
{ |
cnt++; | cnt++; |
} | } |
} | } |
|
|
| |
// Return the String value of the nth instance of | // Return the String value of the nth instance of |
// a particular short flag on the command line | // a particular short flag on the command line |
const String & |
const String& getoopt::value( |
getoopt::value(char opt, unsigned int idx) const { |
char opt, |
|
unsigned int idx) const |
|
{ |
unsigned int cnt = 0; | unsigned int cnt = 0; |
for (unsigned int i = 0; i < _args.size(); i++) { |
for (unsigned int i = 0; i < _args.size(); i++) |
|
{ |
const Optarg &o = _args[i]; | const Optarg &o = _args[i]; |
if (o.getType() == Optarg::FLAG) { |
if (o.getType() == Optarg::FLAG) |
|
{ |
const String &s = o.getopt(); | const String &s = o.getopt(); |
if (s[0] == opt) { |
if (s[0] == opt) |
if (cnt == idx) { |
{ |
|
if (cnt == idx) |
|
{ |
return o.optarg(); | return o.optarg(); |
} else { |
} |
|
else |
|
{ |
cnt++; | cnt++; |
} | } |
} | } |
} | } |
} | } |
return(emptystring); |
return emptystring; |
} | } |
| |
// Return the nth instance of any flag on the command line | // Return the nth instance of any flag on the command line |
const String & |
const String& getoopt::value( |
getoopt::value(const String &opt, unsigned int idx) const { |
const String& opt, |
|
unsigned int idx) const |
|
{ |
unsigned int cnt = 0; | unsigned int cnt = 0; |
for (unsigned int i = 0; i < _args.size(); i++) { |
for (unsigned int i = 0; i < _args.size(); i++) |
|
{ |
const Optarg &o = _args[i]; | const Optarg &o = _args[i]; |
if (o.optarg() == opt) { |
if (o.optarg() == opt) |
if (cnt == idx) { |
{ |
|
if (cnt == idx) |
|
{ |
return o.getopt(); | return o.getopt(); |
} else { |
} |
|
else |
|
{ |
cnt++; | cnt++; |
} | } |
} | } |
} | } |
return(emptystring); |
return emptystring; |
} | } |
| |
// Of the command line arguments, how many are flags? | // Of the command line arguments, how many are flags? |
unsigned int |
unsigned int getoopt::flagcnt() const |
getoopt::flagcnt() const { |
{ |
unsigned int cnt = 0; | unsigned int cnt = 0; |
for (Uint32 i = 0; i < _args.size(); i++) { |
for (Uint32 i = 0; i < _args.size(); i++) |
|
{ |
if (_args[i].getType() != Optarg::REGULAR) | if (_args[i].getType() != Optarg::REGULAR) |
cnt++; | cnt++; |
} | } |
|
|
} | } |
| |
// How many command line arguments were there? | // How many command line arguments were there? |
unsigned int |
unsigned int getoopt::size() const |
getoopt::size() const { |
{ |
return _args.size(); | return _args.size(); |
} | } |
| |
// Return the list of command line arguments for use by | // Return the list of command line arguments for use by |
// the program. | // the program. |
const getoopt::Arg_List & |
const getoopt::Arg_List& getoopt::getArgs() const |
getoopt::getArgs() const { return _args; } |
{ |
|
return _args; |
|
} |
| |
| |
//----------------------------------------------------------- | //----------------------------------------------------------- |
|
|
//---------------------------------------------------------- | //---------------------------------------------------------- |
| |
// Add an error into the list | // Add an error into the list |
void |
void getoopt::addError(const String& s) |
getoopt::addError(const String &s) |
|
{ | { |
_errorStrings.append(s); | _errorStrings.append(s); |
} | } |
| |
// Return a list of the errors | // Return a list of the errors |
const getoopt::Error_List & |
const getoopt::Error_List& getoopt::getErrorStrings() const |
getoopt::getErrorStrings() const { |
{ |
return _errorStrings; | return _errorStrings; |
} | } |
| |
// Did any errors occur? | // Did any errors occur? |
Boolean |
Boolean getoopt::hasErrors() const |
getoopt::hasErrors() const { |
{ |
return _errorStrings.size() ? true : false; | return _errorStrings.size() ? true : false; |
} | } |
| |
| |
| |
flagspec * |
flagspec* getoopt::getFlagspecForUpdate(const String& s) |
getoopt::getFlagspecForUpdate(const String &s) { |
{ |
for (unsigned int i = 0; i < _flagspecs.size(); i++) { |
for (unsigned int i = 0; i < _flagspecs.size(); i++) |
|
{ |
flagspec &o = _flagspecs[i]; | flagspec &o = _flagspecs[i]; |
if (o.islong && s == o.name) | if (o.islong && s == o.name) |
return &_flagspecs[i]; | return &_flagspecs[i]; |
|
|
return 0; | return 0; |
} | } |
| |
const flagspec * |
const flagspec* getoopt::getFlagspec(const String& s) |
getoopt::getFlagspec(const String &s) { |
{ |
return (const flagspec *)getFlagspecForUpdate(s); | return (const flagspec *)getFlagspecForUpdate(s); |
} | } |
| |
ostream & |
ostream& getoopt::printErrors(ostream& os) const |
getoopt::printErrors(ostream &os) const { |
{ |
for (Uint32 i = 0; i < _errorStrings.size(); i++) { |
for (Uint32 i = 0; i < _errorStrings.size(); i++) |
|
{ |
os << "> " << _errorStrings[i] << endl; | os << "> " << _errorStrings[i] << endl; |
} | } |
return os; | return os; |
} | } |
| |
void |
void getoopt::printErrors(String& s) const |
getoopt::printErrors(String &s) const { |
{ |
for (Uint32 i = 0; i < _errorStrings.size(); i++) { |
for (Uint32 i = 0; i < _errorStrings.size(); i++) |
|
{ |
s.append("> " + _errorStrings[i] + "\n"); | s.append("> " + _errorStrings[i] + "\n"); |
} | } |
} | } |
|
|
//--------------------------------------------------------------- | //--------------------------------------------------------------- |
// Private methods | // Private methods |
//--------------------------------------------------------------- | //--------------------------------------------------------------- |
flagspec * |
flagspec* getoopt::getFlagspecForUpdate(char c) |
getoopt::getFlagspecForUpdate(char c) { |
{ |
for (unsigned int i = 0; i < _flagspecs.size(); i++) { |
for (unsigned int i = 0; i < _flagspecs.size(); i++) |
|
{ |
flagspec &o = _flagspecs[i]; | flagspec &o = _flagspecs[i]; |
if (!o.islong && c == o.name[0]) | if (!o.islong && c == o.name[0]) |
return &_flagspecs[i]; | return &_flagspecs[i]; |
|
|
return 0; | return 0; |
} | } |
| |
const flagspec * |
const flagspec* getoopt::getFlagspec(char c) |
getoopt::getFlagspec(char c) { |
{ |
return (const flagspec *)getFlagspecForUpdate(c); | return (const flagspec *)getFlagspecForUpdate(c); |
} | } |