Commit c4f1fc8ea7762afe8b9247242893ef8ddb97c1e4

Authored by Henry Schreiner
Committed by GitHub
1 parent e2e3cb2f

refactor!: remove add_complex (#600)

CHANGELOG.md
@@ -23,6 +23,7 @@ @@ -23,6 +23,7 @@
23 * The final "defaulted" bool has been removed, use `->capture_default_str()` 23 * The final "defaulted" bool has been removed, use `->capture_default_str()`
24 instead. Use `app.option_defaults()->always_capture_default()` to set this for 24 instead. Use `app.option_defaults()->always_capture_default()` to set this for
25 all future options. [#597][] 25 all future options. [#597][]
  26 +* Use `add_option` on a complex number instead of `add_complex`, which has been removed.
26 27
27 28
28 [#435]: https://github.com/CLIUtils/CLI11/pull/435 29 [#435]: https://github.com/CLIUtils/CLI11/pull/435
README.md
@@ -231,8 +231,6 @@ app.add_option_function<type>(option_name, @@ -231,8 +231,6 @@ app.add_option_function<type>(option_name,
231 function <void(const type &value)>, // type can be any type supported by add_option 231 function <void(const type &value)>, // type can be any type supported by add_option
232 help_string="") 232 help_string="")
233 233
234 -app.add_complex(... // Special case: support for complex numbers โš ๏ธ. Complex numbers are now fully supported in the add_option so this function is redundant.  
235 -  
236 // char as an option type is supported before 2.0 but in 2.0 it defaulted to allowing single non numerical characters in addition to the numeric values. 234 // char as an option type is supported before 2.0 but in 2.0 it defaulted to allowing single non numerical characters in addition to the numeric values.
237 235
238 // ๐Ÿ†• There is a template overload which takes two template parameters the first is the type of object to assign the value to, the second is the conversion type. The conversion type should have a known way to convert from a string, such as any of the types that work in the non-template version. If XC is a std::pair and T is some non pair type. Then a two argument constructor for T is called to assign the value. For tuples or other multi element types, XC must be a single type or a tuple like object of the same size as the assignment type 236 // ๐Ÿ†• There is a template overload which takes two template parameters the first is the type of object to assign the value to, the second is the conversion type. The conversion type should have a known way to convert from a string, such as any of the types that work in the non-template version. If XC is a std::pair and T is some non pair type. Then a two argument constructor for T is called to assign the value. For tuples or other multi element types, XC must be a single type or a tuple like object of the same size as the assignment type
include/CLI/App.hpp
@@ -896,56 +896,6 @@ class App { @@ -896,56 +896,6 @@ class App {
896 } 896 }
897 #endif 897 #endif
898 898
899 - /// Add a complex number DEPRECATED --use add_option instead  
900 - template <typename T, typename XC = double>  
901 - Option *add_complex(std::string option_name,  
902 - T &variable,  
903 - std::string option_description = "",  
904 - bool defaulted = false,  
905 - std::string label = "COMPLEX") {  
906 -  
907 - CLI::callback_t fun = [&variable](const results_t &res) {  
908 - XC x, y;  
909 - bool worked;  
910 - if(res.size() >= 2 && !res[1].empty()) {  
911 - auto str1 = res[1];  
912 - if(str1.back() == 'i' || str1.back() == 'j')  
913 - str1.pop_back();  
914 - worked = detail::lexical_cast(res[0], x) && detail::lexical_cast(str1, y);  
915 - } else {  
916 - auto str1 = res.front();  
917 - auto nloc = str1.find_last_of('-');  
918 - if(nloc != std::string::npos && nloc > 0) {  
919 - worked = detail::lexical_cast(str1.substr(0, nloc), x);  
920 - str1 = str1.substr(nloc);  
921 - if(str1.back() == 'i' || str1.back() == 'j')  
922 - str1.pop_back();  
923 - worked = worked && detail::lexical_cast(str1, y);  
924 - } else {  
925 - if(str1.back() == 'i' || str1.back() == 'j') {  
926 - str1.pop_back();  
927 - worked = detail::lexical_cast(str1, y);  
928 - x = XC{0};  
929 - } else {  
930 - worked = detail::lexical_cast(str1, x);  
931 - y = XC{0};  
932 - }  
933 - }  
934 - }  
935 - if(worked)  
936 - variable = T{x, y};  
937 - return worked;  
938 - };  
939 -  
940 - auto default_function = [&variable]() { return CLI::detail::checked_to_string<T, T>(variable); };  
941 -  
942 - CLI::Option *opt =  
943 - add_option(option_name, std::move(fun), std::move(option_description), defaulted, default_function);  
944 -  
945 - opt->type_name(label)->type_size(1, 2)->delimiter('+')->run_callback_for_default();  
946 - return opt;  
947 - }  
948 -  
949 /// Set a configuration ini file option, or clear it if no name passed 899 /// Set a configuration ini file option, or clear it if no name passed
950 Option *set_config(std::string option_name = "", 900 Option *set_config(std::string option_name = "",
951 std::string default_filename = "", 901 std::string default_filename = "",
tests/AppTest.cpp
@@ -809,7 +809,7 @@ TEST_CASE_METHOD(TApp, &quot;TakeFirstOptMulti&quot;, &quot;[app]&quot;) { @@ -809,7 +809,7 @@ TEST_CASE_METHOD(TApp, &quot;TakeFirstOptMulti&quot;, &quot;[app]&quot;) {
809 809
810 TEST_CASE_METHOD(TApp, "ComplexOptMulti", "[app]") { 810 TEST_CASE_METHOD(TApp, "ComplexOptMulti", "[app]") {
811 std::complex<double> val; 811 std::complex<double> val;
812 - app.add_complex("--long", val)->take_first()->allow_extra_args(); 812 + app.add_option("--long", val)->take_first()->allow_extra_args();
813 813
814 args = {"--long", "1", "2", "3", "4"}; 814 args = {"--long", "1", "2", "3", "4"};
815 815
tests/NewParseTest.cpp
@@ -13,26 +13,6 @@ using Catch::Matchers::Contains; @@ -13,26 +13,6 @@ using Catch::Matchers::Contains;
13 13
14 using cx = std::complex<double>; 14 using cx = std::complex<double>;
15 15
16 -TEST_CASE_METHOD(TApp, "Complex", "[newparse]") {  
17 - cx comp{1, 2};  
18 - app.add_complex("-c,--complex", comp, "", true);  
19 -  
20 - args = {"-c", "4", "3"};  
21 -  
22 - std::string help = app.help();  
23 - CHECK_THAT(help, Contains("1"));  
24 - CHECK_THAT(help, Contains("2"));  
25 - CHECK_THAT(help, Contains("COMPLEX"));  
26 -  
27 - CHECK(comp.real() == Approx(1));  
28 - CHECK(comp.imag() == Approx(2));  
29 -  
30 - run();  
31 -  
32 - CHECK(comp.real() == Approx(4));  
33 - CHECK(comp.imag() == Approx(3));  
34 -}  
35 -  
36 TEST_CASE_METHOD(TApp, "ComplexOption", "[newparse]") { 16 TEST_CASE_METHOD(TApp, "ComplexOption", "[newparse]") {
37 cx comp{1, 2}; 17 cx comp{1, 2};
38 app.add_option("-c,--complex", comp)->capture_default_str(); 18 app.add_option("-c,--complex", comp)->capture_default_str();
@@ -53,26 +33,6 @@ TEST_CASE_METHOD(TApp, &quot;ComplexOption&quot;, &quot;[newparse]&quot;) { @@ -53,26 +33,6 @@ TEST_CASE_METHOD(TApp, &quot;ComplexOption&quot;, &quot;[newparse]&quot;) {
53 CHECK(comp.imag() == Approx(3)); 33 CHECK(comp.imag() == Approx(3));
54 } 34 }
55 35
56 -TEST_CASE_METHOD(TApp, "ComplexFloat", "[newparse]") {  
57 - std::complex<float> comp{1, 2};  
58 - app.add_complex<std::complex<float>, float>("-c,--complex", comp)->capture_default_str();  
59 -  
60 - args = {"-c", "4", "3"};  
61 -  
62 - std::string help = app.help();  
63 - CHECK_THAT(help, Contains("1"));  
64 - CHECK_THAT(help, Contains("2"));  
65 - CHECK_THAT(help, Contains("COMPLEX"));  
66 -  
67 - CHECK(comp.real() == Approx(1));  
68 - CHECK(comp.imag() == Approx(2));  
69 -  
70 - run();  
71 -  
72 - CHECK(comp.real() == Approx(4));  
73 - CHECK(comp.imag() == Approx(3));  
74 -}  
75 -  
76 TEST_CASE_METHOD(TApp, "ComplexFloatOption", "[newparse]") { 36 TEST_CASE_METHOD(TApp, "ComplexFloatOption", "[newparse]") {
77 std::complex<float> comp{1, 2}; 37 std::complex<float> comp{1, 2};
78 app.add_option("-c,--complex", comp)->capture_default_str(); 38 app.add_option("-c,--complex", comp)->capture_default_str();
@@ -93,38 +53,6 @@ TEST_CASE_METHOD(TApp, &quot;ComplexFloatOption&quot;, &quot;[newparse]&quot;) { @@ -93,38 +53,6 @@ TEST_CASE_METHOD(TApp, &quot;ComplexFloatOption&quot;, &quot;[newparse]&quot;) {
93 CHECK(comp.imag() == Approx(3)); 53 CHECK(comp.imag() == Approx(3));
94 } 54 }
95 55
96 -TEST_CASE_METHOD(TApp, "ComplexWithDelimiter", "[newparse]") {  
97 - cx comp{1, 2};  
98 - app.add_complex("-c,--complex", comp)->capture_default_str()->delimiter('+');  
99 -  
100 - args = {"-c", "4+3i"};  
101 -  
102 - std::string help = app.help();  
103 - CHECK_THAT(help, Contains("1"));  
104 - CHECK_THAT(help, Contains("2"));  
105 - CHECK_THAT(help, Contains("COMPLEX"));  
106 -  
107 - CHECK(comp.real() == Approx(1));  
108 - CHECK(comp.imag() == Approx(2));  
109 -  
110 - run();  
111 -  
112 - CHECK(comp.real() == Approx(4));  
113 - CHECK(comp.imag() == Approx(3));  
114 -  
115 - args = {"-c", "5+-3i"};  
116 - run();  
117 -  
118 - CHECK(comp.real() == Approx(5));  
119 - CHECK(comp.imag() == Approx(-3));  
120 -  
121 - args = {"-c", "6", "-4i"};  
122 - run();  
123 -  
124 - CHECK(comp.real() == Approx(6));  
125 - CHECK(comp.imag() == Approx(-4));  
126 -}  
127 -  
128 TEST_CASE_METHOD(TApp, "ComplexWithDelimiterOption", "[newparse]") { 56 TEST_CASE_METHOD(TApp, "ComplexWithDelimiterOption", "[newparse]") {
129 cx comp{1, 2}; 57 cx comp{1, 2};
130 app.add_option("-c,--complex", comp)->capture_default_str()->delimiter('+'); 58 app.add_option("-c,--complex", comp)->capture_default_str()->delimiter('+');
@@ -157,18 +85,6 @@ TEST_CASE_METHOD(TApp, &quot;ComplexWithDelimiterOption&quot;, &quot;[newparse]&quot;) { @@ -157,18 +85,6 @@ TEST_CASE_METHOD(TApp, &quot;ComplexWithDelimiterOption&quot;, &quot;[newparse]&quot;) {
157 CHECK(comp.imag() == Approx(-4)); 85 CHECK(comp.imag() == Approx(-4));
158 } 86 }
159 87
160 -TEST_CASE_METHOD(TApp, "ComplexIgnoreI", "[newparse]") {  
161 - cx comp{1, 2};  
162 - app.add_complex("-c,--complex", comp);  
163 -  
164 - args = {"-c", "4", "3i"};  
165 -  
166 - run();  
167 -  
168 - CHECK(comp.real() == Approx(4));  
169 - CHECK(comp.imag() == Approx(3));  
170 -}  
171 -  
172 TEST_CASE_METHOD(TApp, "ComplexIgnoreIOption", "[newparse]") { 88 TEST_CASE_METHOD(TApp, "ComplexIgnoreIOption", "[newparse]") {
173 cx comp{1, 2}; 89 cx comp{1, 2};
174 app.add_option("-c,--complex", comp); 90 app.add_option("-c,--complex", comp);
@@ -181,40 +97,6 @@ TEST_CASE_METHOD(TApp, &quot;ComplexIgnoreIOption&quot;, &quot;[newparse]&quot;) { @@ -181,40 +97,6 @@ TEST_CASE_METHOD(TApp, &quot;ComplexIgnoreIOption&quot;, &quot;[newparse]&quot;) {
181 CHECK(comp.imag() == Approx(3)); 97 CHECK(comp.imag() == Approx(3));
182 } 98 }
183 99
184 -TEST_CASE_METHOD(TApp, "ComplexSingleArg", "[newparse]") {  
185 - cx comp{1, 2};  
186 - app.add_complex("-c,--complex", comp);  
187 -  
188 - args = {"-c", "4"};  
189 - run();  
190 - CHECK(comp.real() == Approx(4));  
191 - CHECK(comp.imag() == Approx(0));  
192 -  
193 - args = {"-c", "4-2i"};  
194 - run();  
195 - CHECK(comp.real() == Approx(4));  
196 - CHECK(comp.imag() == Approx(-2));  
197 - args = {"-c", "4+2i"};  
198 - run();  
199 - CHECK(comp.real() == Approx(4));  
200 - CHECK(comp.imag() == Approx(2));  
201 -  
202 - args = {"-c", "-4+2j"};  
203 - run();  
204 - CHECK(comp.real() == Approx(-4));  
205 - CHECK(comp.imag() == Approx(2));  
206 -  
207 - args = {"-c", "-4.2-2j"};  
208 - run();  
209 - CHECK(comp.real() == Approx(-4.2));  
210 - CHECK(comp.imag() == Approx(-2));  
211 -  
212 - args = {"-c", "-4.2-2.7i"};  
213 - run();  
214 - CHECK(comp.real() == Approx(-4.2));  
215 - CHECK(comp.imag() == Approx(-2.7));  
216 -}  
217 -  
218 TEST_CASE_METHOD(TApp, "ComplexSingleArgOption", "[newparse]") { 100 TEST_CASE_METHOD(TApp, "ComplexSingleArgOption", "[newparse]") {
219 cx comp{1, 2}; 101 cx comp{1, 2};
220 app.add_option("-c,--complex", comp); 102 app.add_option("-c,--complex", comp);
@@ -249,29 +131,6 @@ TEST_CASE_METHOD(TApp, &quot;ComplexSingleArgOption&quot;, &quot;[newparse]&quot;) { @@ -249,29 +131,6 @@ TEST_CASE_METHOD(TApp, &quot;ComplexSingleArgOption&quot;, &quot;[newparse]&quot;) {
249 CHECK(comp.imag() == Approx(-2.7)); 131 CHECK(comp.imag() == Approx(-2.7));
250 } 132 }
251 133
252 -TEST_CASE_METHOD(TApp, "ComplexSingleImag", "[newparse]") {  
253 - cx comp{1, 2};  
254 - app.add_complex("-c,--complex", comp);  
255 -  
256 - args = {"-c", "4j"};  
257 - run();  
258 - CHECK(comp.real() == Approx(0));  
259 - CHECK(comp.imag() == Approx(4));  
260 -  
261 - args = {"-c", "-4j"};  
262 - run();  
263 - CHECK(comp.real() == Approx(0));  
264 - CHECK(comp.imag() == Approx(-4));  
265 - args = {"-c", "-4"};  
266 - run();  
267 - CHECK(comp.real() == Approx(-4));  
268 - CHECK(comp.imag() == Approx(0));  
269 - args = {"-c", "+4"};  
270 - run();  
271 - CHECK(comp.real() == Approx(4));  
272 - CHECK(comp.imag() == Approx(0));  
273 -}  
274 -  
275 TEST_CASE_METHOD(TApp, "ComplexSingleImagOption", "[newparse]") { 134 TEST_CASE_METHOD(TApp, "ComplexSingleImagOption", "[newparse]") {
276 cx comp{1, 2}; 135 cx comp{1, 2};
277 app.add_option("-c,--complex", comp); 136 app.add_option("-c,--complex", comp);