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,6 +83,29 @@ template <typename CRTP> class OptionBase {
83 83
84 /// The status of the multi option policy 84 /// The status of the multi option policy
85 MultiOptionPolicy get_multi_option_policy() const { return multi_option_policy_; } 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 class OptionDefaults : public OptionBase<OptionDefaults> { 111 class OptionDefaults : public OptionBase<OptionDefaults> {
@@ -92,7 +115,7 @@ class OptionDefaults : public OptionBase&lt;OptionDefaults&gt; { @@ -92,7 +115,7 @@ class OptionDefaults : public OptionBase&lt;OptionDefaults&gt; {
92 // Methods here need a different implementation if they are Option vs. OptionDefault 115 // Methods here need a different implementation if they are Option vs. OptionDefault
93 116
94 /// Take the last argument if given multiple times 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 multi_option_policy_ = value; 119 multi_option_policy_ = value;
97 return this; 120 return this;
98 } 121 }
@@ -307,7 +330,7 @@ class Option : public OptionBase&lt;Option&gt; { @@ -307,7 +330,7 @@ class Option : public OptionBase&lt;Option&gt; {
307 } 330 }
308 331
309 /// Take the last argument if given multiple times 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 if(get_expected() != 0 && get_expected() != 1) 334 if(get_expected() != 0 && get_expected() != 1)
312 throw IncorrectConstruction("multi_option_policy only works for flags and single value options!"); 335 throw IncorrectConstruction("multi_option_policy only works for flags and single value options!");
313 multi_option_policy_ = value; 336 multi_option_policy_ = value;
tests/AppTest.cpp
@@ -230,6 +230,18 @@ TEST_F(TApp, TakeLastOpt) { @@ -230,6 +230,18 @@ TEST_F(TApp, TakeLastOpt) {
230 EXPECT_EQ(str, "two"); 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 TEST_F(TApp, TakeFirstOpt) { 245 TEST_F(TApp, TakeFirstOpt) {
234 246
235 std::string str; 247 std::string str;
@@ -242,6 +254,18 @@ TEST_F(TApp, TakeFirstOpt) { @@ -242,6 +254,18 @@ TEST_F(TApp, TakeFirstOpt) {
242 EXPECT_EQ(str, "one"); 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 TEST_F(TApp, JoinOpt) { 269 TEST_F(TApp, JoinOpt) {
246 270
247 std::string str; 271 std::string str;
@@ -254,6 +278,18 @@ TEST_F(TApp, JoinOpt) { @@ -254,6 +278,18 @@ TEST_F(TApp, JoinOpt) {
254 EXPECT_EQ(str, "one\ntwo"); 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 TEST_F(TApp, MissingValueNonRequiredOpt) { 293 TEST_F(TApp, MissingValueNonRequiredOpt) {
258 int count; 294 int count;
259 app.add_option("-c,--count", count); 295 app.add_option("-c,--count", count);