Commit 268c26ad62667ae891cad167eca36085e7478859
Committed by
Henry Schreiner
1 parent
2b4780d7
Fix #64 by auto-adding symmetric excludes
Showing
4 changed files
with
32 additions
and
9 deletions
CHANGELOG.md
| 1 | 1 | ## In progress |
| 2 | 2 | |
| 3 | -* Fix unlimited short options eating two values before checking for positionals when no space present #90 | |
| 3 | +* Fix unlimited short options eating two values before checking for positionals when no space present [#90] | |
| 4 | +* Symmetric exclude text when excluding options, exclude can be called multiple times [#64] | |
| 5 | + | |
| 6 | +[#64]: https://github.com/CLIUtils/CLI11/issues/64 | |
| 7 | +[#90]: https://github.com/CLIUtils/CLI11/issues/90 | |
| 8 | + | |
| 4 | 9 | |
| 5 | 10 | ## Version 1.4: More feedback |
| 6 | 11 | ... | ... |
include/CLI/Option.hpp
| ... | ... | @@ -315,9 +315,14 @@ class Option : public OptionBase<Option> { |
| 315 | 315 | |
| 316 | 316 | /// Sets excluded options |
| 317 | 317 | Option *excludes(Option *opt) { |
| 318 | - auto tup = excludes_.insert(opt); | |
| 319 | - if(!tup.second) | |
| 320 | - throw OptionAlreadyAdded::Excludes(get_name(), opt->get_name()); | |
| 318 | + excludes_.insert(opt); | |
| 319 | + | |
| 320 | + // Help text should be symmetric - excluding a should exclude b | |
| 321 | + opt->excludes_.insert(this); | |
| 322 | + | |
| 323 | + // Ignoring the insert return value, excluding twice is now allowed. | |
| 324 | + // (Mostly to allow both directions to be excluded by user, even though the library does it for you.) | |
| 325 | + | |
| 321 | 326 | return this; |
| 322 | 327 | } |
| 323 | 328 | ... | ... |
tests/CreationTest.cpp
| ... | ... | @@ -187,18 +187,20 @@ TEST_F(TApp, IncorrectConstructionDuplicateNeedsTxt) { |
| 187 | 187 | EXPECT_THROW(cat->needs("--other"), CLI::OptionAlreadyAdded); |
| 188 | 188 | } |
| 189 | 189 | |
| 190 | -TEST_F(TApp, IncorrectConstructionDuplicateExcludes) { | |
| 190 | +// Now allowed | |
| 191 | +TEST_F(TApp, CorrectConstructionDuplicateExcludes) { | |
| 191 | 192 | auto cat = app.add_flag("--cat"); |
| 192 | 193 | auto other = app.add_flag("--other"); |
| 193 | 194 | ASSERT_NO_THROW(cat->excludes(other)); |
| 194 | - EXPECT_THROW(cat->excludes(other), CLI::OptionAlreadyAdded); | |
| 195 | + ASSERT_NO_THROW(other->excludes(cat)); | |
| 195 | 196 | } |
| 196 | 197 | |
| 197 | -TEST_F(TApp, IncorrectConstructionDuplicateExcludesTxt) { | |
| 198 | +// Now allowed | |
| 199 | +TEST_F(TApp, CorrectConstructionDuplicateExcludesTxt) { | |
| 198 | 200 | auto cat = app.add_flag("--cat"); |
| 199 | - app.add_flag("--other"); | |
| 201 | + auto other = app.add_flag("--other"); | |
| 200 | 202 | ASSERT_NO_THROW(cat->excludes("--other")); |
| 201 | - EXPECT_THROW(cat->excludes("--other"), CLI::OptionAlreadyAdded); | |
| 203 | + ASSERT_NO_THROW(other->excludes("--cat")); | |
| 202 | 204 | } |
| 203 | 205 | |
| 204 | 206 | TEST_F(TApp, CheckName) { | ... | ... |
tests/HelpTest.cpp
| ... | ... | @@ -203,6 +203,17 @@ TEST(THelp, ExcludesPositional) { |
| 203 | 203 | EXPECT_THAT(help, HasSubstr("Excludes: op1")); |
| 204 | 204 | } |
| 205 | 205 | |
| 206 | +TEST(THelp, ExcludesSymmetric) { | |
| 207 | + CLI::App app{"My prog"}; | |
| 208 | + | |
| 209 | + CLI::Option *op1 = app.add_flag("--op1"); | |
| 210 | + app.add_flag("--op2")->excludes(op1); | |
| 211 | + | |
| 212 | + std::string help = app.help(); | |
| 213 | + | |
| 214 | + EXPECT_THAT(help, HasSubstr("Excludes: --op2")); | |
| 215 | +} | |
| 216 | + | |
| 206 | 217 | TEST(THelp, ManualSetters) { |
| 207 | 218 | |
| 208 | 219 | CLI::App app{"My prog"}; | ... | ... |