1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-30 20:53:52 +00:00

Adding tidy cleanups (modernize)

This commit is contained in:
Henry Fredrick Schreiner 2017-05-31 07:38:22 -04:00
parent d697cb6d85
commit 101c926dac
13 changed files with 36 additions and 33 deletions

View File

@ -12,6 +12,7 @@
#include <sstream> #include <sstream>
#include <set> #include <set>
#include <numeric> #include <numeric>
#include <utility>
#include <vector> #include <vector>
@ -33,7 +34,7 @@ struct AppFriend;
class App; class App;
typedef std::unique_ptr<App> App_p; using App_p = std::unique_ptr<App>;
/// Creates a command line program, with very few defaults. /// Creates a command line program, with very few defaults.
/** To use, create a new `Program()` instance with `argc`, `argv`, and a help description. The templated /** To use, create a new `Program()` instance with `argc`, `argv`, and a help description. The templated
@ -122,7 +123,7 @@ protected:
/// Special private constructor for subcommand /// Special private constructor for subcommand
App(std::string description_, bool help, detail::enabler) App(std::string description_, bool help, detail::enabler)
: description_(description_) { : description_(std::move(description_)) {
if(help) if(help)
help_ptr_ = add_flag("-h,--help", "Print this help message and exit"); help_ptr_ = add_flag("-h,--help", "Print this help message and exit");
@ -514,7 +515,7 @@ public:
name_ = argv[0]; name_ = argv[0];
std::vector<std::string> args; std::vector<std::string> args;
for(int i=argc-1; i>0; i--) for(int i=argc-1; i>0; i--)
args.push_back(argv[i]); args.emplace_back(argv[i]);
return parse(args); return parse(args);
} }
@ -949,7 +950,7 @@ protected:
try { try {
size_t ui = std::stoul(val); size_t ui = std::stoul(val);
for (size_t i=0; i<ui; i++) for (size_t i=0; i<ui; i++)
op->results_.push_back(""); op->results_.emplace_back("");
} catch (const std::invalid_argument &) { } catch (const std::invalid_argument &) {
throw ConversionError(current.fullname + ": Should be true/false or a number"); throw ConversionError(current.fullname + ": Should be true/false or a number");
} }

View File

@ -24,10 +24,10 @@ inline std::string inijoin(std::vector<std::string> args) {
auto it = std::find_if(arg.begin(), arg.end(), [](char ch){ return std::isspace<char>(ch , std::locale());}); auto it = std::find_if(arg.begin(), arg.end(), [](char ch){ return std::isspace<char>(ch , std::locale());});
if(it == arg.end()) if(it == arg.end())
s << arg; s << arg;
else if(arg.find("\"") == std::string::npos) else if(arg.find(R"(")") == std::string::npos)
s << "\"" << arg << "\""; s << R"(")" << arg << R"(")";
else else
s << "\'" << arg << "\'"; s << R"(')" << arg << R"(')";
} }
return s.str(); return s.str();

View File

@ -5,6 +5,7 @@
#include <string> #include <string>
#include <functional> #include <functional>
#include <utility>
#include <vector> #include <vector>
#include <tuple> #include <tuple>
#include <algorithm> #include <algorithm>
@ -17,13 +18,13 @@
namespace CLI { namespace CLI {
typedef std::vector<std::string> results_t; using results_t = std::vector<std::string>;
typedef std::function<bool(results_t)> callback_t; using callback_t = std::function<bool (results_t)>;
class Option; class Option;
class App; class App;
typedef std::unique_ptr<Option> Option_p; using Option_p = std::unique_ptr<Option>;
class Option { class Option {
@ -113,7 +114,7 @@ protected:
/// Making an option by hand is not defined, it must be made by the App class /// Making an option by hand is not defined, it must be made by the App class
Option(std::string name, std::string description = "", std::function<bool(results_t)> callback=[](results_t){return true;}, bool default_=true, App* parent = nullptr) : Option(std::string name, std::string description = "", std::function<bool(results_t)> callback=[](results_t){return true;}, bool default_=true, App* parent = nullptr) :
description_(description), default_(default_), parent_(parent), callback_(callback) { description_(std::move(description)), default_(default_), parent_(parent), callback_(std::move(callback)) {
std::tie(snames_, lnames_, pname_) = detail::get_names(detail::split_names(name)); std::tie(snames_, lnames_, pname_) = detail::get_names(detail::split_names(name));
} }

View File

@ -65,7 +65,7 @@ inline std::tuple<std::vector<std::string>,std::vector<std::string>, std::string
continue; continue;
else if(name.length() > 1 && name[0] == '-' && name[1] != '-') { else if(name.length() > 1 && name[0] == '-' && name[1] != '-') {
if(name.length()==2 && valid_first_char(name[1])) if(name.length()==2 && valid_first_char(name[1]))
short_names.push_back(std::string(1,name[1])); short_names.emplace_back(1,name[1]);
else else
throw BadNameString("Invalid one char name: "+name); throw BadNameString("Invalid one char name: "+name);
} else if(name.length() > 2 && name.substr(0,2) == "--") { } else if(name.length() > 2 && name.substr(0,2) == "--") {

View File

@ -20,7 +20,7 @@ inline std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems; std::vector<std::string> elems;
// Check to see if emtpy string, give consistent result // Check to see if emtpy string, give consistent result
if(s=="") if(s=="")
elems.push_back(""); elems.emplace_back("");
else { else {
std::stringstream ss; std::stringstream ss;
ss.str(s); ss.str(s);

View File

@ -7,19 +7,20 @@
#include <iostream> #include <iostream>
#include <chrono> #include <chrono>
#include <functional> #include <functional>
#include <utility>
namespace CLI { namespace CLI {
class Timer { class Timer {
protected: protected:
/// This is a typedef to make clocks easier to use /// This is a typedef to make clocks easier to use
typedef std::chrono::steady_clock clock; using clock = std::chrono::steady_clock;
/// This typedef is for points in time /// This typedef is for points in time
typedef std::chrono::time_point<clock> time_point; using time_point = std::chrono::time_point<clock>;
/// This is the type of a printing function, you can make your own /// This is the type of a printing function, you can make your own
typedef std::function<std::string(std::string, std::string)> time_print_t; using time_print_t = std::function<std::string (std::string, std::string)>;
/// This is the title of the timer /// This is the title of the timer
std::string title_; std::string title_;
@ -50,7 +51,7 @@ public:
public: public:
/// Standard constructor, can set title and print function /// Standard constructor, can set title and print function
Timer(std::string title="Timer", time_print_t time_print = Simple) Timer(std::string title="Timer", time_print_t time_print = Simple)
: title_(title), time_print_(time_print), start_(clock::now()) {} : title_(std::move(title)), time_print_(std::move(time_print)), start_(clock::now()) {}
/// Time a function by running it multiple times. Target time is the len to target. /// Time a function by running it multiple times. Target time is the len to target.
std::string time_it(std::function<void()> f, double target_time=1) { std::string time_it(std::function<void()> f, double target_time=1) {

View File

@ -1,5 +1,5 @@
#include "app_helper.hpp" #include "app_helper.hpp"
#include <stdlib.h> #include <cstdlib>
TEST_F(TApp, OneFlagShort) { TEST_F(TApp, OneFlagShort) {

View File

@ -1,5 +1,5 @@
#include "app_helper.hpp" #include "app_helper.hpp"
#include <stdlib.h> #include <cstdlib>
TEST_F(TApp, AddingExistingShort) { TEST_F(TApp, AddingExistingShort) {
app.add_flag("-c,--count"); app.add_flag("-c,--count");

View File

@ -283,7 +283,7 @@ TEST(Exit, ErrorWithoutHelp) {
TEST(Exit, ExitCodes) { TEST(Exit, ExitCodes) {
CLI::App app; CLI::App app;
int i = static_cast<int>(CLI::ExitCodes::Extras); auto i = static_cast<int>(CLI::ExitCodes::Extras);
EXPECT_EQ(0, app.exit(CLI::Success())); EXPECT_EQ(0, app.exit(CLI::Success()));
EXPECT_EQ(0, app.exit(CLI::CallForHelp())); EXPECT_EQ(0, app.exit(CLI::CallForHelp()));
EXPECT_EQ(i, app.exit(CLI::ExtrasError("Thing"))); EXPECT_EQ(i, app.exit(CLI::ExtrasError("Thing")));

View File

@ -2,7 +2,7 @@
#include <cstdio> #include <cstdio>
#include <fstream> #include <fstream>
#include <stdint.h> #include <cstdint>
#include <string> #include <string>
TEST(Split, SimpleByToken) { TEST(Split, SimpleByToken) {
@ -278,33 +278,33 @@ TEST(Join, Backward) {
TEST(SplitUp, Simple) { TEST(SplitUp, Simple) {
std::vector<std::string> oput = {"one", "two three"}; std::vector<std::string> oput = {"one", "two three"};
std::string orig {"one \"two three\""}; std::string orig {R"(one "two three")"};
std::vector<std::string> result = CLI::detail::split_up(orig); std::vector<std::string> result = CLI::detail::split_up(orig);
EXPECT_EQ(oput, result); EXPECT_EQ(oput, result);
} }
TEST(SplitUp, Layered) { TEST(SplitUp, Layered) {
std::vector<std::string> output = {"one \'two three\'"}; std::vector<std::string> output = {R"(one 'two three')"};
std::string orig {"\"one \'two three\'\""}; std::string orig {R"("one 'two three'")"};
std::vector<std::string> result = CLI::detail::split_up(orig); std::vector<std::string> result = CLI::detail::split_up(orig);
EXPECT_EQ(output, result); EXPECT_EQ(output, result);
} }
TEST(SplitUp, Spaces) { TEST(SplitUp, Spaces) {
std::vector<std::string> oput = {"one", " two three"}; std::vector<std::string> oput = {"one", " two three"};
std::string orig {" one \" two three\" "}; std::string orig {R"( one " two three" )"};
std::vector<std::string> result = CLI::detail::split_up(orig); std::vector<std::string> result = CLI::detail::split_up(orig);
EXPECT_EQ(oput, result); EXPECT_EQ(oput, result);
} }
TEST(SplitUp, BadStrings) { TEST(SplitUp, BadStrings) {
std::vector<std::string> oput = {"one", " two three"}; std::vector<std::string> oput = {"one", " two three"};
std::string orig {" one \" two three "}; std::string orig {R"( one " two three )"};
std::vector<std::string> result = CLI::detail::split_up(orig); std::vector<std::string> result = CLI::detail::split_up(orig);
EXPECT_EQ(oput, result); EXPECT_EQ(oput, result);
oput = {"one", " two three"}; oput = {"one", " two three"};
orig = " one \' two three "; orig = R"( one ' two three )";
result = CLI::detail::split_up(orig); result = CLI::detail::split_up(orig);
EXPECT_EQ(oput, result); EXPECT_EQ(oput, result);
} }

View File

@ -51,9 +51,9 @@ TEST(StringBased, FirstWithComments) {
TEST(StringBased, Quotes) { TEST(StringBased, Quotes) {
std::stringstream ofile; std::stringstream ofile;
ofile << "one = \"three\"" << std::endl; ofile << R"(one = "three")" << std::endl;
ofile << "two = \'four\'" << std::endl; ofile << R"(two = 'four')" << std::endl;
ofile << "five = \"six and seven\"" << std::endl; ofile << R"(five = "six and seven")" << std::endl;
ofile.seekg(0, std::ios::beg); ofile.seekg(0, std::ios::beg);
@ -575,7 +575,7 @@ TEST_F(TApp, IniQuotedOutput) {
std::string val2; std::string val2;
app.add_option("--val2", val2); app.add_option("--val2", val2);
args = {"--val1", "I am a string", "--val2", "I am a \"confusing\" string"}; args = {"--val1", "I am a string", "--val2", R"(I am a "confusing" string)"};
run(); run();

View File

@ -4,7 +4,7 @@
using ::testing::HasSubstr; using ::testing::HasSubstr;
typedef std::complex<double> cx; using cx = std::complex<double>;
CLI::Option* add_option(CLI::App& app, CLI::Option* add_option(CLI::App& app,
std::string name, cx& variable, std::string name, cx& variable,

View File

@ -6,7 +6,7 @@
#include "gtest/gtest.h" #include "gtest/gtest.h"
typedef std::vector<std::string> input_t; using input_t = std::vector<std::string>;
TEST(Basic, Empty) { TEST(Basic, Empty) {