Commit 101c926dacf4dd79894909f6557dcd09f629b6b3

Authored by Henry Fredrick Schreiner
1 parent d697cb6d

Adding tidy cleanups (modernize)

include/CLI/App.hpp
@@ -12,6 +12,7 @@ @@ -12,6 +12,7 @@
12 #include <sstream> 12 #include <sstream>
13 #include <set> 13 #include <set>
14 #include <numeric> 14 #include <numeric>
  15 +#include <utility>
15 #include <vector> 16 #include <vector>
16 17
17 18
@@ -33,7 +34,7 @@ struct AppFriend; @@ -33,7 +34,7 @@ struct AppFriend;
33 class App; 34 class App;
34 35
35 36
36 -typedef std::unique_ptr<App> App_p; 37 +using App_p = std::unique_ptr<App>;
37 38
38 /// Creates a command line program, with very few defaults. 39 /// Creates a command line program, with very few defaults.
39 /** To use, create a new `Program()` instance with `argc`, `argv`, and a help description. The templated 40 /** To use, create a new `Program()` instance with `argc`, `argv`, and a help description. The templated
@@ -122,7 +123,7 @@ protected: @@ -122,7 +123,7 @@ protected:
122 123
123 /// Special private constructor for subcommand 124 /// Special private constructor for subcommand
124 App(std::string description_, bool help, detail::enabler) 125 App(std::string description_, bool help, detail::enabler)
125 - : description_(description_) { 126 + : description_(std::move(description_)) {
126 127
127 if(help) 128 if(help)
128 help_ptr_ = add_flag("-h,--help", "Print this help message and exit"); 129 help_ptr_ = add_flag("-h,--help", "Print this help message and exit");
@@ -514,7 +515,7 @@ public: @@ -514,7 +515,7 @@ public:
514 name_ = argv[0]; 515 name_ = argv[0];
515 std::vector<std::string> args; 516 std::vector<std::string> args;
516 for(int i=argc-1; i>0; i--) 517 for(int i=argc-1; i>0; i--)
517 - args.push_back(argv[i]); 518 + args.emplace_back(argv[i]);
518 return parse(args); 519 return parse(args);
519 520
520 } 521 }
@@ -949,7 +950,7 @@ protected: @@ -949,7 +950,7 @@ protected:
949 try { 950 try {
950 size_t ui = std::stoul(val); 951 size_t ui = std::stoul(val);
951 for (size_t i=0; i<ui; i++) 952 for (size_t i=0; i<ui; i++)
952 - op->results_.push_back(""); 953 + op->results_.emplace_back("");
953 } catch (const std::invalid_argument &) { 954 } catch (const std::invalid_argument &) {
954 throw ConversionError(current.fullname + ": Should be true/false or a number"); 955 throw ConversionError(current.fullname + ": Should be true/false or a number");
955 } 956 }
include/CLI/Ini.hpp
@@ -24,10 +24,10 @@ inline std::string inijoin(std::vector&lt;std::string&gt; args) { @@ -24,10 +24,10 @@ inline std::string inijoin(std::vector&lt;std::string&gt; args) {
24 auto it = std::find_if(arg.begin(), arg.end(), [](char ch){ return std::isspace<char>(ch , std::locale());}); 24 auto it = std::find_if(arg.begin(), arg.end(), [](char ch){ return std::isspace<char>(ch , std::locale());});
25 if(it == arg.end()) 25 if(it == arg.end())
26 s << arg; 26 s << arg;
27 - else if(arg.find("\"") == std::string::npos)  
28 - s << "\"" << arg << "\""; 27 + else if(arg.find(R"(")") == std::string::npos)
  28 + s << R"(")" << arg << R"(")";
29 else 29 else
30 - s << "\'" << arg << "\'"; 30 + s << R"(')" << arg << R"(')";
31 } 31 }
32 32
33 return s.str(); 33 return s.str();
include/CLI/Option.hpp
@@ -5,6 +5,7 @@ @@ -5,6 +5,7 @@
5 5
6 #include <string> 6 #include <string>
7 #include <functional> 7 #include <functional>
  8 +#include <utility>
8 #include <vector> 9 #include <vector>
9 #include <tuple> 10 #include <tuple>
10 #include <algorithm> 11 #include <algorithm>
@@ -17,13 +18,13 @@ @@ -17,13 +18,13 @@
17 18
18 namespace CLI { 19 namespace CLI {
19 20
20 -typedef std::vector<std::string> results_t;  
21 -typedef std::function<bool(results_t)> callback_t; 21 +using results_t = std::vector<std::string>;
  22 +using callback_t = std::function<bool (results_t)>;
22 23
23 class Option; 24 class Option;
24 class App; 25 class App;
25 26
26 -typedef std::unique_ptr<Option> Option_p; 27 +using Option_p = std::unique_ptr<Option>;
27 28
28 29
29 class Option { 30 class Option {
@@ -113,7 +114,7 @@ protected: @@ -113,7 +114,7 @@ protected:
113 114
114 /// Making an option by hand is not defined, it must be made by the App class 115 /// Making an option by hand is not defined, it must be made by the App class
115 Option(std::string name, std::string description = "", std::function<bool(results_t)> callback=[](results_t){return true;}, bool default_=true, App* parent = nullptr) : 116 Option(std::string name, std::string description = "", std::function<bool(results_t)> callback=[](results_t){return true;}, bool default_=true, App* parent = nullptr) :
116 - description_(description), default_(default_), parent_(parent), callback_(callback) { 117 + description_(std::move(description)), default_(default_), parent_(parent), callback_(std::move(callback)) {
117 std::tie(snames_, lnames_, pname_) = detail::get_names(detail::split_names(name)); 118 std::tie(snames_, lnames_, pname_) = detail::get_names(detail::split_names(name));
118 } 119 }
119 120
include/CLI/Split.hpp
@@ -65,7 +65,7 @@ inline std::tuple&lt;std::vector&lt;std::string&gt;,std::vector&lt;std::string&gt;, std::string @@ -65,7 +65,7 @@ inline std::tuple&lt;std::vector&lt;std::string&gt;,std::vector&lt;std::string&gt;, std::string
65 continue; 65 continue;
66 else if(name.length() > 1 && name[0] == '-' && name[1] != '-') { 66 else if(name.length() > 1 && name[0] == '-' && name[1] != '-') {
67 if(name.length()==2 && valid_first_char(name[1])) 67 if(name.length()==2 && valid_first_char(name[1]))
68 - short_names.push_back(std::string(1,name[1])); 68 + short_names.emplace_back(1,name[1]);
69 else 69 else
70 throw BadNameString("Invalid one char name: "+name); 70 throw BadNameString("Invalid one char name: "+name);
71 } else if(name.length() > 2 && name.substr(0,2) == "--") { 71 } else if(name.length() > 2 && name.substr(0,2) == "--") {
include/CLI/StringTools.hpp
@@ -20,7 +20,7 @@ inline std::vector&lt;std::string&gt; split(const std::string &amp;s, char delim) { @@ -20,7 +20,7 @@ inline std::vector&lt;std::string&gt; split(const std::string &amp;s, char delim) {
20 std::vector<std::string> elems; 20 std::vector<std::string> elems;
21 // Check to see if emtpy string, give consistent result 21 // Check to see if emtpy string, give consistent result
22 if(s=="") 22 if(s=="")
23 - elems.push_back(""); 23 + elems.emplace_back("");
24 else { 24 else {
25 std::stringstream ss; 25 std::stringstream ss;
26 ss.str(s); 26 ss.str(s);
include/CLI/Timer.hpp
@@ -7,19 +7,20 @@ @@ -7,19 +7,20 @@
7 #include <iostream> 7 #include <iostream>
8 #include <chrono> 8 #include <chrono>
9 #include <functional> 9 #include <functional>
  10 +#include <utility>
10 11
11 namespace CLI { 12 namespace CLI {
12 13
13 class Timer { 14 class Timer {
14 protected: 15 protected:
15 /// This is a typedef to make clocks easier to use 16 /// This is a typedef to make clocks easier to use
16 - typedef std::chrono::steady_clock clock; 17 + using clock = std::chrono::steady_clock;
17 18
18 /// This typedef is for points in time 19 /// This typedef is for points in time
19 - typedef std::chrono::time_point<clock> time_point; 20 + using time_point = std::chrono::time_point<clock>;
20 21
21 /// This is the type of a printing function, you can make your own 22 /// This is the type of a printing function, you can make your own
22 - typedef std::function<std::string(std::string, std::string)> time_print_t; 23 + using time_print_t = std::function<std::string (std::string, std::string)>;
23 24
24 /// This is the title of the timer 25 /// This is the title of the timer
25 std::string title_; 26 std::string title_;
@@ -50,7 +51,7 @@ public: @@ -50,7 +51,7 @@ public:
50 public: 51 public:
51 /// Standard constructor, can set title and print function 52 /// Standard constructor, can set title and print function
52 Timer(std::string title="Timer", time_print_t time_print = Simple) 53 Timer(std::string title="Timer", time_print_t time_print = Simple)
53 - : title_(title), time_print_(time_print), start_(clock::now()) {} 54 + : title_(std::move(title)), time_print_(std::move(time_print)), start_(clock::now()) {}
54 55
55 /// Time a function by running it multiple times. Target time is the len to target. 56 /// Time a function by running it multiple times. Target time is the len to target.
56 std::string time_it(std::function<void()> f, double target_time=1) { 57 std::string time_it(std::function<void()> f, double target_time=1) {
tests/AppTest.cpp
1 #include "app_helper.hpp" 1 #include "app_helper.hpp"
2 -#include <stdlib.h> 2 +#include <cstdlib>
3 3
4 4
5 TEST_F(TApp, OneFlagShort) { 5 TEST_F(TApp, OneFlagShort) {
tests/CreationTest.cpp
1 #include "app_helper.hpp" 1 #include "app_helper.hpp"
2 -#include <stdlib.h> 2 +#include <cstdlib>
3 3
4 TEST_F(TApp, AddingExistingShort) { 4 TEST_F(TApp, AddingExistingShort) {
5 app.add_flag("-c,--count"); 5 app.add_flag("-c,--count");
tests/HelpTest.cpp
@@ -283,7 +283,7 @@ TEST(Exit, ErrorWithoutHelp) { @@ -283,7 +283,7 @@ TEST(Exit, ErrorWithoutHelp) {
283 TEST(Exit, ExitCodes) { 283 TEST(Exit, ExitCodes) {
284 CLI::App app; 284 CLI::App app;
285 285
286 - int i = static_cast<int>(CLI::ExitCodes::Extras); 286 + auto i = static_cast<int>(CLI::ExitCodes::Extras);
287 EXPECT_EQ(0, app.exit(CLI::Success())); 287 EXPECT_EQ(0, app.exit(CLI::Success()));
288 EXPECT_EQ(0, app.exit(CLI::CallForHelp())); 288 EXPECT_EQ(0, app.exit(CLI::CallForHelp()));
289 EXPECT_EQ(i, app.exit(CLI::ExtrasError("Thing"))); 289 EXPECT_EQ(i, app.exit(CLI::ExtrasError("Thing")));
tests/HelpersTest.cpp
@@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
2 2
3 #include <cstdio> 3 #include <cstdio>
4 #include <fstream> 4 #include <fstream>
5 -#include <stdint.h> 5 +#include <cstdint>
6 #include <string> 6 #include <string>
7 7
8 TEST(Split, SimpleByToken) { 8 TEST(Split, SimpleByToken) {
@@ -278,33 +278,33 @@ TEST(Join, Backward) { @@ -278,33 +278,33 @@ TEST(Join, Backward) {
278 278
279 TEST(SplitUp, Simple) { 279 TEST(SplitUp, Simple) {
280 std::vector<std::string> oput = {"one", "two three"}; 280 std::vector<std::string> oput = {"one", "two three"};
281 - std::string orig {"one \"two three\""}; 281 + std::string orig {R"(one "two three")"};
282 std::vector<std::string> result = CLI::detail::split_up(orig); 282 std::vector<std::string> result = CLI::detail::split_up(orig);
283 EXPECT_EQ(oput, result); 283 EXPECT_EQ(oput, result);
284 } 284 }
285 285
286 TEST(SplitUp, Layered) { 286 TEST(SplitUp, Layered) {
287 - std::vector<std::string> output = {"one \'two three\'"};  
288 - std::string orig {"\"one \'two three\'\""}; 287 + std::vector<std::string> output = {R"(one 'two three')"};
  288 + std::string orig {R"("one 'two three'")"};
289 std::vector<std::string> result = CLI::detail::split_up(orig); 289 std::vector<std::string> result = CLI::detail::split_up(orig);
290 EXPECT_EQ(output, result); 290 EXPECT_EQ(output, result);
291 } 291 }
292 292
293 TEST(SplitUp, Spaces) { 293 TEST(SplitUp, Spaces) {
294 std::vector<std::string> oput = {"one", " two three"}; 294 std::vector<std::string> oput = {"one", " two three"};
295 - std::string orig {" one \" two three\" "}; 295 + std::string orig {R"( one " two three" )"};
296 std::vector<std::string> result = CLI::detail::split_up(orig); 296 std::vector<std::string> result = CLI::detail::split_up(orig);
297 EXPECT_EQ(oput, result); 297 EXPECT_EQ(oput, result);
298 } 298 }
299 299
300 TEST(SplitUp, BadStrings) { 300 TEST(SplitUp, BadStrings) {
301 std::vector<std::string> oput = {"one", " two three"}; 301 std::vector<std::string> oput = {"one", " two three"};
302 - std::string orig {" one \" two three "}; 302 + std::string orig {R"( one " two three )"};
303 std::vector<std::string> result = CLI::detail::split_up(orig); 303 std::vector<std::string> result = CLI::detail::split_up(orig);
304 EXPECT_EQ(oput, result); 304 EXPECT_EQ(oput, result);
305 305
306 oput = {"one", " two three"}; 306 oput = {"one", " two three"};
307 - orig = " one \' two three "; 307 + orig = R"( one ' two three )";
308 result = CLI::detail::split_up(orig); 308 result = CLI::detail::split_up(orig);
309 EXPECT_EQ(oput, result); 309 EXPECT_EQ(oput, result);
310 } 310 }
tests/IniTest.cpp
@@ -51,9 +51,9 @@ TEST(StringBased, FirstWithComments) { @@ -51,9 +51,9 @@ TEST(StringBased, FirstWithComments) {
51 TEST(StringBased, Quotes) { 51 TEST(StringBased, Quotes) {
52 std::stringstream ofile; 52 std::stringstream ofile;
53 53
54 - ofile << "one = \"three\"" << std::endl;  
55 - ofile << "two = \'four\'" << std::endl;  
56 - ofile << "five = \"six and seven\"" << std::endl; 54 + ofile << R"(one = "three")" << std::endl;
  55 + ofile << R"(two = 'four')" << std::endl;
  56 + ofile << R"(five = "six and seven")" << std::endl;
57 57
58 ofile.seekg(0, std::ios::beg); 58 ofile.seekg(0, std::ios::beg);
59 59
@@ -575,7 +575,7 @@ TEST_F(TApp, IniQuotedOutput) { @@ -575,7 +575,7 @@ TEST_F(TApp, IniQuotedOutput) {
575 std::string val2; 575 std::string val2;
576 app.add_option("--val2", val2); 576 app.add_option("--val2", val2);
577 577
578 - args = {"--val1", "I am a string", "--val2", "I am a \"confusing\" string"}; 578 + args = {"--val1", "I am a string", "--val2", R"(I am a "confusing" string)"};
579 579
580 run(); 580 run();
581 581
tests/NewParseTest.cpp
@@ -4,7 +4,7 @@ @@ -4,7 +4,7 @@
4 4
5 using ::testing::HasSubstr; 5 using ::testing::HasSubstr;
6 6
7 -typedef std::complex<double> cx; 7 +using cx = std::complex<double>;
8 8
9 CLI::Option* add_option(CLI::App& app, 9 CLI::Option* add_option(CLI::App& app,
10 std::string name, cx& variable, 10 std::string name, cx& variable,
tests/SimpleTest.cpp
@@ -6,7 +6,7 @@ @@ -6,7 +6,7 @@
6 6
7 #include "gtest/gtest.h" 7 #include "gtest/gtest.h"
8 8
9 -typedef std::vector<std::string> input_t; 9 +using input_t = std::vector<std::string>;
10 10
11 TEST(Basic, Empty) { 11 TEST(Basic, Empty) {
12 12