Commit 239525bcf40db03fb75091a68ae1cdbcdc632af6
1 parent
c8b22d11
parse into value
Showing
2 changed files
with
13 additions
and
3 deletions
src/cxxopts.hpp
| @@ -194,6 +194,8 @@ namespace cxxopts | @@ -194,6 +194,8 @@ namespace cxxopts | ||
| 194 | void | 194 | void |
| 195 | parse_value(const std::string& text, bool& value) | 195 | parse_value(const std::string& text, bool& value) |
| 196 | { | 196 | { |
| 197 | + //TODO recognise on, off, yes, no, enable, disable | ||
| 198 | + //so that we can write --long=yes explicitly | ||
| 197 | value = true; | 199 | value = true; |
| 198 | } | 200 | } |
| 199 | 201 | ||
| @@ -263,6 +265,13 @@ namespace cxxopts | @@ -263,6 +265,13 @@ namespace cxxopts | ||
| 263 | return std::make_shared<values::default_value<T>>(); | 265 | return std::make_shared<values::default_value<T>>(); |
| 264 | } | 266 | } |
| 265 | 267 | ||
| 268 | + template <typename T> | ||
| 269 | + std::shared_ptr<Value> | ||
| 270 | + value(T& t) | ||
| 271 | + { | ||
| 272 | + return std::make_shared<values::default_value<T>>(&t); | ||
| 273 | + } | ||
| 274 | + | ||
| 266 | class OptionAdder; | 275 | class OptionAdder; |
| 267 | 276 | ||
| 268 | class OptionDetails | 277 | class OptionDetails |
src/example.cpp
| @@ -30,11 +30,12 @@ int main(int argc, char* argv[]) | @@ -30,11 +30,12 @@ int main(int argc, char* argv[]) | ||
| 30 | { | 30 | { |
| 31 | try | 31 | try |
| 32 | { | 32 | { |
| 33 | - | ||
| 34 | cxxopts::Options options(argv[0]); | 33 | cxxopts::Options options(argv[0]); |
| 35 | 34 | ||
| 35 | + bool apple = false; | ||
| 36 | + | ||
| 36 | options.add_options() | 37 | options.add_options() |
| 37 | - ("a,apple", "an apple") | 38 | + ("a,apple", "an apple", cxxopts::value<bool>(apple)) |
| 38 | ("b,bob", "Bob") | 39 | ("b,bob", "Bob") |
| 39 | ("f,file", "File", cxxopts::value<std::vector<std::string>>()) | 40 | ("f,file", "File", cxxopts::value<std::vector<std::string>>()) |
| 40 | ("positional", | 41 | ("positional", |
| @@ -51,7 +52,7 @@ int main(int argc, char* argv[]) | @@ -51,7 +52,7 @@ int main(int argc, char* argv[]) | ||
| 51 | 52 | ||
| 52 | options.parse(argc, argv); | 53 | options.parse(argc, argv); |
| 53 | 54 | ||
| 54 | - if (options.count("a")) | 55 | + if (apple) |
| 55 | { | 56 | { |
| 56 | std::cout << "Saw option ‘a’" << std::endl; | 57 | std::cout << "Saw option ‘a’" << std::endl; |
| 57 | } | 58 | } |