Commit 3ef9fddc7b643b2bbd85a3d5788083004c183533

Authored by jarro2783
Committed by GitHub
1 parent 4b63c333

Fix passing a const array to parse (#258)

Fixes #257. The input array is not modified, so we can declare this as
`char const* const*`.
include/cxxopts.hpp
... ... @@ -1325,7 +1325,7 @@ namespace cxxopts
1325 1325 }
1326 1326  
1327 1327 ParseResult
1328   - parse(int argc, const char** argv);
  1328 + parse(int argc, const char* const* argv);
1329 1329  
1330 1330 bool
1331 1331 consume_positional(const std::string& a, PositionalListIterator& next);
... ... @@ -1334,7 +1334,7 @@ namespace cxxopts
1334 1334 checked_parse_arg
1335 1335 (
1336 1336 int argc,
1337   - const char* argv[],
  1337 + const char* const* argv,
1338 1338 int& current,
1339 1339 const std::shared_ptr<OptionDetails>& value,
1340 1340 const std::string& name
... ... @@ -1412,7 +1412,7 @@ namespace cxxopts
1412 1412 }
1413 1413  
1414 1414 ParseResult
1415   - parse(int argc, const char** argv);
  1415 + parse(int argc, const char* const* argv);
1416 1416  
1417 1417 OptionAdder
1418 1418 add_options(std::string group = "");
... ... @@ -1772,7 +1772,7 @@ void
1772 1772 OptionParser::checked_parse_arg
1773 1773 (
1774 1774 int argc,
1775   - const char* argv[],
  1775 + const char* const* argv,
1776 1776 int& current,
1777 1777 const std::shared_ptr<OptionDetails>& value,
1778 1778 const std::string& name
... ... @@ -1865,7 +1865,7 @@ Options::parse_positional(std::initializer_list&lt;std::string&gt; options)
1865 1865  
1866 1866 inline
1867 1867 ParseResult
1868   -Options::parse(int argc, const char** argv)
  1868 +Options::parse(int argc, const char* const* argv)
1869 1869 {
1870 1870 OptionParser parser(*m_options, m_positional, m_allow_unrecognised);
1871 1871  
... ... @@ -1873,7 +1873,7 @@ Options::parse(int argc, const char** argv)
1873 1873 }
1874 1874  
1875 1875 inline ParseResult
1876   -OptionParser::parse(int argc, const char** argv)
  1876 +OptionParser::parse(int argc, const char* const* argv)
1877 1877 {
1878 1878 int current = 1;
1879 1879 bool consume_remaining = false;
... ...
test/options.cpp
... ... @@ -773,3 +773,9 @@ TEST_CASE(&quot;Option add with add_option(string, Option)&quot;, &quot;[options]&quot;) {
773 773 CHECK(result["aggregate"].as<int>() == 4);
774 774 CHECK(result["test"].as<int>() == 5);
775 775 }
  776 +
  777 +TEST_CASE("Const array", "[const]") {
  778 + const char* const option_list[] = {"empty", "options"};
  779 + cxxopts::Options options("Empty options", " - test constness");
  780 + auto result = options.parse(2, option_list);
  781 +}
... ...