From 27f718125d0e0e2b6c49d5092f82bafcbb8a3ee3 Mon Sep 17 00:00:00 2001 From: Henry Fredrick Schreiner Date: Wed, 31 May 2017 12:03:05 -0400 Subject: [PATCH] Reformat with clang-format --- .clang-format | 86 ++++++ examples/try.cpp | 27 +- examples/try1.cpp | 13 +- examples/try2.cpp | 32 +-- include/CLI/App.hpp | 554 ++++++++++++++++-------------------- include/CLI/CLI.hpp | 1 - include/CLI/Error.hpp | 52 ++-- include/CLI/Ini.hpp | 26 +- include/CLI/Option.hpp | 216 ++++++-------- include/CLI/Split.hpp | 41 ++- include/CLI/StringTools.hpp | 93 +++--- include/CLI/Timer.hpp | 70 ++--- include/CLI/TypeTools.hpp | 168 +++++------ include/CLI/Validators.hpp | 28 +- tests/.syntastic_cpp_config | 3 +- tests/AppTest.cpp | 182 ++++++------ tests/CreationTest.cpp | 27 +- tests/HelpTest.cpp | 53 ++-- tests/HelpersTest.cpp | 82 +++--- tests/IniTest.cpp | 89 +++--- tests/NewParseTest.cpp | 43 ++- tests/SimpleTest.cpp | 20 +- tests/SubcommandTest.cpp | 72 ++--- tests/TimerTest.cpp | 5 +- tests/app_helper.hpp | 11 +- tests/link_test_1.cpp | 4 +- tests/link_test_2.cpp | 1 - 27 files changed, 902 insertions(+), 1097 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 00000000..666a2ba7 --- /dev/null +++ b/.clang-format @@ -0,0 +1,86 @@ +Language: Cpp +BasedOnStyle: LLVM +# AccessModifierOffset: -2 +# AlignAfterOpenBracket: Align +# AlignConsecutiveAssignments: false +# AlignConsecutiveDeclarations: false +# AlignEscapedNewlinesLeft: false +# AlignOperands: true +# AlignTrailingComments: true +# AllowAllParametersOfDeclarationOnNextLine: true +# AllowShortBlocksOnASingleLine: false +# AllowShortCaseLabelsOnASingleLine: false +# AllowShortFunctionsOnASingleLine: All +# AllowShortIfStatementsOnASingleLine: false +# AllowShortLoopsOnASingleLine: false +# AlwaysBreakAfterDefinitionReturnType: None +# AlwaysBreakAfterReturnType: None +# AlwaysBreakBeforeMultilineStrings: false +# AlwaysBreakTemplateDeclarations: false +BinPackArguments: false +BinPackParameters: false +# BraceWrapping: +# AfterClass: false +# AfterControlStatement: false +# AfterEnum: false +# AfterFunction: false +# AfterNamespace: false +# AfterObjCDeclaration: false +# AfterStruct: false +# AfterUnion: false +# BeforeCatch: false +# BeforeElse: false +# IndentBraces: false +# BreakBeforeBinaryOperators: None +# BreakBeforeBraces: Attach +# BreakBeforeTernaryOperators: true +# BreakConstructorInitializersBeforeComma: false +# BreakAfterJavaFieldAnnotations: false +# BreakStringLiterals: true +ColumnLimit: 120 +# CommentPragmas: '^ IWYU pragma:' +# ConstructorInitializerAllOnOneLineOrOnePerLine: false +# ConstructorInitializerIndentWidth: 4 +# ContinuationIndentWidth: 4 +# Cpp11BracedListStyle: true +# DerivePointerAlignment: false +# DisableFormat: false +# ExperimentalAutoDetectBinPacking: false +# ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ] +# IncludeIsMainRegex: '$' +# IndentCaseLabels: false +IndentWidth: 4 +# IndentWrappedFunctionNames: false +# JavaScriptQuotes: Leave +# JavaScriptWrapImports: true +# KeepEmptyLinesAtTheStartOfBlocks: true +# MacroBlockBegin: '' +# MacroBlockEnd: '' +# MaxEmptyLinesToKeep: 1 +# NamespaceIndentation: None +# ObjCBlockIndentWidth: 2 +# ObjCSpaceAfterProperty: false +# ObjCSpaceBeforeProtocolList: true +# PenaltyBreakBeforeFirstCallParameter: 19 +# PenaltyBreakComment: 300 +# PenaltyBreakFirstLessLess: 120 +# PenaltyBreakString: 1000 +# PenaltyExcessCharacter: 1000000 +# PenaltyReturnTypeOnItsOwnLine: 60 +# PointerAlignment: Right +# ReflowComments: true +SortIncludes: false +# SpaceAfterCStyleCast: false +# SpaceAfterTemplateKeyword: true +# SpaceBeforeAssignmentOperators: true +SpaceBeforeParens: Never +# SpaceInEmptyParentheses: false +# SpacesBeforeTrailingComments: 1 +# SpacesInAngles: false +# SpacesInContainerLiterals: true +# SpacesInCStyleCastParentheses: false +# SpacesInParentheses: false +# SpacesInSquareBrackets: false +Standard: Cpp11 +TabWidth: 4 +UseTab: Never diff --git a/examples/try.cpp b/examples/try.cpp index 3d967810..505af06f 100644 --- a/examples/try.cpp +++ b/examples/try.cpp @@ -1,33 +1,28 @@ #include "CLI/CLI.hpp" - -int main (int argc, char** argv) { +int main(int argc, char **argv) { CLI::App app("K3Pi goofit fitter"); std::string file; - CLI::Option* opt = app.add_option("-f,--file,file", file, "File name"); - - int count; - CLI::Option* copt = app.add_flag("-c,--count", count, "Counter"); + CLI::Option *opt = app.add_option("-f,--file,file", file, "File name"); - double value;// = 3.14; + int count; + CLI::Option *copt = app.add_flag("-c,--count", count, "Counter"); + + double value; // = 3.14; app.add_option("-d,--double", value, "Some Value"); try { app.parse(argc, argv); - } catch (const CLI::Error &e) { + } catch(const CLI::Error &e) { return app.exit(e); } - std::cout << "Working on file: " << file - << ", direct count: " << app.count("--file") - << ", opt count: " << opt->count() - << std::endl; - std::cout << "Working on count: " << count - << ", direct count: " << app.count("--count") - << ", opt count: " << copt->count() - << std::endl; + std::cout << "Working on file: " << file << ", direct count: " << app.count("--file") + << ", opt count: " << opt->count() << std::endl; + std::cout << "Working on count: " << count << ", direct count: " << app.count("--count") + << ", opt count: " << copt->count() << std::endl; std::cout << "Some value: " << value << std::endl; return 0; diff --git a/examples/try1.cpp b/examples/try1.cpp index f4a37bb0..39bc7eb6 100644 --- a/examples/try1.cpp +++ b/examples/try1.cpp @@ -1,21 +1,20 @@ #include "CLI/CLI.hpp" - -int main (int argc, char** argv) { +int main(int argc, char **argv) { CLI::App app("K3Pi goofit fitter"); app.add_flag("--random", "Some random flag"); - CLI::App* start = app.add_subcommand("start", "A great subcommand"); - CLI::App* stop = app.add_subcommand("stop", "Do you really want to stop?"); + CLI::App *start = app.add_subcommand("start", "A great subcommand"); + CLI::App *stop = app.add_subcommand("stop", "Do you really want to stop?"); std::string file; start->add_option("-f,--file", file, "File name"); - - CLI::Option* s = stop->add_flag("-c,--count", "Counter"); + + CLI::Option *s = stop->add_flag("-c,--count", "Counter"); try { app.parse(argc, argv); - } catch (const CLI::Error &e) { + } catch(const CLI::Error &e) { return app.exit(e); } diff --git a/examples/try2.cpp b/examples/try2.cpp index 47e41dad..0b6b109b 100644 --- a/examples/try2.cpp +++ b/examples/try2.cpp @@ -1,38 +1,30 @@ #include "CLI/CLI.hpp" #include "CLI/Timer.hpp" - -int main (int argc, char** argv) { +int main(int argc, char **argv) { CLI::AutoTimer("This is a timer"); CLI::App app("K3Pi goofit fitter"); std::string file; - CLI::Option* opt = app.add_option("-f,--file,file", file, "File name") - ->required()->group("Important"); - - int count; - CLI::Option* copt = app.add_flag("-c,--count", count, "Counter") - ->required()->group("Important"); + CLI::Option *opt = app.add_option("-f,--file,file", file, "File name")->required()->group("Important"); - double value;// = 3.14; - app.add_option("-d,--double", value, "Some Value") - ->group("Other"); + int count; + CLI::Option *copt = app.add_flag("-c,--count", count, "Counter")->required()->group("Important"); + + double value; // = 3.14; + app.add_option("-d,--double", value, "Some Value")->group("Other"); try { app.parse(argc, argv); - } catch (const CLI::Error &e) { + } catch(const CLI::Error &e) { return app.exit(e); } - std::cout << "Working on file: " << file - << ", direct count: " << app.count("--file") - << ", opt count: " << opt->count() - << std::endl; - std::cout << "Working on count: " << count - << ", direct count: " << app.count("--count") - << ", opt count: " << copt->count() - << std::endl; + std::cout << "Working on file: " << file << ", direct count: " << app.count("--file") + << ", opt count: " << opt->count() << std::endl; + std::cout << "Working on count: " << count << ", direct count: " << app.count("--count") + << ", opt count: " << copt->count() << std::endl; std::cout << "Some value: " << value << std::endl; return 0; diff --git a/include/CLI/App.hpp b/include/CLI/App.hpp index d6047f1a..a9f63eaa 100644 --- a/include/CLI/App.hpp +++ b/include/CLI/App.hpp @@ -15,7 +15,6 @@ #include #include - // CLI Library includes #include "CLI/Error.hpp" #include "CLI/Ini.hpp" @@ -27,13 +26,12 @@ namespace CLI { namespace detail { -enum class Classifer {NONE, POSITIONAL_MARK, SHORT, LONG, SUBCOMMAND}; +enum class Classifer { NONE, POSITIONAL_MARK, SHORT, LONG, SUBCOMMAND }; struct AppFriend; } // namespace detail class App; - using App_p = std::unique_ptr; /// Creates a command line program, with very few defaults. @@ -43,21 +41,21 @@ using App_p = std::unique_ptr; class App { friend Option; friend detail::AppFriend; -protected: - + + protected: // This library follows the Google style guide for member names ending in underscores /// @name Basics ///@{ - + /// Subcommand name or program name (from parser) - std::string name_ {"program"}; - + std::string name_{"program"}; + /// Description of the current program/subcommand std::string description_; /// If true, allow extra arguments (ie, don't throw an error). - bool allow_extras_ {false}; + bool allow_extras_{false}; /// This is a function that runs when complete. Great for subcommands. Can throw. std::function callback_; @@ -68,9 +66,9 @@ protected: /// The list of options, stored locally std::vector options_; - + /// A pointer to the help flag if there is one - Option* help_ptr_ {nullptr}; + Option *help_ptr_{nullptr}; ///@} /// @name Parsing @@ -79,28 +77,28 @@ protected: using missing_t = std::vector>; /// Pair of classifier, string for missing options. (extra detail is removed on returning from parse) - /// + /// /// This is faster and cleaner than storing just a list of strings and reparsing. This may contain the -- separator. missing_t missing_; ///@} /// @name Subcommands ///@{ - + /// Storage for subcommand list std::vector subcommands_; /// If true, the program name is not case sensitive - bool ignore_case_ {false}; + bool ignore_case_{false}; /// Allow subcommand fallthrough, so that parent commands can collect commands after subcommand. - bool fallthrough_ {false}; + bool fallthrough_{false}; /// A pointer to the parent if this is a subcommand - App* parent_ {nullptr}; + App *parent_{nullptr}; /// True if this command/subcommand was parsed - bool parsed_ {false}; + bool parsed_{false}; /// -1 for 1 or more, 0 for not required, # for exact number required int require_subcommand_ = 0; @@ -112,53 +110,47 @@ protected: /// The name of the connected config file std::string config_name_; - /// True if ini is required (throws if not present), if false simply keep going. - bool config_required_ {false}; + /// True if ini is required (throws if not present), if false simply keep going. + bool config_required_{false}; /// Pointer to the config option - Option* config_ptr_ {nullptr}; - + Option *config_ptr_{nullptr}; ///@} - + /// Special private constructor for subcommand - App(std::string description_, bool help, detail::enabler) - : description_(std::move(description_)) { + App(std::string description_, bool help, detail::enabler) : description_(std::move(description_)) { if(help) help_ptr_ = add_flag("-h,--help", "Print this help message and exit"); - } -public: + public: /// @name Basic ///@{ /// Create a new program. Pass in the same arguments as main(), along with a help string. - App(std::string description_="", bool help=true) - : App(description_, help, detail::dummy) { - - } + App(std::string description_ = "", bool help = true) : App(description_, help, detail::dummy) {} /// Set a callback for the end of parsing. - /// + /// /// Due to a bug in c++11, /// it is not possible to overload on std::function (fixed in c++14 /// and backported to c++11 on newer compilers). Use capture by reference /// to get a pointer to App if needed. - App* set_callback(std::function callback) { + App *set_callback(std::function callback) { callback_ = callback; return this; } /// Remove the error when extras are left over on the command line. - App* allow_extras (bool allow=true) { + App *allow_extras(bool allow = true) { allow_extras_ = allow; return this; } /// Ignore case. Subcommand inherit value. - App* ignore_case(bool value = true) { + App *ignore_case(bool value = true) { ignore_case_ = value; if(parent_ != nullptr) { for(const auto &subc : parent_->subcommands_) { @@ -170,81 +162,72 @@ public: } /// Check to see if this subcommand was parsed, true only if received on command line. - bool parsed() const {return parsed_;} + bool parsed() const { return parsed_; } /// Check to see if this subcommand was parsed, true only if received on command line. /// This allows the subcommand to be directly checked. - operator bool () const { return parsed_;} + operator bool() const { return parsed_; } /// Require a subcommand to be given (does not affect help call) /// Does not return a pointer since it is supposed to be called on the main App. - App* require_subcommand(int value = -1) { + App *require_subcommand(int value = -1) { require_subcommand_ = value; return this; } /// Stop subcommand fallthrough, so that parent commands cannot collect commands after subcommand. /// Default from parent, usually set on parent. - App* fallthrough(bool value=true) { + App *fallthrough(bool value = true) { fallthrough_ = value; return this; } - ///@} /// @name Adding options ///@{ - + /// Add an option, will automatically understand the type for common types. /// /// To use, create a variable with the expected type, and pass it in after the name. /// After start is called, you can use count to see if the value was passed, and /// the value will be initialized properly. Numbers, vectors, and strings are supported. - /// - /// ->required(), ->default, and the validators are options, + /// + /// ->required(), ->default, and the validators are options, /// The positional options take an optional number of arguments. - /// + /// /// For example, - /// + /// /// std::string filename; /// program.add_option("filename", filename, "description of filename"); /// - Option* add_option( - std::string name, - callback_t callback, - std::string description="", - bool defaulted=false - ) { + Option *add_option(std::string name, callback_t callback, std::string description = "", bool defaulted = false) { Option myopt{name, description, callback, defaulted, this}; - if(std::find_if(std::begin(options_), std::end(options_), - [&myopt](const Option_p &v){return *v == myopt;}) == std::end(options_)) { + if(std::find_if(std::begin(options_), std::end(options_), [&myopt](const Option_p &v) { + return *v == myopt; + }) == std::end(options_)) { options_.emplace_back(); - Option_p& option = options_.back(); + Option_p &option = options_.back(); option.reset(new Option(name, description, callback, defaulted, this)); return option.get(); } else throw OptionAlreadyAdded(myopt.get_name()); - } /// Add option for non-vectors - template::value, detail::enabler> = detail::dummy> - Option* add_option( - std::string name, - T &variable, ///< The variable to set - std::string description="", - bool defaulted=false - ) { + template ::value, detail::enabler> = detail::dummy> + Option *add_option(std::string name, + T &variable, ///< The variable to set + std::string description = "", + bool defaulted = false) { - - CLI::callback_t fun = [&variable](CLI::results_t res){ - if(res.size()!=1) + CLI::callback_t fun = [&variable](CLI::results_t res) { + if(res.size() != 1) return false; return detail::lexical_cast(res[0], variable); }; - Option* opt = add_option(name, fun, description, defaulted); + Option *opt = add_option(name, fun, description, defaulted); opt->set_custom_option(detail::type_name()); if(defaulted) { std::stringstream out; @@ -255,15 +238,13 @@ public: } /// Add option for vectors - template - Option* add_option( - std::string name, - std::vector &variable, ///< The variable vector to set - std::string description="", - bool defaulted=false - ) { + template + Option *add_option(std::string name, + std::vector &variable, ///< The variable vector to set + std::string description = "", + bool defaulted = false) { - CLI::callback_t fun = [&variable](CLI::results_t res){ + CLI::callback_t fun = [&variable](CLI::results_t res) { bool retval = true; variable.clear(); for(const auto &a : res) { @@ -273,7 +254,7 @@ public: return variable.size() > 0 && retval; }; - Option* opt = add_option(name, fun, description, defaulted); + Option *opt = add_option(name, fun, description, defaulted); opt->set_custom_option(detail::type_name(), -1, true); if(defaulted) opt->set_default_val("[" + detail::join(variable) + "]"); @@ -281,15 +262,10 @@ public: } /// Add option for flag - Option* add_flag( - std::string name, - std::string description="" - ) { - CLI::callback_t fun = [](CLI::results_t){ - return true; - }; - - Option* opt = add_option(name, fun, description, false); + Option *add_flag(std::string name, std::string description = "") { + CLI::callback_t fun = [](CLI::results_t) { return true; }; + + Option *opt = add_option(name, fun, description, false); if(opt->get_positional()) throw IncorrectConstruction("Flags cannot be positional"); opt->set_custom_option("", 0); @@ -297,21 +273,19 @@ public: } /// Add option for flag integer - template::value && !is_bool::value, detail::enabler> = detail::dummy> - Option* add_flag( - std::string name, - T &count, ///< A varaible holding the count - std::string description="" - ) { + template ::value && !is_bool::value, detail::enabler> = detail::dummy> + Option *add_flag(std::string name, + T &count, ///< A varaible holding the count + std::string description = "") { count = 0; - CLI::callback_t fun = [&count](CLI::results_t res){ + CLI::callback_t fun = [&count](CLI::results_t res) { count = static_cast(res.size()); return true; }; - - Option* opt = add_option(name, fun, description, false); + + Option *opt = add_option(name, fun, description, false); if(opt->get_positional()) throw IncorrectConstruction("Flags cannot be positional"); opt->set_custom_option("", 0); @@ -319,40 +293,34 @@ public: } /// Bool version only allows the flag once - template::value, detail::enabler> = detail::dummy> - Option* add_flag( - std::string name, - T &count, ///< A varaible holding true if passed - std::string description="" - ) { + template ::value, detail::enabler> = detail::dummy> + Option *add_flag(std::string name, + T &count, ///< A varaible holding true if passed + std::string description = "") { count = false; - CLI::callback_t fun = [&count](CLI::results_t res){ + CLI::callback_t fun = [&count](CLI::results_t res) { count = true; return res.size() == 1; }; - - Option* opt = add_option(name, fun, description, false); + + Option *opt = add_option(name, fun, description, false); if(opt->get_positional()) throw IncorrectConstruction("Flags cannot be positional"); opt->set_custom_option("", 0); return opt; } - /// Add set of options - template - Option* add_set( - std::string name, - T &member, ///< The selected member of the set - std::set options, ///< The set of posibilities - std::string description="", - bool defaulted=false - ) { + template + Option *add_set(std::string name, + T &member, ///< The selected member of the set + std::set options, ///< The set of posibilities + std::string description = "", + bool defaulted = false) { - CLI::callback_t fun = [&member, options](CLI::results_t res){ - if(res.size()!=1) { + CLI::callback_t fun = [&member, options](CLI::results_t res) { + if(res.size() != 1) { return false; } bool retval = detail::lexical_cast(res[0], member); @@ -361,7 +329,7 @@ public: return std::find(std::begin(options), std::end(options), member) != std::end(options); }; - Option* opt = add_option(name, fun, description, defaulted); + Option *opt = add_option(name, fun, description, defaulted); std::string typeval = detail::type_name(); typeval += " in {" + detail::join(options) + "}"; opt->set_custom_option(typeval); @@ -374,21 +342,20 @@ public: } /// Add set of options, string only, ignore case - Option* add_set_ignore_case( - std::string name, - std::string &member, ///< The selected member of the set - std::set options, ///< The set of posibilities - std::string description="", - bool defaulted=false - ) { + Option *add_set_ignore_case(std::string name, + std::string &member, ///< The selected member of the set + std::set options, ///< The set of posibilities + std::string description = "", + bool defaulted = false) { - CLI::callback_t fun = [&member, options](CLI::results_t res){ - if(res.size()!=1) { + CLI::callback_t fun = [&member, options](CLI::results_t res) { + if(res.size() != 1) { return false; } member = detail::to_lower(res[0]); - auto iter = std::find_if(std::begin(options), std::end(options), - [&member](std::string val){return detail::to_lower(val) == member;}); + auto iter = std::find_if(std::begin(options), std::end(options), [&member](std::string val) { + return detail::to_lower(val) == member; + }); if(iter == std::end(options)) return false; else { @@ -397,7 +364,7 @@ public: } }; - Option* opt = add_option(name, fun, description, defaulted); + Option *opt = add_option(name, fun, description, defaulted); std::string typeval = detail::type_name(); typeval += " in {" + detail::join(options) + "}"; opt->set_custom_option(typeval); @@ -408,94 +375,93 @@ public: } /// Add a complex number - template - Option* add_complex( - std::string name, T& variable, - std::string description="", bool defaulted=false, - std::string label="COMPLEX") { - CLI::callback_t fun = [&variable](results_t res){ - if(res.size()!=2) - return false; - double x,y; - bool worked = detail::lexical_cast(res[0], x) - && detail::lexical_cast(res[1], y); - if(worked) - variable = T(x,y); - return worked; - }; - - CLI::Option* opt = add_option(name, fun, description, defaulted); - opt->set_custom_option(label, 2); - if(defaulted) { - std::stringstream out; - out << variable; - opt->set_default_val(out.str()); - } - return opt; - } + template + Option *add_complex(std::string name, + T &variable, + std::string description = "", + bool defaulted = false, + std::string label = "COMPLEX") { + CLI::callback_t fun = [&variable](results_t res) { + if(res.size() != 2) + return false; + double x, y; + bool worked = detail::lexical_cast(res[0], x) && detail::lexical_cast(res[1], y); + if(worked) + variable = T(x, y); + return worked; + }; + CLI::Option *opt = add_option(name, fun, description, defaulted); + opt->set_custom_option(label, 2); + if(defaulted) { + std::stringstream out; + out << variable; + opt->set_default_val(out.str()); + } + return opt; + } /// Add a configuration ini file option - Option* add_config(std::string name="--config", - std::string default_filename="", - std::string help="Read an ini file", - bool required=false) { + Option *add_config(std::string name = "--config", + std::string default_filename = "", + std::string help = "Read an ini file", + bool required = false) { // Remove existing config if present if(config_ptr_ != nullptr) remove_option(config_ptr_); config_name_ = default_filename; config_required_ = required; - config_ptr_ = add_option(name, config_name_, help, default_filename!=""); + config_ptr_ = add_option(name, config_name_, help, default_filename != ""); return config_ptr_; } /// Removes an option from the App. Takes an option pointer. Returns true if found and removed. - bool remove_option(Option* opt) { - auto iterator = std::find_if(std::begin(options_), std::end(options_), - [opt](const Option_p &v){return v.get() == opt;}); - if (iterator != std::end(options_)) { + bool remove_option(Option *opt) { + auto iterator = + std::find_if(std::begin(options_), std::end(options_), [opt](const Option_p &v) { return v.get() == opt; }); + if(iterator != std::end(options_)) { options_.erase(iterator); return true; } return false; } - + ///@} /// @name Subcommmands ///@{ /// Add a subcommand. Like the constructor, you can override the help message addition by setting help=false - App* add_subcommand(std::string name, std::string description="", bool help=true) { + App *add_subcommand(std::string name, std::string description = "", bool help = true) { subcommands_.emplace_back(new App(description, help, detail::dummy)); subcommands_.back()->name_ = name; subcommands_.back()->allow_extras(); subcommands_.back()->parent_ = this; subcommands_.back()->ignore_case_ = ignore_case_; subcommands_.back()->fallthrough_ = fallthrough_; - for(const auto& subc : subcommands_) + for(const auto &subc : subcommands_) if(subc.get() != subcommands_.back().get()) - if(subc->check_name(subcommands_.back()->name_) || subcommands_.back()->check_name(subc->name_)) + if(subc->check_name(subcommands_.back()->name_) || subcommands_.back()->check_name(subc->name_)) throw OptionAlreadyAdded(subc->name_); return subcommands_.back().get(); } /// Check to see if a subcommand is part of this command (doesn't have to be in command line) - App* get_subcommand(App* subcom) const { + App *get_subcommand(App *subcom) const { for(const App_p &subcomptr : subcommands_) if(subcomptr.get() == subcom) return subcom; throw CLI::OptionNotFound(subcom->get_name()); } - + /// Check to see if a subcommand is part of this command (text version) - App* get_subcommand(std::string subcom) const { + App *get_subcommand(std::string subcom) const { for(const App_p &subcomptr : subcommands_) if(subcomptr->check_name(subcom)) return subcomptr.get(); throw CLI::OptionNotFound(subcom); } - + ///@} /// @name Extras for subclassing ///@{ @@ -514,15 +480,14 @@ public: std::vector parse(int argc, char **argv) { name_ = argv[0]; std::vector args; - for(int i=argc-1; i>0; i--) + for(int i = argc - 1; i > 0; i--) args.emplace_back(argv[i]); return parse(args); - } /// The real work is done here. Expects a reversed vector. /// Changes the vector to the remaining options. - std::vector& parse(std::vector &args) { + std::vector &parse(std::vector &args) { _validate(); _parse(args); run_callback(); @@ -530,7 +495,7 @@ public: } /// Print a nice error message and return the exit code - int exit(const Error& e) const { + int exit(const Error &e) const { if(e.exit_code != static_cast(ExitCodes::Success)) { std::cerr << "ERROR: "; std::cerr << e.what() << std::endl; @@ -572,32 +537,30 @@ public: } /// Get a subcommand pointer list to the currently selected subcommands (after parsing) - std::vector get_subcommands() const { - std::vector subcomms; + std::vector get_subcommands() const { + std::vector subcomms; for(const App_p &subcomptr : subcommands_) if(subcomptr->parsed_) subcomms.push_back(subcomptr.get()); return subcomms; } - /// Check to see if given subcommand was selected - bool got_subcommand(App* subcom) const { + bool got_subcommand(App *subcom) const { // get subcom needed to verify that this was a real subcommand return get_subcommand(subcom)->parsed_; } /// Check with name instead of pointer to see if subcommand was selected - bool got_subcommand(std::string name) const { - return get_subcommand(name)->parsed_; - } - + bool got_subcommand(std::string name) const { return get_subcommand(name)->parsed_; } + ///@} /// @name Help ///@{ - /// Produce a string that could be read in as a config of the current values of the App. Set default_also to include default arguments. Prefix will add a string to the beginning of each option. - std::string config_to_str(bool default_also=false, std::string prefix="") const { + /// Produce a string that could be read in as a config of the current values of the App. Set default_also to include + /// default arguments. Prefix will add a string to the beginning of each option. + std::string config_to_str(bool default_also = false, std::string prefix = "") const { std::stringstream out; for(const Option_p &opt : options_) { @@ -615,19 +578,18 @@ public: // If the option has a default and is requested by optional argument else if(default_also && opt->defaultval_ != "") out << name << "=" << opt->defaultval_ << std::endl; - // Flag, one passed + // Flag, one passed } else if(opt->count() == 1) { out << name << "=true" << std::endl; - // Flag, multiple passed + // Flag, multiple passed } else if(opt->count() > 1) { out << name << "=" << opt->count() << std::endl; - // Flag, not present + // Flag, not present } else if(opt->count() == 0 && default_also && opt.get() != get_help_ptr()) { out << name << "=false" << std::endl; } - } } for(const App_p &subcom : subcommands_) @@ -636,7 +598,7 @@ public: } /// Makes a help message, with a column wid for column 1 - std::string help(size_t wid=30, std::string prev="") const { + std::string help(size_t wid = 30, std::string prev = "") const { // Delegate to subcommand if needed if(prev == "") prev = name_; @@ -650,7 +612,7 @@ public: std::stringstream out; out << description_ << std::endl; out << "Usage: " << prev; - + // Check for options_ bool npos = false; std::set groups; @@ -665,15 +627,15 @@ public: out << " [OPTIONS]"; // Positionals - bool pos=false; + bool pos = false; for(const Option_p &opt : options_) if(opt->get_positional()) { // A hidden positional should still show up in the usage statement - //if(detail::to_lower(opt->get_group()) == "hidden") + // if(detail::to_lower(opt->get_group()) == "hidden") // continue; out << " " << opt->help_positional(); if(opt->_has_help_positional()) - pos=true; + pos = true; } if(!subcommands_.empty()) { @@ -695,20 +657,17 @@ public: detail::format_help(out, opt->help_pname(), opt->get_description(), wid); } out << std::endl; - } - // Options if(npos) { - for (const std::string& group : groups) { + for(const std::string &group : groups) { if(detail::to_lower(group) == "hidden") continue; out << group << ":" << std::endl; for(const Option_p &opt : options_) { if(opt->nonpositional() && opt->get_group() == group) detail::format_help(out, opt->help_name(), opt->get_description(), wid); - } out << std::endl; } @@ -728,28 +687,18 @@ public: ///@{ /// Get a pointer to the help flag. - Option* get_help_ptr() { - return help_ptr_; - } + Option *get_help_ptr() { return help_ptr_; } /// Get a pointer to the help flag. (const) - const Option* get_help_ptr() const { - return help_ptr_; - } + const Option *get_help_ptr() const { return help_ptr_; } /// Get a pointer to the config option. - Option* get_config_ptr() { - return config_ptr_; - } + Option *get_config_ptr() { return config_ptr_; } /// Get a pointer to the config option. (const) - const Option* get_config_ptr() const { - return config_ptr_; - } + const Option *get_config_ptr() const { return config_ptr_; } /// Get the name of the current app - std::string get_name() const { - return name_; - } + std::string get_name() const { return name_; } /// Check the name, case insensitive if set bool check_name(std::string name_to_check) const { @@ -764,24 +713,22 @@ public: ///@} - -protected: - + protected: /// Check the options to make sure there are no conficts. /// /// Currenly checks to see if mutiple positionals exist with -1 args void _validate() const { - auto count = std::count_if(std::begin(options_), std::end(options_), - [](const Option_p& opt){return opt->get_expected() == -1 && opt->get_positional();}); + auto count = std::count_if(std::begin(options_), std::end(options_), [](const Option_p &opt) { + return opt->get_expected() == -1 && opt->get_positional(); + }); if(count > 1) throw InvalidError(name_ + ": Too many positional arguments with unlimited expected args"); - for(const App_p& app : subcommands_) + for(const App_p &app : subcommands_) app->_validate(); } - /// Return missing from the master - missing_t* missing() { + missing_t *missing() { if(parent_ != nullptr) return parent_->missing(); return &missing_; @@ -792,7 +739,7 @@ protected: pre_callback(); if(callback_) callback_(); - for(App* subc : get_subcommands()) { + for(App *subc : get_subcommands()) { subc->run_callback(); } } @@ -807,7 +754,6 @@ protected: return false; } - /// Selects a Classifer enum based on the type of the current argument detail::Classifer _recognize(const std::string ¤t) const { std::string dummy1, dummy2; @@ -823,23 +769,21 @@ protected: return detail::Classifer::NONE; } - /// Internal parse function void _parse(std::vector &args) { parsed_ = true; bool positional_only = false; - + while(!args.empty()) { _parse_single(args, positional_only); } - if (help_ptr_ != nullptr && help_ptr_->count() > 0) { + if(help_ptr_ != nullptr && help_ptr_->count() > 0) { throw CallForHelp(); } - // Process an INI file - if (config_ptr_ != nullptr && config_name_ != "") { + if(config_ptr_ != nullptr && config_name_ != "") { try { std::vector values = detail::parse_ini(config_name_); while(!values.empty()) { @@ -847,16 +791,15 @@ protected: throw ExtrasINIError(values.back().fullname); } } - } catch (const FileError &) { + } catch(const FileError &) { if(config_required_) throw; } } - // Get envname options if not yet passed - for(const Option_p& opt : options_) { - if (opt->count() == 0 && opt->envname_ != "") { + for(const Option_p &opt : options_) { + if(opt->count() == 0 && opt->envname_ != "") { char *ename = std::getenv(opt->envname_.c_str()); if(ename != nullptr) { opt->add_result(std::string(ename)); @@ -865,45 +808,48 @@ protected: } // Process callbacks - for(const Option_p& opt : options_) { - if (opt->count() > 0 && !opt->get_callback_run()) { + for(const Option_p &opt : options_) { + if(opt->count() > 0 && !opt->get_callback_run()) { opt->run_callback(); } } - // Verify required options - for(const Option_p& opt : options_) { + // Verify required options + for(const Option_p &opt : options_) { // Required - if (opt->get_required() - && (static_cast( opt->count()) < opt->get_expected() || opt->count() == 0)) + if(opt->get_required() && (static_cast(opt->count()) < opt->get_expected() || opt->count() == 0)) throw RequiredError(opt->get_name()); // Requires - for (const Option* opt_req : opt->requires_) - if (opt->count() > 0 && opt_req->count() == 0) + for(const Option *opt_req : opt->requires_) + if(opt->count() > 0 && opt_req->count() == 0) throw RequiresError(opt->get_name(), opt_req->get_name()); // Excludes - for (const Option* opt_ex : opt->excludes_) - if (opt->count() > 0 && opt_ex->count() != 0) + for(const Option *opt_ex : opt->excludes_) + if(opt->count() > 0 && opt_ex->count() != 0) throw ExcludesError(opt->get_name(), opt_ex->get_name()); } - auto selected_subcommands =get_subcommands(); + auto selected_subcommands = get_subcommands(); if(require_subcommand_ < 0 && selected_subcommands.empty()) throw RequiredError("Subcommand required"); - else if(require_subcommand_ > 0 && static_cast( selected_subcommands.size()) != require_subcommand_) + else if(require_subcommand_ > 0 && static_cast(selected_subcommands.size()) != require_subcommand_) throw RequiredError(std::to_string(require_subcommand_) + " subcommand(s) required"); // Convert missing (pairs) to extras (string only) if(parent_ == nullptr) { args.resize(missing()->size()); - std::transform(std::begin(*missing()), std::end(*missing()), std::begin(args), - [](const std::pair& val){return val.second;}); + std::transform(std::begin(*missing()), + std::end(*missing()), + std::begin(args), + [](const std::pair &val) { return val.second; }); std::reverse(std::begin(args), std::end(args)); - size_t num_left_over = std::count_if(std::begin(*missing()), std::end(*missing()), - [](std::pair& val){return val.first != detail::Classifer::POSITIONAL_MARK;}); + size_t num_left_over = std::count_if( + std::begin(*missing()), std::end(*missing()), [](std::pair &val) { + return val.first != detail::Classifer::POSITIONAL_MARK; + }); - if(num_left_over>0 && !allow_extras_) + if(num_left_over > 0 && !allow_extras_) throw ExtrasError("[" + detail::rjoin(args, " ") + "]"); } } @@ -913,29 +859,28 @@ protected: /// If this has more than one dot.separated.name, go into the subcommand matching it /// Returns true if it managed to find the option, if false you'll need to remove the arg manully. bool _parse_ini(std::vector &args) { - detail::ini_ret_t& current = args.back(); + detail::ini_ret_t ¤t = args.back(); std::string parent = current.parent(); // respects curent.level std::string name = current.name(); // If a parent is listed, go to a subcommand if(parent != "") { current.level++; - for(const App_p &com : subcommands_) + for(const App_p &com : subcommands_) if(com->check_name(parent)) return com->_parse_ini(args); return false; } - auto op_ptr = std::find_if(std::begin(options_), std::end(options_), - [name](const Option_p &v){return v->check_lname(name);}); - - if(op_ptr == std::end(options_)) - return false; - - // Let's not go crazy with pointer syntax - Option_p& op = *op_ptr; + auto op_ptr = std::find_if( + std::begin(options_), std::end(options_), [name](const Option_p &v) { return v->check_lname(name); }); + + if(op_ptr == std::end(options_)) + return false; + + // Let's not go crazy with pointer syntax + Option_p &op = *op_ptr; - if(op->results_.empty()) { // Flag parsing if(op->get_expected() == 0) { @@ -949,9 +894,9 @@ protected: else try { size_t ui = std::stoul(val); - for (size_t i=0; iresults_.emplace_back(""); - } catch (const std::invalid_argument &) { + } catch(const std::invalid_argument &) { throw ConversionError(current.fullname + ": Should be true/false or a number"); } } else @@ -966,7 +911,8 @@ protected: return true; } - /// Parse "one" argument (some may eat more than one), delegate to parent if fails, add to missing if missing from master + /// Parse "one" argument (some may eat more than one), delegate to parent if fails, add to missing if missing from + /// master void _parse_single(std::vector &args, bool &positional_only) { detail::Classifer classifer = positional_only ? detail::Classifer::NONE : _recognize(args.back()); @@ -991,20 +937,16 @@ protected: // Probably a positional or something for a parent (sub)command _parse_positional(args); } - - } /// Parse a positional, go up the tree to check void _parse_positional(std::vector &args) { std::string positional = args.back(); - for(const Option_p& opt : options_) { + for(const Option_p &opt : options_) { // Eat options, one by one, until done - if ( opt->get_positional() - && (static_cast( opt->count()) < opt->get_expected() - || opt->get_expected() < 0) - ) { + if(opt->get_positional() && + (static_cast(opt->count()) < opt->get_expected() || opt->get_expected() < 0)) { opt->add_result(positional); args.pop_back(); @@ -1025,7 +967,7 @@ protected: /// Unlike the others, this one will always allow fallthrough void _parse_subcommand(std::vector &args) { for(const App_p &com : subcommands_) { - if(com->check_name(args.back())){ + if(com->check_name(args.back())) { args.pop_back(); com->_parse(args); return; @@ -1036,7 +978,7 @@ protected: else throw HorribleError("Subcommand " + args.back() + " missing"); } - + /// Parse a short argument, must be at the top of the list void _parse_short(std::vector &args) { std::string current = args.back(); @@ -1046,8 +988,8 @@ protected: if(!detail::split_short(current, name, rest)) throw HorribleError("Short"); - auto op_ptr = std::find_if(std::begin(options_), std::end(options_), [name](const Option_p &opt){return opt->check_sname(name);}); - + auto op_ptr = std::find_if( + std::begin(options_), std::end(options_), [name](const Option_p &opt) { return opt->check_sname(name); }); // Option not found if(op_ptr == std::end(options_)) { @@ -1064,33 +1006,32 @@ protected: args.pop_back(); - // Get a reference to the pointer to make syntax bearable - Option_p& op = *op_ptr; + Option_p &op = *op_ptr; int num = op->get_expected(); - + if(num == 0) op->add_result(""); - else if(rest!="") { - if (num > 0) + else if(rest != "") { + if(num > 0) num--; op->add_result(rest); rest = ""; } - if(num == -1) { while(!args.empty() && _recognize(args.back()) == detail::Classifer::NONE) { op->add_result(args.back()); args.pop_back(); } - } else while(num>0 && !args.empty()) { - num--; - std::string current_ = args.back(); - args.pop_back(); - op->add_result(current_); - } + } else + while(num > 0 && !args.empty()) { + num--; + std::string current_ = args.back(); + args.pop_back(); + op->add_result(current_); + } if(rest != "") { rest = "-" + rest; @@ -1107,7 +1048,8 @@ protected: if(!detail::split_long(current, name, value)) throw HorribleError("Long:" + args.back()); - auto op_ptr = std::find_if(std::begin(options_), std::end(options_), [name](const Option_p &v){return v->check_lname(name);}); + auto op_ptr = std::find_if( + std::begin(options_), std::end(options_), [name](const Option_p &v) { return v->check_lname(name); }); // Option not found if(op_ptr == std::end(options_)) { @@ -1125,14 +1067,15 @@ protected: args.pop_back(); // Get a reference to the pointer to make syntax bearable - Option_p& op = *op_ptr; + Option_p &op = *op_ptr; int num = op->get_expected(); - + if(value != "") { - if(num!=-1) num--; + if(num != -1) + num--; op->add_result(value); - } else if (num == 0) { + } else if(num == 0) { op->add_result(""); } @@ -1141,14 +1084,14 @@ protected: op->add_result(args.back()); args.pop_back(); } - } else while(num>0 && !args.empty()) { - num--; - op->add_result(args.back()); - args.pop_back(); - } + } else + while(num > 0 && !args.empty()) { + num--; + op->add_result(args.back()); + args.pop_back(); + } return; } - }; namespace detail { @@ -1156,29 +1099,26 @@ namespace detail { struct AppFriend { /// Wrap _parse_short, perfectly forward arguments and return - template - static auto parse_short(App* app, Args && ...args) - -> typename std::result_of::type { + template + static auto parse_short(App *app, Args &&... args) -> + typename std::result_of::type { return app->_parse_short(std::forward(args)...); } - /// Wrap _parse_long, perfectly forward arguments and return - template - static auto parse_long(App* app, Args && ...args) - -> typename std::result_of::type { + template + static auto parse_long(App *app, Args &&... args) -> + typename std::result_of::type { return app->_parse_long(std::forward(args)...); } /// Wrap _parse_subcommand, perfectly forward arguments and return - template - static auto parse_subcommand(App* app, Args && ...args) - -> typename std::result_of::type { + template + static auto parse_subcommand(App *app, Args &&... args) -> + typename std::result_of::type { return app->_parse_subcommand(std::forward(args)...); } - }; } // namespace detail } // namespace CLI - diff --git a/include/CLI/CLI.hpp b/include/CLI/CLI.hpp index 2beaa5f0..e18bcd4f 100644 --- a/include/CLI/CLI.hpp +++ b/include/CLI/CLI.hpp @@ -12,4 +12,3 @@ #include "CLI/StringTools.hpp" #include "CLI/TypeTools.hpp" #include "CLI/Validators.hpp" - diff --git a/include/CLI/Error.hpp b/include/CLI/Error.hpp index 27fdbd5b..5da610a1 100644 --- a/include/CLI/Error.hpp +++ b/include/CLI/Error.hpp @@ -9,10 +9,11 @@ namespace CLI { -/// These codes are part of every error in CLI. They can be obtained from e using e.exit_code or as a quick shortcut, int values from e.get_error_code(). +/// These codes are part of every error in CLI. They can be obtained from e using e.exit_code or as a quick shortcut, +/// int values from e.get_error_code(). enum class ExitCodes { Success = 0, - IncorrectConstruction=100, + IncorrectConstruction = 100, BadNameString, OptionAlreadyAdded, File, @@ -41,17 +42,23 @@ enum class ExitCodes { struct Error : public std::runtime_error { int exit_code; bool print_help; - int get_exit_code() const {return exit_code;} - Error(std::string parent, std::string name, ExitCodes exit_code=ExitCodes::BaseClass, bool print_help=true) + int get_exit_code() const { return exit_code; } + Error(std::string parent, std::string name, ExitCodes exit_code = ExitCodes::BaseClass, bool print_help = true) : runtime_error(parent + ": " + name), exit_code(static_cast(exit_code)), print_help(print_help) {} - Error(std::string parent, std::string name, int exit_code=static_cast(ExitCodes::BaseClass), bool print_help=true) + Error(std::string parent, + std::string name, + int exit_code = static_cast(ExitCodes::BaseClass), + bool print_help = true) : runtime_error(parent + ": " + name), exit_code(exit_code), print_help(print_help) {} }; /// Construction errors (not in parsing) struct ConstructionError : public Error { // Using Error::Error constructors seem to not work on GCC 4.7 - ConstructionError(std::string parent, std::string name, ExitCodes exit_code=ExitCodes::BaseClass, bool print_help=true) + ConstructionError(std::string parent, + std::string name, + ExitCodes exit_code = ExitCodes::BaseClass, + bool print_help = true) : Error(parent, name, exit_code, print_help) {} }; @@ -63,8 +70,7 @@ struct IncorrectConstruction : public ConstructionError { /// Thrown on construction of a bad name struct BadNameString : public ConstructionError { - BadNameString(std::string name) - : ConstructionError("BadNameString", name, ExitCodes::BadNameString) {} + BadNameString(std::string name) : ConstructionError("BadNameString", name, ExitCodes::BadNameString) {} }; /// Thrown when an option already exists @@ -77,7 +83,7 @@ struct OptionAlreadyAdded : public ConstructionError { /// Anything that can error in Parse struct ParseError : public Error { - ParseError(std::string parent, std::string name, ExitCodes exit_code=ExitCodes::BaseClass, bool print_help=true) + ParseError(std::string parent, std::string name, ExitCodes exit_code = ExitCodes::BaseClass, bool print_help = true) : Error(parent, name, exit_code, print_help) {} }; @@ -85,8 +91,7 @@ struct ParseError : public Error { /// This is a successful completion on parsing, supposed to exit struct Success : public ParseError { - Success() - : ParseError("Success", "Successfully completed, should be caught and quit", ExitCodes::Success, false) {} + Success() : ParseError("Success", "Successfully completed, should be caught and quit", ExitCodes::Success, false) {} }; /// -h or --help on command line @@ -95,29 +100,24 @@ struct CallForHelp : public ParseError { : ParseError("CallForHelp", "This should be caught in your main function, see examples", ExitCodes::Success) {} }; - /// Thrown when parsing an INI file and it is missing struct FileError : public ParseError { - FileError (std::string name) - : ParseError("FileError", name, ExitCodes::File) {} + FileError(std::string name) : ParseError("FileError", name, ExitCodes::File) {} }; /// Thrown when conversion call back fails, such as when an int fails to coerse to a string struct ConversionError : public ParseError { - ConversionError(std::string name) - : ParseError("ConversionError", name, ExitCodes::Conversion) {} + ConversionError(std::string name) : ParseError("ConversionError", name, ExitCodes::Conversion) {} }; /// Thrown when validation of results fails struct ValidationError : public ParseError { - ValidationError(std::string name) - : ParseError("ValidationError", name, ExitCodes::Validation) {} + ValidationError(std::string name) : ParseError("ValidationError", name, ExitCodes::Validation) {} }; /// Thrown when a required option is missing struct RequiredError : public ParseError { - RequiredError(std::string name) - : ParseError("RequiredError", name, ExitCodes::Required) {} + RequiredError(std::string name) : ParseError("RequiredError", name, ExitCodes::Required) {} }; /// Thrown when a requires option is missing @@ -134,20 +134,17 @@ struct ExcludesError : public ParseError { /// Thrown when too many positionals or options are found struct ExtrasError : public ParseError { - ExtrasError(std::string name) - : ParseError("ExtrasError", name, ExitCodes::Extras) {} + ExtrasError(std::string name) : ParseError("ExtrasError", name, ExitCodes::Extras) {} }; /// Thrown when extra values are found in an INI file struct ExtrasINIError : public ParseError { - ExtrasINIError(std::string name) - : ParseError("ExtrasINIError", name, ExitCodes::ExtrasINI) {} + ExtrasINIError(std::string name) : ParseError("ExtrasINIError", name, ExitCodes::ExtrasINI) {} }; /// Thrown when validation fails before parsing struct InvalidError : public ParseError { - InvalidError(std::string name) - : ParseError("InvalidError", name, ExitCodes::Invalid) {} + InvalidError(std::string name) : ParseError("InvalidError", name, ExitCodes::Invalid) {} }; /// This is just a safety check to verify selection and parsing match @@ -160,8 +157,7 @@ struct HorribleError : public ParseError { /// Thrown when counting a non-existent option struct OptionNotFound : public Error { - OptionNotFound(std::string name) - : Error("OptionNotFound", name, ExitCodes::OptionNotFound) {} + OptionNotFound(std::string name) : Error("OptionNotFound", name, ExitCodes::OptionNotFound) {} }; /// @} diff --git a/include/CLI/Ini.hpp b/include/CLI/Ini.hpp index f84f2a6b..082e4ceb 100644 --- a/include/CLI/Ini.hpp +++ b/include/CLI/Ini.hpp @@ -10,18 +10,17 @@ #include "CLI/StringTools.hpp" - namespace CLI { namespace detail { inline std::string inijoin(std::vector args) { std::ostringstream s; size_t start = 0; - for (const auto& arg : args) { + for(const auto &arg : args) { if(start++ > 0) s << " "; - auto it = std::find_if(arg.begin(), arg.end(), [](char ch){ return std::isspace(ch , std::locale());}); + auto it = std::find_if(arg.begin(), arg.end(), [](char ch) { return std::isspace(ch, std::locale()); }); if(it == arg.end()) s << arg; else if(arg.find(R"(")") == std::string::npos) @@ -47,18 +46,18 @@ struct ini_ret_t { /// /// Level 0, a.b.c would return a /// Level 1, a.b.c could return b - std::string parent () const { + std::string parent() const { std::vector plist = detail::split(fullname, '.'); - if(plist.size() > (level+1)) + if(plist.size() > (level + 1)) return plist[level]; else return ""; } /// Return name - std::string name () const { + std::string name() const { std::vector plist = detail::split(fullname, '.'); - return plist.at(plist.size()-1); + return plist.at(plist.size() - 1); } }; @@ -74,17 +73,17 @@ inline std::vector parse_ini(std::istream &input) { detail::trim(line); size_t len = line.length(); - if(len > 1 && line[0] == '[' && line[len-1] == ']') { - section = line.substr(1,len-2); - } else if (len > 0 && line[0] != ';') { + if(len > 1 && line[0] == '[' && line[len - 1] == ']') { + section = line.substr(1, len - 2); + } else if(len > 0 && line[0] != ';') { output.emplace_back(); - ini_ret_t& out = output.back(); + ini_ret_t &out = output.back(); // Find = in string, split and recombine auto pos = line.find("="); if(pos != std::string::npos) { - name = detail::trim_copy(line.substr(0,pos)); - std::string item = detail::trim_copy(line.substr(pos+1)); + name = detail::trim_copy(line.substr(0, pos)); + std::string item = detail::trim_copy(line.substr(pos + 1)); items = detail::split_up(item); } else { name = detail::trim_copy(line); @@ -112,6 +111,5 @@ inline std::vector parse_ini(const std::string &name) { return parse_ini(input); } - } // namespace detail } // namespace CLI diff --git a/include/CLI/Option.hpp b/include/CLI/Option.hpp index 2f42e197..b1e4a30f 100644 --- a/include/CLI/Option.hpp +++ b/include/CLI/Option.hpp @@ -19,17 +19,17 @@ namespace CLI { using results_t = std::vector; -using callback_t = std::function; +using callback_t = std::function; class Option; class App; using Option_p = std::unique_ptr