Commit 6f51b4f218e013091248d260554390622ac4ce55

Authored by Jarryd Beck
1 parent b89dc24e

ready to test

src/cxxopts.cpp
... ... @@ -60,6 +60,37 @@ OptionAdder::operator()
60 60 }
61 61  
62 62 void
  63 +Options::parse_option
  64 +(
  65 + std::shared_ptr<OptionDetails> value,
  66 + const std::string& name,
  67 + const std::string& arg
  68 +)
  69 +{
  70 + auto& v = m_parsed[name];
  71 + value->parse("", v.value);
  72 + ++v.count;
  73 +}
  74 +
  75 +void
  76 +Options::checked_parse_arg
  77 +(
  78 + int argc,
  79 + char* argv[],
  80 + int argPos,
  81 + std::shared_ptr<OptionDetails> value,
  82 + const std::string& name
  83 +)
  84 +{
  85 + if (argPos >= argc)
  86 + {
  87 + throw missing_argument_exception(name);
  88 + }
  89 +
  90 + parse_option(value, name, argv[argPos]);
  91 +}
  92 +
  93 +void
63 94 Options::parse(int& argc, char**& argv)
64 95 {
65 96 int current = 1;
... ... @@ -75,6 +106,9 @@ Options::parse(int&amp; argc, char**&amp; argv)
75 106 {
76 107 //handle empty
77 108  
  109 + //for now, throw an exception
  110 + throw option_not_exists_exception(argv[current]);
  111 +
78 112 //if we return from here then it was parsed successfully, so continue
79 113 }
80 114 else
... ... @@ -92,7 +126,6 @@ Options::parse(int&amp; argc, char**&amp; argv)
92 126 if (iter == m_short.end())
93 127 {
94 128 throw option_not_exists_exception(name);
95   - //argument not found
96 129 }
97 130  
98 131 auto value = iter->second;
... ... @@ -100,15 +133,14 @@ Options::parse(int&amp; argc, char**&amp; argv)
100 133 //if no argument then just add it
101 134 if (!value->has_arg())
102 135 {
103   - auto& v = m_parsed[name];
104   - value->parse("", v.value);
105   - ++v.count;
  136 + parse_option(value, name);
106 137 }
107 138 else
108 139 {
109 140 //it must be the last argument
110 141 if (i + 1 == s.size())
111 142 {
  143 + checked_parse_arg(argc, argv, current+1, value, name);
112 144 }
113 145 else
114 146 {
... ... @@ -135,16 +167,26 @@ Options::parse(int&amp; argc, char**&amp; argv)
135 167 if (result[3].length() != 0)
136 168 {
137 169 //parse the option given
  170 +
  171 + //but if it doesn't take an argument, this is an error
  172 + if (!opt->has_arg())
  173 + {
  174 + throw option_not_has_argument_exception(name, result[3]);
  175 + }
  176 +
  177 + parse_option(opt, name, result[3]);
138 178 }
139 179 else
140 180 {
141 181 if (opt->has_arg())
142 182 {
143 183 //parse the next argument
  184 + checked_parse_arg(argc, argv, current + 1, opt, name);
144 185 }
145 186 else
146 187 {
147 188 //parse with empty argument
  189 + parse_option(opt, name);
148 190 }
149 191 }
150 192 }
... ...
src/cxxopts.hpp
... ... @@ -26,6 +26,7 @@ namespace cxxopts
26 26 void
27 27 parse(const std::string& text, any& result) const
28 28 {
  29 + result = true;
29 30 }
30 31  
31 32 bool
... ... @@ -107,6 +108,15 @@ namespace cxxopts
107 108 }
108 109 };
109 110  
  111 + class missing_argument_exception : public OptionParseException
  112 + {
  113 + public:
  114 + missing_argument_exception(const std::string& option)
  115 + : OptionParseException(u8"Option ‘" + option + u8"’ is missing an argument")
  116 + {
  117 + }
  118 + };
  119 +
110 120 class option_requires_argument_exception : public OptionParseException
111 121 {
112 122 public:
... ... @@ -116,6 +126,21 @@ namespace cxxopts
116 126 }
117 127 };
118 128  
  129 + class option_not_has_argument_exception : public OptionParseException
  130 + {
  131 + public:
  132 + option_not_has_argument_exception
  133 + (
  134 + const std::string& option,
  135 + const std::string& arg
  136 + )
  137 + : OptionParseException(
  138 + u8"Option ‘" + option + u8"’ does not take an argument, but argument‘"
  139 + + arg + "’ given")
  140 + {
  141 + }
  142 + };
  143 +
119 144 class option_not_present_exception : public OptionParseException
120 145 {
121 146 public:
... ... @@ -213,6 +238,25 @@ namespace cxxopts
213 238 private:
214 239 friend class OptionAdder;
215 240  
  241 + void
  242 + parse_option
  243 + (
  244 + std::shared_ptr<OptionDetails> value,
  245 + const std::string& name,
  246 + const std::string& arg = ""
  247 + );
  248 +
  249 + void
  250 + checked_parse_arg
  251 + (
  252 + int argc,
  253 + char* argv[],
  254 + int argPos,
  255 + std::shared_ptr<OptionDetails> value,
  256 + const std::string& name
  257 + );
  258 +
  259 +
216 260 std::map<std::string, std::shared_ptr<OptionDetails>> m_short;
217 261 std::map<std::string, std::shared_ptr<OptionDetails>> m_long;
218 262  
... ...