Skip to content

Commit 990dabb

Browse files
committed
Making mutable sets explicit
1 parent 8d7aefe commit 990dabb

File tree

3 files changed

+84
-87
lines changed

3 files changed

+84
-87
lines changed

include/CLI/App.hpp

Lines changed: 72 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -547,8 +547,8 @@ class App {
547547
/// Add set of options (No default, temp reference, such as an inline set)
548548
template <typename T>
549549
Option *add_set(std::string option_name,
550-
T &member, ///< The selected member of the set
551-
const std::set<T> &&options, ///< The set of possibilities
550+
T &member, ///< The selected member of the set
551+
std::set<T> options, ///< The set of possibilities
552552
std::string description = "") {
553553

554554
std::string simple_name = CLI::detail::split(option_name, ',').at(0);
@@ -566,12 +566,12 @@ class App {
566566
return opt;
567567
}
568568

569-
/// Add set of options (No default, non-temp reference, such as an existing set)
569+
/// Add set of options (No default, set can be changed afterwords - do not destroy the set)
570570
template <typename T>
571-
Option *add_set(std::string option_name,
572-
T &member, ///< The selected member of the set
573-
const std::set<T> &options, ///< The set of possibilities
574-
std::string description = "") {
571+
Option *add_mutable_set(std::string option_name,
572+
T &member, ///< The selected member of the set
573+
const std::set<T> &options, ///< The set of possibilities
574+
std::string description = "") {
575575

576576
std::string simple_name = CLI::detail::split(option_name, ',').at(0);
577577
CLI::callback_t fun = [&member, &options, simple_name](CLI::results_t res) {
@@ -588,11 +588,11 @@ class App {
588588
return opt;
589589
}
590590

591-
/// Add set of options (with default, R value, such as an inline set)
591+
/// Add set of options (with default, static set, such as an inline set)
592592
template <typename T>
593593
Option *add_set(std::string option_name,
594-
T &member, ///< The selected member of the set
595-
const std::set<T> &&options, ///< The set of possibilities
594+
T &member, ///< The selected member of the set
595+
std::set<T> options, ///< The set of possibilities
596596
std::string description,
597597
bool defaulted) {
598598

@@ -616,13 +616,13 @@ class App {
616616
return opt;
617617
}
618618

619-
/// Add set of options (with default, L value reference, such as an existing set)
619+
/// Add set of options (with default, set can be changed afterwards - do not destroy the set)
620620
template <typename T>
621-
Option *add_set(std::string option_name,
622-
T &member, ///< The selected member of the set
623-
const std::set<T> &options, ///< The set of possibilities
624-
std::string description,
625-
bool defaulted) {
621+
Option *add_mutable_set(std::string option_name,
622+
T &member, ///< The selected member of the set
623+
const std::set<T> &options, ///< The set of possibilities
624+
std::string description,
625+
bool defaulted) {
626626

627627
std::string simple_name = CLI::detail::split(option_name, ',').at(0);
628628
CLI::callback_t fun = [&member, &options, simple_name](CLI::results_t res) {
@@ -643,10 +643,10 @@ class App {
643643
return opt;
644644
}
645645

646-
/// Add set of options, string only, ignore case (no default, R value)
646+
/// Add set of options, string only, ignore case (no default, static set)
647647
Option *add_set_ignore_case(std::string option_name,
648-
std::string &member, ///< The selected member of the set
649-
const std::set<std::string> &&options, ///< The set of possibilities
648+
std::string &member, ///< The selected member of the set
649+
std::set<std::string> options, ///< The set of possibilities
650650
std::string description = "") {
651651

652652
std::string simple_name = CLI::detail::split(option_name, ',').at(0);
@@ -671,11 +671,12 @@ class App {
671671
return opt;
672672
}
673673

674-
/// Add set of options, string only, ignore case (no default, L value)
675-
Option *add_set_ignore_case(std::string option_name,
676-
std::string &member, ///< The selected member of the set
677-
const std::set<std::string> &options, ///< The set of possibilities
678-
std::string description = "") {
674+
/// Add set of options, string only, ignore case (no default, set can be changed afterwards - do not destroy the
675+
/// set)
676+
Option *add_mutable_set_ignore_case(std::string option_name,
677+
std::string &member, ///< The selected member of the set
678+
const std::set<std::string> &options, ///< The set of possibilities
679+
std::string description = "") {
679680

680681
std::string simple_name = CLI::detail::split(option_name, ',').at(0);
681682
CLI::callback_t fun = [&member, &options, simple_name](CLI::results_t res) {
@@ -699,10 +700,10 @@ class App {
699700
return opt;
700701
}
701702

702-
/// Add set of options, string only, ignore case (default, R value)
703+
/// Add set of options, string only, ignore case (default, static set)
703704
Option *add_set_ignore_case(std::string option_name,
704-
std::string &member, ///< The selected member of the set
705-
const std::set<std::string> &&options, ///< The set of possibilities
705+
std::string &member, ///< The selected member of the set
706+
std::set<std::string> options, ///< The set of possibilities
706707
std::string description,
707708
bool defaulted) {
708709

@@ -730,12 +731,12 @@ class App {
730731
return opt;
731732
}
732733

733-
/// Add set of options, string only, ignore case (default, L value)
734-
Option *add_set_ignore_case(std::string option_name,
735-
std::string &member, ///< The selected member of the set
736-
const std::set<std::string> &options, ///< The set of possibilities
737-
std::string description,
738-
bool defaulted) {
734+
/// Add set of options, string only, ignore case (default, set can be changed afterwards - do not destroy the set)
735+
Option *add_mutable_set_ignore_case(std::string option_name,
736+
std::string &member, ///< The selected member of the set
737+
const std::set<std::string> &options, ///< The set of possibilities
738+
std::string description,
739+
bool defaulted) {
739740

740741
std::string simple_name = CLI::detail::split(option_name, ',').at(0);
741742
CLI::callback_t fun = [&member, &options, simple_name](CLI::results_t res) {
@@ -761,10 +762,10 @@ class App {
761762
return opt;
762763
}
763764

764-
/// Add set of options, string only, ignore underscore (no default, R value)
765+
/// Add set of options, string only, ignore underscore (no default, static set)
765766
Option *add_set_ignore_underscore(std::string option_name,
766-
std::string &member, ///< The selected member of the set
767-
const std::set<std::string> &&options, ///< The set of possibilities
767+
std::string &member, ///< The selected member of the set
768+
std::set<std::string> options, ///< The set of possibilities
768769
std::string description = "") {
769770

770771
std::string simple_name = CLI::detail::split(option_name, ',').at(0);
@@ -789,11 +790,12 @@ class App {
789790
return opt;
790791
}
791792

792-
/// Add set of options, string only, ignore underscore (no default, L value)
793-
Option *add_set_ignore_underscore(std::string option_name,
794-
std::string &member, ///< The selected member of the set
795-
const std::set<std::string> &options, ///< The set of possibilities
796-
std::string description = "") {
793+
/// Add set of options, string only, ignore underscore (no default, set can be changed afterwards - do not destroy
794+
/// the set)
795+
Option *add_mutable_set_ignore_underscore(std::string option_name,
796+
std::string &member, ///< The selected member of the set
797+
const std::set<std::string> &options, ///< The set of possibilities
798+
std::string description = "") {
797799

798800
std::string simple_name = CLI::detail::split(option_name, ',').at(0);
799801
CLI::callback_t fun = [&member, &options, simple_name](CLI::results_t res) {
@@ -817,10 +819,10 @@ class App {
817819
return opt;
818820
}
819821

820-
/// Add set of options, string only, ignore underscore (default, R value)
822+
/// Add set of options, string only, ignore underscore (default, static set)
821823
Option *add_set_ignore_underscore(std::string option_name,
822-
std::string &member, ///< The selected member of the set
823-
const std::set<std::string> &&options, ///< The set of possibilities
824+
std::string &member, ///< The selected member of the set
825+
std::set<std::string> options, ///< The set of possibilities
824826
std::string description,
825827
bool defaulted) {
826828

@@ -848,12 +850,13 @@ class App {
848850
return opt;
849851
}
850852

851-
/// Add set of options, string only, ignore underscore (default, L value)
852-
Option *add_set_ignore_underscore(std::string option_name,
853-
std::string &member, ///< The selected member of the set
854-
const std::set<std::string> &options, ///< The set of possibilities
855-
std::string description,
856-
bool defaulted) {
853+
/// Add set of options, string only, ignore underscore (default, set can be changed afterwards - do not destroy the
854+
/// set)
855+
Option *add_mutable_set_ignore_underscore(std::string option_name,
856+
std::string &member, ///< The selected member of the set
857+
const std::set<std::string> &options, ///< The set of possibilities
858+
std::string description,
859+
bool defaulted) {
857860

858861
std::string simple_name = CLI::detail::split(option_name, ',').at(0);
859862
CLI::callback_t fun = [&member, &options, simple_name](CLI::results_t res) {
@@ -879,10 +882,10 @@ class App {
879882
return opt;
880883
}
881884

882-
/// Add set of options, string only, ignore underscore and case(no default, R value)
885+
/// Add set of options, string only, ignore underscore and case (no default, static set)
883886
Option *add_set_ignore_case_underscore(std::string option_name,
884-
std::string &member, ///< The selected member of the set
885-
const std::set<std::string> &&options, ///< The set of possibilities
887+
std::string &member, ///< The selected member of the set
888+
std::set<std::string> options, ///< The set of possibilities
886889
std::string description = "") {
887890

888891
std::string simple_name = CLI::detail::split(option_name, ',').at(0);
@@ -907,11 +910,12 @@ class App {
907910
return opt;
908911
}
909912

910-
/// Add set of options, string only, ignore underscore and case(no default, L value)
911-
Option *add_set_ignore_case_underscore(std::string option_name,
912-
std::string &member, ///< The selected member of the set
913-
const std::set<std::string> &options, ///< The set of possibilities
914-
std::string description = "") {
913+
/// Add set of options, string only, ignore underscore and case (no default, set can be changed afterwards - do not
914+
/// destroy the set)
915+
Option *add_mutable_set_ignore_case_underscore(std::string option_name,
916+
std::string &member, ///< The selected member of the set
917+
const std::set<std::string> &options, ///< The set of possibilities
918+
std::string description = "") {
915919

916920
std::string simple_name = CLI::detail::split(option_name, ',').at(0);
917921
CLI::callback_t fun = [&member, &options, simple_name](CLI::results_t res) {
@@ -935,10 +939,10 @@ class App {
935939
return opt;
936940
}
937941

938-
/// Add set of options, string only, ignore underscore and case (default, R value)
942+
/// Add set of options, string only, ignore underscore and case (default, static set)
939943
Option *add_set_ignore_case_underscore(std::string option_name,
940-
std::string &member, ///< The selected member of the set
941-
const std::set<std::string> &&options, ///< The set of possibilities
944+
std::string &member, ///< The selected member of the set
945+
std::set<std::string> options, ///< The set of possibilities
942946
std::string description,
943947
bool defaulted) {
944948

@@ -966,12 +970,13 @@ class App {
966970
return opt;
967971
}
968972

969-
/// Add set of options, string only, ignore underscore and case (default, L value)
970-
Option *add_set_ignore_case_underscore(std::string option_name,
971-
std::string &member, ///< The selected member of the set
972-
const std::set<std::string> &options, ///< The set of possibilities
973-
std::string description,
974-
bool defaulted) {
973+
/// Add set of options, string only, ignore underscore and case (default, set can be changed afterwards - do not
974+
/// destroy the set)
975+
Option *add_mutable_set_ignore_case_underscore(std::string option_name,
976+
std::string &member, ///< The selected member of the set
977+
const std::set<std::string> &options, ///< The set of possibilities
978+
std::string description,
979+
bool defaulted) {
975980

976981
std::string simple_name = CLI::detail::split(option_name, ',').at(0);
977982
CLI::callback_t fun = [&member, &options, simple_name](CLI::results_t res) {

tests/AppTest.cpp

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,12 +1206,11 @@ TEST_F(TApp, InSetIgnoreCase) {
12061206
EXPECT_THROW(run(), CLI::ArgumentMismatch);
12071207
}
12081208

1209-
/*
1210-
TEST_F(TApp, InSetIgnoreCaseLValue) {
1209+
TEST_F(TApp, InSetIgnoreCaseMutableValue) {
12111210

12121211
std::set<std::string> options{"one", "Two", "THREE"};
12131212
std::string choice;
1214-
app.add_set_ignore_case("-q,--quick", choice, options);
1213+
app.add_mutable_set_ignore_case("-q,--quick", choice, options);
12151214

12161215
args = {"--quick", "One"};
12171216
run();
@@ -1227,14 +1226,7 @@ TEST_F(TApp, InSetIgnoreCaseLValue) {
12271226

12281227
options.clear();
12291228
args = {"--quick", "ThrEE"};
1230-
run();
1231-
EXPECT_EQ("THREE", choice); // this will now fail since options was cleared
1232-
1233-
args = {"--quick", "four"};
12341229
EXPECT_THROW(run(), CLI::ConversionError);
1235-
1236-
args = {"--quick=one", "--quick=two"};
1237-
EXPECT_THROW(run(), CLI::ArgumentMismatch);
12381230
}
12391231

12401232
TEST_F(TApp, InSetIgnoreCasePointer) {
@@ -1258,15 +1250,15 @@ TEST_F(TApp, InSetIgnoreCasePointer) {
12581250
delete options;
12591251
args = {"--quick", "ThrEE"};
12601252
run();
1261-
EXPECT_EQ("THREE", choice); // this could cause a seg fault
1253+
EXPECT_EQ("THREE", choice); // this does not throw a segfault
12621254

12631255
args = {"--quick", "four"};
12641256
EXPECT_THROW(run(), CLI::ConversionError);
12651257

12661258
args = {"--quick=one", "--quick=two"};
12671259
EXPECT_THROW(run(), CLI::ArgumentMismatch);
12681260
}
1269-
*/
1261+
12701262
TEST_F(TApp, InSetIgnoreUnderscore) {
12711263

12721264
std::string choice;
@@ -1765,8 +1757,8 @@ TEST_F(TApp, AddRemoveSetItems) {
17651757
std::set<std::string> items{"TYPE1", "TYPE2", "TYPE3", "TYPE4", "TYPE5"};
17661758

17671759
std::string type1, type2;
1768-
app.add_set("--type1", type1, items);
1769-
app.add_set("--type2", type2, items, "", true);
1760+
app.add_mutable_set("--type1", type1, items);
1761+
app.add_mutable_set("--type2", type2, items, "", true);
17701762

17711763
args = {"--type1", "TYPE1", "--type2", "TYPE2"};
17721764

@@ -1796,8 +1788,8 @@ TEST_F(TApp, AddRemoveSetItemsNoCase) {
17961788
std::set<std::string> items{"TYPE1", "TYPE2", "TYPE3", "TYPE4", "TYPE5"};
17971789

17981790
std::string type1, type2;
1799-
app.add_set_ignore_case("--type1", type1, items);
1800-
app.add_set_ignore_case("--type2", type2, items, "", true);
1791+
app.add_mutable_set_ignore_case("--type1", type1, items);
1792+
app.add_mutable_set_ignore_case("--type2", type2, items, "", true);
18011793

18021794
args = {"--type1", "TYPe1", "--type2", "TyPE2"};
18031795

tests/HelpTest.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ TEST(THelp, ChangingSet) {
795795

796796
std::set<int> vals{1, 2, 3};
797797
int val;
798-
app.add_set("--val", val, vals);
798+
app.add_mutable_set("--val", val, vals);
799799

800800
std::string help = app.help();
801801

@@ -816,7 +816,7 @@ TEST(THelp, ChangingSetDefaulted) {
816816

817817
std::set<int> vals{1, 2, 3};
818818
int val = 2;
819-
app.add_set("--val", val, vals, "", true);
819+
app.add_mutable_set("--val", val, vals, "", true);
820820

821821
std::string help = app.help();
822822

@@ -836,7 +836,7 @@ TEST(THelp, ChangingCaselessSet) {
836836

837837
std::set<std::string> vals{"1", "2", "3"};
838838
std::string val;
839-
app.add_set_ignore_case("--val", val, vals);
839+
app.add_mutable_set_ignore_case("--val", val, vals);
840840

841841
std::string help = app.help();
842842

@@ -857,7 +857,7 @@ TEST(THelp, ChangingCaselessSetDefaulted) {
857857

858858
std::set<std::string> vals{"1", "2", "3"};
859859
std::string val = "2";
860-
app.add_set_ignore_case("--val", val, vals, "", true);
860+
app.add_mutable_set_ignore_case("--val", val, vals, "", true);
861861

862862
std::string help = app.help();
863863

0 commit comments

Comments
 (0)