1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
#include <string>
#include <stdexcept>
#include "girstring.h"
#include "casprintf.h"
#include "cmdline_parser.h"
#include "cmdline_parser.hpp"
using namespace std;
static enum optiontype
optTypeConvert(
CmdlineParser::optType const arg) {
enum optiontype retval;
retval = OPTTYPE_FLAG; // defeat compiler warning
switch (arg) {
case CmdlineParser::FLAG: retval = OPTTYPE_FLAG; break;
case CmdlineParser::INT: retval = OPTTYPE_INT; break;
case CmdlineParser::UINT: retval = OPTTYPE_UINT; break;
case CmdlineParser::STRING: retval = OPTTYPE_STRING; break;
case CmdlineParser::BINUINT: retval = OPTTYPE_BINUINT; break;
case CmdlineParser::FLOAT: retval = OPTTYPE_FLOAT; break;
}
return retval;
}
CmdlineParser::CmdlineParser() {
this->cp = cmd_createOptionParser();
}
CmdlineParser::~CmdlineParser() {
cmd_destroyOptionParser(this->cp);
}
void
CmdlineParser::defineOption(
string const optionName,
optType const optionType) {
cmd_defineOption(this->cp, optionName.c_str(),
optTypeConvert(optionType));
}
void
CmdlineParser::processOptions(
int const argc,
const char ** const argv) {
const char * error;
cmd_processOptions(this->cp, argc, argv, &error);
if (error) {
string const errorS(error);
strfree(error);
throw(runtime_error(errorS));
}
}
bool
CmdlineParser::optionIsPresent(
string const optionName) const {
return (cmd_optionIsPresent(this->cp, optionName.c_str()) ? true : false);
}
int
CmdlineParser::getOptionValueInt(
string const optionName) const {
return cmd_getOptionValueInt(this->cp, optionName.c_str());
}
unsigned int
CmdlineParser::getOptionValueUint(
string const optionName) const {
return cmd_getOptionValueUint(this->cp, optionName.c_str());
}
unsigned long long
CmdlineParser::getOptionValueBinUint(
string const optionName) const {
return cmd_getOptionValueBinUint(this->cp, optionName.c_str());
}
double
CmdlineParser::getOptionValueFloat(
string const optionName) const {
return cmd_getOptionValueFloat(this->cp, optionName.c_str());
}
string
CmdlineParser::getOptionValueString(
string const optionName) const {
const char * const value =
cmd_getOptionValueString(this->cp, optionName.c_str());
string retval;
if (value) {
retval = string(value);
strfree(value);
} else
retval = "";
return retval;
}
unsigned int
CmdlineParser::argumentCount() const {
return cmd_argumentCount(this->cp);
}
string
CmdlineParser::getArgument(
unsigned int const argNumber) const {
const char * const value = cmd_getArgument(this->cp, argNumber);
string const retval(value);
strfree(value);
return retval;
}
|