Commit 2ca68adeaf17178b6de018ed1becad1f6989b6f6

Authored by Jarryd Beck
1 parent abe9ebd6

Fix positional arguments overload

Fixes #83.

Adds an overload for positional arguments taking a
`std::initializer_list`. When using an `initializer_list` with one
argument, the function call is ambiguous matching both `std::string`
and `std::vector<std::string>`.
CHANGELOG.md
@@ -12,6 +12,8 @@ options. The project adheres to semantic versioning. @@ -12,6 +12,8 @@ options. The project adheres to semantic versioning.
12 when a positional argument could follow an option with an implicit value. 12 when a positional argument could follow an option with an implicit value.
13 For example, `--foo value`, where `foo` has an implicit value, will be 13 For example, `--foo value`, where `foo` has an implicit value, will be
14 parsed as `--foo=implicit` and a positional argument `value`. 14 parsed as `--foo=implicit` and a positional argument `value`.
  15 +* Fixed an ambiguous overload in the `parse_positional` function when an
  16 + `initializer_list` was directly passed.
15 17
16 ## 2.0 18 ## 2.0
17 19
include/cxxopts.hpp
@@ -1211,6 +1211,9 @@ namespace cxxopts @@ -1211,6 +1211,9 @@ namespace cxxopts
1211 void 1211 void
1212 parse_positional(std::vector<std::string> options); 1212 parse_positional(std::vector<std::string> options);
1213 1213
  1214 + void
  1215 + parse_positional(std::initializer_list<std::string> options);
  1216 +
1214 std::string 1217 std::string
1215 help(const std::vector<std::string>& groups = {""}) const; 1218 help(const std::vector<std::string>& groups = {""}) const;
1216 1219
@@ -1588,7 +1591,7 @@ inline @@ -1588,7 +1591,7 @@ inline
1588 void 1591 void
1589 Options::parse_positional(std::string option) 1592 Options::parse_positional(std::string option)
1590 { 1593 {
1591 - parse_positional(std::vector<std::string>{option}); 1594 + parse_positional(std::vector<std::string>{std::move(option)});
1592 } 1595 }
1593 1596
1594 inline 1597 inline
@@ -1602,6 +1605,13 @@ Options::parse_positional(std::vector&lt;std::string&gt; options) @@ -1602,6 +1605,13 @@ Options::parse_positional(std::vector&lt;std::string&gt; options)
1602 } 1605 }
1603 1606
1604 inline 1607 inline
  1608 +void
  1609 +Options::parse_positional(std::initializer_list<std::string> options)
  1610 +{
  1611 + parse_positional(std::vector<std::string>(std::move(options)));
  1612 +}
  1613 +
  1614 +inline
1605 ParseResult 1615 ParseResult
1606 Options::parse(int& argc, char**& argv) 1616 Options::parse(int& argc, char**& argv)
1607 { 1617 {
test/options.cpp
@@ -145,7 +145,7 @@ TEST_CASE(&quot;All positional&quot;, &quot;[positional]&quot;) @@ -145,7 +145,7 @@ TEST_CASE(&quot;All positional&quot;, &quot;[positional]&quot;)
145 auto argc = av.argc(); 145 auto argc = av.argc();
146 auto argv = av.argv(); 146 auto argv = av.argv();
147 147
148 - options.parse_positional("positional"); 148 + options.parse_positional({"positional"});
149 149
150 auto result = options.parse(argc, argv); 150 auto result = options.parse(argc, argv);
151 151