Commit 056a6281ace938b50948648bb4cdb8b52a659ec2

Authored by spmn
Committed by GitHub
1 parent c04f8a5b

Fix empty option name in `OptionException` (#256)

Fix bug where option name is missing in error.
Showing 1 changed file with 26 additions and 4 deletions
include/cxxopts.hpp
... ... @@ -487,7 +487,7 @@ namespace cxxopts
487 487 public:
488 488 explicit option_has_no_value_exception(const std::string& option)
489 489 : OptionException(
490   - option.empty() ?
  490 + !option.empty() ?
491 491 ("Option " + LQUOTE + option + RQUOTE + " has no value") :
492 492 "Option has no value")
493 493 {
... ... @@ -1380,13 +1380,19 @@ namespace cxxopts
1380 1380 m_value->parse();
1381 1381 }
1382 1382  
  1383 + void
  1384 + parse_no_value(const std::shared_ptr<const OptionDetails>& details)
  1385 + {
  1386 + m_long_name = &details->long_name();
  1387 + }
  1388 +
1383 1389 #if defined(__GNUC__)
1384 1390 #if __GNUC__ <= 10 && __GNUC_MINOR__ <= 1
1385 1391 #pragma GCC diagnostic push
1386 1392 #pragma GCC diagnostic ignored "-Werror=null-dereference"
1387 1393 #endif
1388 1394 #endif
1389   -
  1395 +
1390 1396 CXXOPTS_NODISCARD
1391 1397 size_t
1392 1398 count() const noexcept
... ... @@ -1625,6 +1631,9 @@ namespace cxxopts
1625 1631 void
1626 1632 parse_default(const std::shared_ptr<OptionDetails>& details);
1627 1633  
  1634 + void
  1635 + parse_no_value(const std::shared_ptr<OptionDetails>& details);
  1636 +
1628 1637 private:
1629 1638  
1630 1639 void finalise_aliases();
... ... @@ -2083,6 +2092,14 @@ OptionParser::parse_default(const std::shared_ptr&lt;OptionDetails&gt;&amp; details)
2083 2092  
2084 2093 inline
2085 2094 void
  2095 +OptionParser::parse_no_value(const std::shared_ptr<OptionDetails>& details)
  2096 +{
  2097 + auto& store = m_parsed[details->hash()];
  2098 + store.parse_no_value(details);
  2099 +}
  2100 +
  2101 +inline
  2102 +void
2086 2103 OptionParser::parse_option
2087 2104 (
2088 2105 const std::shared_ptr<OptionDetails>& value,
... ... @@ -2332,8 +2349,13 @@ OptionParser::parse(int argc, const char* const* argv)
2332 2349  
2333 2350 auto& store = m_parsed[detail->hash()];
2334 2351  
2335   - if(value.has_default() && !store.count() && !store.has_default()){
2336   - parse_default(detail);
  2352 + if (value.has_default()) {
  2353 + if (!store.count() && !store.has_default()) {
  2354 + parse_default(detail);
  2355 + }
  2356 + }
  2357 + else {
  2358 + parse_no_value(detail);
2337 2359 }
2338 2360 }
2339 2361  
... ...