cmdline_parser_cpp.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include <string>
  2. #include <stdexcept>
  3. #include "girstring.h"
  4. #include "casprintf.h"
  5. #include "cmdline_parser.h"
  6. #include "cmdline_parser.hpp"
  7. using namespace std;
  8. static enum optiontype
  9. optTypeConvert(
  10. CmdlineParser::optType const arg) {
  11. enum optiontype retval;
  12. retval = OPTTYPE_FLAG; // defeat compiler warning
  13. switch (arg) {
  14. case CmdlineParser::FLAG: retval = OPTTYPE_FLAG; break;
  15. case CmdlineParser::INT: retval = OPTTYPE_INT; break;
  16. case CmdlineParser::UINT: retval = OPTTYPE_UINT; break;
  17. case CmdlineParser::STRING: retval = OPTTYPE_STRING; break;
  18. case CmdlineParser::BINUINT: retval = OPTTYPE_BINUINT; break;
  19. case CmdlineParser::FLOAT: retval = OPTTYPE_FLOAT; break;
  20. }
  21. return retval;
  22. }
  23. CmdlineParser::CmdlineParser() {
  24. this->cp = cmd_createOptionParser();
  25. }
  26. CmdlineParser::~CmdlineParser() {
  27. cmd_destroyOptionParser(this->cp);
  28. }
  29. void
  30. CmdlineParser::defineOption(
  31. string const optionName,
  32. optType const optionType) {
  33. cmd_defineOption(this->cp, optionName.c_str(),
  34. optTypeConvert(optionType));
  35. }
  36. void
  37. CmdlineParser::processOptions(
  38. int const argc,
  39. const char ** const argv) {
  40. const char * error;
  41. cmd_processOptions(this->cp, argc, argv, &error);
  42. if (error) {
  43. string const errorS(error);
  44. strfree(error);
  45. throw(runtime_error(errorS));
  46. }
  47. }
  48. bool
  49. CmdlineParser::optionIsPresent(
  50. string const optionName) const {
  51. return (cmd_optionIsPresent(this->cp, optionName.c_str()) ? true : false);
  52. }
  53. int
  54. CmdlineParser::getOptionValueInt(
  55. string const optionName) const {
  56. return cmd_getOptionValueInt(this->cp, optionName.c_str());
  57. }
  58. unsigned int
  59. CmdlineParser::getOptionValueUint(
  60. string const optionName) const {
  61. return cmd_getOptionValueUint(this->cp, optionName.c_str());
  62. }
  63. unsigned long long
  64. CmdlineParser::getOptionValueBinUint(
  65. string const optionName) const {
  66. return cmd_getOptionValueBinUint(this->cp, optionName.c_str());
  67. }
  68. double
  69. CmdlineParser::getOptionValueFloat(
  70. string const optionName) const {
  71. return cmd_getOptionValueFloat(this->cp, optionName.c_str());
  72. }
  73. string
  74. CmdlineParser::getOptionValueString(
  75. string const optionName) const {
  76. const char * const value =
  77. cmd_getOptionValueString(this->cp, optionName.c_str());
  78. string retval;
  79. if (value) {
  80. retval = string(value);
  81. strfree(value);
  82. } else
  83. retval = "";
  84. return retval;
  85. }
  86. unsigned int
  87. CmdlineParser::argumentCount() const {
  88. return cmd_argumentCount(this->cp);
  89. }
  90. string
  91. CmdlineParser::getArgument(
  92. unsigned int const argNumber) const {
  93. const char * const value = cmd_getArgument(this->cp, argNumber);
  94. string const retval(value);
  95. strfree(value);
  96. return retval;
  97. }