Commit d6b983a2a7a5a91dcc3a1a9b85dba6fa0737d0d1

Authored by Henry Fredrick Schreiner
Committed by Henry Schreiner
1 parent 954c93d5

Adding back short-cuts

include/CLI/Option.hpp
... ... @@ -83,6 +83,29 @@ template <typename CRTP> class OptionBase {
83 83  
84 84 /// The status of the multi option policy
85 85 MultiOptionPolicy get_multi_option_policy() const { return multi_option_policy_; }
  86 +
  87 + // Shortcuts for multi option policy
  88 +
  89 + /// Set the multi option policy to take last
  90 + CRTP *take_last() {
  91 + CRTP *self = static_cast<CRTP *>(this);
  92 + self->multi_option_policy(MultiOptionPolicy::TakeLast);
  93 + return self;
  94 + }
  95 +
  96 + /// Set the multi option policy to take last
  97 + CRTP *take_first() {
  98 + CRTP *self = static_cast<CRTP *>(this);
  99 + self->multi_option_policy(MultiOptionPolicy::TakeFirst);
  100 + return self;
  101 + }
  102 +
  103 + /// Set the multi option policy to take last
  104 + CRTP *join() {
  105 + CRTP *self = static_cast<CRTP *>(this);
  106 + self->multi_option_policy(MultiOptionPolicy::Join);
  107 + return self;
  108 + }
86 109 };
87 110  
88 111 class OptionDefaults : public OptionBase<OptionDefaults> {
... ... @@ -92,7 +115,7 @@ class OptionDefaults : public OptionBase&lt;OptionDefaults&gt; {
92 115 // Methods here need a different implementation if they are Option vs. OptionDefault
93 116  
94 117 /// Take the last argument if given multiple times
95   - OptionDefaults *multi_option_policy(MultiOptionPolicy value) {
  118 + OptionDefaults *multi_option_policy(MultiOptionPolicy value = MultiOptionPolicy::Throw) {
96 119 multi_option_policy_ = value;
97 120 return this;
98 121 }
... ... @@ -307,7 +330,7 @@ class Option : public OptionBase&lt;Option&gt; {
307 330 }
308 331  
309 332 /// Take the last argument if given multiple times
310   - Option *multi_option_policy(MultiOptionPolicy value) {
  333 + Option *multi_option_policy(MultiOptionPolicy value = MultiOptionPolicy::Throw) {
311 334 if(get_expected() != 0 && get_expected() != 1)
312 335 throw IncorrectConstruction("multi_option_policy only works for flags and single value options!");
313 336 multi_option_policy_ = value;
... ...
tests/AppTest.cpp
... ... @@ -230,6 +230,18 @@ TEST_F(TApp, TakeLastOpt) {
230 230 EXPECT_EQ(str, "two");
231 231 }
232 232  
  233 +TEST_F(TApp, TakeLastOpt2) {
  234 +
  235 + std::string str;
  236 + app.add_option("--str", str)->take_last();
  237 +
  238 + args = {"--str=one", "--str=two"};
  239 +
  240 + run();
  241 +
  242 + EXPECT_EQ(str, "two");
  243 +}
  244 +
233 245 TEST_F(TApp, TakeFirstOpt) {
234 246  
235 247 std::string str;
... ... @@ -242,6 +254,18 @@ TEST_F(TApp, TakeFirstOpt) {
242 254 EXPECT_EQ(str, "one");
243 255 }
244 256  
  257 +TEST_F(TApp, TakeFirstOpt2) {
  258 +
  259 + std::string str;
  260 + app.add_option("--str", str)->take_first();
  261 +
  262 + args = {"--str=one", "--str=two"};
  263 +
  264 + run();
  265 +
  266 + EXPECT_EQ(str, "one");
  267 +}
  268 +
245 269 TEST_F(TApp, JoinOpt) {
246 270  
247 271 std::string str;
... ... @@ -254,6 +278,18 @@ TEST_F(TApp, JoinOpt) {
254 278 EXPECT_EQ(str, "one\ntwo");
255 279 }
256 280  
  281 +TEST_F(TApp, JoinOpt2) {
  282 +
  283 + std::string str;
  284 + app.add_option("--str", str)->join();
  285 +
  286 + args = {"--str=one", "--str=two"};
  287 +
  288 + run();
  289 +
  290 + EXPECT_EQ(str, "one\ntwo");
  291 +}
  292 +
257 293 TEST_F(TApp, MissingValueNonRequiredOpt) {
258 294 int count;
259 295 app.add_option("-c,--count", count);
... ...