Commit 27f718125d0e0e2b6c49d5092f82bafcbb8a3ee3
1 parent
f24561f4
Reformat with clang-format
Showing
27 changed files
with
908 additions
and
1103 deletions
.clang-format
0 → 100644
| 1 | +Language: Cpp | ||
| 2 | +BasedOnStyle: LLVM | ||
| 3 | +# AccessModifierOffset: -2 | ||
| 4 | +# AlignAfterOpenBracket: Align | ||
| 5 | +# AlignConsecutiveAssignments: false | ||
| 6 | +# AlignConsecutiveDeclarations: false | ||
| 7 | +# AlignEscapedNewlinesLeft: false | ||
| 8 | +# AlignOperands: true | ||
| 9 | +# AlignTrailingComments: true | ||
| 10 | +# AllowAllParametersOfDeclarationOnNextLine: true | ||
| 11 | +# AllowShortBlocksOnASingleLine: false | ||
| 12 | +# AllowShortCaseLabelsOnASingleLine: false | ||
| 13 | +# AllowShortFunctionsOnASingleLine: All | ||
| 14 | +# AllowShortIfStatementsOnASingleLine: false | ||
| 15 | +# AllowShortLoopsOnASingleLine: false | ||
| 16 | +# AlwaysBreakAfterDefinitionReturnType: None | ||
| 17 | +# AlwaysBreakAfterReturnType: None | ||
| 18 | +# AlwaysBreakBeforeMultilineStrings: false | ||
| 19 | +# AlwaysBreakTemplateDeclarations: false | ||
| 20 | +BinPackArguments: false | ||
| 21 | +BinPackParameters: false | ||
| 22 | +# BraceWrapping: | ||
| 23 | +# AfterClass: false | ||
| 24 | +# AfterControlStatement: false | ||
| 25 | +# AfterEnum: false | ||
| 26 | +# AfterFunction: false | ||
| 27 | +# AfterNamespace: false | ||
| 28 | +# AfterObjCDeclaration: false | ||
| 29 | +# AfterStruct: false | ||
| 30 | +# AfterUnion: false | ||
| 31 | +# BeforeCatch: false | ||
| 32 | +# BeforeElse: false | ||
| 33 | +# IndentBraces: false | ||
| 34 | +# BreakBeforeBinaryOperators: None | ||
| 35 | +# BreakBeforeBraces: Attach | ||
| 36 | +# BreakBeforeTernaryOperators: true | ||
| 37 | +# BreakConstructorInitializersBeforeComma: false | ||
| 38 | +# BreakAfterJavaFieldAnnotations: false | ||
| 39 | +# BreakStringLiterals: true | ||
| 40 | +ColumnLimit: 120 | ||
| 41 | +# CommentPragmas: '^ IWYU pragma:' | ||
| 42 | +# ConstructorInitializerAllOnOneLineOrOnePerLine: false | ||
| 43 | +# ConstructorInitializerIndentWidth: 4 | ||
| 44 | +# ContinuationIndentWidth: 4 | ||
| 45 | +# Cpp11BracedListStyle: true | ||
| 46 | +# DerivePointerAlignment: false | ||
| 47 | +# DisableFormat: false | ||
| 48 | +# ExperimentalAutoDetectBinPacking: false | ||
| 49 | +# ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ] | ||
| 50 | +# IncludeIsMainRegex: '$' | ||
| 51 | +# IndentCaseLabels: false | ||
| 52 | +IndentWidth: 4 | ||
| 53 | +# IndentWrappedFunctionNames: false | ||
| 54 | +# JavaScriptQuotes: Leave | ||
| 55 | +# JavaScriptWrapImports: true | ||
| 56 | +# KeepEmptyLinesAtTheStartOfBlocks: true | ||
| 57 | +# MacroBlockBegin: '' | ||
| 58 | +# MacroBlockEnd: '' | ||
| 59 | +# MaxEmptyLinesToKeep: 1 | ||
| 60 | +# NamespaceIndentation: None | ||
| 61 | +# ObjCBlockIndentWidth: 2 | ||
| 62 | +# ObjCSpaceAfterProperty: false | ||
| 63 | +# ObjCSpaceBeforeProtocolList: true | ||
| 64 | +# PenaltyBreakBeforeFirstCallParameter: 19 | ||
| 65 | +# PenaltyBreakComment: 300 | ||
| 66 | +# PenaltyBreakFirstLessLess: 120 | ||
| 67 | +# PenaltyBreakString: 1000 | ||
| 68 | +# PenaltyExcessCharacter: 1000000 | ||
| 69 | +# PenaltyReturnTypeOnItsOwnLine: 60 | ||
| 70 | +# PointerAlignment: Right | ||
| 71 | +# ReflowComments: true | ||
| 72 | +SortIncludes: false | ||
| 73 | +# SpaceAfterCStyleCast: false | ||
| 74 | +# SpaceAfterTemplateKeyword: true | ||
| 75 | +# SpaceBeforeAssignmentOperators: true | ||
| 76 | +SpaceBeforeParens: Never | ||
| 77 | +# SpaceInEmptyParentheses: false | ||
| 78 | +# SpacesBeforeTrailingComments: 1 | ||
| 79 | +# SpacesInAngles: false | ||
| 80 | +# SpacesInContainerLiterals: true | ||
| 81 | +# SpacesInCStyleCastParentheses: false | ||
| 82 | +# SpacesInParentheses: false | ||
| 83 | +# SpacesInSquareBrackets: false | ||
| 84 | +Standard: Cpp11 | ||
| 85 | +TabWidth: 4 | ||
| 86 | +UseTab: Never |
examples/try.cpp
| 1 | #include "CLI/CLI.hpp" | 1 | #include "CLI/CLI.hpp" |
| 2 | 2 | ||
| 3 | - | ||
| 4 | -int main (int argc, char** argv) { | 3 | +int main(int argc, char **argv) { |
| 5 | 4 | ||
| 6 | CLI::App app("K3Pi goofit fitter"); | 5 | CLI::App app("K3Pi goofit fitter"); |
| 7 | 6 | ||
| 8 | std::string file; | 7 | std::string file; |
| 9 | - CLI::Option* opt = app.add_option("-f,--file,file", file, "File name"); | ||
| 10 | - | 8 | + CLI::Option *opt = app.add_option("-f,--file,file", file, "File name"); |
| 9 | + | ||
| 11 | int count; | 10 | int count; |
| 12 | - CLI::Option* copt = app.add_flag("-c,--count", count, "Counter"); | 11 | + CLI::Option *copt = app.add_flag("-c,--count", count, "Counter"); |
| 13 | 12 | ||
| 14 | - double value;// = 3.14; | 13 | + double value; // = 3.14; |
| 15 | app.add_option("-d,--double", value, "Some Value"); | 14 | app.add_option("-d,--double", value, "Some Value"); |
| 16 | 15 | ||
| 17 | try { | 16 | try { |
| 18 | app.parse(argc, argv); | 17 | app.parse(argc, argv); |
| 19 | - } catch (const CLI::Error &e) { | 18 | + } catch(const CLI::Error &e) { |
| 20 | return app.exit(e); | 19 | return app.exit(e); |
| 21 | } | 20 | } |
| 22 | 21 | ||
| 23 | - std::cout << "Working on file: " << file | ||
| 24 | - << ", direct count: " << app.count("--file") | ||
| 25 | - << ", opt count: " << opt->count() | ||
| 26 | - << std::endl; | ||
| 27 | - std::cout << "Working on count: " << count | ||
| 28 | - << ", direct count: " << app.count("--count") | ||
| 29 | - << ", opt count: " << copt->count() | ||
| 30 | - << std::endl; | 22 | + std::cout << "Working on file: " << file << ", direct count: " << app.count("--file") |
| 23 | + << ", opt count: " << opt->count() << std::endl; | ||
| 24 | + std::cout << "Working on count: " << count << ", direct count: " << app.count("--count") | ||
| 25 | + << ", opt count: " << copt->count() << std::endl; | ||
| 31 | std::cout << "Some value: " << value << std::endl; | 26 | std::cout << "Some value: " << value << std::endl; |
| 32 | 27 | ||
| 33 | return 0; | 28 | return 0; |
examples/try1.cpp
| 1 | #include "CLI/CLI.hpp" | 1 | #include "CLI/CLI.hpp" |
| 2 | 2 | ||
| 3 | - | ||
| 4 | -int main (int argc, char** argv) { | 3 | +int main(int argc, char **argv) { |
| 5 | 4 | ||
| 6 | CLI::App app("K3Pi goofit fitter"); | 5 | CLI::App app("K3Pi goofit fitter"); |
| 7 | app.add_flag("--random", "Some random flag"); | 6 | app.add_flag("--random", "Some random flag"); |
| 8 | - CLI::App* start = app.add_subcommand("start", "A great subcommand"); | ||
| 9 | - CLI::App* stop = app.add_subcommand("stop", "Do you really want to stop?"); | 7 | + CLI::App *start = app.add_subcommand("start", "A great subcommand"); |
| 8 | + CLI::App *stop = app.add_subcommand("stop", "Do you really want to stop?"); | ||
| 10 | 9 | ||
| 11 | std::string file; | 10 | std::string file; |
| 12 | start->add_option("-f,--file", file, "File name"); | 11 | start->add_option("-f,--file", file, "File name"); |
| 13 | - | ||
| 14 | - CLI::Option* s = stop->add_flag("-c,--count", "Counter"); | 12 | + |
| 13 | + CLI::Option *s = stop->add_flag("-c,--count", "Counter"); | ||
| 15 | 14 | ||
| 16 | try { | 15 | try { |
| 17 | app.parse(argc, argv); | 16 | app.parse(argc, argv); |
| 18 | - } catch (const CLI::Error &e) { | 17 | + } catch(const CLI::Error &e) { |
| 19 | return app.exit(e); | 18 | return app.exit(e); |
| 20 | } | 19 | } |
| 21 | 20 |
examples/try2.cpp
| 1 | #include "CLI/CLI.hpp" | 1 | #include "CLI/CLI.hpp" |
| 2 | #include "CLI/Timer.hpp" | 2 | #include "CLI/Timer.hpp" |
| 3 | 3 | ||
| 4 | - | ||
| 5 | -int main (int argc, char** argv) { | 4 | +int main(int argc, char **argv) { |
| 6 | CLI::AutoTimer("This is a timer"); | 5 | CLI::AutoTimer("This is a timer"); |
| 7 | 6 | ||
| 8 | CLI::App app("K3Pi goofit fitter"); | 7 | CLI::App app("K3Pi goofit fitter"); |
| 9 | 8 | ||
| 10 | std::string file; | 9 | std::string file; |
| 11 | - CLI::Option* opt = app.add_option("-f,--file,file", file, "File name") | ||
| 12 | - ->required()->group("Important"); | ||
| 13 | - | 10 | + CLI::Option *opt = app.add_option("-f,--file,file", file, "File name")->required()->group("Important"); |
| 11 | + | ||
| 14 | int count; | 12 | int count; |
| 15 | - CLI::Option* copt = app.add_flag("-c,--count", count, "Counter") | ||
| 16 | - ->required()->group("Important"); | 13 | + CLI::Option *copt = app.add_flag("-c,--count", count, "Counter")->required()->group("Important"); |
| 17 | 14 | ||
| 18 | - double value;// = 3.14; | ||
| 19 | - app.add_option("-d,--double", value, "Some Value") | ||
| 20 | - ->group("Other"); | 15 | + double value; // = 3.14; |
| 16 | + app.add_option("-d,--double", value, "Some Value")->group("Other"); | ||
| 21 | 17 | ||
| 22 | try { | 18 | try { |
| 23 | app.parse(argc, argv); | 19 | app.parse(argc, argv); |
| 24 | - } catch (const CLI::Error &e) { | 20 | + } catch(const CLI::Error &e) { |
| 25 | return app.exit(e); | 21 | return app.exit(e); |
| 26 | } | 22 | } |
| 27 | 23 | ||
| 28 | - std::cout << "Working on file: " << file | ||
| 29 | - << ", direct count: " << app.count("--file") | ||
| 30 | - << ", opt count: " << opt->count() | ||
| 31 | - << std::endl; | ||
| 32 | - std::cout << "Working on count: " << count | ||
| 33 | - << ", direct count: " << app.count("--count") | ||
| 34 | - << ", opt count: " << copt->count() | ||
| 35 | - << std::endl; | 24 | + std::cout << "Working on file: " << file << ", direct count: " << app.count("--file") |
| 25 | + << ", opt count: " << opt->count() << std::endl; | ||
| 26 | + std::cout << "Working on count: " << count << ", direct count: " << app.count("--count") | ||
| 27 | + << ", opt count: " << copt->count() << std::endl; | ||
| 36 | std::cout << "Some value: " << value << std::endl; | 28 | std::cout << "Some value: " << value << std::endl; |
| 37 | 29 | ||
| 38 | return 0; | 30 | return 0; |
include/CLI/App.hpp
| @@ -15,7 +15,6 @@ | @@ -15,7 +15,6 @@ | ||
| 15 | #include <utility> | 15 | #include <utility> |
| 16 | #include <vector> | 16 | #include <vector> |
| 17 | 17 | ||
| 18 | - | ||
| 19 | // CLI Library includes | 18 | // CLI Library includes |
| 20 | #include "CLI/Error.hpp" | 19 | #include "CLI/Error.hpp" |
| 21 | #include "CLI/Ini.hpp" | 20 | #include "CLI/Ini.hpp" |
| @@ -27,13 +26,12 @@ | @@ -27,13 +26,12 @@ | ||
| 27 | namespace CLI { | 26 | namespace CLI { |
| 28 | 27 | ||
| 29 | namespace detail { | 28 | namespace detail { |
| 30 | -enum class Classifer {NONE, POSITIONAL_MARK, SHORT, LONG, SUBCOMMAND}; | 29 | +enum class Classifer { NONE, POSITIONAL_MARK, SHORT, LONG, SUBCOMMAND }; |
| 31 | struct AppFriend; | 30 | struct AppFriend; |
| 32 | } // namespace detail | 31 | } // namespace detail |
| 33 | 32 | ||
| 34 | class App; | 33 | class App; |
| 35 | 34 | ||
| 36 | - | ||
| 37 | using App_p = std::unique_ptr<App>; | 35 | using App_p = std::unique_ptr<App>; |
| 38 | 36 | ||
| 39 | /// Creates a command line program, with very few defaults. | 37 | /// Creates a command line program, with very few defaults. |
| @@ -43,21 +41,21 @@ using App_p = std::unique_ptr<App>; | @@ -43,21 +41,21 @@ using App_p = std::unique_ptr<App>; | ||
| 43 | class App { | 41 | class App { |
| 44 | friend Option; | 42 | friend Option; |
| 45 | friend detail::AppFriend; | 43 | friend detail::AppFriend; |
| 46 | -protected: | ||
| 47 | - | 44 | + |
| 45 | + protected: | ||
| 48 | // This library follows the Google style guide for member names ending in underscores | 46 | // This library follows the Google style guide for member names ending in underscores |
| 49 | 47 | ||
| 50 | /// @name Basics | 48 | /// @name Basics |
| 51 | ///@{ | 49 | ///@{ |
| 52 | - | 50 | + |
| 53 | /// Subcommand name or program name (from parser) | 51 | /// Subcommand name or program name (from parser) |
| 54 | - std::string name_ {"program"}; | ||
| 55 | - | 52 | + std::string name_{"program"}; |
| 53 | + | ||
| 56 | /// Description of the current program/subcommand | 54 | /// Description of the current program/subcommand |
| 57 | std::string description_; | 55 | std::string description_; |
| 58 | 56 | ||
| 59 | /// If true, allow extra arguments (ie, don't throw an error). | 57 | /// If true, allow extra arguments (ie, don't throw an error). |
| 60 | - bool allow_extras_ {false}; | 58 | + bool allow_extras_{false}; |
| 61 | 59 | ||
| 62 | /// This is a function that runs when complete. Great for subcommands. Can throw. | 60 | /// This is a function that runs when complete. Great for subcommands. Can throw. |
| 63 | std::function<void()> callback_; | 61 | std::function<void()> callback_; |
| @@ -68,9 +66,9 @@ protected: | @@ -68,9 +66,9 @@ protected: | ||
| 68 | 66 | ||
| 69 | /// The list of options, stored locally | 67 | /// The list of options, stored locally |
| 70 | std::vector<Option_p> options_; | 68 | std::vector<Option_p> options_; |
| 71 | - | 69 | + |
| 72 | /// A pointer to the help flag if there is one | 70 | /// A pointer to the help flag if there is one |
| 73 | - Option* help_ptr_ {nullptr}; | 71 | + Option *help_ptr_{nullptr}; |
| 74 | 72 | ||
| 75 | ///@} | 73 | ///@} |
| 76 | /// @name Parsing | 74 | /// @name Parsing |
| @@ -79,28 +77,28 @@ protected: | @@ -79,28 +77,28 @@ protected: | ||
| 79 | using missing_t = std::vector<std::pair<detail::Classifer, std::string>>; | 77 | using missing_t = std::vector<std::pair<detail::Classifer, std::string>>; |
| 80 | 78 | ||
| 81 | /// Pair of classifier, string for missing options. (extra detail is removed on returning from parse) | 79 | /// Pair of classifier, string for missing options. (extra detail is removed on returning from parse) |
| 82 | - /// | 80 | + /// |
| 83 | /// This is faster and cleaner than storing just a list of strings and reparsing. This may contain the -- separator. | 81 | /// This is faster and cleaner than storing just a list of strings and reparsing. This may contain the -- separator. |
| 84 | missing_t missing_; | 82 | missing_t missing_; |
| 85 | 83 | ||
| 86 | ///@} | 84 | ///@} |
| 87 | /// @name Subcommands | 85 | /// @name Subcommands |
| 88 | ///@{ | 86 | ///@{ |
| 89 | - | 87 | + |
| 90 | /// Storage for subcommand list | 88 | /// Storage for subcommand list |
| 91 | std::vector<App_p> subcommands_; | 89 | std::vector<App_p> subcommands_; |
| 92 | 90 | ||
| 93 | /// If true, the program name is not case sensitive | 91 | /// If true, the program name is not case sensitive |
| 94 | - bool ignore_case_ {false}; | 92 | + bool ignore_case_{false}; |
| 95 | 93 | ||
| 96 | /// Allow subcommand fallthrough, so that parent commands can collect commands after subcommand. | 94 | /// Allow subcommand fallthrough, so that parent commands can collect commands after subcommand. |
| 97 | - bool fallthrough_ {false}; | 95 | + bool fallthrough_{false}; |
| 98 | 96 | ||
| 99 | /// A pointer to the parent if this is a subcommand | 97 | /// A pointer to the parent if this is a subcommand |
| 100 | - App* parent_ {nullptr}; | 98 | + App *parent_{nullptr}; |
| 101 | 99 | ||
| 102 | /// True if this command/subcommand was parsed | 100 | /// True if this command/subcommand was parsed |
| 103 | - bool parsed_ {false}; | 101 | + bool parsed_{false}; |
| 104 | 102 | ||
| 105 | /// -1 for 1 or more, 0 for not required, # for exact number required | 103 | /// -1 for 1 or more, 0 for not required, # for exact number required |
| 106 | int require_subcommand_ = 0; | 104 | int require_subcommand_ = 0; |
| @@ -112,53 +110,47 @@ protected: | @@ -112,53 +110,47 @@ protected: | ||
| 112 | /// The name of the connected config file | 110 | /// The name of the connected config file |
| 113 | std::string config_name_; | 111 | std::string config_name_; |
| 114 | 112 | ||
| 115 | - /// True if ini is required (throws if not present), if false simply keep going. | ||
| 116 | - bool config_required_ {false}; | 113 | + /// True if ini is required (throws if not present), if false simply keep going. |
| 114 | + bool config_required_{false}; | ||
| 117 | 115 | ||
| 118 | /// Pointer to the config option | 116 | /// Pointer to the config option |
| 119 | - Option* config_ptr_ {nullptr}; | ||
| 120 | - | 117 | + Option *config_ptr_{nullptr}; |
| 121 | 118 | ||
| 122 | ///@} | 119 | ///@} |
| 123 | - | 120 | + |
| 124 | /// Special private constructor for subcommand | 121 | /// Special private constructor for subcommand |
| 125 | - App(std::string description_, bool help, detail::enabler) | ||
| 126 | - : description_(std::move(description_)) { | 122 | + App(std::string description_, bool help, detail::enabler) : description_(std::move(description_)) { |
| 127 | 123 | ||
| 128 | if(help) | 124 | if(help) |
| 129 | help_ptr_ = add_flag("-h,--help", "Print this help message and exit"); | 125 | help_ptr_ = add_flag("-h,--help", "Print this help message and exit"); |
| 130 | - | ||
| 131 | } | 126 | } |
| 132 | 127 | ||
| 133 | -public: | 128 | + public: |
| 134 | /// @name Basic | 129 | /// @name Basic |
| 135 | ///@{ | 130 | ///@{ |
| 136 | 131 | ||
| 137 | /// Create a new program. Pass in the same arguments as main(), along with a help string. | 132 | /// Create a new program. Pass in the same arguments as main(), along with a help string. |
| 138 | - App(std::string description_="", bool help=true) | ||
| 139 | - : App(description_, help, detail::dummy) { | ||
| 140 | - | ||
| 141 | - } | 133 | + App(std::string description_ = "", bool help = true) : App(description_, help, detail::dummy) {} |
| 142 | 134 | ||
| 143 | /// Set a callback for the end of parsing. | 135 | /// Set a callback for the end of parsing. |
| 144 | - /// | 136 | + /// |
| 145 | /// Due to a bug in c++11, | 137 | /// Due to a bug in c++11, |
| 146 | /// it is not possible to overload on std::function (fixed in c++14 | 138 | /// it is not possible to overload on std::function (fixed in c++14 |
| 147 | /// and backported to c++11 on newer compilers). Use capture by reference | 139 | /// and backported to c++11 on newer compilers). Use capture by reference |
| 148 | /// to get a pointer to App if needed. | 140 | /// to get a pointer to App if needed. |
| 149 | - App* set_callback(std::function<void()> callback) { | 141 | + App *set_callback(std::function<void()> callback) { |
| 150 | callback_ = callback; | 142 | callback_ = callback; |
| 151 | return this; | 143 | return this; |
| 152 | } | 144 | } |
| 153 | 145 | ||
| 154 | /// Remove the error when extras are left over on the command line. | 146 | /// Remove the error when extras are left over on the command line. |
| 155 | - App* allow_extras (bool allow=true) { | 147 | + App *allow_extras(bool allow = true) { |
| 156 | allow_extras_ = allow; | 148 | allow_extras_ = allow; |
| 157 | return this; | 149 | return this; |
| 158 | } | 150 | } |
| 159 | 151 | ||
| 160 | /// Ignore case. Subcommand inherit value. | 152 | /// Ignore case. Subcommand inherit value. |
| 161 | - App* ignore_case(bool value = true) { | 153 | + App *ignore_case(bool value = true) { |
| 162 | ignore_case_ = value; | 154 | ignore_case_ = value; |
| 163 | if(parent_ != nullptr) { | 155 | if(parent_ != nullptr) { |
| 164 | for(const auto &subc : parent_->subcommands_) { | 156 | for(const auto &subc : parent_->subcommands_) { |
| @@ -170,81 +162,72 @@ public: | @@ -170,81 +162,72 @@ public: | ||
| 170 | } | 162 | } |
| 171 | 163 | ||
| 172 | /// Check to see if this subcommand was parsed, true only if received on command line. | 164 | /// Check to see if this subcommand was parsed, true only if received on command line. |
| 173 | - bool parsed() const {return parsed_;} | 165 | + bool parsed() const { return parsed_; } |
| 174 | 166 | ||
| 175 | /// Check to see if this subcommand was parsed, true only if received on command line. | 167 | /// Check to see if this subcommand was parsed, true only if received on command line. |
| 176 | /// This allows the subcommand to be directly checked. | 168 | /// This allows the subcommand to be directly checked. |
| 177 | - operator bool () const { return parsed_;} | 169 | + operator bool() const { return parsed_; } |
| 178 | 170 | ||
| 179 | /// Require a subcommand to be given (does not affect help call) | 171 | /// Require a subcommand to be given (does not affect help call) |
| 180 | /// Does not return a pointer since it is supposed to be called on the main App. | 172 | /// Does not return a pointer since it is supposed to be called on the main App. |
| 181 | - App* require_subcommand(int value = -1) { | 173 | + App *require_subcommand(int value = -1) { |
| 182 | require_subcommand_ = value; | 174 | require_subcommand_ = value; |
| 183 | return this; | 175 | return this; |
| 184 | } | 176 | } |
| 185 | 177 | ||
| 186 | /// Stop subcommand fallthrough, so that parent commands cannot collect commands after subcommand. | 178 | /// Stop subcommand fallthrough, so that parent commands cannot collect commands after subcommand. |
| 187 | /// Default from parent, usually set on parent. | 179 | /// Default from parent, usually set on parent. |
| 188 | - App* fallthrough(bool value=true) { | 180 | + App *fallthrough(bool value = true) { |
| 189 | fallthrough_ = value; | 181 | fallthrough_ = value; |
| 190 | return this; | 182 | return this; |
| 191 | } | 183 | } |
| 192 | 184 | ||
| 193 | - | ||
| 194 | ///@} | 185 | ///@} |
| 195 | /// @name Adding options | 186 | /// @name Adding options |
| 196 | ///@{ | 187 | ///@{ |
| 197 | - | 188 | + |
| 198 | /// Add an option, will automatically understand the type for common types. | 189 | /// Add an option, will automatically understand the type for common types. |
| 199 | /// | 190 | /// |
| 200 | /// To use, create a variable with the expected type, and pass it in after the name. | 191 | /// To use, create a variable with the expected type, and pass it in after the name. |
| 201 | /// After start is called, you can use count to see if the value was passed, and | 192 | /// After start is called, you can use count to see if the value was passed, and |
| 202 | /// the value will be initialized properly. Numbers, vectors, and strings are supported. | 193 | /// the value will be initialized properly. Numbers, vectors, and strings are supported. |
| 203 | - /// | ||
| 204 | - /// ->required(), ->default, and the validators are options, | 194 | + /// |
| 195 | + /// ->required(), ->default, and the validators are options, | ||
| 205 | /// The positional options take an optional number of arguments. | 196 | /// The positional options take an optional number of arguments. |
| 206 | - /// | 197 | + /// |
| 207 | /// For example, | 198 | /// For example, |
| 208 | - /// | 199 | + /// |
| 209 | /// std::string filename; | 200 | /// std::string filename; |
| 210 | /// program.add_option("filename", filename, "description of filename"); | 201 | /// program.add_option("filename", filename, "description of filename"); |
| 211 | /// | 202 | /// |
| 212 | - Option* add_option( | ||
| 213 | - std::string name, | ||
| 214 | - callback_t callback, | ||
| 215 | - std::string description="", | ||
| 216 | - bool defaulted=false | ||
| 217 | - ) { | 203 | + Option *add_option(std::string name, callback_t callback, std::string description = "", bool defaulted = false) { |
| 218 | Option myopt{name, description, callback, defaulted, this}; | 204 | Option myopt{name, description, callback, defaulted, this}; |
| 219 | 205 | ||
| 220 | - if(std::find_if(std::begin(options_), std::end(options_), | ||
| 221 | - [&myopt](const Option_p &v){return *v == myopt;}) == std::end(options_)) { | 206 | + if(std::find_if(std::begin(options_), std::end(options_), [&myopt](const Option_p &v) { |
| 207 | + return *v == myopt; | ||
| 208 | + }) == std::end(options_)) { | ||
| 222 | options_.emplace_back(); | 209 | options_.emplace_back(); |
| 223 | - Option_p& option = options_.back(); | 210 | + Option_p &option = options_.back(); |
| 224 | option.reset(new Option(name, description, callback, defaulted, this)); | 211 | option.reset(new Option(name, description, callback, defaulted, this)); |
| 225 | return option.get(); | 212 | return option.get(); |
| 226 | } else | 213 | } else |
| 227 | throw OptionAlreadyAdded(myopt.get_name()); | 214 | throw OptionAlreadyAdded(myopt.get_name()); |
| 228 | - | ||
| 229 | } | 215 | } |
| 230 | 216 | ||
| 231 | /// Add option for non-vectors | 217 | /// Add option for non-vectors |
| 232 | - template<typename T, enable_if_t<!is_vector<T>::value, detail::enabler> = detail::dummy> | ||
| 233 | - Option* add_option( | ||
| 234 | - std::string name, | ||
| 235 | - T &variable, ///< The variable to set | ||
| 236 | - std::string description="", | ||
| 237 | - bool defaulted=false | ||
| 238 | - ) { | ||
| 239 | - | ||
| 240 | - | ||
| 241 | - CLI::callback_t fun = [&variable](CLI::results_t res){ | ||
| 242 | - if(res.size()!=1) | 218 | + template <typename T, enable_if_t<!is_vector<T>::value, detail::enabler> = detail::dummy> |
| 219 | + Option *add_option(std::string name, | ||
| 220 | + T &variable, ///< The variable to set | ||
| 221 | + std::string description = "", | ||
| 222 | + bool defaulted = false) { | ||
| 223 | + | ||
| 224 | + CLI::callback_t fun = [&variable](CLI::results_t res) { | ||
| 225 | + if(res.size() != 1) | ||
| 243 | return false; | 226 | return false; |
| 244 | return detail::lexical_cast(res[0], variable); | 227 | return detail::lexical_cast(res[0], variable); |
| 245 | }; | 228 | }; |
| 246 | 229 | ||
| 247 | - Option* opt = add_option(name, fun, description, defaulted); | 230 | + Option *opt = add_option(name, fun, description, defaulted); |
| 248 | opt->set_custom_option(detail::type_name<T>()); | 231 | opt->set_custom_option(detail::type_name<T>()); |
| 249 | if(defaulted) { | 232 | if(defaulted) { |
| 250 | std::stringstream out; | 233 | std::stringstream out; |
| @@ -255,15 +238,13 @@ public: | @@ -255,15 +238,13 @@ public: | ||
| 255 | } | 238 | } |
| 256 | 239 | ||
| 257 | /// Add option for vectors | 240 | /// Add option for vectors |
| 258 | - template<typename T> | ||
| 259 | - Option* add_option( | ||
| 260 | - std::string name, | ||
| 261 | - std::vector<T> &variable, ///< The variable vector to set | ||
| 262 | - std::string description="", | ||
| 263 | - bool defaulted=false | ||
| 264 | - ) { | ||
| 265 | - | ||
| 266 | - CLI::callback_t fun = [&variable](CLI::results_t res){ | 241 | + template <typename T> |
| 242 | + Option *add_option(std::string name, | ||
| 243 | + std::vector<T> &variable, ///< The variable vector to set | ||
| 244 | + std::string description = "", | ||
| 245 | + bool defaulted = false) { | ||
| 246 | + | ||
| 247 | + CLI::callback_t fun = [&variable](CLI::results_t res) { | ||
| 267 | bool retval = true; | 248 | bool retval = true; |
| 268 | variable.clear(); | 249 | variable.clear(); |
| 269 | for(const auto &a : res) { | 250 | for(const auto &a : res) { |
| @@ -273,7 +254,7 @@ public: | @@ -273,7 +254,7 @@ public: | ||
| 273 | return variable.size() > 0 && retval; | 254 | return variable.size() > 0 && retval; |
| 274 | }; | 255 | }; |
| 275 | 256 | ||
| 276 | - Option* opt = add_option(name, fun, description, defaulted); | 257 | + Option *opt = add_option(name, fun, description, defaulted); |
| 277 | opt->set_custom_option(detail::type_name<T>(), -1, true); | 258 | opt->set_custom_option(detail::type_name<T>(), -1, true); |
| 278 | if(defaulted) | 259 | if(defaulted) |
| 279 | opt->set_default_val("[" + detail::join(variable) + "]"); | 260 | opt->set_default_val("[" + detail::join(variable) + "]"); |
| @@ -281,15 +262,10 @@ public: | @@ -281,15 +262,10 @@ public: | ||
| 281 | } | 262 | } |
| 282 | 263 | ||
| 283 | /// Add option for flag | 264 | /// Add option for flag |
| 284 | - Option* add_flag( | ||
| 285 | - std::string name, | ||
| 286 | - std::string description="" | ||
| 287 | - ) { | ||
| 288 | - CLI::callback_t fun = [](CLI::results_t){ | ||
| 289 | - return true; | ||
| 290 | - }; | ||
| 291 | - | ||
| 292 | - Option* opt = add_option(name, fun, description, false); | 265 | + Option *add_flag(std::string name, std::string description = "") { |
| 266 | + CLI::callback_t fun = [](CLI::results_t) { return true; }; | ||
| 267 | + | ||
| 268 | + Option *opt = add_option(name, fun, description, false); | ||
| 293 | if(opt->get_positional()) | 269 | if(opt->get_positional()) |
| 294 | throw IncorrectConstruction("Flags cannot be positional"); | 270 | throw IncorrectConstruction("Flags cannot be positional"); |
| 295 | opt->set_custom_option("", 0); | 271 | opt->set_custom_option("", 0); |
| @@ -297,21 +273,19 @@ public: | @@ -297,21 +273,19 @@ public: | ||
| 297 | } | 273 | } |
| 298 | 274 | ||
| 299 | /// Add option for flag integer | 275 | /// Add option for flag integer |
| 300 | - template<typename T, | ||
| 301 | - enable_if_t<std::is_integral<T>::value && !is_bool<T>::value, detail::enabler> = detail::dummy> | ||
| 302 | - Option* add_flag( | ||
| 303 | - std::string name, | ||
| 304 | - T &count, ///< A varaible holding the count | ||
| 305 | - std::string description="" | ||
| 306 | - ) { | 276 | + template <typename T, |
| 277 | + enable_if_t<std::is_integral<T>::value && !is_bool<T>::value, detail::enabler> = detail::dummy> | ||
| 278 | + Option *add_flag(std::string name, | ||
| 279 | + T &count, ///< A varaible holding the count | ||
| 280 | + std::string description = "") { | ||
| 307 | 281 | ||
| 308 | count = 0; | 282 | count = 0; |
| 309 | - CLI::callback_t fun = [&count](CLI::results_t res){ | 283 | + CLI::callback_t fun = [&count](CLI::results_t res) { |
| 310 | count = static_cast<T>(res.size()); | 284 | count = static_cast<T>(res.size()); |
| 311 | return true; | 285 | return true; |
| 312 | }; | 286 | }; |
| 313 | - | ||
| 314 | - Option* opt = add_option(name, fun, description, false); | 287 | + |
| 288 | + Option *opt = add_option(name, fun, description, false); | ||
| 315 | if(opt->get_positional()) | 289 | if(opt->get_positional()) |
| 316 | throw IncorrectConstruction("Flags cannot be positional"); | 290 | throw IncorrectConstruction("Flags cannot be positional"); |
| 317 | opt->set_custom_option("", 0); | 291 | opt->set_custom_option("", 0); |
| @@ -319,40 +293,34 @@ public: | @@ -319,40 +293,34 @@ public: | ||
| 319 | } | 293 | } |
| 320 | 294 | ||
| 321 | /// Bool version only allows the flag once | 295 | /// Bool version only allows the flag once |
| 322 | - template<typename T, | ||
| 323 | - enable_if_t<is_bool<T>::value, detail::enabler> = detail::dummy> | ||
| 324 | - Option* add_flag( | ||
| 325 | - std::string name, | ||
| 326 | - T &count, ///< A varaible holding true if passed | ||
| 327 | - std::string description="" | ||
| 328 | - ) { | 296 | + template <typename T, enable_if_t<is_bool<T>::value, detail::enabler> = detail::dummy> |
| 297 | + Option *add_flag(std::string name, | ||
| 298 | + T &count, ///< A varaible holding true if passed | ||
| 299 | + std::string description = "") { | ||
| 329 | 300 | ||
| 330 | count = false; | 301 | count = false; |
| 331 | - CLI::callback_t fun = [&count](CLI::results_t res){ | 302 | + CLI::callback_t fun = [&count](CLI::results_t res) { |
| 332 | count = true; | 303 | count = true; |
| 333 | return res.size() == 1; | 304 | return res.size() == 1; |
| 334 | }; | 305 | }; |
| 335 | - | ||
| 336 | - Option* opt = add_option(name, fun, description, false); | 306 | + |
| 307 | + Option *opt = add_option(name, fun, description, false); | ||
| 337 | if(opt->get_positional()) | 308 | if(opt->get_positional()) |
| 338 | throw IncorrectConstruction("Flags cannot be positional"); | 309 | throw IncorrectConstruction("Flags cannot be positional"); |
| 339 | opt->set_custom_option("", 0); | 310 | opt->set_custom_option("", 0); |
| 340 | return opt; | 311 | return opt; |
| 341 | } | 312 | } |
| 342 | 313 | ||
| 343 | - | ||
| 344 | /// Add set of options | 314 | /// Add set of options |
| 345 | - template<typename T> | ||
| 346 | - Option* add_set( | ||
| 347 | - std::string name, | ||
| 348 | - T &member, ///< The selected member of the set | ||
| 349 | - std::set<T> options, ///< The set of posibilities | ||
| 350 | - std::string description="", | ||
| 351 | - bool defaulted=false | ||
| 352 | - ) { | ||
| 353 | - | ||
| 354 | - CLI::callback_t fun = [&member, options](CLI::results_t res){ | ||
| 355 | - if(res.size()!=1) { | 315 | + template <typename T> |
| 316 | + Option *add_set(std::string name, | ||
| 317 | + T &member, ///< The selected member of the set | ||
| 318 | + std::set<T> options, ///< The set of posibilities | ||
| 319 | + std::string description = "", | ||
| 320 | + bool defaulted = false) { | ||
| 321 | + | ||
| 322 | + CLI::callback_t fun = [&member, options](CLI::results_t res) { | ||
| 323 | + if(res.size() != 1) { | ||
| 356 | return false; | 324 | return false; |
| 357 | } | 325 | } |
| 358 | bool retval = detail::lexical_cast(res[0], member); | 326 | bool retval = detail::lexical_cast(res[0], member); |
| @@ -361,7 +329,7 @@ public: | @@ -361,7 +329,7 @@ public: | ||
| 361 | return std::find(std::begin(options), std::end(options), member) != std::end(options); | 329 | return std::find(std::begin(options), std::end(options), member) != std::end(options); |
| 362 | }; | 330 | }; |
| 363 | 331 | ||
| 364 | - Option* opt = add_option(name, fun, description, defaulted); | 332 | + Option *opt = add_option(name, fun, description, defaulted); |
| 365 | std::string typeval = detail::type_name<T>(); | 333 | std::string typeval = detail::type_name<T>(); |
| 366 | typeval += " in {" + detail::join(options) + "}"; | 334 | typeval += " in {" + detail::join(options) + "}"; |
| 367 | opt->set_custom_option(typeval); | 335 | opt->set_custom_option(typeval); |
| @@ -374,21 +342,20 @@ public: | @@ -374,21 +342,20 @@ public: | ||
| 374 | } | 342 | } |
| 375 | 343 | ||
| 376 | /// Add set of options, string only, ignore case | 344 | /// Add set of options, string only, ignore case |
| 377 | - Option* add_set_ignore_case( | ||
| 378 | - std::string name, | ||
| 379 | - std::string &member, ///< The selected member of the set | ||
| 380 | - std::set<std::string> options, ///< The set of posibilities | ||
| 381 | - std::string description="", | ||
| 382 | - bool defaulted=false | ||
| 383 | - ) { | ||
| 384 | - | ||
| 385 | - CLI::callback_t fun = [&member, options](CLI::results_t res){ | ||
| 386 | - if(res.size()!=1) { | 345 | + Option *add_set_ignore_case(std::string name, |
| 346 | + std::string &member, ///< The selected member of the set | ||
| 347 | + std::set<std::string> options, ///< The set of posibilities | ||
| 348 | + std::string description = "", | ||
| 349 | + bool defaulted = false) { | ||
| 350 | + | ||
| 351 | + CLI::callback_t fun = [&member, options](CLI::results_t res) { | ||
| 352 | + if(res.size() != 1) { | ||
| 387 | return false; | 353 | return false; |
| 388 | } | 354 | } |
| 389 | member = detail::to_lower(res[0]); | 355 | member = detail::to_lower(res[0]); |
| 390 | - auto iter = std::find_if(std::begin(options), std::end(options), | ||
| 391 | - [&member](std::string val){return detail::to_lower(val) == member;}); | 356 | + auto iter = std::find_if(std::begin(options), std::end(options), [&member](std::string val) { |
| 357 | + return detail::to_lower(val) == member; | ||
| 358 | + }); | ||
| 392 | if(iter == std::end(options)) | 359 | if(iter == std::end(options)) |
| 393 | return false; | 360 | return false; |
| 394 | else { | 361 | else { |
| @@ -397,7 +364,7 @@ public: | @@ -397,7 +364,7 @@ public: | ||
| 397 | } | 364 | } |
| 398 | }; | 365 | }; |
| 399 | 366 | ||
| 400 | - Option* opt = add_option(name, fun, description, defaulted); | 367 | + Option *opt = add_option(name, fun, description, defaulted); |
| 401 | std::string typeval = detail::type_name<std::string>(); | 368 | std::string typeval = detail::type_name<std::string>(); |
| 402 | typeval += " in {" + detail::join(options) + "}"; | 369 | typeval += " in {" + detail::join(options) + "}"; |
| 403 | opt->set_custom_option(typeval); | 370 | opt->set_custom_option(typeval); |
| @@ -408,94 +375,93 @@ public: | @@ -408,94 +375,93 @@ public: | ||
| 408 | } | 375 | } |
| 409 | 376 | ||
| 410 | /// Add a complex number | 377 | /// Add a complex number |
| 411 | - template<typename T> | ||
| 412 | - Option* add_complex( | ||
| 413 | - std::string name, T& variable, | ||
| 414 | - std::string description="", bool defaulted=false, | ||
| 415 | - std::string label="COMPLEX") { | ||
| 416 | - CLI::callback_t fun = [&variable](results_t res){ | ||
| 417 | - if(res.size()!=2) | ||
| 418 | - return false; | ||
| 419 | - double x,y; | ||
| 420 | - bool worked = detail::lexical_cast(res[0], x) | ||
| 421 | - && detail::lexical_cast(res[1], y); | ||
| 422 | - if(worked) | ||
| 423 | - variable = T(x,y); | ||
| 424 | - return worked; | ||
| 425 | - }; | ||
| 426 | - | ||
| 427 | - CLI::Option* opt = add_option(name, fun, description, defaulted); | ||
| 428 | - opt->set_custom_option(label, 2); | ||
| 429 | - if(defaulted) { | ||
| 430 | - std::stringstream out; | ||
| 431 | - out << variable; | ||
| 432 | - opt->set_default_val(out.str()); | ||
| 433 | - } | ||
| 434 | - return opt; | ||
| 435 | - } | 378 | + template <typename T> |
| 379 | + Option *add_complex(std::string name, | ||
| 380 | + T &variable, | ||
| 381 | + std::string description = "", | ||
| 382 | + bool defaulted = false, | ||
| 383 | + std::string label = "COMPLEX") { | ||
| 384 | + CLI::callback_t fun = [&variable](results_t res) { | ||
| 385 | + if(res.size() != 2) | ||
| 386 | + return false; | ||
| 387 | + double x, y; | ||
| 388 | + bool worked = detail::lexical_cast(res[0], x) && detail::lexical_cast(res[1], y); | ||
| 389 | + if(worked) | ||
| 390 | + variable = T(x, y); | ||
| 391 | + return worked; | ||
| 392 | + }; | ||
| 436 | 393 | ||
| 394 | + CLI::Option *opt = add_option(name, fun, description, defaulted); | ||
| 395 | + opt->set_custom_option(label, 2); | ||
| 396 | + if(defaulted) { | ||
| 397 | + std::stringstream out; | ||
| 398 | + out << variable; | ||
| 399 | + opt->set_default_val(out.str()); | ||
| 400 | + } | ||
| 401 | + return opt; | ||
| 402 | + } | ||
| 437 | 403 | ||
| 438 | /// Add a configuration ini file option | 404 | /// Add a configuration ini file option |
| 439 | - Option* add_config(std::string name="--config", | ||
| 440 | - std::string default_filename="", | ||
| 441 | - std::string help="Read an ini file", | ||
| 442 | - bool required=false) { | 405 | + Option *add_config(std::string name = "--config", |
| 406 | + std::string default_filename = "", | ||
| 407 | + std::string help = "Read an ini file", | ||
| 408 | + bool required = false) { | ||
| 443 | 409 | ||
| 444 | // Remove existing config if present | 410 | // Remove existing config if present |
| 445 | if(config_ptr_ != nullptr) | 411 | if(config_ptr_ != nullptr) |
| 446 | remove_option(config_ptr_); | 412 | remove_option(config_ptr_); |
| 447 | config_name_ = default_filename; | 413 | config_name_ = default_filename; |
| 448 | config_required_ = required; | 414 | config_required_ = required; |
| 449 | - config_ptr_ = add_option(name, config_name_, help, default_filename!=""); | 415 | + config_ptr_ = add_option(name, config_name_, help, default_filename != ""); |
| 450 | return config_ptr_; | 416 | return config_ptr_; |
| 451 | } | 417 | } |
| 452 | 418 | ||
| 453 | /// Removes an option from the App. Takes an option pointer. Returns true if found and removed. | 419 | /// Removes an option from the App. Takes an option pointer. Returns true if found and removed. |
| 454 | - bool remove_option(Option* opt) { | ||
| 455 | - auto iterator = std::find_if(std::begin(options_), std::end(options_), | ||
| 456 | - [opt](const Option_p &v){return v.get() == opt;}); | ||
| 457 | - if (iterator != std::end(options_)) { | 420 | + bool remove_option(Option *opt) { |
| 421 | + auto iterator = | ||
| 422 | + std::find_if(std::begin(options_), std::end(options_), [opt](const Option_p &v) { return v.get() == opt; }); | ||
| 423 | + if(iterator != std::end(options_)) { | ||
| 458 | options_.erase(iterator); | 424 | options_.erase(iterator); |
| 459 | return true; | 425 | return true; |
| 460 | } | 426 | } |
| 461 | return false; | 427 | return false; |
| 462 | } | 428 | } |
| 463 | - | 429 | + |
| 464 | ///@} | 430 | ///@} |
| 465 | /// @name Subcommmands | 431 | /// @name Subcommmands |
| 466 | ///@{ | 432 | ///@{ |
| 467 | 433 | ||
| 468 | /// Add a subcommand. Like the constructor, you can override the help message addition by setting help=false | 434 | /// Add a subcommand. Like the constructor, you can override the help message addition by setting help=false |
| 469 | - App* add_subcommand(std::string name, std::string description="", bool help=true) { | 435 | + App *add_subcommand(std::string name, std::string description = "", bool help = true) { |
| 470 | subcommands_.emplace_back(new App(description, help, detail::dummy)); | 436 | subcommands_.emplace_back(new App(description, help, detail::dummy)); |
| 471 | subcommands_.back()->name_ = name; | 437 | subcommands_.back()->name_ = name; |
| 472 | subcommands_.back()->allow_extras(); | 438 | subcommands_.back()->allow_extras(); |
| 473 | subcommands_.back()->parent_ = this; | 439 | subcommands_.back()->parent_ = this; |
| 474 | subcommands_.back()->ignore_case_ = ignore_case_; | 440 | subcommands_.back()->ignore_case_ = ignore_case_; |
| 475 | subcommands_.back()->fallthrough_ = fallthrough_; | 441 | subcommands_.back()->fallthrough_ = fallthrough_; |
| 476 | - for(const auto& subc : subcommands_) | 442 | + for(const auto &subc : subcommands_) |
| 477 | if(subc.get() != subcommands_.back().get()) | 443 | if(subc.get() != subcommands_.back().get()) |
| 478 | - if(subc->check_name(subcommands_.back()->name_) || subcommands_.back()->check_name(subc->name_)) | 444 | + if(subc->check_name(subcommands_.back()->name_) || subcommands_.back()->check_name(subc->name_)) |
| 479 | throw OptionAlreadyAdded(subc->name_); | 445 | throw OptionAlreadyAdded(subc->name_); |
| 480 | return subcommands_.back().get(); | 446 | return subcommands_.back().get(); |
| 481 | } | 447 | } |
| 482 | 448 | ||
| 483 | /// Check to see if a subcommand is part of this command (doesn't have to be in command line) | 449 | /// Check to see if a subcommand is part of this command (doesn't have to be in command line) |
| 484 | - App* get_subcommand(App* subcom) const { | 450 | + App *get_subcommand(App *subcom) const { |
| 485 | for(const App_p &subcomptr : subcommands_) | 451 | for(const App_p &subcomptr : subcommands_) |
| 486 | if(subcomptr.get() == subcom) | 452 | if(subcomptr.get() == subcom) |
| 487 | return subcom; | 453 | return subcom; |
| 488 | throw CLI::OptionNotFound(subcom->get_name()); | 454 | throw CLI::OptionNotFound(subcom->get_name()); |
| 489 | } | 455 | } |
| 490 | - | 456 | + |
| 491 | /// Check to see if a subcommand is part of this command (text version) | 457 | /// Check to see if a subcommand is part of this command (text version) |
| 492 | - App* get_subcommand(std::string subcom) const { | 458 | + App *get_subcommand(std::string subcom) const { |
| 493 | for(const App_p &subcomptr : subcommands_) | 459 | for(const App_p &subcomptr : subcommands_) |
| 494 | if(subcomptr->check_name(subcom)) | 460 | if(subcomptr->check_name(subcom)) |
| 495 | return subcomptr.get(); | 461 | return subcomptr.get(); |
| 496 | throw CLI::OptionNotFound(subcom); | 462 | throw CLI::OptionNotFound(subcom); |
| 497 | } | 463 | } |
| 498 | - | 464 | + |
| 499 | ///@} | 465 | ///@} |
| 500 | /// @name Extras for subclassing | 466 | /// @name Extras for subclassing |
| 501 | ///@{ | 467 | ///@{ |
| @@ -514,15 +480,14 @@ public: | @@ -514,15 +480,14 @@ public: | ||
| 514 | std::vector<std::string> parse(int argc, char **argv) { | 480 | std::vector<std::string> parse(int argc, char **argv) { |
| 515 | name_ = argv[0]; | 481 | name_ = argv[0]; |
| 516 | std::vector<std::string> args; | 482 | std::vector<std::string> args; |
| 517 | - for(int i=argc-1; i>0; i--) | 483 | + for(int i = argc - 1; i > 0; i--) |
| 518 | args.emplace_back(argv[i]); | 484 | args.emplace_back(argv[i]); |
| 519 | return parse(args); | 485 | return parse(args); |
| 520 | - | ||
| 521 | } | 486 | } |
| 522 | 487 | ||
| 523 | /// The real work is done here. Expects a reversed vector. | 488 | /// The real work is done here. Expects a reversed vector. |
| 524 | /// Changes the vector to the remaining options. | 489 | /// Changes the vector to the remaining options. |
| 525 | - std::vector<std::string>& parse(std::vector<std::string> &args) { | 490 | + std::vector<std::string> &parse(std::vector<std::string> &args) { |
| 526 | _validate(); | 491 | _validate(); |
| 527 | _parse(args); | 492 | _parse(args); |
| 528 | run_callback(); | 493 | run_callback(); |
| @@ -530,7 +495,7 @@ public: | @@ -530,7 +495,7 @@ public: | ||
| 530 | } | 495 | } |
| 531 | 496 | ||
| 532 | /// Print a nice error message and return the exit code | 497 | /// Print a nice error message and return the exit code |
| 533 | - int exit(const Error& e) const { | 498 | + int exit(const Error &e) const { |
| 534 | if(e.exit_code != static_cast<int>(ExitCodes::Success)) { | 499 | if(e.exit_code != static_cast<int>(ExitCodes::Success)) { |
| 535 | std::cerr << "ERROR: "; | 500 | std::cerr << "ERROR: "; |
| 536 | std::cerr << e.what() << std::endl; | 501 | std::cerr << e.what() << std::endl; |
| @@ -572,32 +537,30 @@ public: | @@ -572,32 +537,30 @@ public: | ||
| 572 | } | 537 | } |
| 573 | 538 | ||
| 574 | /// Get a subcommand pointer list to the currently selected subcommands (after parsing) | 539 | /// Get a subcommand pointer list to the currently selected subcommands (after parsing) |
| 575 | - std::vector<App*> get_subcommands() const { | ||
| 576 | - std::vector<App*> subcomms; | 540 | + std::vector<App *> get_subcommands() const { |
| 541 | + std::vector<App *> subcomms; | ||
| 577 | for(const App_p &subcomptr : subcommands_) | 542 | for(const App_p &subcomptr : subcommands_) |
| 578 | if(subcomptr->parsed_) | 543 | if(subcomptr->parsed_) |
| 579 | subcomms.push_back(subcomptr.get()); | 544 | subcomms.push_back(subcomptr.get()); |
| 580 | return subcomms; | 545 | return subcomms; |
| 581 | } | 546 | } |
| 582 | 547 | ||
| 583 | - | ||
| 584 | /// Check to see if given subcommand was selected | 548 | /// Check to see if given subcommand was selected |
| 585 | - bool got_subcommand(App* subcom) const { | 549 | + bool got_subcommand(App *subcom) const { |
| 586 | // get subcom needed to verify that this was a real subcommand | 550 | // get subcom needed to verify that this was a real subcommand |
| 587 | return get_subcommand(subcom)->parsed_; | 551 | return get_subcommand(subcom)->parsed_; |
| 588 | } | 552 | } |
| 589 | 553 | ||
| 590 | /// Check with name instead of pointer to see if subcommand was selected | 554 | /// Check with name instead of pointer to see if subcommand was selected |
| 591 | - bool got_subcommand(std::string name) const { | ||
| 592 | - return get_subcommand(name)->parsed_; | ||
| 593 | - } | ||
| 594 | - | 555 | + bool got_subcommand(std::string name) const { return get_subcommand(name)->parsed_; } |
| 556 | + | ||
| 595 | ///@} | 557 | ///@} |
| 596 | /// @name Help | 558 | /// @name Help |
| 597 | ///@{ | 559 | ///@{ |
| 598 | 560 | ||
| 599 | - /// 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. | ||
| 600 | - std::string config_to_str(bool default_also=false, std::string prefix="") const { | 561 | + /// Produce a string that could be read in as a config of the current values of the App. Set default_also to include |
| 562 | + /// default arguments. Prefix will add a string to the beginning of each option. | ||
| 563 | + std::string config_to_str(bool default_also = false, std::string prefix = "") const { | ||
| 601 | std::stringstream out; | 564 | std::stringstream out; |
| 602 | for(const Option_p &opt : options_) { | 565 | for(const Option_p &opt : options_) { |
| 603 | 566 | ||
| @@ -615,19 +578,18 @@ public: | @@ -615,19 +578,18 @@ public: | ||
| 615 | // If the option has a default and is requested by optional argument | 578 | // If the option has a default and is requested by optional argument |
| 616 | else if(default_also && opt->defaultval_ != "") | 579 | else if(default_also && opt->defaultval_ != "") |
| 617 | out << name << "=" << opt->defaultval_ << std::endl; | 580 | out << name << "=" << opt->defaultval_ << std::endl; |
| 618 | - // Flag, one passed | 581 | + // Flag, one passed |
| 619 | } else if(opt->count() == 1) { | 582 | } else if(opt->count() == 1) { |
| 620 | out << name << "=true" << std::endl; | 583 | out << name << "=true" << std::endl; |
| 621 | 584 | ||
| 622 | - // Flag, multiple passed | 585 | + // Flag, multiple passed |
| 623 | } else if(opt->count() > 1) { | 586 | } else if(opt->count() > 1) { |
| 624 | out << name << "=" << opt->count() << std::endl; | 587 | out << name << "=" << opt->count() << std::endl; |
| 625 | 588 | ||
| 626 | - // Flag, not present | 589 | + // Flag, not present |
| 627 | } else if(opt->count() == 0 && default_also && opt.get() != get_help_ptr()) { | 590 | } else if(opt->count() == 0 && default_also && opt.get() != get_help_ptr()) { |
| 628 | out << name << "=false" << std::endl; | 591 | out << name << "=false" << std::endl; |
| 629 | } | 592 | } |
| 630 | - | ||
| 631 | } | 593 | } |
| 632 | } | 594 | } |
| 633 | for(const App_p &subcom : subcommands_) | 595 | for(const App_p &subcom : subcommands_) |
| @@ -636,7 +598,7 @@ public: | @@ -636,7 +598,7 @@ public: | ||
| 636 | } | 598 | } |
| 637 | 599 | ||
| 638 | /// Makes a help message, with a column wid for column 1 | 600 | /// Makes a help message, with a column wid for column 1 |
| 639 | - std::string help(size_t wid=30, std::string prev="") const { | 601 | + std::string help(size_t wid = 30, std::string prev = "") const { |
| 640 | // Delegate to subcommand if needed | 602 | // Delegate to subcommand if needed |
| 641 | if(prev == "") | 603 | if(prev == "") |
| 642 | prev = name_; | 604 | prev = name_; |
| @@ -650,7 +612,7 @@ public: | @@ -650,7 +612,7 @@ public: | ||
| 650 | std::stringstream out; | 612 | std::stringstream out; |
| 651 | out << description_ << std::endl; | 613 | out << description_ << std::endl; |
| 652 | out << "Usage: " << prev; | 614 | out << "Usage: " << prev; |
| 653 | - | 615 | + |
| 654 | // Check for options_ | 616 | // Check for options_ |
| 655 | bool npos = false; | 617 | bool npos = false; |
| 656 | std::set<std::string> groups; | 618 | std::set<std::string> groups; |
| @@ -665,15 +627,15 @@ public: | @@ -665,15 +627,15 @@ public: | ||
| 665 | out << " [OPTIONS]"; | 627 | out << " [OPTIONS]"; |
| 666 | 628 | ||
| 667 | // Positionals | 629 | // Positionals |
| 668 | - bool pos=false; | 630 | + bool pos = false; |
| 669 | for(const Option_p &opt : options_) | 631 | for(const Option_p &opt : options_) |
| 670 | if(opt->get_positional()) { | 632 | if(opt->get_positional()) { |
| 671 | // A hidden positional should still show up in the usage statement | 633 | // A hidden positional should still show up in the usage statement |
| 672 | - //if(detail::to_lower(opt->get_group()) == "hidden") | 634 | + // if(detail::to_lower(opt->get_group()) == "hidden") |
| 673 | // continue; | 635 | // continue; |
| 674 | out << " " << opt->help_positional(); | 636 | out << " " << opt->help_positional(); |
| 675 | if(opt->_has_help_positional()) | 637 | if(opt->_has_help_positional()) |
| 676 | - pos=true; | 638 | + pos = true; |
| 677 | } | 639 | } |
| 678 | 640 | ||
| 679 | if(!subcommands_.empty()) { | 641 | if(!subcommands_.empty()) { |
| @@ -695,20 +657,17 @@ public: | @@ -695,20 +657,17 @@ public: | ||
| 695 | detail::format_help(out, opt->help_pname(), opt->get_description(), wid); | 657 | detail::format_help(out, opt->help_pname(), opt->get_description(), wid); |
| 696 | } | 658 | } |
| 697 | out << std::endl; | 659 | out << std::endl; |
| 698 | - | ||
| 699 | } | 660 | } |
| 700 | 661 | ||
| 701 | - | ||
| 702 | // Options | 662 | // Options |
| 703 | if(npos) { | 663 | if(npos) { |
| 704 | - for (const std::string& group : groups) { | 664 | + for(const std::string &group : groups) { |
| 705 | if(detail::to_lower(group) == "hidden") | 665 | if(detail::to_lower(group) == "hidden") |
| 706 | continue; | 666 | continue; |
| 707 | out << group << ":" << std::endl; | 667 | out << group << ":" << std::endl; |
| 708 | for(const Option_p &opt : options_) { | 668 | for(const Option_p &opt : options_) { |
| 709 | if(opt->nonpositional() && opt->get_group() == group) | 669 | if(opt->nonpositional() && opt->get_group() == group) |
| 710 | detail::format_help(out, opt->help_name(), opt->get_description(), wid); | 670 | detail::format_help(out, opt->help_name(), opt->get_description(), wid); |
| 711 | - | ||
| 712 | } | 671 | } |
| 713 | out << std::endl; | 672 | out << std::endl; |
| 714 | } | 673 | } |
| @@ -728,28 +687,18 @@ public: | @@ -728,28 +687,18 @@ public: | ||
| 728 | ///@{ | 687 | ///@{ |
| 729 | 688 | ||
| 730 | /// Get a pointer to the help flag. | 689 | /// Get a pointer to the help flag. |
| 731 | - Option* get_help_ptr() { | ||
| 732 | - return help_ptr_; | ||
| 733 | - } | 690 | + Option *get_help_ptr() { return help_ptr_; } |
| 734 | 691 | ||
| 735 | /// Get a pointer to the help flag. (const) | 692 | /// Get a pointer to the help flag. (const) |
| 736 | - const Option* get_help_ptr() const { | ||
| 737 | - return help_ptr_; | ||
| 738 | - } | 693 | + const Option *get_help_ptr() const { return help_ptr_; } |
| 739 | 694 | ||
| 740 | /// Get a pointer to the config option. | 695 | /// Get a pointer to the config option. |
| 741 | - Option* get_config_ptr() { | ||
| 742 | - return config_ptr_; | ||
| 743 | - } | 696 | + Option *get_config_ptr() { return config_ptr_; } |
| 744 | 697 | ||
| 745 | /// Get a pointer to the config option. (const) | 698 | /// Get a pointer to the config option. (const) |
| 746 | - const Option* get_config_ptr() const { | ||
| 747 | - return config_ptr_; | ||
| 748 | - } | 699 | + const Option *get_config_ptr() const { return config_ptr_; } |
| 749 | /// Get the name of the current app | 700 | /// Get the name of the current app |
| 750 | - std::string get_name() const { | ||
| 751 | - return name_; | ||
| 752 | - } | 701 | + std::string get_name() const { return name_; } |
| 753 | 702 | ||
| 754 | /// Check the name, case insensitive if set | 703 | /// Check the name, case insensitive if set |
| 755 | bool check_name(std::string name_to_check) const { | 704 | bool check_name(std::string name_to_check) const { |
| @@ -764,24 +713,22 @@ public: | @@ -764,24 +713,22 @@ public: | ||
| 764 | 713 | ||
| 765 | ///@} | 714 | ///@} |
| 766 | 715 | ||
| 767 | - | ||
| 768 | -protected: | ||
| 769 | - | 716 | + protected: |
| 770 | /// Check the options to make sure there are no conficts. | 717 | /// Check the options to make sure there are no conficts. |
| 771 | /// | 718 | /// |
| 772 | /// Currenly checks to see if mutiple positionals exist with -1 args | 719 | /// Currenly checks to see if mutiple positionals exist with -1 args |
| 773 | void _validate() const { | 720 | void _validate() const { |
| 774 | - auto count = std::count_if(std::begin(options_), std::end(options_), | ||
| 775 | - [](const Option_p& opt){return opt->get_expected() == -1 && opt->get_positional();}); | 721 | + auto count = std::count_if(std::begin(options_), std::end(options_), [](const Option_p &opt) { |
| 722 | + return opt->get_expected() == -1 && opt->get_positional(); | ||
| 723 | + }); | ||
| 776 | if(count > 1) | 724 | if(count > 1) |
| 777 | throw InvalidError(name_ + ": Too many positional arguments with unlimited expected args"); | 725 | throw InvalidError(name_ + ": Too many positional arguments with unlimited expected args"); |
| 778 | - for(const App_p& app : subcommands_) | 726 | + for(const App_p &app : subcommands_) |
| 779 | app->_validate(); | 727 | app->_validate(); |
| 780 | } | 728 | } |
| 781 | 729 | ||
| 782 | - | ||
| 783 | /// Return missing from the master | 730 | /// Return missing from the master |
| 784 | - missing_t* missing() { | 731 | + missing_t *missing() { |
| 785 | if(parent_ != nullptr) | 732 | if(parent_ != nullptr) |
| 786 | return parent_->missing(); | 733 | return parent_->missing(); |
| 787 | return &missing_; | 734 | return &missing_; |
| @@ -792,7 +739,7 @@ protected: | @@ -792,7 +739,7 @@ protected: | ||
| 792 | pre_callback(); | 739 | pre_callback(); |
| 793 | if(callback_) | 740 | if(callback_) |
| 794 | callback_(); | 741 | callback_(); |
| 795 | - for(App* subc : get_subcommands()) { | 742 | + for(App *subc : get_subcommands()) { |
| 796 | subc->run_callback(); | 743 | subc->run_callback(); |
| 797 | } | 744 | } |
| 798 | } | 745 | } |
| @@ -807,7 +754,6 @@ protected: | @@ -807,7 +754,6 @@ protected: | ||
| 807 | return false; | 754 | return false; |
| 808 | } | 755 | } |
| 809 | 756 | ||
| 810 | - | ||
| 811 | /// Selects a Classifer enum based on the type of the current argument | 757 | /// Selects a Classifer enum based on the type of the current argument |
| 812 | detail::Classifer _recognize(const std::string ¤t) const { | 758 | detail::Classifer _recognize(const std::string ¤t) const { |
| 813 | std::string dummy1, dummy2; | 759 | std::string dummy1, dummy2; |
| @@ -823,23 +769,21 @@ protected: | @@ -823,23 +769,21 @@ protected: | ||
| 823 | return detail::Classifer::NONE; | 769 | return detail::Classifer::NONE; |
| 824 | } | 770 | } |
| 825 | 771 | ||
| 826 | - | ||
| 827 | /// Internal parse function | 772 | /// Internal parse function |
| 828 | void _parse(std::vector<std::string> &args) { | 773 | void _parse(std::vector<std::string> &args) { |
| 829 | parsed_ = true; | 774 | parsed_ = true; |
| 830 | bool positional_only = false; | 775 | bool positional_only = false; |
| 831 | - | 776 | + |
| 832 | while(!args.empty()) { | 777 | while(!args.empty()) { |
| 833 | _parse_single(args, positional_only); | 778 | _parse_single(args, positional_only); |
| 834 | } | 779 | } |
| 835 | 780 | ||
| 836 | - if (help_ptr_ != nullptr && help_ptr_->count() > 0) { | 781 | + if(help_ptr_ != nullptr && help_ptr_->count() > 0) { |
| 837 | throw CallForHelp(); | 782 | throw CallForHelp(); |
| 838 | } | 783 | } |
| 839 | 784 | ||
| 840 | - | ||
| 841 | // Process an INI file | 785 | // Process an INI file |
| 842 | - if (config_ptr_ != nullptr && config_name_ != "") { | 786 | + if(config_ptr_ != nullptr && config_name_ != "") { |
| 843 | try { | 787 | try { |
| 844 | std::vector<detail::ini_ret_t> values = detail::parse_ini(config_name_); | 788 | std::vector<detail::ini_ret_t> values = detail::parse_ini(config_name_); |
| 845 | while(!values.empty()) { | 789 | while(!values.empty()) { |
| @@ -847,16 +791,15 @@ protected: | @@ -847,16 +791,15 @@ protected: | ||
| 847 | throw ExtrasINIError(values.back().fullname); | 791 | throw ExtrasINIError(values.back().fullname); |
| 848 | } | 792 | } |
| 849 | } | 793 | } |
| 850 | - } catch (const FileError &) { | 794 | + } catch(const FileError &) { |
| 851 | if(config_required_) | 795 | if(config_required_) |
| 852 | throw; | 796 | throw; |
| 853 | } | 797 | } |
| 854 | } | 798 | } |
| 855 | 799 | ||
| 856 | - | ||
| 857 | // Get envname options if not yet passed | 800 | // Get envname options if not yet passed |
| 858 | - for(const Option_p& opt : options_) { | ||
| 859 | - if (opt->count() == 0 && opt->envname_ != "") { | 801 | + for(const Option_p &opt : options_) { |
| 802 | + if(opt->count() == 0 && opt->envname_ != "") { | ||
| 860 | char *ename = std::getenv(opt->envname_.c_str()); | 803 | char *ename = std::getenv(opt->envname_.c_str()); |
| 861 | if(ename != nullptr) { | 804 | if(ename != nullptr) { |
| 862 | opt->add_result(std::string(ename)); | 805 | opt->add_result(std::string(ename)); |
| @@ -865,45 +808,48 @@ protected: | @@ -865,45 +808,48 @@ protected: | ||
| 865 | } | 808 | } |
| 866 | 809 | ||
| 867 | // Process callbacks | 810 | // Process callbacks |
| 868 | - for(const Option_p& opt : options_) { | ||
| 869 | - if (opt->count() > 0 && !opt->get_callback_run()) { | 811 | + for(const Option_p &opt : options_) { |
| 812 | + if(opt->count() > 0 && !opt->get_callback_run()) { | ||
| 870 | opt->run_callback(); | 813 | opt->run_callback(); |
| 871 | } | 814 | } |
| 872 | } | 815 | } |
| 873 | 816 | ||
| 874 | - // Verify required options | ||
| 875 | - for(const Option_p& opt : options_) { | 817 | + // Verify required options |
| 818 | + for(const Option_p &opt : options_) { | ||
| 876 | // Required | 819 | // Required |
| 877 | - if (opt->get_required() | ||
| 878 | - && (static_cast<int>( opt->count()) < opt->get_expected() || opt->count() == 0)) | 820 | + if(opt->get_required() && (static_cast<int>(opt->count()) < opt->get_expected() || opt->count() == 0)) |
| 879 | throw RequiredError(opt->get_name()); | 821 | throw RequiredError(opt->get_name()); |
| 880 | // Requires | 822 | // Requires |
| 881 | - for (const Option* opt_req : opt->requires_) | ||
| 882 | - if (opt->count() > 0 && opt_req->count() == 0) | 823 | + for(const Option *opt_req : opt->requires_) |
| 824 | + if(opt->count() > 0 && opt_req->count() == 0) | ||
| 883 | throw RequiresError(opt->get_name(), opt_req->get_name()); | 825 | throw RequiresError(opt->get_name(), opt_req->get_name()); |
| 884 | // Excludes | 826 | // Excludes |
| 885 | - for (const Option* opt_ex : opt->excludes_) | ||
| 886 | - if (opt->count() > 0 && opt_ex->count() != 0) | 827 | + for(const Option *opt_ex : opt->excludes_) |
| 828 | + if(opt->count() > 0 && opt_ex->count() != 0) | ||
| 887 | throw ExcludesError(opt->get_name(), opt_ex->get_name()); | 829 | throw ExcludesError(opt->get_name(), opt_ex->get_name()); |
| 888 | } | 830 | } |
| 889 | 831 | ||
| 890 | - auto selected_subcommands =get_subcommands(); | 832 | + auto selected_subcommands = get_subcommands(); |
| 891 | if(require_subcommand_ < 0 && selected_subcommands.empty()) | 833 | if(require_subcommand_ < 0 && selected_subcommands.empty()) |
| 892 | throw RequiredError("Subcommand required"); | 834 | throw RequiredError("Subcommand required"); |
| 893 | - else if(require_subcommand_ > 0 && static_cast<int>( selected_subcommands.size()) != require_subcommand_) | 835 | + else if(require_subcommand_ > 0 && static_cast<int>(selected_subcommands.size()) != require_subcommand_) |
| 894 | throw RequiredError(std::to_string(require_subcommand_) + " subcommand(s) required"); | 836 | throw RequiredError(std::to_string(require_subcommand_) + " subcommand(s) required"); |
| 895 | 837 | ||
| 896 | // Convert missing (pairs) to extras (string only) | 838 | // Convert missing (pairs) to extras (string only) |
| 897 | if(parent_ == nullptr) { | 839 | if(parent_ == nullptr) { |
| 898 | args.resize(missing()->size()); | 840 | args.resize(missing()->size()); |
| 899 | - std::transform(std::begin(*missing()), std::end(*missing()), std::begin(args), | ||
| 900 | - [](const std::pair<detail::Classifer, std::string>& val){return val.second;}); | 841 | + std::transform(std::begin(*missing()), |
| 842 | + std::end(*missing()), | ||
| 843 | + std::begin(args), | ||
| 844 | + [](const std::pair<detail::Classifer, std::string> &val) { return val.second; }); | ||
| 901 | std::reverse(std::begin(args), std::end(args)); | 845 | std::reverse(std::begin(args), std::end(args)); |
| 902 | 846 | ||
| 903 | - size_t num_left_over = std::count_if(std::begin(*missing()), std::end(*missing()), | ||
| 904 | - [](std::pair<detail::Classifer, std::string>& val){return val.first != detail::Classifer::POSITIONAL_MARK;}); | 847 | + size_t num_left_over = std::count_if( |
| 848 | + std::begin(*missing()), std::end(*missing()), [](std::pair<detail::Classifer, std::string> &val) { | ||
| 849 | + return val.first != detail::Classifer::POSITIONAL_MARK; | ||
| 850 | + }); | ||
| 905 | 851 | ||
| 906 | - if(num_left_over>0 && !allow_extras_) | 852 | + if(num_left_over > 0 && !allow_extras_) |
| 907 | throw ExtrasError("[" + detail::rjoin(args, " ") + "]"); | 853 | throw ExtrasError("[" + detail::rjoin(args, " ") + "]"); |
| 908 | } | 854 | } |
| 909 | } | 855 | } |
| @@ -913,29 +859,28 @@ protected: | @@ -913,29 +859,28 @@ protected: | ||
| 913 | /// If this has more than one dot.separated.name, go into the subcommand matching it | 859 | /// If this has more than one dot.separated.name, go into the subcommand matching it |
| 914 | /// Returns true if it managed to find the option, if false you'll need to remove the arg manully. | 860 | /// Returns true if it managed to find the option, if false you'll need to remove the arg manully. |
| 915 | bool _parse_ini(std::vector<detail::ini_ret_t> &args) { | 861 | bool _parse_ini(std::vector<detail::ini_ret_t> &args) { |
| 916 | - detail::ini_ret_t& current = args.back(); | 862 | + detail::ini_ret_t ¤t = args.back(); |
| 917 | std::string parent = current.parent(); // respects curent.level | 863 | std::string parent = current.parent(); // respects curent.level |
| 918 | std::string name = current.name(); | 864 | std::string name = current.name(); |
| 919 | 865 | ||
| 920 | // If a parent is listed, go to a subcommand | 866 | // If a parent is listed, go to a subcommand |
| 921 | if(parent != "") { | 867 | if(parent != "") { |
| 922 | current.level++; | 868 | current.level++; |
| 923 | - for(const App_p &com : subcommands_) | 869 | + for(const App_p &com : subcommands_) |
| 924 | if(com->check_name(parent)) | 870 | if(com->check_name(parent)) |
| 925 | return com->_parse_ini(args); | 871 | return com->_parse_ini(args); |
| 926 | return false; | 872 | return false; |
| 927 | } | 873 | } |
| 928 | 874 | ||
| 929 | - auto op_ptr = std::find_if(std::begin(options_), std::end(options_), | ||
| 930 | - [name](const Option_p &v){return v->check_lname(name);}); | ||
| 931 | - | ||
| 932 | - if(op_ptr == std::end(options_)) | 875 | + auto op_ptr = std::find_if( |
| 876 | + std::begin(options_), std::end(options_), [name](const Option_p &v) { return v->check_lname(name); }); | ||
| 877 | + | ||
| 878 | + if(op_ptr == std::end(options_)) | ||
| 933 | return false; | 879 | return false; |
| 934 | - | 880 | + |
| 935 | // Let's not go crazy with pointer syntax | 881 | // Let's not go crazy with pointer syntax |
| 936 | - Option_p& op = *op_ptr; | 882 | + Option_p &op = *op_ptr; |
| 937 | 883 | ||
| 938 | - | ||
| 939 | if(op->results_.empty()) { | 884 | if(op->results_.empty()) { |
| 940 | // Flag parsing | 885 | // Flag parsing |
| 941 | if(op->get_expected() == 0) { | 886 | if(op->get_expected() == 0) { |
| @@ -949,9 +894,9 @@ protected: | @@ -949,9 +894,9 @@ protected: | ||
| 949 | else | 894 | else |
| 950 | try { | 895 | try { |
| 951 | size_t ui = std::stoul(val); | 896 | size_t ui = std::stoul(val); |
| 952 | - for (size_t i=0; i<ui; i++) | 897 | + for(size_t i = 0; i < ui; i++) |
| 953 | op->results_.emplace_back(""); | 898 | op->results_.emplace_back(""); |
| 954 | - } catch (const std::invalid_argument &) { | 899 | + } catch(const std::invalid_argument &) { |
| 955 | throw ConversionError(current.fullname + ": Should be true/false or a number"); | 900 | throw ConversionError(current.fullname + ": Should be true/false or a number"); |
| 956 | } | 901 | } |
| 957 | } else | 902 | } else |
| @@ -966,7 +911,8 @@ protected: | @@ -966,7 +911,8 @@ protected: | ||
| 966 | return true; | 911 | return true; |
| 967 | } | 912 | } |
| 968 | 913 | ||
| 969 | - /// Parse "one" argument (some may eat more than one), delegate to parent if fails, add to missing if missing from master | 914 | + /// Parse "one" argument (some may eat more than one), delegate to parent if fails, add to missing if missing from |
| 915 | + /// master | ||
| 970 | void _parse_single(std::vector<std::string> &args, bool &positional_only) { | 916 | void _parse_single(std::vector<std::string> &args, bool &positional_only) { |
| 971 | 917 | ||
| 972 | detail::Classifer classifer = positional_only ? detail::Classifer::NONE : _recognize(args.back()); | 918 | detail::Classifer classifer = positional_only ? detail::Classifer::NONE : _recognize(args.back()); |
| @@ -991,20 +937,16 @@ protected: | @@ -991,20 +937,16 @@ protected: | ||
| 991 | // Probably a positional or something for a parent (sub)command | 937 | // Probably a positional or something for a parent (sub)command |
| 992 | _parse_positional(args); | 938 | _parse_positional(args); |
| 993 | } | 939 | } |
| 994 | - | ||
| 995 | - | ||
| 996 | } | 940 | } |
| 997 | 941 | ||
| 998 | /// Parse a positional, go up the tree to check | 942 | /// Parse a positional, go up the tree to check |
| 999 | void _parse_positional(std::vector<std::string> &args) { | 943 | void _parse_positional(std::vector<std::string> &args) { |
| 1000 | 944 | ||
| 1001 | std::string positional = args.back(); | 945 | std::string positional = args.back(); |
| 1002 | - for(const Option_p& opt : options_) { | 946 | + for(const Option_p &opt : options_) { |
| 1003 | // Eat options, one by one, until done | 947 | // Eat options, one by one, until done |
| 1004 | - if ( opt->get_positional() | ||
| 1005 | - && (static_cast<int>( opt->count()) < opt->get_expected() | ||
| 1006 | - || opt->get_expected() < 0) | ||
| 1007 | - ) { | 948 | + if(opt->get_positional() && |
| 949 | + (static_cast<int>(opt->count()) < opt->get_expected() || opt->get_expected() < 0)) { | ||
| 1008 | 950 | ||
| 1009 | opt->add_result(positional); | 951 | opt->add_result(positional); |
| 1010 | args.pop_back(); | 952 | args.pop_back(); |
| @@ -1025,7 +967,7 @@ protected: | @@ -1025,7 +967,7 @@ protected: | ||
| 1025 | /// Unlike the others, this one will always allow fallthrough | 967 | /// Unlike the others, this one will always allow fallthrough |
| 1026 | void _parse_subcommand(std::vector<std::string> &args) { | 968 | void _parse_subcommand(std::vector<std::string> &args) { |
| 1027 | for(const App_p &com : subcommands_) { | 969 | for(const App_p &com : subcommands_) { |
| 1028 | - if(com->check_name(args.back())){ | 970 | + if(com->check_name(args.back())) { |
| 1029 | args.pop_back(); | 971 | args.pop_back(); |
| 1030 | com->_parse(args); | 972 | com->_parse(args); |
| 1031 | return; | 973 | return; |
| @@ -1036,7 +978,7 @@ protected: | @@ -1036,7 +978,7 @@ protected: | ||
| 1036 | else | 978 | else |
| 1037 | throw HorribleError("Subcommand " + args.back() + " missing"); | 979 | throw HorribleError("Subcommand " + args.back() + " missing"); |
| 1038 | } | 980 | } |
| 1039 | - | 981 | + |
| 1040 | /// Parse a short argument, must be at the top of the list | 982 | /// Parse a short argument, must be at the top of the list |
| 1041 | void _parse_short(std::vector<std::string> &args) { | 983 | void _parse_short(std::vector<std::string> &args) { |
| 1042 | std::string current = args.back(); | 984 | std::string current = args.back(); |
| @@ -1046,8 +988,8 @@ protected: | @@ -1046,8 +988,8 @@ protected: | ||
| 1046 | if(!detail::split_short(current, name, rest)) | 988 | if(!detail::split_short(current, name, rest)) |
| 1047 | throw HorribleError("Short"); | 989 | throw HorribleError("Short"); |
| 1048 | 990 | ||
| 1049 | - auto op_ptr = std::find_if(std::begin(options_), std::end(options_), [name](const Option_p &opt){return opt->check_sname(name);}); | ||
| 1050 | - | 991 | + auto op_ptr = std::find_if( |
| 992 | + std::begin(options_), std::end(options_), [name](const Option_p &opt) { return opt->check_sname(name); }); | ||
| 1051 | 993 | ||
| 1052 | // Option not found | 994 | // Option not found |
| 1053 | if(op_ptr == std::end(options_)) { | 995 | if(op_ptr == std::end(options_)) { |
| @@ -1064,33 +1006,32 @@ protected: | @@ -1064,33 +1006,32 @@ protected: | ||
| 1064 | 1006 | ||
| 1065 | args.pop_back(); | 1007 | args.pop_back(); |
| 1066 | 1008 | ||
| 1067 | - | ||
| 1068 | // Get a reference to the pointer to make syntax bearable | 1009 | // Get a reference to the pointer to make syntax bearable |
| 1069 | - Option_p& op = *op_ptr; | 1010 | + Option_p &op = *op_ptr; |
| 1070 | 1011 | ||
| 1071 | int num = op->get_expected(); | 1012 | int num = op->get_expected(); |
| 1072 | - | 1013 | + |
| 1073 | if(num == 0) | 1014 | if(num == 0) |
| 1074 | op->add_result(""); | 1015 | op->add_result(""); |
| 1075 | - else if(rest!="") { | ||
| 1076 | - if (num > 0) | 1016 | + else if(rest != "") { |
| 1017 | + if(num > 0) | ||
| 1077 | num--; | 1018 | num--; |
| 1078 | op->add_result(rest); | 1019 | op->add_result(rest); |
| 1079 | rest = ""; | 1020 | rest = ""; |
| 1080 | } | 1021 | } |
| 1081 | 1022 | ||
| 1082 | - | ||
| 1083 | if(num == -1) { | 1023 | if(num == -1) { |
| 1084 | while(!args.empty() && _recognize(args.back()) == detail::Classifer::NONE) { | 1024 | while(!args.empty() && _recognize(args.back()) == detail::Classifer::NONE) { |
| 1085 | op->add_result(args.back()); | 1025 | op->add_result(args.back()); |
| 1086 | args.pop_back(); | 1026 | args.pop_back(); |
| 1087 | } | 1027 | } |
| 1088 | - } else while(num>0 && !args.empty()) { | ||
| 1089 | - num--; | ||
| 1090 | - std::string current_ = args.back(); | ||
| 1091 | - args.pop_back(); | ||
| 1092 | - op->add_result(current_); | ||
| 1093 | - } | 1028 | + } else |
| 1029 | + while(num > 0 && !args.empty()) { | ||
| 1030 | + num--; | ||
| 1031 | + std::string current_ = args.back(); | ||
| 1032 | + args.pop_back(); | ||
| 1033 | + op->add_result(current_); | ||
| 1034 | + } | ||
| 1094 | 1035 | ||
| 1095 | if(rest != "") { | 1036 | if(rest != "") { |
| 1096 | rest = "-" + rest; | 1037 | rest = "-" + rest; |
| @@ -1107,7 +1048,8 @@ protected: | @@ -1107,7 +1048,8 @@ protected: | ||
| 1107 | if(!detail::split_long(current, name, value)) | 1048 | if(!detail::split_long(current, name, value)) |
| 1108 | throw HorribleError("Long:" + args.back()); | 1049 | throw HorribleError("Long:" + args.back()); |
| 1109 | 1050 | ||
| 1110 | - auto op_ptr = std::find_if(std::begin(options_), std::end(options_), [name](const Option_p &v){return v->check_lname(name);}); | 1051 | + auto op_ptr = std::find_if( |
| 1052 | + std::begin(options_), std::end(options_), [name](const Option_p &v) { return v->check_lname(name); }); | ||
| 1111 | 1053 | ||
| 1112 | // Option not found | 1054 | // Option not found |
| 1113 | if(op_ptr == std::end(options_)) { | 1055 | if(op_ptr == std::end(options_)) { |
| @@ -1125,14 +1067,15 @@ protected: | @@ -1125,14 +1067,15 @@ protected: | ||
| 1125 | args.pop_back(); | 1067 | args.pop_back(); |
| 1126 | 1068 | ||
| 1127 | // Get a reference to the pointer to make syntax bearable | 1069 | // Get a reference to the pointer to make syntax bearable |
| 1128 | - Option_p& op = *op_ptr; | 1070 | + Option_p &op = *op_ptr; |
| 1129 | 1071 | ||
| 1130 | int num = op->get_expected(); | 1072 | int num = op->get_expected(); |
| 1131 | - | 1073 | + |
| 1132 | if(value != "") { | 1074 | if(value != "") { |
| 1133 | - if(num!=-1) num--; | 1075 | + if(num != -1) |
| 1076 | + num--; | ||
| 1134 | op->add_result(value); | 1077 | op->add_result(value); |
| 1135 | - } else if (num == 0) { | 1078 | + } else if(num == 0) { |
| 1136 | op->add_result(""); | 1079 | op->add_result(""); |
| 1137 | } | 1080 | } |
| 1138 | 1081 | ||
| @@ -1141,14 +1084,14 @@ protected: | @@ -1141,14 +1084,14 @@ protected: | ||
| 1141 | op->add_result(args.back()); | 1084 | op->add_result(args.back()); |
| 1142 | args.pop_back(); | 1085 | args.pop_back(); |
| 1143 | } | 1086 | } |
| 1144 | - } else while(num>0 && !args.empty()) { | ||
| 1145 | - num--; | ||
| 1146 | - op->add_result(args.back()); | ||
| 1147 | - args.pop_back(); | ||
| 1148 | - } | 1087 | + } else |
| 1088 | + while(num > 0 && !args.empty()) { | ||
| 1089 | + num--; | ||
| 1090 | + op->add_result(args.back()); | ||
| 1091 | + args.pop_back(); | ||
| 1092 | + } | ||
| 1149 | return; | 1093 | return; |
| 1150 | } | 1094 | } |
| 1151 | - | ||
| 1152 | }; | 1095 | }; |
| 1153 | 1096 | ||
| 1154 | namespace detail { | 1097 | namespace detail { |
| @@ -1156,29 +1099,26 @@ namespace detail { | @@ -1156,29 +1099,26 @@ namespace detail { | ||
| 1156 | struct AppFriend { | 1099 | struct AppFriend { |
| 1157 | 1100 | ||
| 1158 | /// Wrap _parse_short, perfectly forward arguments and return | 1101 | /// Wrap _parse_short, perfectly forward arguments and return |
| 1159 | - template<typename ...Args> | ||
| 1160 | - static auto parse_short(App* app, Args && ...args) | ||
| 1161 | - -> typename std::result_of<decltype(&App::_parse_short)(App, Args...)>::type { | 1102 | + template <typename... Args> |
| 1103 | + static auto parse_short(App *app, Args &&... args) -> | ||
| 1104 | + typename std::result_of<decltype (&App::_parse_short)(App, Args...)>::type { | ||
| 1162 | return app->_parse_short(std::forward<Args>(args)...); | 1105 | return app->_parse_short(std::forward<Args>(args)...); |
| 1163 | } | 1106 | } |
| 1164 | 1107 | ||
| 1165 | - | ||
| 1166 | /// Wrap _parse_long, perfectly forward arguments and return | 1108 | /// Wrap _parse_long, perfectly forward arguments and return |
| 1167 | - template<typename ...Args> | ||
| 1168 | - static auto parse_long(App* app, Args && ...args) | ||
| 1169 | - -> typename std::result_of<decltype(&App::_parse_long)(App, Args...)>::type { | 1109 | + template <typename... Args> |
| 1110 | + static auto parse_long(App *app, Args &&... args) -> | ||
| 1111 | + typename std::result_of<decltype (&App::_parse_long)(App, Args...)>::type { | ||
| 1170 | return app->_parse_long(std::forward<Args>(args)...); | 1112 | return app->_parse_long(std::forward<Args>(args)...); |
| 1171 | } | 1113 | } |
| 1172 | 1114 | ||
| 1173 | /// Wrap _parse_subcommand, perfectly forward arguments and return | 1115 | /// Wrap _parse_subcommand, perfectly forward arguments and return |
| 1174 | - template<typename ...Args> | ||
| 1175 | - static auto parse_subcommand(App* app, Args && ...args) | ||
| 1176 | - -> typename std::result_of<decltype(&App::_parse_subcommand)(App, Args...)>::type { | 1116 | + template <typename... Args> |
| 1117 | + static auto parse_subcommand(App *app, Args &&... args) -> | ||
| 1118 | + typename std::result_of<decltype (&App::_parse_subcommand)(App, Args...)>::type { | ||
| 1177 | return app->_parse_subcommand(std::forward<Args>(args)...); | 1119 | return app->_parse_subcommand(std::forward<Args>(args)...); |
| 1178 | } | 1120 | } |
| 1179 | - | ||
| 1180 | }; | 1121 | }; |
| 1181 | } // namespace detail | 1122 | } // namespace detail |
| 1182 | 1123 | ||
| 1183 | } // namespace CLI | 1124 | } // namespace CLI |
| 1184 | - |
include/CLI/CLI.hpp
include/CLI/Error.hpp
| @@ -9,10 +9,11 @@ | @@ -9,10 +9,11 @@ | ||
| 9 | 9 | ||
| 10 | namespace CLI { | 10 | namespace CLI { |
| 11 | 11 | ||
| 12 | -/// 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(). | 12 | +/// These codes are part of every error in CLI. They can be obtained from e using e.exit_code or as a quick shortcut, |
| 13 | +/// int values from e.get_error_code(). | ||
| 13 | enum class ExitCodes { | 14 | enum class ExitCodes { |
| 14 | Success = 0, | 15 | Success = 0, |
| 15 | - IncorrectConstruction=100, | 16 | + IncorrectConstruction = 100, |
| 16 | BadNameString, | 17 | BadNameString, |
| 17 | OptionAlreadyAdded, | 18 | OptionAlreadyAdded, |
| 18 | File, | 19 | File, |
| @@ -41,17 +42,23 @@ enum class ExitCodes { | @@ -41,17 +42,23 @@ enum class ExitCodes { | ||
| 41 | struct Error : public std::runtime_error { | 42 | struct Error : public std::runtime_error { |
| 42 | int exit_code; | 43 | int exit_code; |
| 43 | bool print_help; | 44 | bool print_help; |
| 44 | - int get_exit_code() const {return exit_code;} | ||
| 45 | - Error(std::string parent, std::string name, ExitCodes exit_code=ExitCodes::BaseClass, bool print_help=true) | 45 | + int get_exit_code() const { return exit_code; } |
| 46 | + Error(std::string parent, std::string name, ExitCodes exit_code = ExitCodes::BaseClass, bool print_help = true) | ||
| 46 | : runtime_error(parent + ": " + name), exit_code(static_cast<int>(exit_code)), print_help(print_help) {} | 47 | : runtime_error(parent + ": " + name), exit_code(static_cast<int>(exit_code)), print_help(print_help) {} |
| 47 | - Error(std::string parent, std::string name, int exit_code=static_cast<int>(ExitCodes::BaseClass), bool print_help=true) | 48 | + Error(std::string parent, |
| 49 | + std::string name, | ||
| 50 | + int exit_code = static_cast<int>(ExitCodes::BaseClass), | ||
| 51 | + bool print_help = true) | ||
| 48 | : runtime_error(parent + ": " + name), exit_code(exit_code), print_help(print_help) {} | 52 | : runtime_error(parent + ": " + name), exit_code(exit_code), print_help(print_help) {} |
| 49 | }; | 53 | }; |
| 50 | 54 | ||
| 51 | /// Construction errors (not in parsing) | 55 | /// Construction errors (not in parsing) |
| 52 | struct ConstructionError : public Error { | 56 | struct ConstructionError : public Error { |
| 53 | // Using Error::Error constructors seem to not work on GCC 4.7 | 57 | // Using Error::Error constructors seem to not work on GCC 4.7 |
| 54 | - ConstructionError(std::string parent, std::string name, ExitCodes exit_code=ExitCodes::BaseClass, bool print_help=true) | 58 | + ConstructionError(std::string parent, |
| 59 | + std::string name, | ||
| 60 | + ExitCodes exit_code = ExitCodes::BaseClass, | ||
| 61 | + bool print_help = true) | ||
| 55 | : Error(parent, name, exit_code, print_help) {} | 62 | : Error(parent, name, exit_code, print_help) {} |
| 56 | }; | 63 | }; |
| 57 | 64 | ||
| @@ -63,8 +70,7 @@ struct IncorrectConstruction : public ConstructionError { | @@ -63,8 +70,7 @@ struct IncorrectConstruction : public ConstructionError { | ||
| 63 | 70 | ||
| 64 | /// Thrown on construction of a bad name | 71 | /// Thrown on construction of a bad name |
| 65 | struct BadNameString : public ConstructionError { | 72 | struct BadNameString : public ConstructionError { |
| 66 | - BadNameString(std::string name) | ||
| 67 | - : ConstructionError("BadNameString", name, ExitCodes::BadNameString) {} | 73 | + BadNameString(std::string name) : ConstructionError("BadNameString", name, ExitCodes::BadNameString) {} |
| 68 | }; | 74 | }; |
| 69 | 75 | ||
| 70 | /// Thrown when an option already exists | 76 | /// Thrown when an option already exists |
| @@ -77,7 +83,7 @@ struct OptionAlreadyAdded : public ConstructionError { | @@ -77,7 +83,7 @@ struct OptionAlreadyAdded : public ConstructionError { | ||
| 77 | 83 | ||
| 78 | /// Anything that can error in Parse | 84 | /// Anything that can error in Parse |
| 79 | struct ParseError : public Error { | 85 | struct ParseError : public Error { |
| 80 | - ParseError(std::string parent, std::string name, ExitCodes exit_code=ExitCodes::BaseClass, bool print_help=true) | 86 | + ParseError(std::string parent, std::string name, ExitCodes exit_code = ExitCodes::BaseClass, bool print_help = true) |
| 81 | : Error(parent, name, exit_code, print_help) {} | 87 | : Error(parent, name, exit_code, print_help) {} |
| 82 | }; | 88 | }; |
| 83 | 89 | ||
| @@ -85,8 +91,7 @@ struct ParseError : public Error { | @@ -85,8 +91,7 @@ struct ParseError : public Error { | ||
| 85 | 91 | ||
| 86 | /// This is a successful completion on parsing, supposed to exit | 92 | /// This is a successful completion on parsing, supposed to exit |
| 87 | struct Success : public ParseError { | 93 | struct Success : public ParseError { |
| 88 | - Success() | ||
| 89 | - : ParseError("Success", "Successfully completed, should be caught and quit", ExitCodes::Success, false) {} | 94 | + Success() : ParseError("Success", "Successfully completed, should be caught and quit", ExitCodes::Success, false) {} |
| 90 | }; | 95 | }; |
| 91 | 96 | ||
| 92 | /// -h or --help on command line | 97 | /// -h or --help on command line |
| @@ -95,29 +100,24 @@ struct CallForHelp : public ParseError { | @@ -95,29 +100,24 @@ struct CallForHelp : public ParseError { | ||
| 95 | : ParseError("CallForHelp", "This should be caught in your main function, see examples", ExitCodes::Success) {} | 100 | : ParseError("CallForHelp", "This should be caught in your main function, see examples", ExitCodes::Success) {} |
| 96 | }; | 101 | }; |
| 97 | 102 | ||
| 98 | - | ||
| 99 | /// Thrown when parsing an INI file and it is missing | 103 | /// Thrown when parsing an INI file and it is missing |
| 100 | struct FileError : public ParseError { | 104 | struct FileError : public ParseError { |
| 101 | - FileError (std::string name) | ||
| 102 | - : ParseError("FileError", name, ExitCodes::File) {} | 105 | + FileError(std::string name) : ParseError("FileError", name, ExitCodes::File) {} |
| 103 | }; | 106 | }; |
| 104 | 107 | ||
| 105 | /// Thrown when conversion call back fails, such as when an int fails to coerse to a string | 108 | /// Thrown when conversion call back fails, such as when an int fails to coerse to a string |
| 106 | struct ConversionError : public ParseError { | 109 | struct ConversionError : public ParseError { |
| 107 | - ConversionError(std::string name) | ||
| 108 | - : ParseError("ConversionError", name, ExitCodes::Conversion) {} | 110 | + ConversionError(std::string name) : ParseError("ConversionError", name, ExitCodes::Conversion) {} |
| 109 | }; | 111 | }; |
| 110 | 112 | ||
| 111 | /// Thrown when validation of results fails | 113 | /// Thrown when validation of results fails |
| 112 | struct ValidationError : public ParseError { | 114 | struct ValidationError : public ParseError { |
| 113 | - ValidationError(std::string name) | ||
| 114 | - : ParseError("ValidationError", name, ExitCodes::Validation) {} | 115 | + ValidationError(std::string name) : ParseError("ValidationError", name, ExitCodes::Validation) {} |
| 115 | }; | 116 | }; |
| 116 | 117 | ||
| 117 | /// Thrown when a required option is missing | 118 | /// Thrown when a required option is missing |
| 118 | struct RequiredError : public ParseError { | 119 | struct RequiredError : public ParseError { |
| 119 | - RequiredError(std::string name) | ||
| 120 | - : ParseError("RequiredError", name, ExitCodes::Required) {} | 120 | + RequiredError(std::string name) : ParseError("RequiredError", name, ExitCodes::Required) {} |
| 121 | }; | 121 | }; |
| 122 | 122 | ||
| 123 | /// Thrown when a requires option is missing | 123 | /// Thrown when a requires option is missing |
| @@ -134,20 +134,17 @@ struct ExcludesError : public ParseError { | @@ -134,20 +134,17 @@ struct ExcludesError : public ParseError { | ||
| 134 | 134 | ||
| 135 | /// Thrown when too many positionals or options are found | 135 | /// Thrown when too many positionals or options are found |
| 136 | struct ExtrasError : public ParseError { | 136 | struct ExtrasError : public ParseError { |
| 137 | - ExtrasError(std::string name) | ||
| 138 | - : ParseError("ExtrasError", name, ExitCodes::Extras) {} | 137 | + ExtrasError(std::string name) : ParseError("ExtrasError", name, ExitCodes::Extras) {} |
| 139 | }; | 138 | }; |
| 140 | 139 | ||
| 141 | /// Thrown when extra values are found in an INI file | 140 | /// Thrown when extra values are found in an INI file |
| 142 | struct ExtrasINIError : public ParseError { | 141 | struct ExtrasINIError : public ParseError { |
| 143 | - ExtrasINIError(std::string name) | ||
| 144 | - : ParseError("ExtrasINIError", name, ExitCodes::ExtrasINI) {} | 142 | + ExtrasINIError(std::string name) : ParseError("ExtrasINIError", name, ExitCodes::ExtrasINI) {} |
| 145 | }; | 143 | }; |
| 146 | 144 | ||
| 147 | /// Thrown when validation fails before parsing | 145 | /// Thrown when validation fails before parsing |
| 148 | struct InvalidError : public ParseError { | 146 | struct InvalidError : public ParseError { |
| 149 | - InvalidError(std::string name) | ||
| 150 | - : ParseError("InvalidError", name, ExitCodes::Invalid) {} | 147 | + InvalidError(std::string name) : ParseError("InvalidError", name, ExitCodes::Invalid) {} |
| 151 | }; | 148 | }; |
| 152 | 149 | ||
| 153 | /// This is just a safety check to verify selection and parsing match | 150 | /// This is just a safety check to verify selection and parsing match |
| @@ -160,8 +157,7 @@ struct HorribleError : public ParseError { | @@ -160,8 +157,7 @@ struct HorribleError : public ParseError { | ||
| 160 | 157 | ||
| 161 | /// Thrown when counting a non-existent option | 158 | /// Thrown when counting a non-existent option |
| 162 | struct OptionNotFound : public Error { | 159 | struct OptionNotFound : public Error { |
| 163 | - OptionNotFound(std::string name) | ||
| 164 | - : Error("OptionNotFound", name, ExitCodes::OptionNotFound) {} | 160 | + OptionNotFound(std::string name) : Error("OptionNotFound", name, ExitCodes::OptionNotFound) {} |
| 165 | }; | 161 | }; |
| 166 | 162 | ||
| 167 | /// @} | 163 | /// @} |
include/CLI/Ini.hpp
| @@ -10,18 +10,17 @@ | @@ -10,18 +10,17 @@ | ||
| 10 | 10 | ||
| 11 | #include "CLI/StringTools.hpp" | 11 | #include "CLI/StringTools.hpp" |
| 12 | 12 | ||
| 13 | - | ||
| 14 | namespace CLI { | 13 | namespace CLI { |
| 15 | namespace detail { | 14 | namespace detail { |
| 16 | 15 | ||
| 17 | inline std::string inijoin(std::vector<std::string> args) { | 16 | inline std::string inijoin(std::vector<std::string> args) { |
| 18 | std::ostringstream s; | 17 | std::ostringstream s; |
| 19 | size_t start = 0; | 18 | size_t start = 0; |
| 20 | - for (const auto& arg : args) { | 19 | + for(const auto &arg : args) { |
| 21 | if(start++ > 0) | 20 | if(start++ > 0) |
| 22 | s << " "; | 21 | s << " "; |
| 23 | 22 | ||
| 24 | - auto it = std::find_if(arg.begin(), arg.end(), [](char ch){ return std::isspace<char>(ch , std::locale());}); | 23 | + auto it = std::find_if(arg.begin(), arg.end(), [](char ch) { return std::isspace<char>(ch, std::locale()); }); |
| 25 | if(it == arg.end()) | 24 | if(it == arg.end()) |
| 26 | s << arg; | 25 | s << arg; |
| 27 | else if(arg.find(R"(")") == std::string::npos) | 26 | else if(arg.find(R"(")") == std::string::npos) |
| @@ -47,18 +46,18 @@ struct ini_ret_t { | @@ -47,18 +46,18 @@ struct ini_ret_t { | ||
| 47 | /// | 46 | /// |
| 48 | /// Level 0, a.b.c would return a | 47 | /// Level 0, a.b.c would return a |
| 49 | /// Level 1, a.b.c could return b | 48 | /// Level 1, a.b.c could return b |
| 50 | - std::string parent () const { | 49 | + std::string parent() const { |
| 51 | std::vector<std::string> plist = detail::split(fullname, '.'); | 50 | std::vector<std::string> plist = detail::split(fullname, '.'); |
| 52 | - if(plist.size() > (level+1)) | 51 | + if(plist.size() > (level + 1)) |
| 53 | return plist[level]; | 52 | return plist[level]; |
| 54 | else | 53 | else |
| 55 | return ""; | 54 | return ""; |
| 56 | } | 55 | } |
| 57 | 56 | ||
| 58 | /// Return name | 57 | /// Return name |
| 59 | - std::string name () const { | 58 | + std::string name() const { |
| 60 | std::vector<std::string> plist = detail::split(fullname, '.'); | 59 | std::vector<std::string> plist = detail::split(fullname, '.'); |
| 61 | - return plist.at(plist.size()-1); | 60 | + return plist.at(plist.size() - 1); |
| 62 | } | 61 | } |
| 63 | }; | 62 | }; |
| 64 | 63 | ||
| @@ -74,17 +73,17 @@ inline std::vector<ini_ret_t> parse_ini(std::istream &input) { | @@ -74,17 +73,17 @@ inline std::vector<ini_ret_t> parse_ini(std::istream &input) { | ||
| 74 | 73 | ||
| 75 | detail::trim(line); | 74 | detail::trim(line); |
| 76 | size_t len = line.length(); | 75 | size_t len = line.length(); |
| 77 | - if(len > 1 && line[0] == '[' && line[len-1] == ']') { | ||
| 78 | - section = line.substr(1,len-2); | ||
| 79 | - } else if (len > 0 && line[0] != ';') { | 76 | + if(len > 1 && line[0] == '[' && line[len - 1] == ']') { |
| 77 | + section = line.substr(1, len - 2); | ||
| 78 | + } else if(len > 0 && line[0] != ';') { | ||
| 80 | output.emplace_back(); | 79 | output.emplace_back(); |
| 81 | - ini_ret_t& out = output.back(); | 80 | + ini_ret_t &out = output.back(); |
| 82 | 81 | ||
| 83 | // Find = in string, split and recombine | 82 | // Find = in string, split and recombine |
| 84 | auto pos = line.find("="); | 83 | auto pos = line.find("="); |
| 85 | if(pos != std::string::npos) { | 84 | if(pos != std::string::npos) { |
| 86 | - name = detail::trim_copy(line.substr(0,pos)); | ||
| 87 | - std::string item = detail::trim_copy(line.substr(pos+1)); | 85 | + name = detail::trim_copy(line.substr(0, pos)); |
| 86 | + std::string item = detail::trim_copy(line.substr(pos + 1)); | ||
| 88 | items = detail::split_up(item); | 87 | items = detail::split_up(item); |
| 89 | } else { | 88 | } else { |
| 90 | name = detail::trim_copy(line); | 89 | name = detail::trim_copy(line); |
| @@ -112,6 +111,5 @@ inline std::vector<ini_ret_t> parse_ini(const std::string &name) { | @@ -112,6 +111,5 @@ inline std::vector<ini_ret_t> parse_ini(const std::string &name) { | ||
| 112 | return parse_ini(input); | 111 | return parse_ini(input); |
| 113 | } | 112 | } |
| 114 | 113 | ||
| 115 | - | ||
| 116 | } // namespace detail | 114 | } // namespace detail |
| 117 | } // namespace CLI | 115 | } // namespace CLI |
include/CLI/Option.hpp
| @@ -19,17 +19,17 @@ | @@ -19,17 +19,17 @@ | ||
| 19 | namespace CLI { | 19 | namespace CLI { |
| 20 | 20 | ||
| 21 | using results_t = std::vector<std::string>; | 21 | using results_t = std::vector<std::string>; |
| 22 | -using callback_t = std::function<bool (results_t)>; | 22 | +using callback_t = std::function<bool(results_t)>; |
| 23 | 23 | ||
| 24 | class Option; | 24 | class Option; |
| 25 | class App; | 25 | class App; |
| 26 | 26 | ||
| 27 | using Option_p = std::unique_ptr<Option>; | 27 | using Option_p = std::unique_ptr<Option>; |
| 28 | 28 | ||
| 29 | - | ||
| 30 | class Option { | 29 | class Option { |
| 31 | friend App; | 30 | friend App; |
| 32 | -protected: | 31 | + |
| 32 | + protected: | ||
| 33 | /// @name Names | 33 | /// @name Names |
| 34 | ///@{ | 34 | ///@{ |
| 35 | 35 | ||
| @@ -48,7 +48,7 @@ protected: | @@ -48,7 +48,7 @@ protected: | ||
| 48 | ///@} | 48 | ///@} |
| 49 | /// @name Help | 49 | /// @name Help |
| 50 | ///@{ | 50 | ///@{ |
| 51 | - | 51 | + |
| 52 | /// The description for help strings | 52 | /// The description for help strings |
| 53 | std::string description_; | 53 | std::string description_; |
| 54 | 54 | ||
| @@ -59,47 +59,46 @@ protected: | @@ -59,47 +59,46 @@ protected: | ||
| 59 | std::string typeval_; | 59 | std::string typeval_; |
| 60 | 60 | ||
| 61 | /// The group membership | 61 | /// The group membership |
| 62 | - std::string group_ {"Options"}; | 62 | + std::string group_{"Options"}; |
| 63 | 63 | ||
| 64 | /// True if this option has a default | 64 | /// True if this option has a default |
| 65 | - bool default_ {false}; | 65 | + bool default_{false}; |
| 66 | 66 | ||
| 67 | ///@} | 67 | ///@} |
| 68 | /// @name Configuration | 68 | /// @name Configuration |
| 69 | ///@{ | 69 | ///@{ |
| 70 | - | 70 | + |
| 71 | /// True if this is a required option | 71 | /// True if this is a required option |
| 72 | - bool required_ {false}; | 72 | + bool required_{false}; |
| 73 | 73 | ||
| 74 | /// The number of expected values, 0 for flag, -1 for unlimited vector | 74 | /// The number of expected values, 0 for flag, -1 for unlimited vector |
| 75 | - int expected_ {1}; | 75 | + int expected_{1}; |
| 76 | 76 | ||
| 77 | /// A private setting to allow args to not be able to accept incorrect expected values | 77 | /// A private setting to allow args to not be able to accept incorrect expected values |
| 78 | - bool changeable_ {false}; | ||
| 79 | - | 78 | + bool changeable_{false}; |
| 79 | + | ||
| 80 | /// Ignore the case when matching (option, not value) | 80 | /// Ignore the case when matching (option, not value) |
| 81 | - bool ignore_case_ {false}; | 81 | + bool ignore_case_{false}; |
| 82 | 82 | ||
| 83 | /// A list of validators to run on each value parsed | 83 | /// A list of validators to run on each value parsed |
| 84 | std::vector<std::function<bool(std::string)>> validators_; | 84 | std::vector<std::function<bool(std::string)>> validators_; |
| 85 | 85 | ||
| 86 | /// A list of options that are required with this option | 86 | /// A list of options that are required with this option |
| 87 | - std::set<Option*> requires_; | 87 | + std::set<Option *> requires_; |
| 88 | 88 | ||
| 89 | /// A list of options that are excluded with this option | 89 | /// A list of options that are excluded with this option |
| 90 | - std::set<Option*> excludes_; | 90 | + std::set<Option *> excludes_; |
| 91 | 91 | ||
| 92 | ///@} | 92 | ///@} |
| 93 | /// @name Other | 93 | /// @name Other |
| 94 | ///@{ | 94 | ///@{ |
| 95 | 95 | ||
| 96 | /// Remember the parent app | 96 | /// Remember the parent app |
| 97 | - App* parent_; | 97 | + App *parent_; |
| 98 | 98 | ||
| 99 | /// Options store a callback to do all the work | 99 | /// Options store a callback to do all the work |
| 100 | callback_t callback_; | 100 | callback_t callback_; |
| 101 | 101 | ||
| 102 | - | ||
| 103 | ///@} | 102 | ///@} |
| 104 | /// @name Parsing results | 103 | /// @name Parsing results |
| 105 | ///@{ | 104 | ///@{ |
| @@ -108,54 +107,48 @@ protected: | @@ -108,54 +107,48 @@ protected: | ||
| 108 | results_t results_; | 107 | results_t results_; |
| 109 | 108 | ||
| 110 | /// Whether the callback has run (needed for INI parsing) | 109 | /// Whether the callback has run (needed for INI parsing) |
| 111 | - bool callback_run_ {false}; | 110 | + bool callback_run_{false}; |
| 112 | 111 | ||
| 113 | ///@} | 112 | ///@} |
| 114 | 113 | ||
| 115 | /// Making an option by hand is not defined, it must be made by the App class | 114 | /// Making an option by hand is not defined, it must be made by the App class |
| 116 | - Option(std::string name, std::string description = "", std::function<bool(results_t)> callback=[](results_t){return true;}, bool default_=true, App* parent = nullptr) : | ||
| 117 | - description_(std::move(description)), default_(default_), parent_(parent), callback_(std::move(callback)) { | 115 | + Option(std::string name, |
| 116 | + std::string description = "", | ||
| 117 | + std::function<bool(results_t)> callback = [](results_t) { return true; }, | ||
| 118 | + bool default_ = true, | ||
| 119 | + App *parent = nullptr) | ||
| 120 | + : description_(std::move(description)), default_(default_), parent_(parent), callback_(std::move(callback)) { | ||
| 118 | std::tie(snames_, lnames_, pname_) = detail::get_names(detail::split_names(name)); | 121 | std::tie(snames_, lnames_, pname_) = detail::get_names(detail::split_names(name)); |
| 119 | } | 122 | } |
| 120 | 123 | ||
| 121 | -public: | ||
| 122 | - | 124 | + public: |
| 123 | /// @name Basic | 125 | /// @name Basic |
| 124 | ///@{ | 126 | ///@{ |
| 125 | 127 | ||
| 126 | /// Count the total number of times an option was passed | 128 | /// Count the total number of times an option was passed |
| 127 | - size_t count() const { | ||
| 128 | - return results_.size(); | ||
| 129 | - } | 129 | + size_t count() const { return results_.size(); } |
| 130 | 130 | ||
| 131 | - | ||
| 132 | /// This class is true if option is passed. | 131 | /// This class is true if option is passed. |
| 133 | - operator bool() const { | ||
| 134 | - return count() > 0; | ||
| 135 | - } | 132 | + operator bool() const { return count() > 0; } |
| 136 | 133 | ||
| 137 | /// Clear the parsed results (mostly for testing) | 134 | /// Clear the parsed results (mostly for testing) |
| 138 | - void clear() { | ||
| 139 | - results_.clear(); | ||
| 140 | - } | 135 | + void clear() { results_.clear(); } |
| 141 | 136 | ||
| 142 | ///@} | 137 | ///@} |
| 143 | /// @name Setting options | 138 | /// @name Setting options |
| 144 | ///@{ | 139 | ///@{ |
| 145 | 140 | ||
| 146 | /// Set the option as required | 141 | /// Set the option as required |
| 147 | - Option* required(bool value = true) { | 142 | + Option *required(bool value = true) { |
| 148 | required_ = value; | 143 | required_ = value; |
| 149 | return this; | 144 | return this; |
| 150 | } | 145 | } |
| 151 | 146 | ||
| 152 | /// Support Plubmum term | 147 | /// Support Plubmum term |
| 153 | - Option* mandatory(bool value = true) { | ||
| 154 | - return required(value); | ||
| 155 | - } | 148 | + Option *mandatory(bool value = true) { return required(value); } |
| 156 | 149 | ||
| 157 | /// Set the number of expected arguments (Flags bypass this) | 150 | /// Set the number of expected arguments (Flags bypass this) |
| 158 | - Option* expected(int value) { | 151 | + Option *expected(int value) { |
| 159 | if(value == 0) | 152 | if(value == 0) |
| 160 | throw IncorrectConstruction("Cannot set 0 expected, use a flag instead"); | 153 | throw IncorrectConstruction("Cannot set 0 expected, use a flag instead"); |
| 161 | else if(expected_ == 0) | 154 | else if(expected_ == 0) |
| @@ -167,21 +160,20 @@ public: | @@ -167,21 +160,20 @@ public: | ||
| 167 | } | 160 | } |
| 168 | 161 | ||
| 169 | /// Adds a validator | 162 | /// Adds a validator |
| 170 | - Option* check(std::function<bool(std::string)> validator) { | 163 | + Option *check(std::function<bool(std::string)> validator) { |
| 171 | 164 | ||
| 172 | validators_.push_back(validator); | 165 | validators_.push_back(validator); |
| 173 | return this; | 166 | return this; |
| 174 | } | 167 | } |
| 175 | 168 | ||
| 176 | /// Changes the group membership | 169 | /// Changes the group membership |
| 177 | - Option* group(std::string name) { | 170 | + Option *group(std::string name) { |
| 178 | group_ = name; | 171 | group_ = name; |
| 179 | return this; | 172 | return this; |
| 180 | } | 173 | } |
| 181 | 174 | ||
| 182 | - | ||
| 183 | /// Sets required options | 175 | /// Sets required options |
| 184 | - Option* requires(Option* opt) { | 176 | + Option *requires(Option *opt) { |
| 185 | auto tup = requires_.insert(opt); | 177 | auto tup = requires_.insert(opt); |
| 186 | if(!tup.second) | 178 | if(!tup.second) |
| 187 | throw OptionAlreadyAdded(get_name() + " requires " + opt->get_name()); | 179 | throw OptionAlreadyAdded(get_name() + " requires " + opt->get_name()); |
| @@ -189,24 +181,21 @@ public: | @@ -189,24 +181,21 @@ public: | ||
| 189 | } | 181 | } |
| 190 | 182 | ||
| 191 | /// Can find a string if needed | 183 | /// Can find a string if needed |
| 192 | - template<typename T=App> | ||
| 193 | - Option* requires(std::string opt_name) { | ||
| 194 | - for(const Option_p& opt : dynamic_cast<T*>(parent_)->options_) | 184 | + template <typename T = App> Option *requires(std::string opt_name) { |
| 185 | + for(const Option_p &opt : dynamic_cast<T *>(parent_)->options_) | ||
| 195 | if(opt.get() != this && opt->check_name(opt_name)) | 186 | if(opt.get() != this && opt->check_name(opt_name)) |
| 196 | return requires(opt.get()); | 187 | return requires(opt.get()); |
| 197 | throw IncorrectConstruction("Option " + opt_name + " is not defined"); | 188 | throw IncorrectConstruction("Option " + opt_name + " is not defined"); |
| 198 | - | ||
| 199 | } | 189 | } |
| 200 | 190 | ||
| 201 | /// Any number supported, any mix of string and Opt | 191 | /// Any number supported, any mix of string and Opt |
| 202 | - template<typename A, typename B, typename... ARG> | ||
| 203 | - Option* requires(A opt, B opt1, ARG... args) { | 192 | + template <typename A, typename B, typename... ARG> Option *requires(A opt, B opt1, ARG... args) { |
| 204 | requires(opt); | 193 | requires(opt); |
| 205 | return requires(opt1, args...); | 194 | return requires(opt1, args...); |
| 206 | } | 195 | } |
| 207 | 196 | ||
| 208 | /// Sets excluded options | 197 | /// Sets excluded options |
| 209 | - Option* excludes(Option* opt) { | 198 | + Option *excludes(Option *opt) { |
| 210 | auto tup = excludes_.insert(opt); | 199 | auto tup = excludes_.insert(opt); |
| 211 | if(!tup.second) | 200 | if(!tup.second) |
| 212 | throw OptionAlreadyAdded(get_name() + " excludes " + opt->get_name()); | 201 | throw OptionAlreadyAdded(get_name() + " excludes " + opt->get_name()); |
| @@ -214,23 +203,20 @@ public: | @@ -214,23 +203,20 @@ public: | ||
| 214 | } | 203 | } |
| 215 | 204 | ||
| 216 | /// Can find a string if needed | 205 | /// Can find a string if needed |
| 217 | - template<typename T=App> | ||
| 218 | - Option* excludes(std::string opt_name) { | ||
| 219 | - for(const Option_p& opt : dynamic_cast<T*>(parent_)->options_) | 206 | + template <typename T = App> Option *excludes(std::string opt_name) { |
| 207 | + for(const Option_p &opt : dynamic_cast<T *>(parent_)->options_) | ||
| 220 | if(opt.get() != this && opt->check_name(opt_name)) | 208 | if(opt.get() != this && opt->check_name(opt_name)) |
| 221 | return excludes(opt.get()); | 209 | return excludes(opt.get()); |
| 222 | throw IncorrectConstruction("Option " + opt_name + " is not defined"); | 210 | throw IncorrectConstruction("Option " + opt_name + " is not defined"); |
| 223 | - | ||
| 224 | } | 211 | } |
| 225 | /// Any number supported, any mix of string and Opt | 212 | /// Any number supported, any mix of string and Opt |
| 226 | - template<typename A, typename B, typename... ARG> | ||
| 227 | - Option* excludes(A opt, B opt1, ARG... args) { | 213 | + template <typename A, typename B, typename... ARG> Option *excludes(A opt, B opt1, ARG... args) { |
| 228 | excludes(opt); | 214 | excludes(opt); |
| 229 | return excludes(opt1, args...); | 215 | return excludes(opt1, args...); |
| 230 | } | 216 | } |
| 231 | 217 | ||
| 232 | /// Sets environment variable to read if no option given | 218 | /// Sets environment variable to read if no option given |
| 233 | - Option* envname(std::string name) { | 219 | + Option *envname(std::string name) { |
| 234 | envname_ = name; | 220 | envname_ = name; |
| 235 | return this; | 221 | return this; |
| 236 | } | 222 | } |
| @@ -239,10 +225,9 @@ public: | @@ -239,10 +225,9 @@ public: | ||
| 239 | /// | 225 | /// |
| 240 | /// The template hides the fact that we don't have the definition of App yet. | 226 | /// The template hides the fact that we don't have the definition of App yet. |
| 241 | /// You are never expected to add an argument to the template here. | 227 | /// You are never expected to add an argument to the template here. |
| 242 | - template<typename T=App> | ||
| 243 | - Option* ignore_case(bool value = true) { | 228 | + template <typename T = App> Option *ignore_case(bool value = true) { |
| 244 | ignore_case_ = value; | 229 | ignore_case_ = value; |
| 245 | - for(const Option_p& opt : dynamic_cast<T*>(parent_)->options_) | 230 | + for(const Option_p &opt : dynamic_cast<T *>(parent_)->options_) |
| 246 | if(opt.get() != this && *opt == *this) | 231 | if(opt.get() != this && *opt == *this) |
| 247 | throw OptionAlreadyAdded(opt->get_name()); | 232 | throw OptionAlreadyAdded(opt->get_name()); |
| 248 | return this; | 233 | return this; |
| @@ -251,76 +236,58 @@ public: | @@ -251,76 +236,58 @@ public: | ||
| 251 | ///@} | 236 | ///@} |
| 252 | /// @name Accessors | 237 | /// @name Accessors |
| 253 | ///@{ | 238 | ///@{ |
| 254 | - | 239 | + |
| 255 | /// True if this is a required option | 240 | /// True if this is a required option |
| 256 | - bool get_required() const { | ||
| 257 | - return required_; | ||
| 258 | - } | 241 | + bool get_required() const { return required_; } |
| 259 | 242 | ||
| 260 | /// The number of arguments the option expects | 243 | /// The number of arguments the option expects |
| 261 | - int get_expected() const { | ||
| 262 | - return expected_; | ||
| 263 | - } | 244 | + int get_expected() const { return expected_; } |
| 264 | 245 | ||
| 265 | /// True if this has a default value | 246 | /// True if this has a default value |
| 266 | - int get_default() const { | ||
| 267 | - return default_; | ||
| 268 | - } | 247 | + int get_default() const { return default_; } |
| 269 | 248 | ||
| 270 | /// True if the argument can be given directly | 249 | /// True if the argument can be given directly |
| 271 | - bool get_positional() const { | ||
| 272 | - return pname_.length() > 0; | ||
| 273 | - } | 250 | + bool get_positional() const { return pname_.length() > 0; } |
| 274 | 251 | ||
| 275 | /// True if option has at least one non-positional name | 252 | /// True if option has at least one non-positional name |
| 276 | - bool nonpositional() const { | ||
| 277 | - return (snames_.size() + lnames_.size()) > 0; | ||
| 278 | - } | 253 | + bool nonpositional() const { return (snames_.size() + lnames_.size()) > 0; } |
| 279 | 254 | ||
| 280 | /// True if option has description | 255 | /// True if option has description |
| 281 | - bool has_description() const { | ||
| 282 | - return description_.length() > 0; | ||
| 283 | - } | 256 | + bool has_description() const { return description_.length() > 0; } |
| 284 | 257 | ||
| 285 | /// Get the group of this option | 258 | /// Get the group of this option |
| 286 | - const std::string& get_group() const { | ||
| 287 | - return group_; | ||
| 288 | - } | 259 | + const std::string &get_group() const { return group_; } |
| 289 | 260 | ||
| 290 | /// Get the description | 261 | /// Get the description |
| 291 | - const std::string& get_description() const { | ||
| 292 | - return description_; | ||
| 293 | - } | 262 | + const std::string &get_description() const { return description_; } |
| 294 | 263 | ||
| 295 | // Just the pname | 264 | // Just the pname |
| 296 | - std::string get_pname() const { | ||
| 297 | - return pname_; | ||
| 298 | - } | ||
| 299 | - | 265 | + std::string get_pname() const { return pname_; } |
| 266 | + | ||
| 300 | ///@} | 267 | ///@} |
| 301 | /// @name Help tools | 268 | /// @name Help tools |
| 302 | ///@{ | 269 | ///@{ |
| 303 | 270 | ||
| 304 | /// Gets a , sep list of names. Does not include the positional name if opt_only=true. | 271 | /// Gets a , sep list of names. Does not include the positional name if opt_only=true. |
| 305 | - std::string get_name(bool opt_only=false) const { | 272 | + std::string get_name(bool opt_only = false) const { |
| 306 | std::vector<std::string> name_list; | 273 | std::vector<std::string> name_list; |
| 307 | if(!opt_only && pname_.length() > 0) | 274 | if(!opt_only && pname_.length() > 0) |
| 308 | name_list.push_back(pname_); | 275 | name_list.push_back(pname_); |
| 309 | - for(const std::string& sname : snames_) | ||
| 310 | - name_list.push_back("-"+sname); | ||
| 311 | - for(const std::string& lname : lnames_) | ||
| 312 | - name_list.push_back("--"+lname); | 276 | + for(const std::string &sname : snames_) |
| 277 | + name_list.push_back("-" + sname); | ||
| 278 | + for(const std::string &lname : lnames_) | ||
| 279 | + name_list.push_back("--" + lname); | ||
| 313 | return detail::join(name_list); | 280 | return detail::join(name_list); |
| 314 | } | 281 | } |
| 315 | 282 | ||
| 316 | /// The name and any extras needed for positionals | 283 | /// The name and any extras needed for positionals |
| 317 | std::string help_positional() const { | 284 | std::string help_positional() const { |
| 318 | std::string out = pname_; | 285 | std::string out = pname_; |
| 319 | - if(get_expected()>1) | 286 | + if(get_expected() > 1) |
| 320 | out = out + "(" + std::to_string(get_expected()) + "x)"; | 287 | out = out + "(" + std::to_string(get_expected()) + "x)"; |
| 321 | - else if(get_expected()==-1) | 288 | + else if(get_expected() == -1) |
| 322 | out = out + "..."; | 289 | out = out + "..."; |
| 323 | - out = get_required() ? out : "["+out+"]"; | 290 | + out = get_required() ? out : "[" + out + "]"; |
| 324 | return out; | 291 | return out; |
| 325 | } | 292 | } |
| 326 | 293 | ||
| @@ -330,7 +297,7 @@ public: | @@ -330,7 +297,7 @@ public: | ||
| 330 | out << get_name(true) << help_aftername(); | 297 | out << get_name(true) << help_aftername(); |
| 331 | return out.str(); | 298 | return out.str(); |
| 332 | } | 299 | } |
| 333 | - | 300 | + |
| 334 | /// pname with type info | 301 | /// pname with type info |
| 335 | std::string help_pname() const { | 302 | std::string help_pname() const { |
| 336 | std::stringstream out; | 303 | std::stringstream out; |
| @@ -346,7 +313,7 @@ public: | @@ -346,7 +313,7 @@ public: | ||
| 346 | if(typeval_ != "") | 313 | if(typeval_ != "") |
| 347 | out << " " << typeval_; | 314 | out << " " << typeval_; |
| 348 | if(defaultval_ != "") | 315 | if(defaultval_ != "") |
| 349 | - out << "=" << defaultval_; | 316 | + out << "=" << defaultval_; |
| 350 | if(get_expected() > 1) | 317 | if(get_expected() > 1) |
| 351 | out << " x " << get_expected(); | 318 | out << " x " << get_expected(); |
| 352 | if(get_expected() == -1) | 319 | if(get_expected() == -1) |
| @@ -356,29 +323,27 @@ public: | @@ -356,29 +323,27 @@ public: | ||
| 356 | out << " (env:" << envname_ << ")"; | 323 | out << " (env:" << envname_ << ")"; |
| 357 | if(!requires_.empty()) { | 324 | if(!requires_.empty()) { |
| 358 | out << " Requires:"; | 325 | out << " Requires:"; |
| 359 | - for(const Option* opt : requires_) | 326 | + for(const Option *opt : requires_) |
| 360 | out << " " << opt->get_name(); | 327 | out << " " << opt->get_name(); |
| 361 | } | 328 | } |
| 362 | if(!excludes_.empty()) { | 329 | if(!excludes_.empty()) { |
| 363 | out << " Excludes:"; | 330 | out << " Excludes:"; |
| 364 | - for(const Option* opt : excludes_) | 331 | + for(const Option *opt : excludes_) |
| 365 | out << " " << opt->get_name(); | 332 | out << " " << opt->get_name(); |
| 366 | } | 333 | } |
| 367 | return out.str(); | 334 | return out.str(); |
| 368 | - | ||
| 369 | } | 335 | } |
| 370 | 336 | ||
| 371 | - | ||
| 372 | ///@} | 337 | ///@} |
| 373 | /// @name Parser tools | 338 | /// @name Parser tools |
| 374 | ///@{ | 339 | ///@{ |
| 375 | - | 340 | + |
| 376 | /// Process the callback | 341 | /// Process the callback |
| 377 | void run_callback() const { | 342 | void run_callback() const { |
| 378 | if(!callback_(results_)) | 343 | if(!callback_(results_)) |
| 379 | throw ConversionError(get_name() + "=" + detail::join(results_)); | 344 | throw ConversionError(get_name() + "=" + detail::join(results_)); |
| 380 | if(!validators_.empty()) { | 345 | if(!validators_.empty()) { |
| 381 | - for(const std::string & result : results_) | 346 | + for(const std::string &result : results_) |
| 382 | for(const std::function<bool(std::string)> &vali : validators_) | 347 | for(const std::function<bool(std::string)> &vali : validators_) |
| 383 | if(!vali(result)) | 348 | if(!vali(result)) |
| 384 | throw ValidationError(get_name() + "=" + result); | 349 | throw ValidationError(get_name() + "=" + result); |
| @@ -386,7 +351,7 @@ public: | @@ -386,7 +351,7 @@ public: | ||
| 386 | } | 351 | } |
| 387 | 352 | ||
| 388 | /// If options share any of the same names, they are equal (not counting positional) | 353 | /// If options share any of the same names, they are equal (not counting positional) |
| 389 | - bool operator== (const Option& other) const { | 354 | + bool operator==(const Option &other) const { |
| 390 | for(const std::string &sname : snames_) | 355 | for(const std::string &sname : snames_) |
| 391 | if(other.check_sname(sname)) | 356 | if(other.check_sname(sname)) |
| 392 | return true; | 357 | return true; |
| @@ -406,9 +371,9 @@ public: | @@ -406,9 +371,9 @@ public: | ||
| 406 | /// Check a name. Requires "-" or "--" for short / long, supports positional name | 371 | /// Check a name. Requires "-" or "--" for short / long, supports positional name |
| 407 | bool check_name(std::string name) const { | 372 | bool check_name(std::string name) const { |
| 408 | 373 | ||
| 409 | - if(name.length()>2 && name.substr(0,2) == "--") | 374 | + if(name.length() > 2 && name.substr(0, 2) == "--") |
| 410 | return check_lname(name.substr(2)); | 375 | return check_lname(name.substr(2)); |
| 411 | - else if (name.length()>1 && name.substr(0,1) == "-") | 376 | + else if(name.length() > 1 && name.substr(0, 1) == "-") |
| 412 | return check_sname(name.substr(1)); | 377 | return check_sname(name.substr(1)); |
| 413 | else { | 378 | else { |
| 414 | std::string local_pname = pname_; | 379 | std::string local_pname = pname_; |
| @@ -424,73 +389,60 @@ public: | @@ -424,73 +389,60 @@ public: | ||
| 424 | bool check_sname(std::string name) const { | 389 | bool check_sname(std::string name) const { |
| 425 | if(ignore_case_) { | 390 | if(ignore_case_) { |
| 426 | name = detail::to_lower(name); | 391 | name = detail::to_lower(name); |
| 427 | - return std::find_if(std::begin(snames_), std::end(snames_), | ||
| 428 | - [&name](std::string local_sname){return detail::to_lower(local_sname) == name;}) | ||
| 429 | - != std::end(snames_); | 392 | + return std::find_if(std::begin(snames_), std::end(snames_), [&name](std::string local_sname) { |
| 393 | + return detail::to_lower(local_sname) == name; | ||
| 394 | + }) != std::end(snames_); | ||
| 430 | } else | 395 | } else |
| 431 | return std::find(std::begin(snames_), std::end(snames_), name) != std::end(snames_); | 396 | return std::find(std::begin(snames_), std::end(snames_), name) != std::end(snames_); |
| 432 | } | 397 | } |
| 433 | 398 | ||
| 434 | /// Requires "--" to be removed from string | 399 | /// Requires "--" to be removed from string |
| 435 | bool check_lname(std::string name) const { | 400 | bool check_lname(std::string name) const { |
| 436 | - if(ignore_case_) { | ||
| 437 | - name = detail::to_lower(name); | ||
| 438 | - return std::find_if(std::begin(lnames_), std::end(lnames_), | ||
| 439 | - [&name](std::string local_sname){return detail::to_lower(local_sname) == name;}) | ||
| 440 | - != std::end(lnames_); | 401 | + if(ignore_case_) { |
| 402 | + name = detail::to_lower(name); | ||
| 403 | + return std::find_if(std::begin(lnames_), std::end(lnames_), [&name](std::string local_sname) { | ||
| 404 | + return detail::to_lower(local_sname) == name; | ||
| 405 | + }) != std::end(lnames_); | ||
| 441 | } else | 406 | } else |
| 442 | return std::find(std::begin(lnames_), std::end(lnames_), name) != std::end(lnames_); | 407 | return std::find(std::begin(lnames_), std::end(lnames_), name) != std::end(lnames_); |
| 443 | } | 408 | } |
| 444 | 409 | ||
| 445 | - | ||
| 446 | /// Puts a result at position r | 410 | /// Puts a result at position r |
| 447 | void add_result(std::string s) { | 411 | void add_result(std::string s) { |
| 448 | results_.push_back(s); | 412 | results_.push_back(s); |
| 449 | callback_run_ = false; | 413 | callback_run_ = false; |
| 450 | } | 414 | } |
| 451 | 415 | ||
| 452 | - | ||
| 453 | /// Get a copy of the results | 416 | /// Get a copy of the results |
| 454 | - std::vector<std::string> results() const { | ||
| 455 | - return results_; | ||
| 456 | - } | 417 | + std::vector<std::string> results() const { return results_; } |
| 457 | 418 | ||
| 458 | /// See if the callback has been run already | 419 | /// See if the callback has been run already |
| 459 | - bool get_callback_run() const { | ||
| 460 | - return callback_run_; | ||
| 461 | - } | 420 | + bool get_callback_run() const { return callback_run_; } |
| 462 | 421 | ||
| 463 | ///@} | 422 | ///@} |
| 464 | /// @name Custom options | 423 | /// @name Custom options |
| 465 | ///@{ | 424 | ///@{ |
| 466 | 425 | ||
| 467 | /// Set a custom option, typestring, expected, and changeable | 426 | /// Set a custom option, typestring, expected, and changeable |
| 468 | - void set_custom_option(std::string typeval, int expected=1, bool changeable = false) { | 427 | + void set_custom_option(std::string typeval, int expected = 1, bool changeable = false) { |
| 469 | typeval_ = typeval; | 428 | typeval_ = typeval; |
| 470 | expected_ = expected; | 429 | expected_ = expected; |
| 471 | changeable_ = changeable; | 430 | changeable_ = changeable; |
| 472 | } | 431 | } |
| 473 | 432 | ||
| 474 | /// Set the default value string representation | 433 | /// Set the default value string representation |
| 475 | - void set_default_val(std::string val) { | ||
| 476 | - defaultval_ = val; | ||
| 477 | - } | 434 | + void set_default_val(std::string val) { defaultval_ = val; } |
| 478 | 435 | ||
| 479 | ///@} | 436 | ///@} |
| 480 | 437 | ||
| 481 | - | ||
| 482 | - protected: | ||
| 483 | - | ||
| 484 | - | 438 | + protected: |
| 485 | /// @name App Helpers | 439 | /// @name App Helpers |
| 486 | ///@{ | 440 | ///@{ |
| 487 | /// Can print positional name detailed option if true | 441 | /// Can print positional name detailed option if true |
| 488 | bool _has_help_positional() const { | 442 | bool _has_help_positional() const { |
| 489 | - return get_positional() && (has_description() || !requires_.empty() || !excludes_.empty() ); | 443 | + return get_positional() && (has_description() || !requires_.empty() || !excludes_.empty()); |
| 490 | } | 444 | } |
| 491 | ///@} | 445 | ///@} |
| 492 | }; | 446 | }; |
| 493 | 447 | ||
| 494 | - | ||
| 495 | - | ||
| 496 | } // namespace CLI | 448 | } // namespace CLI |
include/CLI/Split.hpp
| @@ -15,8 +15,8 @@ namespace detail { | @@ -15,8 +15,8 @@ namespace detail { | ||
| 15 | 15 | ||
| 16 | // Returns false if not a short option. Otherwise, sets opt name and rest and returns true | 16 | // Returns false if not a short option. Otherwise, sets opt name and rest and returns true |
| 17 | inline bool split_short(const std::string ¤t, std::string &name, std::string &rest) { | 17 | inline bool split_short(const std::string ¤t, std::string &name, std::string &rest) { |
| 18 | - if(current.size()>1 && current[0] == '-' && valid_first_char(current[1])) { | ||
| 19 | - name = current.substr(1,1); | 18 | + if(current.size() > 1 && current[0] == '-' && valid_first_char(current[1])) { |
| 19 | + name = current.substr(1, 1); | ||
| 20 | rest = current.substr(2); | 20 | rest = current.substr(2); |
| 21 | return true; | 21 | return true; |
| 22 | } else | 22 | } else |
| @@ -25,11 +25,11 @@ inline bool split_short(const std::string &current, std::string &name, std::stri | @@ -25,11 +25,11 @@ inline bool split_short(const std::string &current, std::string &name, std::stri | ||
| 25 | 25 | ||
| 26 | // Returns false if not a long option. Otherwise, sets opt name and other side of = and returns true | 26 | // Returns false if not a long option. Otherwise, sets opt name and other side of = and returns true |
| 27 | inline bool split_long(const std::string ¤t, std::string &name, std::string &value) { | 27 | inline bool split_long(const std::string ¤t, std::string &name, std::string &value) { |
| 28 | - if(current.size()>2 && current.substr(0,2) == "--" && valid_first_char(current[2])) { | 28 | + if(current.size() > 2 && current.substr(0, 2) == "--" && valid_first_char(current[2])) { |
| 29 | auto loc = current.find("="); | 29 | auto loc = current.find("="); |
| 30 | if(loc != std::string::npos) { | 30 | if(loc != std::string::npos) { |
| 31 | - name = current.substr(2,loc-2); | ||
| 32 | - value = current.substr(loc+1); | 31 | + name = current.substr(2, loc - 2); |
| 32 | + value = current.substr(loc + 1); | ||
| 33 | } else { | 33 | } else { |
| 34 | name = current.substr(2); | 34 | name = current.substr(2); |
| 35 | value = ""; | 35 | value = ""; |
| @@ -44,18 +44,17 @@ inline std::vector<std::string> split_names(std::string current) { | @@ -44,18 +44,17 @@ inline std::vector<std::string> split_names(std::string current) { | ||
| 44 | std::vector<std::string> output; | 44 | std::vector<std::string> output; |
| 45 | size_t val; | 45 | size_t val; |
| 46 | while((val = current.find(",")) != std::string::npos) { | 46 | while((val = current.find(",")) != std::string::npos) { |
| 47 | - output.push_back(current.substr(0,val)); | ||
| 48 | - current = current.substr(val+1); | 47 | + output.push_back(current.substr(0, val)); |
| 48 | + current = current.substr(val + 1); | ||
| 49 | } | 49 | } |
| 50 | output.push_back(current); | 50 | output.push_back(current); |
| 51 | return output; | 51 | return output; |
| 52 | - | ||
| 53 | } | 52 | } |
| 54 | 53 | ||
| 55 | /// Get a vector of short names, one of long names, and a single name | 54 | /// Get a vector of short names, one of long names, and a single name |
| 56 | -inline std::tuple<std::vector<std::string>,std::vector<std::string>, std::string> | ||
| 57 | - get_names(const std::vector<std::string> &input) { | ||
| 58 | - | 55 | +inline std::tuple<std::vector<std::string>, std::vector<std::string>, std::string> |
| 56 | +get_names(const std::vector<std::string> &input) { | ||
| 57 | + | ||
| 59 | std::vector<std::string> short_names; | 58 | std::vector<std::string> short_names; |
| 60 | std::vector<std::string> long_names; | 59 | std::vector<std::string> long_names; |
| 61 | std::string pos_name; | 60 | std::string pos_name; |
| @@ -64,30 +63,28 @@ inline std::tuple<std::vector<std::string>,std::vector<std::string>, std::string | @@ -64,30 +63,28 @@ inline std::tuple<std::vector<std::string>,std::vector<std::string>, std::string | ||
| 64 | if(name.length() == 0) | 63 | if(name.length() == 0) |
| 65 | continue; | 64 | continue; |
| 66 | else if(name.length() > 1 && name[0] == '-' && name[1] != '-') { | 65 | else if(name.length() > 1 && name[0] == '-' && name[1] != '-') { |
| 67 | - if(name.length()==2 && valid_first_char(name[1])) | ||
| 68 | - short_names.emplace_back(1,name[1]); | 66 | + if(name.length() == 2 && valid_first_char(name[1])) |
| 67 | + short_names.emplace_back(1, name[1]); | ||
| 69 | else | 68 | else |
| 70 | - throw BadNameString("Invalid one char name: "+name); | ||
| 71 | - } else if(name.length() > 2 && name.substr(0,2) == "--") { | 69 | + throw BadNameString("Invalid one char name: " + name); |
| 70 | + } else if(name.length() > 2 && name.substr(0, 2) == "--") { | ||
| 72 | name = name.substr(2); | 71 | name = name.substr(2); |
| 73 | if(valid_name_string(name)) | 72 | if(valid_name_string(name)) |
| 74 | long_names.push_back(name); | 73 | long_names.push_back(name); |
| 75 | else | 74 | else |
| 76 | - throw BadNameString("Bad long name: "+name); | 75 | + throw BadNameString("Bad long name: " + name); |
| 77 | } else if(name == "-" || name == "--") { | 76 | } else if(name == "-" || name == "--") { |
| 78 | throw BadNameString("Must have a name, not just dashes"); | 77 | throw BadNameString("Must have a name, not just dashes"); |
| 79 | } else { | 78 | } else { |
| 80 | if(pos_name.length() > 0) | 79 | if(pos_name.length() > 0) |
| 81 | - throw BadNameString("Only one positional name allowed, remove: "+name); | 80 | + throw BadNameString("Only one positional name allowed, remove: " + name); |
| 82 | pos_name = name; | 81 | pos_name = name; |
| 83 | - | ||
| 84 | } | 82 | } |
| 85 | } | 83 | } |
| 86 | - | ||
| 87 | - return std::tuple<std::vector<std::string>,std::vector<std::string>, std::string> | ||
| 88 | - (short_names, long_names, pos_name); | ||
| 89 | -} | ||
| 90 | 84 | ||
| 85 | + return std::tuple<std::vector<std::string>, std::vector<std::string>, std::string>( | ||
| 86 | + short_names, long_names, pos_name); | ||
| 87 | +} | ||
| 91 | 88 | ||
| 92 | } // namespace detail | 89 | } // namespace detail |
| 93 | } // namespace CLI | 90 | } // namespace CLI |
include/CLI/StringTools.hpp
| @@ -13,32 +13,29 @@ | @@ -13,32 +13,29 @@ | ||
| 13 | namespace CLI { | 13 | namespace CLI { |
| 14 | namespace detail { | 14 | namespace detail { |
| 15 | 15 | ||
| 16 | - | ||
| 17 | // Based on http://stackoverflow.com/questions/236129/split-a-string-in-c | 16 | // Based on http://stackoverflow.com/questions/236129/split-a-string-in-c |
| 18 | -///Split a string by a delim | 17 | +/// Split a string by a delim |
| 19 | inline std::vector<std::string> split(const std::string &s, char delim) { | 18 | inline std::vector<std::string> split(const std::string &s, char delim) { |
| 20 | std::vector<std::string> elems; | 19 | std::vector<std::string> elems; |
| 21 | // Check to see if emtpy string, give consistent result | 20 | // Check to see if emtpy string, give consistent result |
| 22 | - if(s=="") | 21 | + if(s == "") |
| 23 | elems.emplace_back(""); | 22 | elems.emplace_back(""); |
| 24 | else { | 23 | else { |
| 25 | std::stringstream ss; | 24 | std::stringstream ss; |
| 26 | ss.str(s); | 25 | ss.str(s); |
| 27 | std::string item; | 26 | std::string item; |
| 28 | - while (std::getline(ss, item, delim)) { | 27 | + while(std::getline(ss, item, delim)) { |
| 29 | elems.push_back(item); | 28 | elems.push_back(item); |
| 30 | } | 29 | } |
| 31 | } | 30 | } |
| 32 | return elems; | 31 | return elems; |
| 33 | } | 32 | } |
| 34 | 33 | ||
| 35 | - | ||
| 36 | /// Simple function to join a string | 34 | /// Simple function to join a string |
| 37 | -template <typename T> | ||
| 38 | -std::string join(const T& v, std::string delim = ",") { | 35 | +template <typename T> std::string join(const T &v, std::string delim = ",") { |
| 39 | std::ostringstream s; | 36 | std::ostringstream s; |
| 40 | size_t start = 0; | 37 | size_t start = 0; |
| 41 | - for (const auto& i : v) { | 38 | + for(const auto &i : v) { |
| 42 | if(start++ > 0) | 39 | if(start++ > 0) |
| 43 | s << delim; | 40 | s << delim; |
| 44 | s << i; | 41 | s << i; |
| @@ -47,10 +44,9 @@ std::string join(const T& v, std::string delim = ",") { | @@ -47,10 +44,9 @@ std::string join(const T& v, std::string delim = ",") { | ||
| 47 | } | 44 | } |
| 48 | 45 | ||
| 49 | /// Join a string in reverse order | 46 | /// Join a string in reverse order |
| 50 | -template<typename T> | ||
| 51 | -std::string rjoin(const T& v, std::string delim = ",") { | 47 | +template <typename T> std::string rjoin(const T &v, std::string delim = ",") { |
| 52 | std::ostringstream s; | 48 | std::ostringstream s; |
| 53 | - for(size_t start=0; start<v.size(); start++) { | 49 | + for(size_t start = 0; start < v.size(); start++) { |
| 54 | if(start > 0) | 50 | if(start > 0) |
| 55 | s << delim; | 51 | s << delim; |
| 56 | s << v[v.size() - start - 1]; | 52 | s << v[v.size() - start - 1]; |
| @@ -61,43 +57,39 @@ std::string rjoin(const T& v, std::string delim = ",") { | @@ -61,43 +57,39 @@ std::string rjoin(const T& v, std::string delim = ",") { | ||
| 61 | // Based roughly on http://stackoverflow.com/questions/25829143/c-trim-whitespace-from-a-string | 57 | // Based roughly on http://stackoverflow.com/questions/25829143/c-trim-whitespace-from-a-string |
| 62 | 58 | ||
| 63 | /// Trim whitespace from left of string | 59 | /// Trim whitespace from left of string |
| 64 | -inline std::string& ltrim(std::string &str) { | ||
| 65 | - auto it = std::find_if(str.begin(), str.end(), [](char ch){ return !std::isspace<char>(ch , std::locale());}); | 60 | +inline std::string <rim(std::string &str) { |
| 61 | + auto it = std::find_if(str.begin(), str.end(), [](char ch) { return !std::isspace<char>(ch, std::locale()); }); | ||
| 66 | str.erase(str.begin(), it); | 62 | str.erase(str.begin(), it); |
| 67 | - return str; | 63 | + return str; |
| 68 | } | 64 | } |
| 69 | 65 | ||
| 70 | /// Trim anything from left of string | 66 | /// Trim anything from left of string |
| 71 | -inline std::string& ltrim(std::string &str, const std::string &filter) { | ||
| 72 | - auto it = std::find_if(str.begin(), str.end(), [&filter](char ch){return filter.find(ch) == std::string::npos;}); | 67 | +inline std::string <rim(std::string &str, const std::string &filter) { |
| 68 | + auto it = std::find_if(str.begin(), str.end(), [&filter](char ch) { return filter.find(ch) == std::string::npos; }); | ||
| 73 | str.erase(str.begin(), it); | 69 | str.erase(str.begin(), it); |
| 74 | return str; | 70 | return str; |
| 75 | } | 71 | } |
| 76 | 72 | ||
| 77 | - | ||
| 78 | /// Trim whitespace from right of string | 73 | /// Trim whitespace from right of string |
| 79 | -inline std::string& rtrim(std::string &str) { | ||
| 80 | - auto it = std::find_if(str.rbegin(), str.rend(), [](char ch){ return !std::isspace<char>(ch, std::locale());}); | ||
| 81 | - str.erase(it.base() , str.end() ); | ||
| 82 | - return str; | 74 | +inline std::string &rtrim(std::string &str) { |
| 75 | + auto it = std::find_if(str.rbegin(), str.rend(), [](char ch) { return !std::isspace<char>(ch, std::locale()); }); | ||
| 76 | + str.erase(it.base(), str.end()); | ||
| 77 | + return str; | ||
| 83 | } | 78 | } |
| 84 | 79 | ||
| 85 | /// Trim anything from right of string | 80 | /// Trim anything from right of string |
| 86 | -inline std::string& rtrim(std::string &str, const std::string &filter) { | ||
| 87 | - auto it = std::find_if(str.rbegin(), str.rend(), [&filter](char ch){return filter.find(ch) == std::string::npos;}); | 81 | +inline std::string &rtrim(std::string &str, const std::string &filter) { |
| 82 | + auto it = | ||
| 83 | + std::find_if(str.rbegin(), str.rend(), [&filter](char ch) { return filter.find(ch) == std::string::npos; }); | ||
| 88 | str.erase(it.base(), str.end()); | 84 | str.erase(it.base(), str.end()); |
| 89 | - return str; | 85 | + return str; |
| 90 | } | 86 | } |
| 91 | 87 | ||
| 92 | /// Trim whitespace from string | 88 | /// Trim whitespace from string |
| 93 | -inline std::string& trim(std::string &str) { | ||
| 94 | - return ltrim(rtrim(str)); | ||
| 95 | -} | 89 | +inline std::string &trim(std::string &str) { return ltrim(rtrim(str)); } |
| 96 | 90 | ||
| 97 | /// Trim anything from string | 91 | /// Trim anything from string |
| 98 | -inline std::string& trim(std::string &str, const std::string filter) { | ||
| 99 | - return ltrim(rtrim(str, filter), filter); | ||
| 100 | -} | 92 | +inline std::string &trim(std::string &str, const std::string filter) { return ltrim(rtrim(str, filter), filter); } |
| 101 | 93 | ||
| 102 | /// Make a copy of the string and then trim it | 94 | /// Make a copy of the string and then trim it |
| 103 | inline std::string trim_copy(const std::string &str) { | 95 | inline std::string trim_copy(const std::string &str) { |
| @@ -113,25 +105,21 @@ inline std::string trim_copy(const std::string &str, const std::string &filter) | @@ -113,25 +105,21 @@ inline std::string trim_copy(const std::string &str, const std::string &filter) | ||
| 113 | /// Print a two part "help" string | 105 | /// Print a two part "help" string |
| 114 | inline void format_help(std::stringstream &out, std::string name, std::string description, size_t wid) { | 106 | inline void format_help(std::stringstream &out, std::string name, std::string description, size_t wid) { |
| 115 | name = " " + name; | 107 | name = " " + name; |
| 116 | - out << std::setw(static_cast<int>( wid)) << std::left << name; | 108 | + out << std::setw(static_cast<int>(wid)) << std::left << name; |
| 117 | if(description != "") { | 109 | if(description != "") { |
| 118 | - if(name.length()>=wid) | ||
| 119 | - out << std::endl << std::setw(static_cast<int>( wid)) << ""; | 110 | + if(name.length() >= wid) |
| 111 | + out << std::endl << std::setw(static_cast<int>(wid)) << ""; | ||
| 120 | out << description; | 112 | out << description; |
| 121 | } | 113 | } |
| 122 | out << std::endl; | 114 | out << std::endl; |
| 123 | } | 115 | } |
| 124 | 116 | ||
| 125 | /// Verify the first character of an option | 117 | /// Verify the first character of an option |
| 126 | -template<typename T> | ||
| 127 | -bool valid_first_char(T c) { | ||
| 128 | - return std::isalpha(c, std::locale()) || c=='_'; | ||
| 129 | -} | 118 | +template <typename T> bool valid_first_char(T c) { return std::isalpha(c, std::locale()) || c == '_'; } |
| 130 | 119 | ||
| 131 | /// Verify following characters of an option | 120 | /// Verify following characters of an option |
| 132 | -template<typename T> | ||
| 133 | -bool valid_later_char(T c) { | ||
| 134 | - return std::isalnum(c, std::locale()) || c=='_' || c=='.' || c=='-'; | 121 | +template <typename T> bool valid_later_char(T c) { |
| 122 | + return std::isalnum(c, std::locale()) || c == '_' || c == '.' || c == '-'; | ||
| 135 | } | 123 | } |
| 136 | 124 | ||
| 137 | /// Verify an option name | 125 | /// Verify an option name |
| @@ -146,16 +134,17 @@ inline bool valid_name_string(const std::string &str) { | @@ -146,16 +134,17 @@ inline bool valid_name_string(const std::string &str) { | ||
| 146 | 134 | ||
| 147 | /// Return a lower case version of a string | 135 | /// Return a lower case version of a string |
| 148 | inline std::string to_lower(std::string str) { | 136 | inline std::string to_lower(std::string str) { |
| 149 | - std::transform(std::begin(str), std::end(str), std::begin(str), | ||
| 150 | - [](const std::string::value_type &x){return std::tolower(x,std::locale());}); | 137 | + std::transform(std::begin(str), std::end(str), std::begin(str), [](const std::string::value_type &x) { |
| 138 | + return std::tolower(x, std::locale()); | ||
| 139 | + }); | ||
| 151 | return str; | 140 | return str; |
| 152 | } | 141 | } |
| 153 | 142 | ||
| 154 | /// Split a string '"one two" "three"' into 'one two', 'three' | 143 | /// Split a string '"one two" "three"' into 'one two', 'three' |
| 155 | inline std::vector<std::string> split_up(std::string str) { | 144 | inline std::vector<std::string> split_up(std::string str) { |
| 156 | - | 145 | + |
| 157 | std::vector<char> delims = {'\'', '\"'}; | 146 | std::vector<char> delims = {'\'', '\"'}; |
| 158 | - auto find_ws = [](char ch){ return std::isspace<char>(ch , std::locale());}; | 147 | + auto find_ws = [](char ch) { return std::isspace<char>(ch, std::locale()); }; |
| 159 | trim(str); | 148 | trim(str); |
| 160 | 149 | ||
| 161 | std::vector<std::string> output; | 150 | std::vector<std::string> output; |
| @@ -164,8 +153,8 @@ inline std::vector<std::string> split_up(std::string str) { | @@ -164,8 +153,8 @@ inline std::vector<std::string> split_up(std::string str) { | ||
| 164 | if(str[0] == '\'') { | 153 | if(str[0] == '\'') { |
| 165 | auto end = str.find('\'', 1); | 154 | auto end = str.find('\'', 1); |
| 166 | if(end != std::string::npos) { | 155 | if(end != std::string::npos) { |
| 167 | - output.push_back(str.substr(1,end-1)); | ||
| 168 | - str = str.substr(end+1); | 156 | + output.push_back(str.substr(1, end - 1)); |
| 157 | + str = str.substr(end + 1); | ||
| 169 | } else { | 158 | } else { |
| 170 | output.push_back(str.substr(1)); | 159 | output.push_back(str.substr(1)); |
| 171 | str = ""; | 160 | str = ""; |
| @@ -173,8 +162,8 @@ inline std::vector<std::string> split_up(std::string str) { | @@ -173,8 +162,8 @@ inline std::vector<std::string> split_up(std::string str) { | ||
| 173 | } else if(str[0] == '\"') { | 162 | } else if(str[0] == '\"') { |
| 174 | auto end = str.find('\"', 1); | 163 | auto end = str.find('\"', 1); |
| 175 | if(end != std::string::npos) { | 164 | if(end != std::string::npos) { |
| 176 | - output.push_back(str.substr(1,end-1)); | ||
| 177 | - str = str.substr(end+1); | 165 | + output.push_back(str.substr(1, end - 1)); |
| 166 | + str = str.substr(end + 1); | ||
| 178 | } else { | 167 | } else { |
| 179 | output.push_back(str.substr(1)); | 168 | output.push_back(str.substr(1)); |
| 180 | str = ""; | 169 | str = ""; |
| @@ -183,7 +172,7 @@ inline std::vector<std::string> split_up(std::string str) { | @@ -183,7 +172,7 @@ inline std::vector<std::string> split_up(std::string str) { | ||
| 183 | } else { | 172 | } else { |
| 184 | auto it = std::find_if(std::begin(str), std::end(str), find_ws); | 173 | auto it = std::find_if(std::begin(str), std::end(str), find_ws); |
| 185 | if(it != std::end(str)) { | 174 | if(it != std::end(str)) { |
| 186 | - std::string value = std::string(str.begin(),it); | 175 | + std::string value = std::string(str.begin(), it); |
| 187 | output.push_back(value); | 176 | output.push_back(value); |
| 188 | str = std::string(it, str.end()); | 177 | str = std::string(it, str.end()); |
| 189 | } else { | 178 | } else { |
include/CLI/Timer.hpp
| @@ -12,7 +12,7 @@ | @@ -12,7 +12,7 @@ | ||
| 12 | namespace CLI { | 12 | namespace CLI { |
| 13 | 13 | ||
| 14 | class Timer { | 14 | class Timer { |
| 15 | -protected: | 15 | + protected: |
| 16 | /// This is a typedef to make clocks easier to use | 16 | /// This is a typedef to make clocks easier to use |
| 17 | using clock = std::chrono::steady_clock; | 17 | using clock = std::chrono::steady_clock; |
| 18 | 18 | ||
| @@ -20,7 +20,7 @@ protected: | @@ -20,7 +20,7 @@ protected: | ||
| 20 | using time_point = std::chrono::time_point<clock>; | 20 | using time_point = std::chrono::time_point<clock>; |
| 21 | 21 | ||
| 22 | /// 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 |
| 23 | - using time_print_t = std::function<std::string (std::string, std::string)>; | 23 | + using time_print_t = std::function<std::string(std::string, std::string)>; |
| 24 | 24 | ||
| 25 | /// This is the title of the timer | 25 | /// This is the title of the timer |
| 26 | std::string title_; | 26 | std::string title_; |
| @@ -30,31 +30,27 @@ protected: | @@ -30,31 +30,27 @@ protected: | ||
| 30 | 30 | ||
| 31 | /// This is the starting point (when the timer was created) | 31 | /// This is the starting point (when the timer was created) |
| 32 | time_point start_; | 32 | time_point start_; |
| 33 | - | ||
| 34 | - /// This is the number of times cycles (print divides by this number) | ||
| 35 | - size_t cycles {1}; | ||
| 36 | 33 | ||
| 37 | -public: | 34 | + /// This is the number of times cycles (print divides by this number) |
| 35 | + size_t cycles{1}; | ||
| 38 | 36 | ||
| 37 | + public: | ||
| 39 | /// Standard print function, this one is set by default | 38 | /// Standard print function, this one is set by default |
| 40 | - static std::string Simple(std::string title, std::string time) { | ||
| 41 | - return title + ": " + time; | ||
| 42 | - } | 39 | + static std::string Simple(std::string title, std::string time) { return title + ": " + time; } |
| 43 | 40 | ||
| 44 | /// This is a fancy print function with --- headers | 41 | /// This is a fancy print function with --- headers |
| 45 | static std::string Big(std::string title, std::string time) { | 42 | static std::string Big(std::string title, std::string time) { |
| 46 | - return std::string("-----------------------------------------\n") | ||
| 47 | - + "| " + title + " | Time = " + time + "\n" | ||
| 48 | - + "-----------------------------------------"; | 43 | + return std::string("-----------------------------------------\n") + "| " + title + " | Time = " + time + "\n" + |
| 44 | + "-----------------------------------------"; | ||
| 49 | } | 45 | } |
| 50 | 46 | ||
| 51 | -public: | 47 | + public: |
| 52 | /// Standard constructor, can set title and print function | 48 | /// Standard constructor, can set title and print function |
| 53 | - Timer(std::string title="Timer", time_print_t time_print = Simple) | 49 | + Timer(std::string title = "Timer", time_print_t time_print = Simple) |
| 54 | : title_(std::move(title)), time_print_(std::move(time_print)), start_(clock::now()) {} | 50 | : title_(std::move(title)), time_print_(std::move(time_print)), start_(clock::now()) {} |
| 55 | 51 | ||
| 56 | /// Time a function by running it multiple times. Target time is the len to target. | 52 | /// Time a function by running it multiple times. Target time is the len to target. |
| 57 | - std::string time_it(std::function<void()> f, double target_time=1) { | 53 | + std::string time_it(std::function<void()> f, double target_time = 1) { |
| 58 | time_point start = start_; | 54 | time_point start = start_; |
| 59 | double total_time; | 55 | double total_time; |
| 60 | 56 | ||
| @@ -62,11 +58,11 @@ public: | @@ -62,11 +58,11 @@ public: | ||
| 62 | size_t n = 0; | 58 | size_t n = 0; |
| 63 | do { | 59 | do { |
| 64 | f(); | 60 | f(); |
| 65 | - std::chrono::duration<double> elapsed = clock::now() - start_; | 61 | + std::chrono::duration<double> elapsed = clock::now() - start_; |
| 66 | total_time = elapsed.count(); | 62 | total_time = elapsed.count(); |
| 67 | - } while (n++ < 100 && total_time < target_time); | 63 | + } while(n++ < 100 && total_time < target_time); |
| 68 | 64 | ||
| 69 | - std::string out = make_time_str(total_time/n) + " for " + std::to_string(n) + " tries"; | 65 | + std::string out = make_time_str(total_time / n) + " for " + std::to_string(n) + " tries"; |
| 70 | start_ = start; | 66 | start_ = start; |
| 71 | return out; | 67 | return out; |
| 72 | } | 68 | } |
| @@ -78,54 +74,48 @@ public: | @@ -78,54 +74,48 @@ public: | ||
| 78 | double time = elapsed.count() / cycles; | 74 | double time = elapsed.count() / cycles; |
| 79 | return make_time_str(time); | 75 | return make_time_str(time); |
| 80 | } | 76 | } |
| 81 | - | 77 | + |
| 82 | // LCOV_EXCL_START | 78 | // LCOV_EXCL_START |
| 83 | std::string make_time_str(double time) const { | 79 | std::string make_time_str(double time) const { |
| 84 | - auto print_it = [](double x, std::string unit){ | 80 | + auto print_it = [](double x, std::string unit) { |
| 85 | char buffer[50]; | 81 | char buffer[50]; |
| 86 | std::snprintf(buffer, 50, "%.5g", x); | 82 | std::snprintf(buffer, 50, "%.5g", x); |
| 87 | return buffer + std::string(" ") + unit; | 83 | return buffer + std::string(" ") + unit; |
| 88 | }; | 84 | }; |
| 89 | - | 85 | + |
| 90 | if(time < .000001) | 86 | if(time < .000001) |
| 91 | - return print_it(time*1000000000, "ns"); | 87 | + return print_it(time * 1000000000, "ns"); |
| 92 | else if(time < .001) | 88 | else if(time < .001) |
| 93 | - return print_it(time*1000000, "us"); | 89 | + return print_it(time * 1000000, "us"); |
| 94 | else if(time < 1) | 90 | else if(time < 1) |
| 95 | - return print_it(time*1000, "ms"); | 91 | + return print_it(time * 1000, "ms"); |
| 96 | else | 92 | else |
| 97 | return print_it(time, "s"); | 93 | return print_it(time, "s"); |
| 98 | } | 94 | } |
| 99 | // LCOV_EXCL_END | 95 | // LCOV_EXCL_END |
| 100 | 96 | ||
| 101 | /// This is the main function, it creates a string | 97 | /// This is the main function, it creates a string |
| 102 | - std::string to_string() const { | ||
| 103 | - return time_print_(title_, make_time_str()); | ||
| 104 | - } | ||
| 105 | - | 98 | + std::string to_string() const { return time_print_(title_, make_time_str()); } |
| 99 | + | ||
| 106 | /// Division sets the number of cycles to divide by (no graphical change) | 100 | /// Division sets the number of cycles to divide by (no graphical change) |
| 107 | - Timer& operator / (size_t val) {cycles = val; return *this;} | 101 | + Timer &operator/(size_t val) { |
| 102 | + cycles = val; | ||
| 103 | + return *this; | ||
| 104 | + } | ||
| 108 | }; | 105 | }; |
| 109 | 106 | ||
| 110 | - | ||
| 111 | - | ||
| 112 | /// This class prints out the time upon destruction | 107 | /// This class prints out the time upon destruction |
| 113 | class AutoTimer : public Timer { | 108 | class AutoTimer : public Timer { |
| 114 | -public: | 109 | + public: |
| 115 | /// Reimplementing the constructor is required in GCC 4.7 | 110 | /// Reimplementing the constructor is required in GCC 4.7 |
| 116 | - AutoTimer(std::string title="Timer", time_print_t time_print = Simple) | ||
| 117 | - : Timer(title, time_print) {} | 111 | + AutoTimer(std::string title = "Timer", time_print_t time_print = Simple) : Timer(title, time_print) {} |
| 118 | // GCC 4.7 does not support using inheriting constructors. | 112 | // GCC 4.7 does not support using inheriting constructors. |
| 119 | 113 | ||
| 120 | /// This desctructor prints the string | 114 | /// This desctructor prints the string |
| 121 | - ~AutoTimer () { | ||
| 122 | - std::cout << to_string() << std::endl; | ||
| 123 | - } | 115 | + ~AutoTimer() { std::cout << to_string() << std::endl; } |
| 124 | }; | 116 | }; |
| 125 | 117 | ||
| 126 | } // namespace CLI | 118 | } // namespace CLI |
| 127 | 119 | ||
| 128 | /// This prints out the time if shifted into a std::cout like stream. | 120 | /// This prints out the time if shifted into a std::cout like stream. |
| 129 | -inline std::ostream & operator<< (std::ostream& in, const CLI::Timer& timer) { | ||
| 130 | - return in << timer.to_string(); | ||
| 131 | -} | 121 | +inline std::ostream &operator<<(std::ostream &in, const CLI::Timer &timer) { return in << timer.to_string(); } |
include/CLI/TypeTools.hpp
| @@ -14,126 +14,100 @@ namespace CLI { | @@ -14,126 +14,100 @@ namespace CLI { | ||
| 14 | 14 | ||
| 15 | // Copied from C++14 | 15 | // Copied from C++14 |
| 16 | #if __cplusplus < 201402L | 16 | #if __cplusplus < 201402L |
| 17 | -template< bool B, class T = void > | ||
| 18 | -using enable_if_t = typename std::enable_if<B,T>::type; | 17 | +template <bool B, class T = void> using enable_if_t = typename std::enable_if<B, T>::type; |
| 19 | #else | 18 | #else |
| 20 | // If your compiler supports C++14, you can use that definition instead | 19 | // If your compiler supports C++14, you can use that definition instead |
| 21 | using std::enable_if_t; | 20 | using std::enable_if_t; |
| 22 | #endif | 21 | #endif |
| 23 | 22 | ||
| 24 | -template <typename T> | ||
| 25 | -struct is_vector { | ||
| 26 | - static const bool value = false; | ||
| 27 | -}; | 23 | +template <typename T> struct is_vector { static const bool value = false; }; |
| 28 | 24 | ||
| 25 | +template <class T, class A> struct is_vector<std::vector<T, A>> { static bool const value = true; }; | ||
| 29 | 26 | ||
| 30 | -template<class T, class A> | ||
| 31 | -struct is_vector<std::vector<T, A> > { | ||
| 32 | - static bool const value = true; | ||
| 33 | -}; | ||
| 34 | - | ||
| 35 | -template <typename T> | ||
| 36 | -struct is_bool { | ||
| 37 | - static const bool value = false; | ||
| 38 | -}; | ||
| 39 | - | ||
| 40 | -template<> | ||
| 41 | -struct is_bool<bool> { | ||
| 42 | - static bool const value = true; | ||
| 43 | -}; | 27 | +template <typename T> struct is_bool { static const bool value = false; }; |
| 44 | 28 | ||
| 29 | +template <> struct is_bool<bool> { static bool const value = true; }; | ||
| 45 | 30 | ||
| 46 | namespace detail { | 31 | namespace detail { |
| 47 | - // Based generally on https://rmf.io/cxx11/almost-static-if | ||
| 48 | - /// Simple empty scoped class | ||
| 49 | - enum class enabler {}; | ||
| 50 | - | ||
| 51 | - /// An instance to use in EnableIf | ||
| 52 | - constexpr enabler dummy = {}; | ||
| 53 | - | ||
| 54 | - | ||
| 55 | - // Type name print | ||
| 56 | - | ||
| 57 | - /// Was going to be based on | ||
| 58 | - /// http://stackoverflow.com/questions/1055452/c-get-name-of-type-in-template | ||
| 59 | - /// But this is cleaner and works better in this case | ||
| 60 | - | ||
| 61 | - template<typename T, | ||
| 62 | - enable_if_t<std::is_integral<T>::value && std::is_signed<T>::value, detail::enabler> = detail::dummy> | ||
| 63 | - constexpr const char* type_name() { | ||
| 64 | - return "INT"; | ||
| 65 | - } | ||
| 66 | - | ||
| 67 | - template<typename T, | ||
| 68 | - enable_if_t<std::is_integral<T>::value && std::is_unsigned<T>::value, detail::enabler> = detail::dummy> | ||
| 69 | - constexpr const char* type_name() { | ||
| 70 | - return "UINT"; | ||
| 71 | - } | ||
| 72 | - | ||
| 73 | - | ||
| 74 | - template<typename T, | ||
| 75 | - enable_if_t<std::is_floating_point<T>::value, detail::enabler> = detail::dummy> | ||
| 76 | - constexpr const char* type_name() { | ||
| 77 | - return "FLOAT"; | ||
| 78 | - } | ||
| 79 | - | ||
| 80 | - | ||
| 81 | - /// This one should not be used, since vector types print the internal type | ||
| 82 | - template<typename T, | ||
| 83 | - enable_if_t<is_vector<T>::value, detail::enabler> = detail::dummy> | ||
| 84 | - constexpr const char* type_name() { | ||
| 85 | - return "VECTOR"; | ||
| 86 | - } | ||
| 87 | - | ||
| 88 | - | ||
| 89 | - template<typename T, | ||
| 90 | - enable_if_t<!std::is_floating_point<T>::value && !std::is_integral<T>::value && !is_vector<T>::value | ||
| 91 | - , detail::enabler> = detail::dummy> | ||
| 92 | - constexpr const char* type_name() { | ||
| 93 | - return "TEXT"; | ||
| 94 | - } | ||
| 95 | - | ||
| 96 | - | ||
| 97 | - | ||
| 98 | - // Lexical cast | ||
| 99 | - | ||
| 100 | - | ||
| 101 | - /// Integers | ||
| 102 | - template<typename T, enable_if_t<std::is_integral<T>::value, detail::enabler> = detail::dummy> | ||
| 103 | - bool lexical_cast(std::string input, T& output) { | ||
| 104 | - try{ | ||
| 105 | - output = static_cast<T>(std::stoll(input)); | ||
| 106 | - return true; | ||
| 107 | - } catch (const std::invalid_argument&) { | ||
| 108 | - return false; | ||
| 109 | - } catch (const std::out_of_range&) { | ||
| 110 | - return false; | ||
| 111 | - } | ||
| 112 | - } | ||
| 113 | - | ||
| 114 | - /// Floats | ||
| 115 | - template<typename T, enable_if_t<std::is_floating_point<T>::value, detail::enabler> = detail::dummy> | ||
| 116 | - bool lexical_cast(std::string input, T& output) { | ||
| 117 | - try{ | ||
| 118 | - output =static_cast<T>(std::stold(input)); | ||
| 119 | - return true; | ||
| 120 | - } catch (const std::invalid_argument&) { | ||
| 121 | - return false; | ||
| 122 | - } catch (const std::out_of_range&) { | ||
| 123 | - return false; | ||
| 124 | - } | 32 | +// Based generally on https://rmf.io/cxx11/almost-static-if |
| 33 | +/// Simple empty scoped class | ||
| 34 | +enum class enabler {}; | ||
| 35 | + | ||
| 36 | +/// An instance to use in EnableIf | ||
| 37 | +constexpr enabler dummy = {}; | ||
| 38 | + | ||
| 39 | +// Type name print | ||
| 40 | + | ||
| 41 | +/// Was going to be based on | ||
| 42 | +/// http://stackoverflow.com/questions/1055452/c-get-name-of-type-in-template | ||
| 43 | +/// But this is cleaner and works better in this case | ||
| 44 | + | ||
| 45 | +template <typename T, | ||
| 46 | + enable_if_t<std::is_integral<T>::value && std::is_signed<T>::value, detail::enabler> = detail::dummy> | ||
| 47 | +constexpr const char *type_name() { | ||
| 48 | + return "INT"; | ||
| 49 | +} | ||
| 50 | + | ||
| 51 | +template <typename T, | ||
| 52 | + enable_if_t<std::is_integral<T>::value && std::is_unsigned<T>::value, detail::enabler> = detail::dummy> | ||
| 53 | +constexpr const char *type_name() { | ||
| 54 | + return "UINT"; | ||
| 55 | +} | ||
| 56 | + | ||
| 57 | +template <typename T, enable_if_t<std::is_floating_point<T>::value, detail::enabler> = detail::dummy> | ||
| 58 | +constexpr const char *type_name() { | ||
| 59 | + return "FLOAT"; | ||
| 60 | +} | ||
| 61 | + | ||
| 62 | +/// This one should not be used, since vector types print the internal type | ||
| 63 | +template <typename T, enable_if_t<is_vector<T>::value, detail::enabler> = detail::dummy> | ||
| 64 | +constexpr const char *type_name() { | ||
| 65 | + return "VECTOR"; | ||
| 66 | +} | ||
| 67 | + | ||
| 68 | +template <typename T, | ||
| 69 | + enable_if_t<!std::is_floating_point<T>::value && !std::is_integral<T>::value && !is_vector<T>::value, | ||
| 70 | + detail::enabler> = detail::dummy> | ||
| 71 | +constexpr const char *type_name() { | ||
| 72 | + return "TEXT"; | ||
| 73 | +} | ||
| 74 | + | ||
| 75 | +// Lexical cast | ||
| 76 | + | ||
| 77 | +/// Integers | ||
| 78 | +template <typename T, enable_if_t<std::is_integral<T>::value, detail::enabler> = detail::dummy> | ||
| 79 | +bool lexical_cast(std::string input, T &output) { | ||
| 80 | + try { | ||
| 81 | + output = static_cast<T>(std::stoll(input)); | ||
| 82 | + return true; | ||
| 83 | + } catch(const std::invalid_argument &) { | ||
| 84 | + return false; | ||
| 85 | + } catch(const std::out_of_range &) { | ||
| 86 | + return false; | ||
| 125 | } | 87 | } |
| 88 | +} | ||
| 126 | 89 | ||
| 127 | - | ||
| 128 | - /// String and similar | ||
| 129 | - template<typename T, | ||
| 130 | - enable_if_t<!std::is_floating_point<T>::value && !std::is_integral<T>::value | ||
| 131 | - , detail::enabler> = detail::dummy> | ||
| 132 | - bool lexical_cast(std::string input, T& output) { | ||
| 133 | - output = input; | 90 | +/// Floats |
| 91 | +template <typename T, enable_if_t<std::is_floating_point<T>::value, detail::enabler> = detail::dummy> | ||
| 92 | +bool lexical_cast(std::string input, T &output) { | ||
| 93 | + try { | ||
| 94 | + output = static_cast<T>(std::stold(input)); | ||
| 134 | return true; | 95 | return true; |
| 96 | + } catch(const std::invalid_argument &) { | ||
| 97 | + return false; | ||
| 98 | + } catch(const std::out_of_range &) { | ||
| 99 | + return false; | ||
| 135 | } | 100 | } |
| 136 | - | 101 | +} |
| 102 | + | ||
| 103 | +/// String and similar | ||
| 104 | +template < | ||
| 105 | + typename T, | ||
| 106 | + enable_if_t<!std::is_floating_point<T>::value && !std::is_integral<T>::value, detail::enabler> = detail::dummy> | ||
| 107 | +bool lexical_cast(std::string input, T &output) { | ||
| 108 | + output = input; | ||
| 109 | + return true; | ||
| 110 | +} | ||
| 137 | 111 | ||
| 138 | } // namespace detail | 112 | } // namespace detail |
| 139 | } // namespace CLI | 113 | } // namespace CLI |
include/CLI/Validators.hpp
| @@ -16,7 +16,6 @@ | @@ -16,7 +16,6 @@ | ||
| 16 | 16 | ||
| 17 | namespace CLI { | 17 | namespace CLI { |
| 18 | 18 | ||
| 19 | - | ||
| 20 | /// @defgroup validator_group Validators | 19 | /// @defgroup validator_group Validators |
| 21 | /// @brief Some validators that are provided | 20 | /// @brief Some validators that are provided |
| 22 | /// | 21 | /// |
| @@ -25,13 +24,13 @@ namespace CLI { | @@ -25,13 +24,13 @@ namespace CLI { | ||
| 25 | 24 | ||
| 26 | /// Check for an existing file | 25 | /// Check for an existing file |
| 27 | inline bool ExistingFile(std::string filename) { | 26 | inline bool ExistingFile(std::string filename) { |
| 28 | - struct stat buffer; | ||
| 29 | - bool exist = stat(filename.c_str(), &buffer) == 0; | 27 | + struct stat buffer; |
| 28 | + bool exist = stat(filename.c_str(), &buffer) == 0; | ||
| 30 | bool is_dir = (buffer.st_mode & S_IFDIR) != 0; | 29 | bool is_dir = (buffer.st_mode & S_IFDIR) != 0; |
| 31 | if(!exist) { | 30 | if(!exist) { |
| 32 | std::cerr << "File does not exist: " << filename << std::endl; | 31 | std::cerr << "File does not exist: " << filename << std::endl; |
| 33 | return false; | 32 | return false; |
| 34 | - } else if (is_dir) { | 33 | + } else if(is_dir) { |
| 35 | std::cerr << "File is actually a directory: " << filename << std::endl; | 34 | std::cerr << "File is actually a directory: " << filename << std::endl; |
| 36 | return false; | 35 | return false; |
| 37 | } else { | 36 | } else { |
| @@ -41,13 +40,13 @@ inline bool ExistingFile(std::string filename) { | @@ -41,13 +40,13 @@ inline bool ExistingFile(std::string filename) { | ||
| 41 | 40 | ||
| 42 | /// Check for an existing directory | 41 | /// Check for an existing directory |
| 43 | inline bool ExistingDirectory(std::string filename) { | 42 | inline bool ExistingDirectory(std::string filename) { |
| 44 | - struct stat buffer; | ||
| 45 | - bool exist = stat(filename.c_str(), &buffer) == 0; | 43 | + struct stat buffer; |
| 44 | + bool exist = stat(filename.c_str(), &buffer) == 0; | ||
| 46 | bool is_dir = (buffer.st_mode & S_IFDIR) != 0; | 45 | bool is_dir = (buffer.st_mode & S_IFDIR) != 0; |
| 47 | if(!exist) { | 46 | if(!exist) { |
| 48 | std::cerr << "Directory does not exist: " << filename << std::endl; | 47 | std::cerr << "Directory does not exist: " << filename << std::endl; |
| 49 | return false; | 48 | return false; |
| 50 | - } else if (is_dir) { | 49 | + } else if(is_dir) { |
| 51 | return true; | 50 | return true; |
| 52 | } else { | 51 | } else { |
| 53 | std::cerr << "Directory is actually a file: " << filename << std::endl; | 52 | std::cerr << "Directory is actually a file: " << filename << std::endl; |
| @@ -55,11 +54,10 @@ inline bool ExistingDirectory(std::string filename) { | @@ -55,11 +54,10 @@ inline bool ExistingDirectory(std::string filename) { | ||
| 55 | } | 54 | } |
| 56 | } | 55 | } |
| 57 | 56 | ||
| 58 | - | ||
| 59 | /// Check for a non-existing path | 57 | /// Check for a non-existing path |
| 60 | inline bool NonexistentPath(std::string filename) { | 58 | inline bool NonexistentPath(std::string filename) { |
| 61 | - struct stat buffer; | ||
| 62 | - bool exist = stat(filename.c_str(), &buffer) == 0; | 59 | + struct stat buffer; |
| 60 | + bool exist = stat(filename.c_str(), &buffer) == 0; | ||
| 63 | if(!exist) { | 61 | if(!exist) { |
| 64 | return true; | 62 | return true; |
| 65 | } else { | 63 | } else { |
| @@ -69,9 +67,8 @@ inline bool NonexistentPath(std::string filename) { | @@ -69,9 +67,8 @@ inline bool NonexistentPath(std::string filename) { | ||
| 69 | } | 67 | } |
| 70 | 68 | ||
| 71 | /// Produce a range validator function | 69 | /// Produce a range validator function |
| 72 | -template<typename T> | ||
| 73 | -std::function<bool(std::string)> Range(T min, T max) { | ||
| 74 | - return [min, max](std::string input){ | 70 | +template <typename T> std::function<bool(std::string)> Range(T min, T max) { |
| 71 | + return [min, max](std::string input) { | ||
| 75 | T val; | 72 | T val; |
| 76 | detail::lexical_cast(input, val); | 73 | detail::lexical_cast(input, val); |
| 77 | return val >= min && val <= max; | 74 | return val >= min && val <= max; |
| @@ -79,10 +76,7 @@ std::function<bool(std::string)> Range(T min, T max) { | @@ -79,10 +76,7 @@ std::function<bool(std::string)> Range(T min, T max) { | ||
| 79 | } | 76 | } |
| 80 | 77 | ||
| 81 | /// Range of one value is 0 to value | 78 | /// Range of one value is 0 to value |
| 82 | -template<typename T> | ||
| 83 | -std::function<bool(std::string)> Range(T max) { | ||
| 84 | - return Range(static_cast<T>(0), max); | ||
| 85 | -} | 79 | +template <typename T> std::function<bool(std::string)> Range(T max) { return Range(static_cast<T>(0), max); } |
| 86 | 80 | ||
| 87 | /// @} | 81 | /// @} |
| 88 | 82 |
tests/.syntastic_cpp_config
tests/AppTest.cpp
| 1 | #include "app_helper.hpp" | 1 | #include "app_helper.hpp" |
| 2 | #include <cstdlib> | 2 | #include <cstdlib> |
| 3 | 3 | ||
| 4 | - | ||
| 5 | TEST_F(TApp, OneFlagShort) { | 4 | TEST_F(TApp, OneFlagShort) { |
| 6 | app.add_flag("-c,--count"); | 5 | app.add_flag("-c,--count"); |
| 7 | args = {"-c"}; | 6 | args = {"-c"}; |
| 8 | run(); | 7 | run(); |
| 9 | - EXPECT_EQ((size_t) 1, app.count("-c")); | ||
| 10 | - EXPECT_EQ((size_t) 1, app.count("--count")); | 8 | + EXPECT_EQ((size_t)1, app.count("-c")); |
| 9 | + EXPECT_EQ((size_t)1, app.count("--count")); | ||
| 11 | } | 10 | } |
| 12 | 11 | ||
| 13 | TEST_F(TApp, CountNonExist) { | 12 | TEST_F(TApp, CountNonExist) { |
| @@ -21,8 +20,8 @@ TEST_F(TApp, OneFlagLong) { | @@ -21,8 +20,8 @@ TEST_F(TApp, OneFlagLong) { | ||
| 21 | app.add_flag("-c,--count"); | 20 | app.add_flag("-c,--count"); |
| 22 | args = {"--count"}; | 21 | args = {"--count"}; |
| 23 | run(); | 22 | run(); |
| 24 | - EXPECT_EQ((size_t) 1, app.count("-c")); | ||
| 25 | - EXPECT_EQ((size_t) 1, app.count("--count")); | 23 | + EXPECT_EQ((size_t)1, app.count("-c")); |
| 24 | + EXPECT_EQ((size_t)1, app.count("--count")); | ||
| 26 | } | 25 | } |
| 27 | 26 | ||
| 28 | TEST_F(TApp, DashedOptions) { | 27 | TEST_F(TApp, DashedOptions) { |
| @@ -32,11 +31,10 @@ TEST_F(TApp, DashedOptions) { | @@ -32,11 +31,10 @@ TEST_F(TApp, DashedOptions) { | ||
| 32 | 31 | ||
| 33 | args = {"-c", "--q", "--this", "--that"}; | 32 | args = {"-c", "--q", "--this", "--that"}; |
| 34 | run(); | 33 | run(); |
| 35 | - EXPECT_EQ((size_t) 1, app.count("-c")); | ||
| 36 | - EXPECT_EQ((size_t) 1, app.count("--q")); | ||
| 37 | - EXPECT_EQ((size_t) 2, app.count("--this")); | ||
| 38 | - EXPECT_EQ((size_t) 2, app.count("--that")); | ||
| 39 | - | 34 | + EXPECT_EQ((size_t)1, app.count("-c")); |
| 35 | + EXPECT_EQ((size_t)1, app.count("--q")); | ||
| 36 | + EXPECT_EQ((size_t)2, app.count("--this")); | ||
| 37 | + EXPECT_EQ((size_t)2, app.count("--that")); | ||
| 40 | } | 38 | } |
| 41 | 39 | ||
| 42 | TEST_F(TApp, OneFlagRef) { | 40 | TEST_F(TApp, OneFlagRef) { |
| @@ -44,8 +42,8 @@ TEST_F(TApp, OneFlagRef) { | @@ -44,8 +42,8 @@ TEST_F(TApp, OneFlagRef) { | ||
| 44 | app.add_flag("-c,--count", ref); | 42 | app.add_flag("-c,--count", ref); |
| 45 | args = {"--count"}; | 43 | args = {"--count"}; |
| 46 | run(); | 44 | run(); |
| 47 | - EXPECT_EQ((size_t) 1, app.count("-c")); | ||
| 48 | - EXPECT_EQ((size_t) 1, app.count("--count")); | 45 | + EXPECT_EQ((size_t)1, app.count("-c")); |
| 46 | + EXPECT_EQ((size_t)1, app.count("--count")); | ||
| 49 | EXPECT_EQ(1, ref); | 47 | EXPECT_EQ(1, ref); |
| 50 | } | 48 | } |
| 51 | 49 | ||
| @@ -54,8 +52,8 @@ TEST_F(TApp, OneString) { | @@ -54,8 +52,8 @@ TEST_F(TApp, OneString) { | ||
| 54 | app.add_option("-s,--string", str); | 52 | app.add_option("-s,--string", str); |
| 55 | args = {"--string", "mystring"}; | 53 | args = {"--string", "mystring"}; |
| 56 | run(); | 54 | run(); |
| 57 | - EXPECT_EQ((size_t) 1, app.count("-s")); | ||
| 58 | - EXPECT_EQ((size_t) 1, app.count("--string")); | 55 | + EXPECT_EQ((size_t)1, app.count("-s")); |
| 56 | + EXPECT_EQ((size_t)1, app.count("--string")); | ||
| 59 | EXPECT_EQ(str, "mystring"); | 57 | EXPECT_EQ(str, "mystring"); |
| 60 | } | 58 | } |
| 61 | 59 | ||
| @@ -64,29 +62,28 @@ TEST_F(TApp, OneStringEqualVersion) { | @@ -64,29 +62,28 @@ TEST_F(TApp, OneStringEqualVersion) { | ||
| 64 | app.add_option("-s,--string", str); | 62 | app.add_option("-s,--string", str); |
| 65 | args = {"--string=mystring"}; | 63 | args = {"--string=mystring"}; |
| 66 | run(); | 64 | run(); |
| 67 | - EXPECT_EQ((size_t) 1, app.count("-s")); | ||
| 68 | - EXPECT_EQ((size_t) 1, app.count("--string")); | 65 | + EXPECT_EQ((size_t)1, app.count("-s")); |
| 66 | + EXPECT_EQ((size_t)1, app.count("--string")); | ||
| 69 | EXPECT_EQ(str, "mystring"); | 67 | EXPECT_EQ(str, "mystring"); |
| 70 | } | 68 | } |
| 71 | 69 | ||
| 72 | - | ||
| 73 | TEST_F(TApp, TogetherInt) { | 70 | TEST_F(TApp, TogetherInt) { |
| 74 | int i; | 71 | int i; |
| 75 | app.add_option("-i,--int", i); | 72 | app.add_option("-i,--int", i); |
| 76 | args = {"-i4"}; | 73 | args = {"-i4"}; |
| 77 | run(); | 74 | run(); |
| 78 | - EXPECT_EQ((size_t) 1, app.count("--int")); | ||
| 79 | - EXPECT_EQ((size_t) 1, app.count("-i")); | 75 | + EXPECT_EQ((size_t)1, app.count("--int")); |
| 76 | + EXPECT_EQ((size_t)1, app.count("-i")); | ||
| 80 | EXPECT_EQ(i, 4); | 77 | EXPECT_EQ(i, 4); |
| 81 | } | 78 | } |
| 82 | 79 | ||
| 83 | TEST_F(TApp, SepInt) { | 80 | TEST_F(TApp, SepInt) { |
| 84 | int i; | 81 | int i; |
| 85 | app.add_option("-i,--int", i); | 82 | app.add_option("-i,--int", i); |
| 86 | - args = {"-i","4"}; | 83 | + args = {"-i", "4"}; |
| 87 | run(); | 84 | run(); |
| 88 | - EXPECT_EQ((size_t) 1, app.count("--int")); | ||
| 89 | - EXPECT_EQ((size_t) 1, app.count("-i")); | 85 | + EXPECT_EQ((size_t)1, app.count("--int")); |
| 86 | + EXPECT_EQ((size_t)1, app.count("-i")); | ||
| 90 | EXPECT_EQ(i, 4); | 87 | EXPECT_EQ(i, 4); |
| 91 | } | 88 | } |
| 92 | 89 | ||
| @@ -95,18 +92,17 @@ TEST_F(TApp, OneStringAgain) { | @@ -95,18 +92,17 @@ TEST_F(TApp, OneStringAgain) { | ||
| 95 | app.add_option("-s,--string", str); | 92 | app.add_option("-s,--string", str); |
| 96 | args = {"--string", "mystring"}; | 93 | args = {"--string", "mystring"}; |
| 97 | run(); | 94 | run(); |
| 98 | - EXPECT_EQ((size_t) 1, app.count("-s")); | ||
| 99 | - EXPECT_EQ((size_t) 1, app.count("--string")); | 95 | + EXPECT_EQ((size_t)1, app.count("-s")); |
| 96 | + EXPECT_EQ((size_t)1, app.count("--string")); | ||
| 100 | EXPECT_EQ(str, "mystring"); | 97 | EXPECT_EQ(str, "mystring"); |
| 101 | } | 98 | } |
| 102 | 99 | ||
| 103 | - | ||
| 104 | TEST_F(TApp, DefaultStringAgain) { | 100 | TEST_F(TApp, DefaultStringAgain) { |
| 105 | std::string str = "previous"; | 101 | std::string str = "previous"; |
| 106 | app.add_option("-s,--string", str); | 102 | app.add_option("-s,--string", str); |
| 107 | run(); | 103 | run(); |
| 108 | - EXPECT_EQ((size_t) 0, app.count("-s")); | ||
| 109 | - EXPECT_EQ((size_t) 0, app.count("--string")); | 104 | + EXPECT_EQ((size_t)0, app.count("-s")); |
| 105 | + EXPECT_EQ((size_t)0, app.count("--string")); | ||
| 110 | EXPECT_EQ(str, "previous"); | 106 | EXPECT_EQ(str, "previous"); |
| 111 | } | 107 | } |
| 112 | 108 | ||
| @@ -132,14 +128,13 @@ TEST_F(TApp, LotsOfFlags) { | @@ -132,14 +128,13 @@ TEST_F(TApp, LotsOfFlags) { | ||
| 132 | app.add_flag("-A"); | 128 | app.add_flag("-A"); |
| 133 | app.add_flag("-b"); | 129 | app.add_flag("-b"); |
| 134 | 130 | ||
| 135 | - args = {"-a","-b","-aA"}; | 131 | + args = {"-a", "-b", "-aA"}; |
| 136 | run(); | 132 | run(); |
| 137 | - EXPECT_EQ((size_t) 2, app.count("-a")); | ||
| 138 | - EXPECT_EQ((size_t) 1, app.count("-b")); | ||
| 139 | - EXPECT_EQ((size_t) 1, app.count("-A")); | 133 | + EXPECT_EQ((size_t)2, app.count("-a")); |
| 134 | + EXPECT_EQ((size_t)1, app.count("-b")); | ||
| 135 | + EXPECT_EQ((size_t)1, app.count("-A")); | ||
| 140 | } | 136 | } |
| 141 | 137 | ||
| 142 | - | ||
| 143 | TEST_F(TApp, BoolAndIntFlags) { | 138 | TEST_F(TApp, BoolAndIntFlags) { |
| 144 | 139 | ||
| 145 | bool bflag; | 140 | bool bflag; |
| @@ -154,7 +149,7 @@ TEST_F(TApp, BoolAndIntFlags) { | @@ -154,7 +149,7 @@ TEST_F(TApp, BoolAndIntFlags) { | ||
| 154 | run(); | 149 | run(); |
| 155 | EXPECT_TRUE(bflag); | 150 | EXPECT_TRUE(bflag); |
| 156 | EXPECT_EQ(1, iflag); | 151 | EXPECT_EQ(1, iflag); |
| 157 | - EXPECT_EQ((unsigned int) 1, uflag); | 152 | + EXPECT_EQ((unsigned int)1, uflag); |
| 158 | 153 | ||
| 159 | app.reset(); | 154 | app.reset(); |
| 160 | 155 | ||
| @@ -168,7 +163,7 @@ TEST_F(TApp, BoolAndIntFlags) { | @@ -168,7 +163,7 @@ TEST_F(TApp, BoolAndIntFlags) { | ||
| 168 | run(); | 163 | run(); |
| 169 | EXPECT_FALSE(bflag); | 164 | EXPECT_FALSE(bflag); |
| 170 | EXPECT_EQ(3, iflag); | 165 | EXPECT_EQ(3, iflag); |
| 171 | - EXPECT_EQ((unsigned int) 2, uflag); | 166 | + EXPECT_EQ((unsigned int)2, uflag); |
| 172 | } | 167 | } |
| 173 | 168 | ||
| 174 | TEST_F(TApp, ShortOpts) { | 169 | TEST_F(TApp, ShortOpts) { |
| @@ -178,13 +173,15 @@ TEST_F(TApp, ShortOpts) { | @@ -178,13 +173,15 @@ TEST_F(TApp, ShortOpts) { | ||
| 178 | app.add_flag("-z", funnyint); | 173 | app.add_flag("-z", funnyint); |
| 179 | app.add_option("-y", someopt); | 174 | app.add_option("-y", someopt); |
| 180 | 175 | ||
| 181 | - args = {"-zzyzyz",}; | 176 | + args = { |
| 177 | + "-zzyzyz", | ||
| 178 | + }; | ||
| 182 | 179 | ||
| 183 | run(); | 180 | run(); |
| 184 | 181 | ||
| 185 | - EXPECT_EQ((size_t) 2, app.count("-z")); | ||
| 186 | - EXPECT_EQ((size_t) 1, app.count("-y")); | ||
| 187 | - EXPECT_EQ((unsigned long long) 2, funnyint); | 182 | + EXPECT_EQ((size_t)2, app.count("-z")); |
| 183 | + EXPECT_EQ((size_t)1, app.count("-y")); | ||
| 184 | + EXPECT_EQ((unsigned long long)2, funnyint); | ||
| 188 | EXPECT_EQ("zyz", someopt); | 185 | EXPECT_EQ("zyz", someopt); |
| 189 | } | 186 | } |
| 190 | 187 | ||
| @@ -200,8 +197,8 @@ TEST_F(TApp, DefaultOpts) { | @@ -200,8 +197,8 @@ TEST_F(TApp, DefaultOpts) { | ||
| 200 | 197 | ||
| 201 | run(); | 198 | run(); |
| 202 | 199 | ||
| 203 | - EXPECT_EQ((size_t) 1, app.count("i")); | ||
| 204 | - EXPECT_EQ((size_t) 1, app.count("-s")); | 200 | + EXPECT_EQ((size_t)1, app.count("i")); |
| 201 | + EXPECT_EQ((size_t)1, app.count("-s")); | ||
| 205 | EXPECT_EQ(2, i); | 202 | EXPECT_EQ(2, i); |
| 206 | EXPECT_EQ("9", s); | 203 | EXPECT_EQ("9", s); |
| 207 | } | 204 | } |
| @@ -224,7 +221,6 @@ TEST_F(TApp, RequiredFlags) { | @@ -224,7 +221,6 @@ TEST_F(TApp, RequiredFlags) { | ||
| 224 | app.reset(); | 221 | app.reset(); |
| 225 | args = {"-a", "-b"}; | 222 | args = {"-a", "-b"}; |
| 226 | run(); | 223 | run(); |
| 227 | - | ||
| 228 | } | 224 | } |
| 229 | 225 | ||
| 230 | TEST_F(TApp, Positionals) { | 226 | TEST_F(TApp, Positionals) { |
| @@ -234,17 +230,16 @@ TEST_F(TApp, Positionals) { | @@ -234,17 +230,16 @@ TEST_F(TApp, Positionals) { | ||
| 234 | app.add_option("posit1", posit1); | 230 | app.add_option("posit1", posit1); |
| 235 | app.add_option("posit2", posit2); | 231 | app.add_option("posit2", posit2); |
| 236 | 232 | ||
| 237 | - args = {"thing1","thing2"}; | 233 | + args = {"thing1", "thing2"}; |
| 238 | 234 | ||
| 239 | run(); | 235 | run(); |
| 240 | 236 | ||
| 241 | - EXPECT_EQ((size_t) 1, app.count("posit1")); | ||
| 242 | - EXPECT_EQ((size_t) 1, app.count("posit2")); | 237 | + EXPECT_EQ((size_t)1, app.count("posit1")); |
| 238 | + EXPECT_EQ((size_t)1, app.count("posit2")); | ||
| 243 | EXPECT_EQ("thing1", posit1); | 239 | EXPECT_EQ("thing1", posit1); |
| 244 | EXPECT_EQ("thing2", posit2); | 240 | EXPECT_EQ("thing2", posit2); |
| 245 | } | 241 | } |
| 246 | 242 | ||
| 247 | - | ||
| 248 | TEST_F(TApp, ForcedPositional) { | 243 | TEST_F(TApp, ForcedPositional) { |
| 249 | std::vector<std::string> posit; | 244 | std::vector<std::string> posit; |
| 250 | auto one = app.add_flag("--one"); | 245 | auto one = app.add_flag("--one"); |
| @@ -266,7 +261,6 @@ TEST_F(TApp, ForcedPositional) { | @@ -266,7 +261,6 @@ TEST_F(TApp, ForcedPositional) { | ||
| 266 | EXPECT_EQ(answers2, posit); | 261 | EXPECT_EQ(answers2, posit); |
| 267 | } | 262 | } |
| 268 | 263 | ||
| 269 | - | ||
| 270 | TEST_F(TApp, MixedPositionals) { | 264 | TEST_F(TApp, MixedPositionals) { |
| 271 | 265 | ||
| 272 | int positional_int; | 266 | int positional_int; |
| @@ -274,12 +268,12 @@ TEST_F(TApp, MixedPositionals) { | @@ -274,12 +268,12 @@ TEST_F(TApp, MixedPositionals) { | ||
| 274 | app.add_option("posit1,--posit1", positional_int, ""); | 268 | app.add_option("posit1,--posit1", positional_int, ""); |
| 275 | app.add_option("posit2,--posit2", positional_string, ""); | 269 | app.add_option("posit2,--posit2", positional_string, ""); |
| 276 | 270 | ||
| 277 | - args = {"--posit2","thing2","7"}; | 271 | + args = {"--posit2", "thing2", "7"}; |
| 278 | 272 | ||
| 279 | run(); | 273 | run(); |
| 280 | 274 | ||
| 281 | - EXPECT_EQ((size_t) 1, app.count("posit2")); | ||
| 282 | - EXPECT_EQ((size_t) 1, app.count("--posit1")); | 275 | + EXPECT_EQ((size_t)1, app.count("posit2")); |
| 276 | + EXPECT_EQ((size_t)1, app.count("--posit1")); | ||
| 283 | EXPECT_EQ(7, positional_int); | 277 | EXPECT_EQ(7, positional_int); |
| 284 | EXPECT_EQ("thing2", positional_string); | 278 | EXPECT_EQ("thing2", positional_string); |
| 285 | } | 279 | } |
| @@ -311,24 +305,22 @@ TEST_F(TApp, Reset) { | @@ -311,24 +305,22 @@ TEST_F(TApp, Reset) { | ||
| 311 | 305 | ||
| 312 | run(); | 306 | run(); |
| 313 | 307 | ||
| 314 | - EXPECT_EQ((size_t) 1, app.count("--simple")); | ||
| 315 | - EXPECT_EQ((size_t) 1, app.count("-d")); | 308 | + EXPECT_EQ((size_t)1, app.count("--simple")); |
| 309 | + EXPECT_EQ((size_t)1, app.count("-d")); | ||
| 316 | EXPECT_DOUBLE_EQ(1.2, doub); | 310 | EXPECT_DOUBLE_EQ(1.2, doub); |
| 317 | 311 | ||
| 318 | app.reset(); | 312 | app.reset(); |
| 319 | 313 | ||
| 320 | - EXPECT_EQ((size_t) 0, app.count("--simple")); | ||
| 321 | - EXPECT_EQ((size_t) 0, app.count("-d")); | ||
| 322 | - | 314 | + EXPECT_EQ((size_t)0, app.count("--simple")); |
| 315 | + EXPECT_EQ((size_t)0, app.count("-d")); | ||
| 316 | + | ||
| 323 | run(); | 317 | run(); |
| 324 | 318 | ||
| 325 | - EXPECT_EQ((size_t) 1, app.count("--simple")); | ||
| 326 | - EXPECT_EQ((size_t) 1, app.count("-d")); | 319 | + EXPECT_EQ((size_t)1, app.count("--simple")); |
| 320 | + EXPECT_EQ((size_t)1, app.count("-d")); | ||
| 327 | EXPECT_DOUBLE_EQ(1.2, doub); | 321 | EXPECT_DOUBLE_EQ(1.2, doub); |
| 328 | - | ||
| 329 | } | 322 | } |
| 330 | 323 | ||
| 331 | - | ||
| 332 | TEST_F(TApp, RemoveOption) { | 324 | TEST_F(TApp, RemoveOption) { |
| 333 | app.add_flag("--one"); | 325 | app.add_flag("--one"); |
| 334 | auto opt = app.add_flag("--two"); | 326 | auto opt = app.add_flag("--two"); |
| @@ -354,7 +346,6 @@ TEST_F(TApp, FileNotExists) { | @@ -354,7 +346,6 @@ TEST_F(TApp, FileNotExists) { | ||
| 354 | 346 | ||
| 355 | app.reset(); | 347 | app.reset(); |
| 356 | 348 | ||
| 357 | - | ||
| 358 | bool ok = static_cast<bool>(std::ofstream(myfile.c_str()).put('a')); // create file | 349 | bool ok = static_cast<bool>(std::ofstream(myfile.c_str()).put('a')); // create file |
| 359 | EXPECT_TRUE(ok); | 350 | EXPECT_TRUE(ok); |
| 360 | EXPECT_THROW(run(), CLI::ValidationError); | 351 | EXPECT_THROW(run(), CLI::ValidationError); |
| @@ -388,7 +379,7 @@ TEST_F(TApp, InSet) { | @@ -388,7 +379,7 @@ TEST_F(TApp, InSet) { | ||
| 388 | 379 | ||
| 389 | std::string choice; | 380 | std::string choice; |
| 390 | app.add_set("-q,--quick", choice, {"one", "two", "three"}); | 381 | app.add_set("-q,--quick", choice, {"one", "two", "three"}); |
| 391 | - | 382 | + |
| 392 | args = {"--quick", "two"}; | 383 | args = {"--quick", "two"}; |
| 393 | 384 | ||
| 394 | run(); | 385 | run(); |
| @@ -404,7 +395,7 @@ TEST_F(TApp, InIntSet) { | @@ -404,7 +395,7 @@ TEST_F(TApp, InIntSet) { | ||
| 404 | 395 | ||
| 405 | int choice; | 396 | int choice; |
| 406 | app.add_set("-q,--quick", choice, {1, 2, 3}); | 397 | app.add_set("-q,--quick", choice, {1, 2, 3}); |
| 407 | - | 398 | + |
| 408 | args = {"--quick", "2"}; | 399 | args = {"--quick", "2"}; |
| 409 | 400 | ||
| 410 | run(); | 401 | run(); |
| @@ -420,7 +411,7 @@ TEST_F(TApp, FailSet) { | @@ -420,7 +411,7 @@ TEST_F(TApp, FailSet) { | ||
| 420 | 411 | ||
| 421 | int choice; | 412 | int choice; |
| 422 | app.add_set("-q,--quick", choice, {1, 2, 3}); | 413 | app.add_set("-q,--quick", choice, {1, 2, 3}); |
| 423 | - | 414 | + |
| 424 | args = {"--quick", "3", "--quick=2"}; | 415 | args = {"--quick", "3", "--quick=2"}; |
| 425 | EXPECT_THROW(run(), CLI::ConversionError); | 416 | EXPECT_THROW(run(), CLI::ConversionError); |
| 426 | 417 | ||
| @@ -430,12 +421,11 @@ TEST_F(TApp, FailSet) { | @@ -430,12 +421,11 @@ TEST_F(TApp, FailSet) { | ||
| 430 | EXPECT_THROW(run(), CLI::ConversionError); | 421 | EXPECT_THROW(run(), CLI::ConversionError); |
| 431 | } | 422 | } |
| 432 | 423 | ||
| 433 | - | ||
| 434 | TEST_F(TApp, InSetIgnoreCase) { | 424 | TEST_F(TApp, InSetIgnoreCase) { |
| 435 | 425 | ||
| 436 | std::string choice; | 426 | std::string choice; |
| 437 | app.add_set_ignore_case("-q,--quick", choice, {"one", "Two", "THREE"}); | 427 | app.add_set_ignore_case("-q,--quick", choice, {"one", "Two", "THREE"}); |
| 438 | - | 428 | + |
| 439 | args = {"--quick", "One"}; | 429 | args = {"--quick", "One"}; |
| 440 | run(); | 430 | run(); |
| 441 | EXPECT_EQ("one", choice); | 431 | EXPECT_EQ("one", choice); |
| @@ -450,7 +440,6 @@ TEST_F(TApp, InSetIgnoreCase) { | @@ -450,7 +440,6 @@ TEST_F(TApp, InSetIgnoreCase) { | ||
| 450 | run(); | 440 | run(); |
| 451 | EXPECT_EQ("THREE", choice); // Keeps caps from set | 441 | EXPECT_EQ("THREE", choice); // Keeps caps from set |
| 452 | 442 | ||
| 453 | - | ||
| 454 | app.reset(); | 443 | app.reset(); |
| 455 | args = {"--quick", "four"}; | 444 | args = {"--quick", "four"}; |
| 456 | EXPECT_THROW(run(), CLI::ConversionError); | 445 | EXPECT_THROW(run(), CLI::ConversionError); |
| @@ -458,54 +447,50 @@ TEST_F(TApp, InSetIgnoreCase) { | @@ -458,54 +447,50 @@ TEST_F(TApp, InSetIgnoreCase) { | ||
| 458 | app.reset(); | 447 | app.reset(); |
| 459 | args = {"--quick=one", "--quick=two"}; | 448 | args = {"--quick=one", "--quick=two"}; |
| 460 | EXPECT_THROW(run(), CLI::ConversionError); | 449 | EXPECT_THROW(run(), CLI::ConversionError); |
| 461 | - | ||
| 462 | } | 450 | } |
| 463 | 451 | ||
| 464 | TEST_F(TApp, VectorFixedString) { | 452 | TEST_F(TApp, VectorFixedString) { |
| 465 | std::vector<std::string> strvec; | 453 | std::vector<std::string> strvec; |
| 466 | std::vector<std::string> answer{"mystring", "mystring2", "mystring3"}; | 454 | std::vector<std::string> answer{"mystring", "mystring2", "mystring3"}; |
| 467 | 455 | ||
| 468 | - CLI::Option* opt = app.add_option("-s,--string", strvec)->expected(3); | 456 | + CLI::Option *opt = app.add_option("-s,--string", strvec)->expected(3); |
| 469 | EXPECT_EQ(3, opt->get_expected()); | 457 | EXPECT_EQ(3, opt->get_expected()); |
| 470 | - | 458 | + |
| 471 | args = {"--string", "mystring", "mystring2", "mystring3"}; | 459 | args = {"--string", "mystring", "mystring2", "mystring3"}; |
| 472 | run(); | 460 | run(); |
| 473 | - EXPECT_EQ((size_t) 3, app.count("--string")); | 461 | + EXPECT_EQ((size_t)3, app.count("--string")); |
| 474 | EXPECT_EQ(answer, strvec); | 462 | EXPECT_EQ(answer, strvec); |
| 475 | } | 463 | } |
| 476 | 464 | ||
| 477 | - | ||
| 478 | - | ||
| 479 | TEST_F(TApp, VectorUnlimString) { | 465 | TEST_F(TApp, VectorUnlimString) { |
| 480 | std::vector<std::string> strvec; | 466 | std::vector<std::string> strvec; |
| 481 | std::vector<std::string> answer{"mystring", "mystring2", "mystring3"}; | 467 | std::vector<std::string> answer{"mystring", "mystring2", "mystring3"}; |
| 482 | 468 | ||
| 483 | - CLI::Option* opt = app.add_option("-s,--string", strvec); | 469 | + CLI::Option *opt = app.add_option("-s,--string", strvec); |
| 484 | EXPECT_EQ(-1, opt->get_expected()); | 470 | EXPECT_EQ(-1, opt->get_expected()); |
| 485 | 471 | ||
| 486 | args = {"--string", "mystring", "mystring2", "mystring3"}; | 472 | args = {"--string", "mystring", "mystring2", "mystring3"}; |
| 487 | run(); | 473 | run(); |
| 488 | - EXPECT_EQ((size_t) 3, app.count("--string")); | 474 | + EXPECT_EQ((size_t)3, app.count("--string")); |
| 489 | EXPECT_EQ(answer, strvec); | 475 | EXPECT_EQ(answer, strvec); |
| 490 | - | 476 | + |
| 491 | app.reset(); | 477 | app.reset(); |
| 492 | args = {"-s", "mystring", "mystring2", "mystring3"}; | 478 | args = {"-s", "mystring", "mystring2", "mystring3"}; |
| 493 | run(); | 479 | run(); |
| 494 | - EXPECT_EQ((size_t) 3, app.count("--string")); | 480 | + EXPECT_EQ((size_t)3, app.count("--string")); |
| 495 | EXPECT_EQ(answer, strvec); | 481 | EXPECT_EQ(answer, strvec); |
| 496 | } | 482 | } |
| 497 | 483 | ||
| 498 | - | ||
| 499 | TEST_F(TApp, VectorFancyOpts) { | 484 | TEST_F(TApp, VectorFancyOpts) { |
| 500 | std::vector<std::string> strvec; | 485 | std::vector<std::string> strvec; |
| 501 | std::vector<std::string> answer{"mystring", "mystring2", "mystring3"}; | 486 | std::vector<std::string> answer{"mystring", "mystring2", "mystring3"}; |
| 502 | 487 | ||
| 503 | - CLI::Option* opt = app.add_option("-s,--string", strvec)->required()->expected(3); | 488 | + CLI::Option *opt = app.add_option("-s,--string", strvec)->required()->expected(3); |
| 504 | EXPECT_EQ(3, opt->get_expected()); | 489 | EXPECT_EQ(3, opt->get_expected()); |
| 505 | 490 | ||
| 506 | args = {"--string", "mystring", "mystring2", "mystring3"}; | 491 | args = {"--string", "mystring", "mystring2", "mystring3"}; |
| 507 | run(); | 492 | run(); |
| 508 | - EXPECT_EQ((size_t) 3, app.count("--string")); | 493 | + EXPECT_EQ((size_t)3, app.count("--string")); |
| 509 | EXPECT_EQ(answer, strvec); | 494 | EXPECT_EQ(answer, strvec); |
| 510 | 495 | ||
| 511 | app.reset(); | 496 | app.reset(); |
| @@ -517,7 +502,7 @@ TEST_F(TApp, VectorFancyOpts) { | @@ -517,7 +502,7 @@ TEST_F(TApp, VectorFancyOpts) { | ||
| 517 | } | 502 | } |
| 518 | 503 | ||
| 519 | TEST_F(TApp, RequiresFlags) { | 504 | TEST_F(TApp, RequiresFlags) { |
| 520 | - CLI::Option* opt = app.add_flag("-s,--string"); | 505 | + CLI::Option *opt = app.add_flag("-s,--string"); |
| 521 | app.add_flag("--both")->requires(opt); | 506 | app.add_flag("--both")->requires(opt); |
| 522 | 507 | ||
| 523 | run(); | 508 | run(); |
| @@ -535,9 +520,8 @@ TEST_F(TApp, RequiresFlags) { | @@ -535,9 +520,8 @@ TEST_F(TApp, RequiresFlags) { | ||
| 535 | EXPECT_THROW(run(), CLI::RequiresError); | 520 | EXPECT_THROW(run(), CLI::RequiresError); |
| 536 | } | 521 | } |
| 537 | 522 | ||
| 538 | - | ||
| 539 | TEST_F(TApp, ExcludesFlags) { | 523 | TEST_F(TApp, ExcludesFlags) { |
| 540 | - CLI::Option* opt = app.add_flag("-s,--string"); | 524 | + CLI::Option *opt = app.add_flag("-s,--string"); |
| 541 | app.add_flag("--nostr")->excludes(opt); | 525 | app.add_flag("--nostr")->excludes(opt); |
| 542 | 526 | ||
| 543 | run(); | 527 | run(); |
| @@ -560,9 +544,9 @@ TEST_F(TApp, ExcludesFlags) { | @@ -560,9 +544,9 @@ TEST_F(TApp, ExcludesFlags) { | ||
| 560 | } | 544 | } |
| 561 | 545 | ||
| 562 | TEST_F(TApp, ExcludesMixedFlags) { | 546 | TEST_F(TApp, ExcludesMixedFlags) { |
| 563 | - CLI::Option* opt1 = app.add_flag("--opt1"); | 547 | + CLI::Option *opt1 = app.add_flag("--opt1"); |
| 564 | app.add_flag("--opt2"); | 548 | app.add_flag("--opt2"); |
| 565 | - CLI::Option* opt3 = app.add_flag("--opt3"); | 549 | + CLI::Option *opt3 = app.add_flag("--opt3"); |
| 566 | app.add_flag("--no")->excludes(opt1, "--opt2", opt3); | 550 | app.add_flag("--no")->excludes(opt1, "--opt2", opt3); |
| 567 | 551 | ||
| 568 | run(); | 552 | run(); |
| @@ -585,9 +569,9 @@ TEST_F(TApp, ExcludesMixedFlags) { | @@ -585,9 +569,9 @@ TEST_F(TApp, ExcludesMixedFlags) { | ||
| 585 | } | 569 | } |
| 586 | 570 | ||
| 587 | TEST_F(TApp, RequiresMultiFlags) { | 571 | TEST_F(TApp, RequiresMultiFlags) { |
| 588 | - CLI::Option* opt1 = app.add_flag("--opt1"); | ||
| 589 | - CLI::Option* opt2 = app.add_flag("--opt2"); | ||
| 590 | - CLI::Option* opt3 = app.add_flag("--opt3"); | 572 | + CLI::Option *opt1 = app.add_flag("--opt1"); |
| 573 | + CLI::Option *opt2 = app.add_flag("--opt2"); | ||
| 574 | + CLI::Option *opt3 = app.add_flag("--opt3"); | ||
| 591 | app.add_flag("--optall")->requires(opt1, opt2, opt3); | 575 | app.add_flag("--optall")->requires(opt1, opt2, opt3); |
| 592 | 576 | ||
| 593 | run(); | 577 | run(); |
| @@ -618,7 +602,7 @@ TEST_F(TApp, RequiresMultiFlags) { | @@ -618,7 +602,7 @@ TEST_F(TApp, RequiresMultiFlags) { | ||
| 618 | } | 602 | } |
| 619 | 603 | ||
| 620 | TEST_F(TApp, RequiresMixedFlags) { | 604 | TEST_F(TApp, RequiresMixedFlags) { |
| 621 | - CLI::Option* opt1 = app.add_flag("--opt1"); | 605 | + CLI::Option *opt1 = app.add_flag("--opt1"); |
| 622 | app.add_flag("--opt2"); | 606 | app.add_flag("--opt2"); |
| 623 | app.add_flag("--opt3"); | 607 | app.add_flag("--opt3"); |
| 624 | app.add_flag("--optall")->requires(opt1, "--opt2", "--opt3"); | 608 | app.add_flag("--optall")->requires(opt1, "--opt2", "--opt3"); |
| @@ -651,8 +635,8 @@ TEST_F(TApp, RequiresMixedFlags) { | @@ -651,8 +635,8 @@ TEST_F(TApp, RequiresMixedFlags) { | ||
| 651 | } | 635 | } |
| 652 | 636 | ||
| 653 | TEST_F(TApp, RequiresChainedFlags) { | 637 | TEST_F(TApp, RequiresChainedFlags) { |
| 654 | - CLI::Option* opt1 = app.add_flag("--opt1"); | ||
| 655 | - CLI::Option* opt2 = app.add_flag("--opt2")->requires(opt1); | 638 | + CLI::Option *opt1 = app.add_flag("--opt1"); |
| 639 | + CLI::Option *opt2 = app.add_flag("--opt2")->requires(opt1); | ||
| 656 | app.add_flag("--opt3")->requires(opt2); | 640 | app.add_flag("--opt3")->requires(opt2); |
| 657 | 641 | ||
| 658 | run(); | 642 | run(); |
| @@ -690,13 +674,13 @@ TEST_F(TApp, Env) { | @@ -690,13 +674,13 @@ TEST_F(TApp, Env) { | ||
| 690 | 674 | ||
| 691 | put_env("CLI11_TEST_ENV_TMP", "2"); | 675 | put_env("CLI11_TEST_ENV_TMP", "2"); |
| 692 | 676 | ||
| 693 | - int val=1; | ||
| 694 | - CLI::Option* vopt = app.add_option("--tmp", val)->envname("CLI11_TEST_ENV_TMP"); | 677 | + int val = 1; |
| 678 | + CLI::Option *vopt = app.add_option("--tmp", val)->envname("CLI11_TEST_ENV_TMP"); | ||
| 695 | 679 | ||
| 696 | run(); | 680 | run(); |
| 697 | 681 | ||
| 698 | EXPECT_EQ(2, val); | 682 | EXPECT_EQ(2, val); |
| 699 | - EXPECT_EQ((size_t) 1, vopt->count()); | 683 | + EXPECT_EQ((size_t)1, vopt->count()); |
| 700 | 684 | ||
| 701 | app.reset(); | 685 | app.reset(); |
| 702 | vopt->required(); | 686 | vopt->required(); |
| @@ -708,8 +692,8 @@ TEST_F(TApp, Env) { | @@ -708,8 +692,8 @@ TEST_F(TApp, Env) { | ||
| 708 | } | 692 | } |
| 709 | 693 | ||
| 710 | TEST_F(TApp, RangeInt) { | 694 | TEST_F(TApp, RangeInt) { |
| 711 | - int x=0; | ||
| 712 | - app.add_option("--one", x)->check(CLI::Range(3,6)); | 695 | + int x = 0; |
| 696 | + app.add_option("--one", x)->check(CLI::Range(3, 6)); | ||
| 713 | 697 | ||
| 714 | args = {"--one=1"}; | 698 | args = {"--one=1"}; |
| 715 | EXPECT_THROW(run(), CLI::ValidationError); | 699 | EXPECT_THROW(run(), CLI::ValidationError); |
| @@ -733,9 +717,9 @@ TEST_F(TApp, RangeInt) { | @@ -733,9 +717,9 @@ TEST_F(TApp, RangeInt) { | ||
| 733 | 717 | ||
| 734 | TEST_F(TApp, RangeDouble) { | 718 | TEST_F(TApp, RangeDouble) { |
| 735 | 719 | ||
| 736 | - double x=0; | 720 | + double x = 0; |
| 737 | /// Note that this must be a double in Range, too | 721 | /// Note that this must be a double in Range, too |
| 738 | - app.add_option("--one", x)->check(CLI::Range(3.0,6.0)); | 722 | + app.add_option("--one", x)->check(CLI::Range(3.0, 6.0)); |
| 739 | 723 | ||
| 740 | args = {"--one=1"}; | 724 | args = {"--one=1"}; |
| 741 | EXPECT_THROW(run(), CLI::ValidationError); | 725 | EXPECT_THROW(run(), CLI::ValidationError); |
| @@ -768,10 +752,9 @@ TEST_F(TApp, AllowExtras) { | @@ -768,10 +752,9 @@ TEST_F(TApp, AllowExtras) { | ||
| 768 | 752 | ||
| 769 | args = {"-x", "-f"}; | 753 | args = {"-x", "-f"}; |
| 770 | std::vector<std::string> left_over; | 754 | std::vector<std::string> left_over; |
| 771 | - EXPECT_NO_THROW({left_over = run();}); | 755 | + EXPECT_NO_THROW({ left_over = run(); }); |
| 772 | EXPECT_TRUE(val); | 756 | EXPECT_TRUE(val); |
| 773 | EXPECT_EQ(std::vector<std::string>({"-x"}), left_over); | 757 | EXPECT_EQ(std::vector<std::string>({"-x"}), left_over); |
| 774 | - | ||
| 775 | } | 758 | } |
| 776 | 759 | ||
| 777 | TEST_F(TApp, AllowExtrasOrder) { | 760 | TEST_F(TApp, AllowExtrasOrder) { |
| @@ -780,14 +763,13 @@ TEST_F(TApp, AllowExtrasOrder) { | @@ -780,14 +763,13 @@ TEST_F(TApp, AllowExtrasOrder) { | ||
| 780 | 763 | ||
| 781 | args = {"-x", "-f"}; | 764 | args = {"-x", "-f"}; |
| 782 | std::vector<std::string> left_over; | 765 | std::vector<std::string> left_over; |
| 783 | - EXPECT_NO_THROW({left_over = run();}); | 766 | + EXPECT_NO_THROW({ left_over = run(); }); |
| 784 | EXPECT_EQ(std::vector<std::string>({"-f", "-x"}), left_over); | 767 | EXPECT_EQ(std::vector<std::string>({"-f", "-x"}), left_over); |
| 785 | app.reset(); | 768 | app.reset(); |
| 786 | 769 | ||
| 787 | std::vector<std::string> left_over_2; | 770 | std::vector<std::string> left_over_2; |
| 788 | left_over_2 = app.parse(left_over); | 771 | left_over_2 = app.parse(left_over); |
| 789 | EXPECT_EQ(left_over, left_over_2); | 772 | EXPECT_EQ(left_over, left_over_2); |
| 790 | - | ||
| 791 | } | 773 | } |
| 792 | 774 | ||
| 793 | // Test horrible error | 775 | // Test horrible error |
tests/CreationTest.cpp
| @@ -53,10 +53,9 @@ TEST_F(TApp, AddingMultipleInfPositionals) { | @@ -53,10 +53,9 @@ TEST_F(TApp, AddingMultipleInfPositionals) { | ||
| 53 | EXPECT_THROW(run(), CLI::InvalidError); | 53 | EXPECT_THROW(run(), CLI::InvalidError); |
| 54 | } | 54 | } |
| 55 | 55 | ||
| 56 | - | ||
| 57 | TEST_F(TApp, AddingMultipleInfPositionalsSubcom) { | 56 | TEST_F(TApp, AddingMultipleInfPositionalsSubcom) { |
| 58 | std::vector<std::string> one, two; | 57 | std::vector<std::string> one, two; |
| 59 | - CLI::App* below = app.add_subcommand("below"); | 58 | + CLI::App *below = app.add_subcommand("below"); |
| 60 | below->add_option("one", one); | 59 | below->add_option("one", one); |
| 61 | below->add_option("two", two); | 60 | below->add_option("two", two); |
| 62 | 61 | ||
| @@ -84,28 +83,26 @@ TEST_F(TApp, MultipleSubcomMatchingWithCaseFirst) { | @@ -84,28 +83,26 @@ TEST_F(TApp, MultipleSubcomMatchingWithCaseFirst) { | ||
| 84 | TEST_F(TApp, MultipleSubcomMatchingWithCaseInplace) { | 83 | TEST_F(TApp, MultipleSubcomMatchingWithCaseInplace) { |
| 85 | app.add_subcommand("first"); | 84 | app.add_subcommand("first"); |
| 86 | auto first = app.add_subcommand("fIrst"); | 85 | auto first = app.add_subcommand("fIrst"); |
| 87 | - | 86 | + |
| 88 | EXPECT_THROW(first->ignore_case(), CLI::OptionAlreadyAdded); | 87 | EXPECT_THROW(first->ignore_case(), CLI::OptionAlreadyAdded); |
| 89 | } | 88 | } |
| 90 | 89 | ||
| 91 | TEST_F(TApp, MultipleSubcomMatchingWithCaseInplace2) { | 90 | TEST_F(TApp, MultipleSubcomMatchingWithCaseInplace2) { |
| 92 | auto first = app.add_subcommand("first"); | 91 | auto first = app.add_subcommand("first"); |
| 93 | app.add_subcommand("fIrst"); | 92 | app.add_subcommand("fIrst"); |
| 94 | - | 93 | + |
| 95 | EXPECT_THROW(first->ignore_case(), CLI::OptionAlreadyAdded); | 94 | EXPECT_THROW(first->ignore_case(), CLI::OptionAlreadyAdded); |
| 96 | } | 95 | } |
| 97 | 96 | ||
| 98 | TEST_F(TApp, MultipleSubcomNoMatchingInplace2) { | 97 | TEST_F(TApp, MultipleSubcomNoMatchingInplace2) { |
| 99 | auto first = app.add_subcommand("first"); | 98 | auto first = app.add_subcommand("first"); |
| 100 | auto second = app.add_subcommand("second"); | 99 | auto second = app.add_subcommand("second"); |
| 101 | - | 100 | + |
| 102 | EXPECT_NO_THROW(first->ignore_case()); | 101 | EXPECT_NO_THROW(first->ignore_case()); |
| 103 | EXPECT_NO_THROW(second->ignore_case()); | 102 | EXPECT_NO_THROW(second->ignore_case()); |
| 104 | } | 103 | } |
| 105 | 104 | ||
| 106 | -TEST_F(TApp, IncorrectConstructionFlagPositional1) { | ||
| 107 | - EXPECT_THROW(app.add_flag("cat"), CLI::IncorrectConstruction); | ||
| 108 | -} | 105 | +TEST_F(TApp, IncorrectConstructionFlagPositional1) { EXPECT_THROW(app.add_flag("cat"), CLI::IncorrectConstruction); } |
| 109 | 106 | ||
| 110 | TEST_F(TApp, IncorrectConstructionFlagPositional2) { | 107 | TEST_F(TApp, IncorrectConstructionFlagPositional2) { |
| 111 | int x; | 108 | int x; |
| @@ -142,40 +139,40 @@ TEST_F(TApp, IncorrectConstructionVectorAsFlag) { | @@ -142,40 +139,40 @@ TEST_F(TApp, IncorrectConstructionVectorAsFlag) { | ||
| 142 | 139 | ||
| 143 | TEST_F(TApp, IncorrectConstructionRequiresCannotFind) { | 140 | TEST_F(TApp, IncorrectConstructionRequiresCannotFind) { |
| 144 | auto cat = app.add_flag("--cat"); | 141 | auto cat = app.add_flag("--cat"); |
| 145 | - EXPECT_THROW(cat->requires("--nothing"),CLI::IncorrectConstruction); | 142 | + EXPECT_THROW(cat->requires("--nothing"), CLI::IncorrectConstruction); |
| 146 | } | 143 | } |
| 147 | 144 | ||
| 148 | TEST_F(TApp, IncorrectConstructionExcludesCannotFind) { | 145 | TEST_F(TApp, IncorrectConstructionExcludesCannotFind) { |
| 149 | auto cat = app.add_flag("--cat"); | 146 | auto cat = app.add_flag("--cat"); |
| 150 | - EXPECT_THROW(cat->excludes("--nothing"),CLI::IncorrectConstruction); | 147 | + EXPECT_THROW(cat->excludes("--nothing"), CLI::IncorrectConstruction); |
| 151 | } | 148 | } |
| 152 | 149 | ||
| 153 | TEST_F(TApp, IncorrectConstructionDuplicateRequires) { | 150 | TEST_F(TApp, IncorrectConstructionDuplicateRequires) { |
| 154 | auto cat = app.add_flag("--cat"); | 151 | auto cat = app.add_flag("--cat"); |
| 155 | auto other = app.add_flag("--other"); | 152 | auto other = app.add_flag("--other"); |
| 156 | ASSERT_NO_THROW(cat->requires(other)); | 153 | ASSERT_NO_THROW(cat->requires(other)); |
| 157 | - EXPECT_THROW(cat->requires(other),CLI::OptionAlreadyAdded); | 154 | + EXPECT_THROW(cat->requires(other), CLI::OptionAlreadyAdded); |
| 158 | } | 155 | } |
| 159 | 156 | ||
| 160 | TEST_F(TApp, IncorrectConstructionDuplicateRequiresTxt) { | 157 | TEST_F(TApp, IncorrectConstructionDuplicateRequiresTxt) { |
| 161 | auto cat = app.add_flag("--cat"); | 158 | auto cat = app.add_flag("--cat"); |
| 162 | app.add_flag("--other"); | 159 | app.add_flag("--other"); |
| 163 | ASSERT_NO_THROW(cat->requires("--other")); | 160 | ASSERT_NO_THROW(cat->requires("--other")); |
| 164 | - EXPECT_THROW(cat->requires("--other"),CLI::OptionAlreadyAdded); | 161 | + EXPECT_THROW(cat->requires("--other"), CLI::OptionAlreadyAdded); |
| 165 | } | 162 | } |
| 166 | 163 | ||
| 167 | TEST_F(TApp, IncorrectConstructionDuplicateExcludes) { | 164 | TEST_F(TApp, IncorrectConstructionDuplicateExcludes) { |
| 168 | auto cat = app.add_flag("--cat"); | 165 | auto cat = app.add_flag("--cat"); |
| 169 | auto other = app.add_flag("--other"); | 166 | auto other = app.add_flag("--other"); |
| 170 | ASSERT_NO_THROW(cat->excludes(other)); | 167 | ASSERT_NO_THROW(cat->excludes(other)); |
| 171 | - EXPECT_THROW(cat->excludes(other),CLI::OptionAlreadyAdded); | 168 | + EXPECT_THROW(cat->excludes(other), CLI::OptionAlreadyAdded); |
| 172 | } | 169 | } |
| 173 | 170 | ||
| 174 | TEST_F(TApp, IncorrectConstructionDuplicateExcludesTxt) { | 171 | TEST_F(TApp, IncorrectConstructionDuplicateExcludesTxt) { |
| 175 | auto cat = app.add_flag("--cat"); | 172 | auto cat = app.add_flag("--cat"); |
| 176 | app.add_flag("--other"); | 173 | app.add_flag("--other"); |
| 177 | ASSERT_NO_THROW(cat->excludes("--other")); | 174 | ASSERT_NO_THROW(cat->excludes("--other")); |
| 178 | - EXPECT_THROW(cat->excludes("--other"),CLI::OptionAlreadyAdded); | 175 | + EXPECT_THROW(cat->excludes("--other"), CLI::OptionAlreadyAdded); |
| 179 | } | 176 | } |
| 180 | 177 | ||
| 181 | TEST_F(TApp, CheckName) { | 178 | TEST_F(TApp, CheckName) { |
| @@ -186,7 +183,7 @@ TEST_F(TApp, CheckName) { | @@ -186,7 +183,7 @@ TEST_F(TApp, CheckName) { | ||
| 186 | int x, y; | 183 | int x, y; |
| 187 | auto pos1 = app.add_option("pos1", x); | 184 | auto pos1 = app.add_option("pos1", x); |
| 188 | auto pos2 = app.add_option("pOs2", y); | 185 | auto pos2 = app.add_option("pOs2", y); |
| 189 | - | 186 | + |
| 190 | EXPECT_TRUE(long1->check_name("--long1")); | 187 | EXPECT_TRUE(long1->check_name("--long1")); |
| 191 | EXPECT_FALSE(long1->check_name("--lonG1")); | 188 | EXPECT_FALSE(long1->check_name("--lonG1")); |
| 192 | 189 |
tests/HelpTest.cpp
| @@ -20,7 +20,6 @@ TEST(THelp, Basic) { | @@ -20,7 +20,6 @@ TEST(THelp, Basic) { | ||
| 20 | EXPECT_THAT(help, HasSubstr("-h,--help")); | 20 | EXPECT_THAT(help, HasSubstr("-h,--help")); |
| 21 | EXPECT_THAT(help, HasSubstr("Options:")); | 21 | EXPECT_THAT(help, HasSubstr("Options:")); |
| 22 | EXPECT_THAT(help, HasSubstr("Usage:")); | 22 | EXPECT_THAT(help, HasSubstr("Usage:")); |
| 23 | - | ||
| 24 | } | 23 | } |
| 25 | 24 | ||
| 26 | TEST(THelp, OptionalPositional) { | 25 | TEST(THelp, OptionalPositional) { |
| @@ -44,11 +43,9 @@ TEST(THelp, Hidden) { | @@ -44,11 +43,9 @@ TEST(THelp, Hidden) { | ||
| 44 | CLI::App app{"My prog"}; | 43 | CLI::App app{"My prog"}; |
| 45 | 44 | ||
| 46 | std::string x; | 45 | std::string x; |
| 47 | - app.add_option("something", x, "My option here") | ||
| 48 | - ->group("Hidden"); | 46 | + app.add_option("something", x, "My option here")->group("Hidden"); |
| 49 | std::string y; | 47 | std::string y; |
| 50 | - app.add_option("--another", y) | ||
| 51 | - ->group("Hidden"); | 48 | + app.add_option("--another", y)->group("Hidden"); |
| 52 | 49 | ||
| 53 | std::string help = app.help(); | 50 | std::string help = app.help(); |
| 54 | 51 | ||
| @@ -73,7 +70,6 @@ TEST(THelp, OptionalPositionalAndOptions) { | @@ -73,7 +70,6 @@ TEST(THelp, OptionalPositionalAndOptions) { | ||
| 73 | EXPECT_THAT(help, HasSubstr("-h,--help")); | 70 | EXPECT_THAT(help, HasSubstr("-h,--help")); |
| 74 | EXPECT_THAT(help, HasSubstr("Options:")); | 71 | EXPECT_THAT(help, HasSubstr("Options:")); |
| 75 | EXPECT_THAT(help, HasSubstr("Usage: program [OPTIONS] [something]")); | 72 | EXPECT_THAT(help, HasSubstr("Usage: program [OPTIONS] [something]")); |
| 76 | - | ||
| 77 | } | 73 | } |
| 78 | 74 | ||
| 79 | TEST(THelp, RequiredPositionalAndOptions) { | 75 | TEST(THelp, RequiredPositionalAndOptions) { |
| @@ -81,8 +77,7 @@ TEST(THelp, RequiredPositionalAndOptions) { | @@ -81,8 +77,7 @@ TEST(THelp, RequiredPositionalAndOptions) { | ||
| 81 | app.add_flag("-q,--quick"); | 77 | app.add_flag("-q,--quick"); |
| 82 | 78 | ||
| 83 | std::string x; | 79 | std::string x; |
| 84 | - app.add_option("something", x, "My option here") | ||
| 85 | - ->required(); | 80 | + app.add_option("something", x, "My option here")->required(); |
| 86 | 81 | ||
| 87 | std::string help = app.help(); | 82 | std::string help = app.help(); |
| 88 | 83 | ||
| @@ -93,7 +88,6 @@ TEST(THelp, RequiredPositionalAndOptions) { | @@ -93,7 +88,6 @@ TEST(THelp, RequiredPositionalAndOptions) { | ||
| 93 | EXPECT_THAT(help, HasSubstr("Usage: program [OPTIONS] something")); | 88 | EXPECT_THAT(help, HasSubstr("Usage: program [OPTIONS] something")); |
| 94 | } | 89 | } |
| 95 | 90 | ||
| 96 | - | ||
| 97 | TEST(THelp, MultiOpts) { | 91 | TEST(THelp, MultiOpts) { |
| 98 | CLI::App app{"My prog"}; | 92 | CLI::App app{"My prog"}; |
| 99 | std::vector<int> x, y; | 93 | std::vector<int> x, y; |
| @@ -111,7 +105,7 @@ TEST(THelp, MultiOpts) { | @@ -111,7 +105,7 @@ TEST(THelp, MultiOpts) { | ||
| 111 | 105 | ||
| 112 | TEST(THelp, VectorOpts) { | 106 | TEST(THelp, VectorOpts) { |
| 113 | CLI::App app{"My prog"}; | 107 | CLI::App app{"My prog"}; |
| 114 | - std::vector<int> x = {1,2}; | 108 | + std::vector<int> x = {1, 2}; |
| 115 | app.add_option("-q,--quick", x, "", true); | 109 | app.add_option("-q,--quick", x, "", true); |
| 116 | 110 | ||
| 117 | std::string help = app.help(); | 111 | std::string help = app.help(); |
| @@ -136,8 +130,6 @@ TEST(THelp, MultiPosOpts) { | @@ -136,8 +130,6 @@ TEST(THelp, MultiPosOpts) { | ||
| 136 | EXPECT_THAT(help, HasSubstr("[vals...]")); | 130 | EXPECT_THAT(help, HasSubstr("[vals...]")); |
| 137 | } | 131 | } |
| 138 | 132 | ||
| 139 | - | ||
| 140 | - | ||
| 141 | TEST(THelp, EnvName) { | 133 | TEST(THelp, EnvName) { |
| 142 | CLI::App app{"My prog"}; | 134 | CLI::App app{"My prog"}; |
| 143 | std::string input; | 135 | std::string input; |
| @@ -151,7 +143,7 @@ TEST(THelp, EnvName) { | @@ -151,7 +143,7 @@ TEST(THelp, EnvName) { | ||
| 151 | TEST(THelp, Requires) { | 143 | TEST(THelp, Requires) { |
| 152 | CLI::App app{"My prog"}; | 144 | CLI::App app{"My prog"}; |
| 153 | 145 | ||
| 154 | - CLI::Option* op1 = app.add_flag("--op1"); | 146 | + CLI::Option *op1 = app.add_flag("--op1"); |
| 155 | app.add_flag("--op2")->requires(op1); | 147 | app.add_flag("--op2")->requires(op1); |
| 156 | 148 | ||
| 157 | std::string help = app.help(); | 149 | std::string help = app.help(); |
| @@ -162,9 +154,9 @@ TEST(THelp, Requires) { | @@ -162,9 +154,9 @@ TEST(THelp, Requires) { | ||
| 162 | TEST(THelp, RequiresPositional) { | 154 | TEST(THelp, RequiresPositional) { |
| 163 | CLI::App app{"My prog"}; | 155 | CLI::App app{"My prog"}; |
| 164 | 156 | ||
| 165 | - int x,y; | 157 | + int x, y; |
| 166 | 158 | ||
| 167 | - CLI::Option* op1 = app.add_option("op1", x, "one"); | 159 | + CLI::Option *op1 = app.add_option("op1", x, "one"); |
| 168 | app.add_option("op2", y, "two")->requires(op1); | 160 | app.add_option("op2", y, "two")->requires(op1); |
| 169 | 161 | ||
| 170 | std::string help = app.help(); | 162 | std::string help = app.help(); |
| @@ -176,7 +168,7 @@ TEST(THelp, RequiresPositional) { | @@ -176,7 +168,7 @@ TEST(THelp, RequiresPositional) { | ||
| 176 | TEST(THelp, Excludes) { | 168 | TEST(THelp, Excludes) { |
| 177 | CLI::App app{"My prog"}; | 169 | CLI::App app{"My prog"}; |
| 178 | 170 | ||
| 179 | - CLI::Option* op1 = app.add_flag("--op1"); | 171 | + CLI::Option *op1 = app.add_flag("--op1"); |
| 180 | app.add_flag("--op2")->excludes(op1); | 172 | app.add_flag("--op2")->excludes(op1); |
| 181 | 173 | ||
| 182 | std::string help = app.help(); | 174 | std::string help = app.help(); |
| @@ -187,9 +179,9 @@ TEST(THelp, Excludes) { | @@ -187,9 +179,9 @@ TEST(THelp, Excludes) { | ||
| 187 | TEST(THelp, ExcludesPositional) { | 179 | TEST(THelp, ExcludesPositional) { |
| 188 | CLI::App app{"My prog"}; | 180 | CLI::App app{"My prog"}; |
| 189 | 181 | ||
| 190 | - int x,y; | 182 | + int x, y; |
| 191 | 183 | ||
| 192 | - CLI::Option* op1 = app.add_option("op1", x); | 184 | + CLI::Option *op1 = app.add_option("op1", x); |
| 193 | app.add_option("op2", y)->excludes(op1); | 185 | app.add_option("op2", y)->excludes(op1); |
| 194 | 186 | ||
| 195 | std::string help = app.help(); | 187 | std::string help = app.help(); |
| @@ -211,15 +203,15 @@ TEST(THelp, Subcom) { | @@ -211,15 +203,15 @@ TEST(THelp, Subcom) { | ||
| 211 | 203 | ||
| 212 | help = app.help(); | 204 | help = app.help(); |
| 213 | EXPECT_THAT(help, HasSubstr("Usage: program [OPTIONS] SUBCOMMAND")); | 205 | EXPECT_THAT(help, HasSubstr("Usage: program [OPTIONS] SUBCOMMAND")); |
| 214 | - | 206 | + |
| 215 | help = sub1->help(); | 207 | help = sub1->help(); |
| 216 | EXPECT_THAT(help, HasSubstr("Usage: sub1")); | 208 | EXPECT_THAT(help, HasSubstr("Usage: sub1")); |
| 217 | 209 | ||
| 218 | - char x[] = "./myprogram"; | 210 | + char x[] = "./myprogram"; |
| 219 | char y[] = "sub2"; | 211 | char y[] = "sub2"; |
| 220 | 212 | ||
| 221 | - std::vector<char*> args = {x,y}; | ||
| 222 | - app.parse((int) args.size(), args.data()); | 213 | + std::vector<char *> args = {x, y}; |
| 214 | + app.parse((int)args.size(), args.data()); | ||
| 223 | 215 | ||
| 224 | help = app.help(); | 216 | help = app.help(); |
| 225 | EXPECT_THAT(help, HasSubstr("Usage: ./myprogram sub2")); | 217 | EXPECT_THAT(help, HasSubstr("Usage: ./myprogram sub2")); |
| @@ -229,8 +221,8 @@ TEST(THelp, IntDefaults) { | @@ -229,8 +221,8 @@ TEST(THelp, IntDefaults) { | ||
| 229 | CLI::App app{"My prog"}; | 221 | CLI::App app{"My prog"}; |
| 230 | 222 | ||
| 231 | int one{1}, two{2}; | 223 | int one{1}, two{2}; |
| 232 | - app.add_option("--one", one, "Help for one", true); | ||
| 233 | - app.add_set("--set", two, {2,3,4}, "Help for set", true); | 224 | + app.add_option("--one", one, "Help for one", true); |
| 225 | + app.add_set("--set", two, {2, 3, 4}, "Help for set", true); | ||
| 234 | 226 | ||
| 235 | std::string help = app.help(); | 227 | std::string help = app.help(); |
| 236 | 228 | ||
| @@ -239,14 +231,13 @@ TEST(THelp, IntDefaults) { | @@ -239,14 +231,13 @@ TEST(THelp, IntDefaults) { | ||
| 239 | EXPECT_THAT(help, HasSubstr("1")); | 231 | EXPECT_THAT(help, HasSubstr("1")); |
| 240 | EXPECT_THAT(help, HasSubstr("=2")); | 232 | EXPECT_THAT(help, HasSubstr("=2")); |
| 241 | EXPECT_THAT(help, HasSubstr("2,3,4")); | 233 | EXPECT_THAT(help, HasSubstr("2,3,4")); |
| 242 | - | ||
| 243 | } | 234 | } |
| 244 | 235 | ||
| 245 | TEST(THelp, SetLower) { | 236 | TEST(THelp, SetLower) { |
| 246 | CLI::App app{"My prog"}; | 237 | CLI::App app{"My prog"}; |
| 247 | 238 | ||
| 248 | std::string def{"One"}; | 239 | std::string def{"One"}; |
| 249 | - app.add_set_ignore_case("--set",def, {"oNe", "twO", "THREE"}, "Help for set", true); | 240 | + app.add_set_ignore_case("--set", def, {"oNe", "twO", "THREE"}, "Help for set", true); |
| 250 | 241 | ||
| 251 | std::string help = app.help(); | 242 | std::string help = app.help(); |
| 252 | 243 | ||
| @@ -255,16 +246,15 @@ TEST(THelp, SetLower) { | @@ -255,16 +246,15 @@ TEST(THelp, SetLower) { | ||
| 255 | EXPECT_THAT(help, HasSubstr("oNe")); | 246 | EXPECT_THAT(help, HasSubstr("oNe")); |
| 256 | EXPECT_THAT(help, HasSubstr("twO")); | 247 | EXPECT_THAT(help, HasSubstr("twO")); |
| 257 | EXPECT_THAT(help, HasSubstr("THREE")); | 248 | EXPECT_THAT(help, HasSubstr("THREE")); |
| 258 | - | ||
| 259 | } | 249 | } |
| 260 | 250 | ||
| 261 | TEST(Exit, ErrorWithHelp) { | 251 | TEST(Exit, ErrorWithHelp) { |
| 262 | CLI::App app{"My prog"}; | 252 | CLI::App app{"My prog"}; |
| 263 | 253 | ||
| 264 | - std::vector<std::string> input {"-h"}; | 254 | + std::vector<std::string> input{"-h"}; |
| 265 | try { | 255 | try { |
| 266 | app.parse(input); | 256 | app.parse(input); |
| 267 | - } catch (const CLI::CallForHelp &e) { | 257 | + } catch(const CLI::CallForHelp &e) { |
| 268 | EXPECT_EQ(static_cast<int>(CLI::ExitCodes::Success), e.get_exit_code()); | 258 | EXPECT_EQ(static_cast<int>(CLI::ExitCodes::Success), e.get_exit_code()); |
| 269 | } | 259 | } |
| 270 | } | 260 | } |
| @@ -272,10 +262,10 @@ TEST(Exit, ErrorWithHelp) { | @@ -272,10 +262,10 @@ TEST(Exit, ErrorWithHelp) { | ||
| 272 | TEST(Exit, ErrorWithoutHelp) { | 262 | TEST(Exit, ErrorWithoutHelp) { |
| 273 | CLI::App app{"My prog"}; | 263 | CLI::App app{"My prog"}; |
| 274 | 264 | ||
| 275 | - std::vector<std::string> input {"--none"}; | 265 | + std::vector<std::string> input{"--none"}; |
| 276 | try { | 266 | try { |
| 277 | app.parse(input); | 267 | app.parse(input); |
| 278 | - } catch (const CLI::ParseError &e) { | 268 | + } catch(const CLI::ParseError &e) { |
| 279 | EXPECT_EQ(static_cast<int>(CLI::ExitCodes::Extras), e.get_exit_code()); | 269 | EXPECT_EQ(static_cast<int>(CLI::ExitCodes::Extras), e.get_exit_code()); |
| 280 | } | 270 | } |
| 281 | } | 271 | } |
| @@ -287,5 +277,4 @@ TEST(Exit, ExitCodes) { | @@ -287,5 +277,4 @@ TEST(Exit, ExitCodes) { | ||
| 287 | EXPECT_EQ(0, app.exit(CLI::Success())); | 277 | EXPECT_EQ(0, app.exit(CLI::Success())); |
| 288 | EXPECT_EQ(0, app.exit(CLI::CallForHelp())); | 278 | EXPECT_EQ(0, app.exit(CLI::CallForHelp())); |
| 289 | EXPECT_EQ(i, app.exit(CLI::ExtrasError("Thing"))); | 279 | EXPECT_EQ(i, app.exit(CLI::ExtrasError("Thing"))); |
| 290 | - | ||
| 291 | } | 280 | } |
tests/HelpersTest.cpp
| @@ -7,7 +7,7 @@ | @@ -7,7 +7,7 @@ | ||
| 7 | 7 | ||
| 8 | TEST(Split, SimpleByToken) { | 8 | TEST(Split, SimpleByToken) { |
| 9 | auto out = CLI::detail::split("one.two.three", '.'); | 9 | auto out = CLI::detail::split("one.two.three", '.'); |
| 10 | - ASSERT_EQ((size_t) 3, out.size()); | 10 | + ASSERT_EQ((size_t)3, out.size()); |
| 11 | EXPECT_EQ("one", out.at(0)); | 11 | EXPECT_EQ("one", out.at(0)); |
| 12 | EXPECT_EQ("two", out.at(1)); | 12 | EXPECT_EQ("two", out.at(1)); |
| 13 | EXPECT_EQ("three", out.at(2)); | 13 | EXPECT_EQ("three", out.at(2)); |
| @@ -15,13 +15,13 @@ TEST(Split, SimpleByToken) { | @@ -15,13 +15,13 @@ TEST(Split, SimpleByToken) { | ||
| 15 | 15 | ||
| 16 | TEST(Split, Single) { | 16 | TEST(Split, Single) { |
| 17 | auto out = CLI::detail::split("one", '.'); | 17 | auto out = CLI::detail::split("one", '.'); |
| 18 | - ASSERT_EQ((size_t) 1, out.size()); | 18 | + ASSERT_EQ((size_t)1, out.size()); |
| 19 | EXPECT_EQ("one", out.at(0)); | 19 | EXPECT_EQ("one", out.at(0)); |
| 20 | } | 20 | } |
| 21 | 21 | ||
| 22 | TEST(Split, Empty) { | 22 | TEST(Split, Empty) { |
| 23 | auto out = CLI::detail::split("", '.'); | 23 | auto out = CLI::detail::split("", '.'); |
| 24 | - ASSERT_EQ((size_t) 1, out.size()); | 24 | + ASSERT_EQ((size_t)1, out.size()); |
| 25 | EXPECT_EQ("", out.at(0)); | 25 | EXPECT_EQ("", out.at(0)); |
| 26 | } | 26 | } |
| 27 | 27 | ||
| @@ -38,7 +38,7 @@ TEST(Trim, Various) { | @@ -38,7 +38,7 @@ TEST(Trim, Various) { | ||
| 38 | std::string a1{"sdlfkj sdflk sd s"}; | 38 | std::string a1{"sdlfkj sdflk sd s"}; |
| 39 | CLI::detail::trim(s1); | 39 | CLI::detail::trim(s1); |
| 40 | EXPECT_EQ(a1, s1); | 40 | EXPECT_EQ(a1, s1); |
| 41 | - | 41 | + |
| 42 | std::string s2{" a \t"}; | 42 | std::string s2{" a \t"}; |
| 43 | CLI::detail::trim(s2); | 43 | CLI::detail::trim(s2); |
| 44 | EXPECT_EQ("a", s2); | 44 | EXPECT_EQ("a", s2); |
| @@ -51,13 +51,12 @@ TEST(Trim, Various) { | @@ -51,13 +51,12 @@ TEST(Trim, Various) { | ||
| 51 | EXPECT_EQ("a b", CLI::detail::trim(s4)); | 51 | EXPECT_EQ("a b", CLI::detail::trim(s4)); |
| 52 | } | 52 | } |
| 53 | 53 | ||
| 54 | - | ||
| 55 | TEST(Trim, VariousFilters) { | 54 | TEST(Trim, VariousFilters) { |
| 56 | std::string s1{" sdlfkj sdflk sd s "}; | 55 | std::string s1{" sdlfkj sdflk sd s "}; |
| 57 | std::string a1{"sdlfkj sdflk sd s"}; | 56 | std::string a1{"sdlfkj sdflk sd s"}; |
| 58 | CLI::detail::trim(s1, " "); | 57 | CLI::detail::trim(s1, " "); |
| 59 | EXPECT_EQ(a1, s1); | 58 | EXPECT_EQ(a1, s1); |
| 60 | - | 59 | + |
| 61 | std::string s2{" a \t"}; | 60 | std::string s2{" a \t"}; |
| 62 | CLI::detail::trim(s2, " "); | 61 | CLI::detail::trim(s2, " "); |
| 63 | EXPECT_EQ("a \t", s2); | 62 | EXPECT_EQ("a \t", s2); |
| @@ -84,10 +83,8 @@ TEST(Trim, TrimCopy) { | @@ -84,10 +83,8 @@ TEST(Trim, TrimCopy) { | ||
| 84 | EXPECT_NE(orig, trimmed); | 83 | EXPECT_NE(orig, trimmed); |
| 85 | CLI::detail::trim(orig, "ab"); | 84 | CLI::detail::trim(orig, "ab"); |
| 86 | EXPECT_EQ(trimmed, orig); | 85 | EXPECT_EQ(trimmed, orig); |
| 87 | - | ||
| 88 | } | 86 | } |
| 89 | 87 | ||
| 90 | - | ||
| 91 | TEST(Validators, FileExists) { | 88 | TEST(Validators, FileExists) { |
| 92 | std::string myfile{"TestFileNotUsed.txt"}; | 89 | std::string myfile{"TestFileNotUsed.txt"}; |
| 93 | EXPECT_FALSE(CLI::ExistingFile(myfile)); | 90 | EXPECT_FALSE(CLI::ExistingFile(myfile)); |
| @@ -140,14 +137,14 @@ TEST(Validators, DirectoryIsFile) { | @@ -140,14 +137,14 @@ TEST(Validators, DirectoryIsFile) { | ||
| 140 | TEST(AppHelper, TempfileCreated) { | 137 | TEST(AppHelper, TempfileCreated) { |
| 141 | std::string name = "TestFileNotUsed.txt"; | 138 | std::string name = "TestFileNotUsed.txt"; |
| 142 | { | 139 | { |
| 143 | - TempFile myfile{name}; | 140 | + TempFile myfile{name}; |
| 144 | 141 | ||
| 145 | - EXPECT_FALSE(CLI::ExistingFile(myfile)); | ||
| 146 | - | ||
| 147 | - bool ok = static_cast<bool>(std::ofstream(myfile.c_str()).put('a')); // create file | ||
| 148 | - EXPECT_TRUE(ok); | ||
| 149 | - EXPECT_TRUE(CLI::ExistingFile(name)); | ||
| 150 | - EXPECT_THROW({TempFile otherfile(name);}, std::runtime_error); | 142 | + EXPECT_FALSE(CLI::ExistingFile(myfile)); |
| 143 | + | ||
| 144 | + bool ok = static_cast<bool>(std::ofstream(myfile.c_str()).put('a')); // create file | ||
| 145 | + EXPECT_TRUE(ok); | ||
| 146 | + EXPECT_TRUE(CLI::ExistingFile(name)); | ||
| 147 | + EXPECT_THROW({ TempFile otherfile(name); }, std::runtime_error); | ||
| 151 | } | 148 | } |
| 152 | EXPECT_FALSE(CLI::ExistingFile(name)); | 149 | EXPECT_FALSE(CLI::ExistingFile(name)); |
| 153 | } | 150 | } |
| @@ -155,9 +152,9 @@ TEST(AppHelper, TempfileCreated) { | @@ -155,9 +152,9 @@ TEST(AppHelper, TempfileCreated) { | ||
| 155 | TEST(AppHelper, TempfileNotCreated) { | 152 | TEST(AppHelper, TempfileNotCreated) { |
| 156 | std::string name = "TestFileNotUsed.txt"; | 153 | std::string name = "TestFileNotUsed.txt"; |
| 157 | { | 154 | { |
| 158 | - TempFile myfile{name}; | 155 | + TempFile myfile{name}; |
| 159 | 156 | ||
| 160 | - EXPECT_FALSE(CLI::ExistingFile(myfile)); | 157 | + EXPECT_FALSE(CLI::ExistingFile(myfile)); |
| 161 | } | 158 | } |
| 162 | EXPECT_FALSE(CLI::ExistingFile(name)); | 159 | EXPECT_FALSE(CLI::ExistingFile(name)); |
| 163 | } | 160 | } |
| @@ -166,22 +163,21 @@ TEST(AppHelper, Ofstream) { | @@ -166,22 +163,21 @@ TEST(AppHelper, Ofstream) { | ||
| 166 | 163 | ||
| 167 | std::string name = "TestFileNotUsed.txt"; | 164 | std::string name = "TestFileNotUsed.txt"; |
| 168 | { | 165 | { |
| 169 | - TempFile myfile(name); | 166 | + TempFile myfile(name); |
| 170 | 167 | ||
| 171 | - { | ||
| 172 | - std::ofstream out{myfile}; | ||
| 173 | - out << "this is output" << std::endl; | ||
| 174 | - } | 168 | + { |
| 169 | + std::ofstream out{myfile}; | ||
| 170 | + out << "this is output" << std::endl; | ||
| 171 | + } | ||
| 175 | 172 | ||
| 176 | - EXPECT_TRUE(CLI::ExistingFile(myfile)); | 173 | + EXPECT_TRUE(CLI::ExistingFile(myfile)); |
| 177 | } | 174 | } |
| 178 | EXPECT_FALSE(CLI::ExistingFile(name)); | 175 | EXPECT_FALSE(CLI::ExistingFile(name)); |
| 179 | - | ||
| 180 | } | 176 | } |
| 181 | 177 | ||
| 182 | TEST(Split, StringList) { | 178 | TEST(Split, StringList) { |
| 183 | 179 | ||
| 184 | - std::vector<std::string> results {"a", "long", "--lone", "-q"}; | 180 | + std::vector<std::string> results{"a", "long", "--lone", "-q"}; |
| 185 | EXPECT_EQ(results, CLI::detail::split_names("a,long,--lone,-q")); | 181 | EXPECT_EQ(results, CLI::detail::split_names("a,long,--lone,-q")); |
| 186 | 182 | ||
| 187 | EXPECT_EQ(std::vector<std::string>({"one"}), CLI::detail::split_names("one")); | 183 | EXPECT_EQ(std::vector<std::string>({"one"}), CLI::detail::split_names("one")); |
| @@ -232,7 +228,6 @@ TEST(RegEx, Longs) { | @@ -232,7 +228,6 @@ TEST(RegEx, Longs) { | ||
| 232 | EXPECT_FALSE(CLI::detail::split_long("-things", name, value)); | 228 | EXPECT_FALSE(CLI::detail::split_long("-things", name, value)); |
| 233 | EXPECT_FALSE(CLI::detail::split_long("Q", name, value)); | 229 | EXPECT_FALSE(CLI::detail::split_long("Q", name, value)); |
| 234 | EXPECT_FALSE(CLI::detail::split_long("--", name, value)); | 230 | EXPECT_FALSE(CLI::detail::split_long("--", name, value)); |
| 235 | - | ||
| 236 | } | 231 | } |
| 237 | 232 | ||
| 238 | TEST(RegEx, SplittingNew) { | 233 | TEST(RegEx, SplittingNew) { |
| @@ -246,7 +241,8 @@ TEST(RegEx, SplittingNew) { | @@ -246,7 +241,8 @@ TEST(RegEx, SplittingNew) { | ||
| 246 | EXPECT_EQ(std::vector<std::string>({"s", "q"}), shorts); | 241 | EXPECT_EQ(std::vector<std::string>({"s", "q"}), shorts); |
| 247 | EXPECT_EQ("", pname); | 242 | EXPECT_EQ("", pname); |
| 248 | 243 | ||
| 249 | - EXPECT_NO_THROW(std::tie(shorts, longs, pname) = CLI::detail::get_names({"--long", "", "-s", "-q", "", "--also-long"})); | 244 | + EXPECT_NO_THROW(std::tie(shorts, longs, pname) = |
| 245 | + CLI::detail::get_names({"--long", "", "-s", "-q", "", "--also-long"})); | ||
| 250 | EXPECT_EQ(std::vector<std::string>({"long", "also-long"}), longs); | 246 | EXPECT_EQ(std::vector<std::string>({"long", "also-long"}), longs); |
| 251 | EXPECT_EQ(std::vector<std::string>({"s", "q"}), shorts); | 247 | EXPECT_EQ(std::vector<std::string>({"s", "q"}), shorts); |
| 252 | 248 | ||
| @@ -254,57 +250,52 @@ TEST(RegEx, SplittingNew) { | @@ -254,57 +250,52 @@ TEST(RegEx, SplittingNew) { | ||
| 254 | EXPECT_THROW(std::tie(shorts, longs, pname) = CLI::detail::get_names({"--"}), CLI::BadNameString); | 250 | EXPECT_THROW(std::tie(shorts, longs, pname) = CLI::detail::get_names({"--"}), CLI::BadNameString); |
| 255 | EXPECT_THROW(std::tie(shorts, longs, pname) = CLI::detail::get_names({"-hi"}), CLI::BadNameString); | 251 | EXPECT_THROW(std::tie(shorts, longs, pname) = CLI::detail::get_names({"-hi"}), CLI::BadNameString); |
| 256 | EXPECT_THROW(std::tie(shorts, longs, pname) = CLI::detail::get_names({"---hi"}), CLI::BadNameString); | 252 | EXPECT_THROW(std::tie(shorts, longs, pname) = CLI::detail::get_names({"---hi"}), CLI::BadNameString); |
| 257 | - EXPECT_THROW(std::tie(shorts, longs, pname) = CLI::detail::get_names({"one","two"}), CLI::BadNameString); | ||
| 258 | - | 253 | + EXPECT_THROW(std::tie(shorts, longs, pname) = CLI::detail::get_names({"one", "two"}), CLI::BadNameString); |
| 259 | } | 254 | } |
| 260 | 255 | ||
| 261 | -TEST(String, ToLower) { | ||
| 262 | - | ||
| 263 | - EXPECT_EQ("one and two", CLI::detail::to_lower("one And TWO")); | ||
| 264 | -} | 256 | +TEST(String, ToLower) { EXPECT_EQ("one and two", CLI::detail::to_lower("one And TWO")); } |
| 265 | 257 | ||
| 266 | TEST(Join, Forward) { | 258 | TEST(Join, Forward) { |
| 267 | - std::vector<std::string> val {{"one", "two", "three"}}; | 259 | + std::vector<std::string> val{{"one", "two", "three"}}; |
| 268 | EXPECT_EQ("one,two,three", CLI::detail::join(val)); | 260 | EXPECT_EQ("one,two,three", CLI::detail::join(val)); |
| 269 | EXPECT_EQ("one;two;three", CLI::detail::join(val, ";")); | 261 | EXPECT_EQ("one;two;three", CLI::detail::join(val, ";")); |
| 270 | } | 262 | } |
| 271 | 263 | ||
| 272 | - | ||
| 273 | TEST(Join, Backward) { | 264 | TEST(Join, Backward) { |
| 274 | - std::vector<std::string> val {{"three", "two", "one"}}; | 265 | + std::vector<std::string> val{{"three", "two", "one"}}; |
| 275 | EXPECT_EQ("one,two,three", CLI::detail::rjoin(val)); | 266 | EXPECT_EQ("one,two,three", CLI::detail::rjoin(val)); |
| 276 | EXPECT_EQ("one;two;three", CLI::detail::rjoin(val, ";")); | 267 | EXPECT_EQ("one;two;three", CLI::detail::rjoin(val, ";")); |
| 277 | } | 268 | } |
| 278 | 269 | ||
| 279 | TEST(SplitUp, Simple) { | 270 | TEST(SplitUp, Simple) { |
| 280 | std::vector<std::string> oput = {"one", "two three"}; | 271 | std::vector<std::string> oput = {"one", "two three"}; |
| 281 | - std::string orig {R"(one "two three")"}; | 272 | + std::string orig{R"(one "two three")"}; |
| 282 | std::vector<std::string> result = CLI::detail::split_up(orig); | 273 | std::vector<std::string> result = CLI::detail::split_up(orig); |
| 283 | EXPECT_EQ(oput, result); | 274 | EXPECT_EQ(oput, result); |
| 284 | } | 275 | } |
| 285 | 276 | ||
| 286 | TEST(SplitUp, Layered) { | 277 | TEST(SplitUp, Layered) { |
| 287 | std::vector<std::string> output = {R"(one 'two three')"}; | 278 | std::vector<std::string> output = {R"(one 'two three')"}; |
| 288 | - std::string orig {R"("one 'two three'")"}; | 279 | + std::string orig{R"("one 'two three'")"}; |
| 289 | std::vector<std::string> result = CLI::detail::split_up(orig); | 280 | std::vector<std::string> result = CLI::detail::split_up(orig); |
| 290 | EXPECT_EQ(output, result); | 281 | EXPECT_EQ(output, result); |
| 291 | } | 282 | } |
| 292 | 283 | ||
| 293 | TEST(SplitUp, Spaces) { | 284 | TEST(SplitUp, Spaces) { |
| 294 | std::vector<std::string> oput = {"one", " two three"}; | 285 | std::vector<std::string> oput = {"one", " two three"}; |
| 295 | - std::string orig {R"( one " two three" )"}; | 286 | + std::string orig{R"( one " two three" )"}; |
| 296 | std::vector<std::string> result = CLI::detail::split_up(orig); | 287 | std::vector<std::string> result = CLI::detail::split_up(orig); |
| 297 | EXPECT_EQ(oput, result); | 288 | EXPECT_EQ(oput, result); |
| 298 | } | 289 | } |
| 299 | 290 | ||
| 300 | TEST(SplitUp, BadStrings) { | 291 | TEST(SplitUp, BadStrings) { |
| 301 | std::vector<std::string> oput = {"one", " two three"}; | 292 | std::vector<std::string> oput = {"one", " two three"}; |
| 302 | - std::string orig {R"( one " two three )"}; | 293 | + std::string orig{R"( one " two three )"}; |
| 303 | std::vector<std::string> result = CLI::detail::split_up(orig); | 294 | std::vector<std::string> result = CLI::detail::split_up(orig); |
| 304 | EXPECT_EQ(oput, result); | 295 | EXPECT_EQ(oput, result); |
| 305 | 296 | ||
| 306 | oput = {"one", " two three"}; | 297 | oput = {"one", " two three"}; |
| 307 | - orig = R"( one ' two three )"; | 298 | + orig = R"( one ' two three )"; |
| 308 | result = CLI::detail::split_up(orig); | 299 | result = CLI::detail::split_up(orig); |
| 309 | EXPECT_EQ(oput, result); | 300 | EXPECT_EQ(oput, result); |
| 310 | } | 301 | } |
| @@ -327,8 +318,8 @@ TEST(Types, TypeName) { | @@ -327,8 +318,8 @@ TEST(Types, TypeName) { | ||
| 327 | 318 | ||
| 328 | std::string text_name = CLI::detail::type_name<std::string>(); | 319 | std::string text_name = CLI::detail::type_name<std::string>(); |
| 329 | EXPECT_EQ("TEXT", text_name); | 320 | EXPECT_EQ("TEXT", text_name); |
| 330 | - | ||
| 331 | - std::string text2_name = CLI::detail::type_name<char*>(); | 321 | + |
| 322 | + std::string text2_name = CLI::detail::type_name<char *>(); | ||
| 332 | EXPECT_EQ("TEXT", text2_name); | 323 | EXPECT_EQ("TEXT", text2_name); |
| 333 | } | 324 | } |
| 334 | 325 | ||
| @@ -350,11 +341,11 @@ TEST(Types, LexicalCastDouble) { | @@ -350,11 +341,11 @@ TEST(Types, LexicalCastDouble) { | ||
| 350 | std::string input = "9.12"; | 341 | std::string input = "9.12"; |
| 351 | long double x; | 342 | long double x; |
| 352 | EXPECT_TRUE(CLI::detail::lexical_cast(input, x)); | 343 | EXPECT_TRUE(CLI::detail::lexical_cast(input, x)); |
| 353 | - EXPECT_FLOAT_EQ((float) 9.12, (float) x); | 344 | + EXPECT_FLOAT_EQ((float)9.12, (float)x); |
| 354 | 345 | ||
| 355 | std::string bad_input = "hello"; | 346 | std::string bad_input = "hello"; |
| 356 | EXPECT_FALSE(CLI::detail::lexical_cast(bad_input, x)); | 347 | EXPECT_FALSE(CLI::detail::lexical_cast(bad_input, x)); |
| 357 | - | 348 | + |
| 358 | std::string overflow_input = "1" + std::to_string(LDBL_MAX); | 349 | std::string overflow_input = "1" + std::to_string(LDBL_MAX); |
| 359 | EXPECT_FALSE(CLI::detail::lexical_cast(overflow_input, x)); | 350 | EXPECT_FALSE(CLI::detail::lexical_cast(overflow_input, x)); |
| 360 | } | 351 | } |
| @@ -365,4 +356,3 @@ TEST(Types, LexicalCastString) { | @@ -365,4 +356,3 @@ TEST(Types, LexicalCastString) { | ||
| 365 | CLI::detail::lexical_cast(input, output); | 356 | CLI::detail::lexical_cast(input, output); |
| 366 | EXPECT_EQ(input, output); | 357 | EXPECT_EQ(input, output); |
| 367 | } | 358 | } |
| 368 | - |
tests/IniTest.cpp
| @@ -3,7 +3,7 @@ | @@ -3,7 +3,7 @@ | ||
| 3 | #include <cstdio> | 3 | #include <cstdio> |
| 4 | #include <sstream> | 4 | #include <sstream> |
| 5 | #include "gmock/gmock.h" | 5 | #include "gmock/gmock.h" |
| 6 | - | 6 | + |
| 7 | using ::testing::HasSubstr; | 7 | using ::testing::HasSubstr; |
| 8 | using ::testing::Not; | 8 | using ::testing::Not; |
| 9 | 9 | ||
| @@ -17,14 +17,13 @@ TEST(StringBased, First) { | @@ -17,14 +17,13 @@ TEST(StringBased, First) { | ||
| 17 | 17 | ||
| 18 | std::vector<CLI::detail::ini_ret_t> output = CLI::detail::parse_ini(ofile); | 18 | std::vector<CLI::detail::ini_ret_t> output = CLI::detail::parse_ini(ofile); |
| 19 | 19 | ||
| 20 | - EXPECT_EQ((size_t) 2, output.size()); | 20 | + EXPECT_EQ((size_t)2, output.size()); |
| 21 | EXPECT_EQ("one", output.at(0).name()); | 21 | EXPECT_EQ("one", output.at(0).name()); |
| 22 | - EXPECT_EQ((size_t) 1, output.at(0).inputs.size()); | 22 | + EXPECT_EQ((size_t)1, output.at(0).inputs.size()); |
| 23 | EXPECT_EQ("three", output.at(0).inputs.at(0)); | 23 | EXPECT_EQ("three", output.at(0).inputs.at(0)); |
| 24 | EXPECT_EQ("two", output.at(1).name()); | 24 | EXPECT_EQ("two", output.at(1).name()); |
| 25 | - EXPECT_EQ((size_t) 1, output.at(1).inputs.size()); | 25 | + EXPECT_EQ((size_t)1, output.at(1).inputs.size()); |
| 26 | EXPECT_EQ("four", output.at(1).inputs.at(0)); | 26 | EXPECT_EQ("four", output.at(1).inputs.at(0)); |
| 27 | - | ||
| 28 | } | 27 | } |
| 29 | 28 | ||
| 30 | TEST(StringBased, FirstWithComments) { | 29 | TEST(StringBased, FirstWithComments) { |
| @@ -39,12 +38,12 @@ TEST(StringBased, FirstWithComments) { | @@ -39,12 +38,12 @@ TEST(StringBased, FirstWithComments) { | ||
| 39 | 38 | ||
| 40 | auto output = CLI::detail::parse_ini(ofile); | 39 | auto output = CLI::detail::parse_ini(ofile); |
| 41 | 40 | ||
| 42 | - EXPECT_EQ((size_t) 2, output.size()); | 41 | + EXPECT_EQ((size_t)2, output.size()); |
| 43 | EXPECT_EQ("one", output.at(0).name()); | 42 | EXPECT_EQ("one", output.at(0).name()); |
| 44 | - EXPECT_EQ((size_t) 1, output.at(0).inputs.size()); | 43 | + EXPECT_EQ((size_t)1, output.at(0).inputs.size()); |
| 45 | EXPECT_EQ("three", output.at(0).inputs.at(0)); | 44 | EXPECT_EQ("three", output.at(0).inputs.at(0)); |
| 46 | EXPECT_EQ("two", output.at(1).name()); | 45 | EXPECT_EQ("two", output.at(1).name()); |
| 47 | - EXPECT_EQ((size_t) 1, output.at(1).inputs.size()); | 46 | + EXPECT_EQ((size_t)1, output.at(1).inputs.size()); |
| 48 | EXPECT_EQ("four", output.at(1).inputs.at(0)); | 47 | EXPECT_EQ("four", output.at(1).inputs.at(0)); |
| 49 | } | 48 | } |
| 50 | 49 | ||
| @@ -59,15 +58,15 @@ TEST(StringBased, Quotes) { | @@ -59,15 +58,15 @@ TEST(StringBased, Quotes) { | ||
| 59 | 58 | ||
| 60 | auto output = CLI::detail::parse_ini(ofile); | 59 | auto output = CLI::detail::parse_ini(ofile); |
| 61 | 60 | ||
| 62 | - EXPECT_EQ((size_t) 3, output.size()); | 61 | + EXPECT_EQ((size_t)3, output.size()); |
| 63 | EXPECT_EQ("one", output.at(0).name()); | 62 | EXPECT_EQ("one", output.at(0).name()); |
| 64 | - EXPECT_EQ((size_t) 1, output.at(0).inputs.size()); | 63 | + EXPECT_EQ((size_t)1, output.at(0).inputs.size()); |
| 65 | EXPECT_EQ("three", output.at(0).inputs.at(0)); | 64 | EXPECT_EQ("three", output.at(0).inputs.at(0)); |
| 66 | EXPECT_EQ("two", output.at(1).name()); | 65 | EXPECT_EQ("two", output.at(1).name()); |
| 67 | - EXPECT_EQ((size_t) 1, output.at(1).inputs.size()); | 66 | + EXPECT_EQ((size_t)1, output.at(1).inputs.size()); |
| 68 | EXPECT_EQ("four", output.at(1).inputs.at(0)); | 67 | EXPECT_EQ("four", output.at(1).inputs.at(0)); |
| 69 | EXPECT_EQ("five", output.at(2).name()); | 68 | EXPECT_EQ("five", output.at(2).name()); |
| 70 | - EXPECT_EQ((size_t) 1, output.at(2).inputs.size()); | 69 | + EXPECT_EQ((size_t)1, output.at(2).inputs.size()); |
| 71 | EXPECT_EQ("six and seven", output.at(2).inputs.at(0)); | 70 | EXPECT_EQ("six and seven", output.at(2).inputs.at(0)); |
| 72 | } | 71 | } |
| 73 | 72 | ||
| @@ -82,21 +81,20 @@ TEST(StringBased, Vector) { | @@ -82,21 +81,20 @@ TEST(StringBased, Vector) { | ||
| 82 | 81 | ||
| 83 | auto output = CLI::detail::parse_ini(ofile); | 82 | auto output = CLI::detail::parse_ini(ofile); |
| 84 | 83 | ||
| 85 | - EXPECT_EQ((size_t) 3, output.size()); | 84 | + EXPECT_EQ((size_t)3, output.size()); |
| 86 | EXPECT_EQ("one", output.at(0).name()); | 85 | EXPECT_EQ("one", output.at(0).name()); |
| 87 | - EXPECT_EQ((size_t) 1, output.at(0).inputs.size()); | 86 | + EXPECT_EQ((size_t)1, output.at(0).inputs.size()); |
| 88 | EXPECT_EQ("three", output.at(0).inputs.at(0)); | 87 | EXPECT_EQ("three", output.at(0).inputs.at(0)); |
| 89 | EXPECT_EQ("two", output.at(1).name()); | 88 | EXPECT_EQ("two", output.at(1).name()); |
| 90 | - EXPECT_EQ((size_t) 1, output.at(1).inputs.size()); | 89 | + EXPECT_EQ((size_t)1, output.at(1).inputs.size()); |
| 91 | EXPECT_EQ("four", output.at(1).inputs.at(0)); | 90 | EXPECT_EQ("four", output.at(1).inputs.at(0)); |
| 92 | EXPECT_EQ("five", output.at(2).name()); | 91 | EXPECT_EQ("five", output.at(2).name()); |
| 93 | - EXPECT_EQ((size_t) 3, output.at(2).inputs.size()); | 92 | + EXPECT_EQ((size_t)3, output.at(2).inputs.size()); |
| 94 | EXPECT_EQ("six", output.at(2).inputs.at(0)); | 93 | EXPECT_EQ("six", output.at(2).inputs.at(0)); |
| 95 | EXPECT_EQ("and", output.at(2).inputs.at(1)); | 94 | EXPECT_EQ("and", output.at(2).inputs.at(1)); |
| 96 | EXPECT_EQ("seven", output.at(2).inputs.at(2)); | 95 | EXPECT_EQ("seven", output.at(2).inputs.at(2)); |
| 97 | } | 96 | } |
| 98 | 97 | ||
| 99 | - | ||
| 100 | TEST(StringBased, Spaces) { | 98 | TEST(StringBased, Spaces) { |
| 101 | std::stringstream ofile; | 99 | std::stringstream ofile; |
| 102 | 100 | ||
| @@ -107,12 +105,12 @@ TEST(StringBased, Spaces) { | @@ -107,12 +105,12 @@ TEST(StringBased, Spaces) { | ||
| 107 | 105 | ||
| 108 | auto output = CLI::detail::parse_ini(ofile); | 106 | auto output = CLI::detail::parse_ini(ofile); |
| 109 | 107 | ||
| 110 | - EXPECT_EQ((size_t) 2, output.size()); | 108 | + EXPECT_EQ((size_t)2, output.size()); |
| 111 | EXPECT_EQ("one", output.at(0).name()); | 109 | EXPECT_EQ("one", output.at(0).name()); |
| 112 | - EXPECT_EQ((size_t) 1, output.at(0).inputs.size()); | 110 | + EXPECT_EQ((size_t)1, output.at(0).inputs.size()); |
| 113 | EXPECT_EQ("three", output.at(0).inputs.at(0)); | 111 | EXPECT_EQ("three", output.at(0).inputs.at(0)); |
| 114 | EXPECT_EQ("two", output.at(1).name()); | 112 | EXPECT_EQ("two", output.at(1).name()); |
| 115 | - EXPECT_EQ((size_t) 1, output.at(1).inputs.size()); | 113 | + EXPECT_EQ((size_t)1, output.at(1).inputs.size()); |
| 116 | EXPECT_EQ("four", output.at(1).inputs.at(0)); | 114 | EXPECT_EQ("four", output.at(1).inputs.at(0)); |
| 117 | } | 115 | } |
| 118 | 116 | ||
| @@ -126,18 +124,17 @@ TEST(StringBased, Sections) { | @@ -126,18 +124,17 @@ TEST(StringBased, Sections) { | ||
| 126 | ofile.seekg(0, std::ios::beg); | 124 | ofile.seekg(0, std::ios::beg); |
| 127 | 125 | ||
| 128 | auto output = CLI::detail::parse_ini(ofile); | 126 | auto output = CLI::detail::parse_ini(ofile); |
| 129 | - | ||
| 130 | - EXPECT_EQ((size_t) 2, output.size()); | 127 | + |
| 128 | + EXPECT_EQ((size_t)2, output.size()); | ||
| 131 | EXPECT_EQ("one", output.at(0).name()); | 129 | EXPECT_EQ("one", output.at(0).name()); |
| 132 | - EXPECT_EQ((size_t) 1, output.at(0).inputs.size()); | 130 | + EXPECT_EQ((size_t)1, output.at(0).inputs.size()); |
| 133 | EXPECT_EQ("three", output.at(0).inputs.at(0)); | 131 | EXPECT_EQ("three", output.at(0).inputs.at(0)); |
| 134 | EXPECT_EQ("two", output.at(1).name()); | 132 | EXPECT_EQ("two", output.at(1).name()); |
| 135 | EXPECT_EQ("second", output.at(1).parent()); | 133 | EXPECT_EQ("second", output.at(1).parent()); |
| 136 | - EXPECT_EQ((size_t) 1, output.at(1).inputs.size()); | 134 | + EXPECT_EQ((size_t)1, output.at(1).inputs.size()); |
| 137 | EXPECT_EQ("four", output.at(1).inputs.at(0)); | 135 | EXPECT_EQ("four", output.at(1).inputs.at(0)); |
| 138 | } | 136 | } |
| 139 | 137 | ||
| 140 | - | ||
| 141 | TEST(StringBased, SpacesSections) { | 138 | TEST(StringBased, SpacesSections) { |
| 142 | std::stringstream ofile; | 139 | std::stringstream ofile; |
| 143 | 140 | ||
| @@ -151,13 +148,13 @@ TEST(StringBased, SpacesSections) { | @@ -151,13 +148,13 @@ TEST(StringBased, SpacesSections) { | ||
| 151 | 148 | ||
| 152 | auto output = CLI::detail::parse_ini(ofile); | 149 | auto output = CLI::detail::parse_ini(ofile); |
| 153 | 150 | ||
| 154 | - EXPECT_EQ((size_t) 2, output.size()); | 151 | + EXPECT_EQ((size_t)2, output.size()); |
| 155 | EXPECT_EQ("one", output.at(0).name()); | 152 | EXPECT_EQ("one", output.at(0).name()); |
| 156 | - EXPECT_EQ((size_t) 1, output.at(0).inputs.size()); | 153 | + EXPECT_EQ((size_t)1, output.at(0).inputs.size()); |
| 157 | EXPECT_EQ("three", output.at(0).inputs.at(0)); | 154 | EXPECT_EQ("three", output.at(0).inputs.at(0)); |
| 158 | EXPECT_EQ("two", output.at(1).name()); | 155 | EXPECT_EQ("two", output.at(1).name()); |
| 159 | EXPECT_EQ("second", output.at(1).parent()); | 156 | EXPECT_EQ("second", output.at(1).parent()); |
| 160 | - EXPECT_EQ((size_t) 1, output.at(1).inputs.size()); | 157 | + EXPECT_EQ((size_t)1, output.at(1).inputs.size()); |
| 161 | EXPECT_EQ("four", output.at(1).inputs.at(0)); | 158 | EXPECT_EQ("four", output.at(1).inputs.at(0)); |
| 162 | } | 159 | } |
| 163 | 160 | ||
| @@ -174,7 +171,7 @@ TEST_F(TApp, IniNotRequired) { | @@ -174,7 +171,7 @@ TEST_F(TApp, IniNotRequired) { | ||
| 174 | out << "three=3" << std::endl; | 171 | out << "three=3" << std::endl; |
| 175 | } | 172 | } |
| 176 | 173 | ||
| 177 | - int one=0, two=0, three=0; | 174 | + int one = 0, two = 0, three = 0; |
| 178 | app.add_option("--one", one); | 175 | app.add_option("--one", one); |
| 179 | app.add_option("--two", two); | 176 | app.add_option("--two", two); |
| 180 | app.add_option("--three", three); | 177 | app.add_option("--three", three); |
| @@ -188,7 +185,7 @@ TEST_F(TApp, IniNotRequired) { | @@ -188,7 +185,7 @@ TEST_F(TApp, IniNotRequired) { | ||
| 188 | EXPECT_EQ(3, three); | 185 | EXPECT_EQ(3, three); |
| 189 | 186 | ||
| 190 | app.reset(); | 187 | app.reset(); |
| 191 | - one=two=three=0; | 188 | + one = two = three = 0; |
| 192 | args = {"--one=1", "--two=2"}; | 189 | args = {"--one=1", "--two=2"}; |
| 193 | 190 | ||
| 194 | run(); | 191 | run(); |
| @@ -196,7 +193,6 @@ TEST_F(TApp, IniNotRequired) { | @@ -196,7 +193,6 @@ TEST_F(TApp, IniNotRequired) { | ||
| 196 | EXPECT_EQ(1, one); | 193 | EXPECT_EQ(1, one); |
| 197 | EXPECT_EQ(2, two); | 194 | EXPECT_EQ(2, two); |
| 198 | EXPECT_EQ(3, three); | 195 | EXPECT_EQ(3, three); |
| 199 | - | ||
| 200 | } | 196 | } |
| 201 | 197 | ||
| 202 | TEST_F(TApp, IniRequiredNotFound) { | 198 | TEST_F(TApp, IniRequiredNotFound) { |
| @@ -205,7 +201,6 @@ TEST_F(TApp, IniRequiredNotFound) { | @@ -205,7 +201,6 @@ TEST_F(TApp, IniRequiredNotFound) { | ||
| 205 | app.add_config("--config", noini, "", true); | 201 | app.add_config("--config", noini, "", true); |
| 206 | 202 | ||
| 207 | EXPECT_THROW(run(), CLI::FileError); | 203 | EXPECT_THROW(run(), CLI::FileError); |
| 208 | - | ||
| 209 | } | 204 | } |
| 210 | 205 | ||
| 211 | TEST_F(TApp, IniOverwrite) { | 206 | TEST_F(TApp, IniOverwrite) { |
| @@ -224,7 +219,7 @@ TEST_F(TApp, IniOverwrite) { | @@ -224,7 +219,7 @@ TEST_F(TApp, IniOverwrite) { | ||
| 224 | app.add_config("--conf", next); | 219 | app.add_config("--conf", next); |
| 225 | int two = 7; | 220 | int two = 7; |
| 226 | app.add_option("--two", two); | 221 | app.add_option("--two", two); |
| 227 | - | 222 | + |
| 228 | run(); | 223 | run(); |
| 229 | 224 | ||
| 230 | EXPECT_EQ(99, two); | 225 | EXPECT_EQ(99, two); |
| @@ -243,7 +238,7 @@ TEST_F(TApp, IniRequired) { | @@ -243,7 +238,7 @@ TEST_F(TApp, IniRequired) { | ||
| 243 | out << "three=3" << std::endl; | 238 | out << "three=3" << std::endl; |
| 244 | } | 239 | } |
| 245 | 240 | ||
| 246 | - int one=0, two=0, three=0; | 241 | + int one = 0, two = 0, three = 0; |
| 247 | app.add_option("--one", one)->required(); | 242 | app.add_option("--one", one)->required(); |
| 248 | app.add_option("--two", two)->required(); | 243 | app.add_option("--two", two)->required(); |
| 249 | app.add_option("--three", three)->required(); | 244 | app.add_option("--three", three)->required(); |
| @@ -253,7 +248,7 @@ TEST_F(TApp, IniRequired) { | @@ -253,7 +248,7 @@ TEST_F(TApp, IniRequired) { | ||
| 253 | run(); | 248 | run(); |
| 254 | 249 | ||
| 255 | app.reset(); | 250 | app.reset(); |
| 256 | - one=two=three=0; | 251 | + one = two = three = 0; |
| 257 | args = {"--one=1", "--two=2"}; | 252 | args = {"--one=1", "--two=2"}; |
| 258 | 253 | ||
| 259 | run(); | 254 | run(); |
| @@ -267,7 +262,6 @@ TEST_F(TApp, IniRequired) { | @@ -267,7 +262,6 @@ TEST_F(TApp, IniRequired) { | ||
| 267 | args = {"--two=2"}; | 262 | args = {"--two=2"}; |
| 268 | 263 | ||
| 269 | EXPECT_THROW(run(), CLI::RequiredError); | 264 | EXPECT_THROW(run(), CLI::RequiredError); |
| 270 | - | ||
| 271 | } | 265 | } |
| 272 | 266 | ||
| 273 | TEST_F(TApp, IniVector) { | 267 | TEST_F(TApp, IniVector) { |
| @@ -289,12 +283,10 @@ TEST_F(TApp, IniVector) { | @@ -289,12 +283,10 @@ TEST_F(TApp, IniVector) { | ||
| 289 | 283 | ||
| 290 | run(); | 284 | run(); |
| 291 | 285 | ||
| 292 | - EXPECT_EQ(std::vector<int>({2,3}), two); | ||
| 293 | - EXPECT_EQ(std::vector<int>({1,2,3}), three); | ||
| 294 | - | 286 | + EXPECT_EQ(std::vector<int>({2, 3}), two); |
| 287 | + EXPECT_EQ(std::vector<int>({1, 2, 3}), three); | ||
| 295 | } | 288 | } |
| 296 | 289 | ||
| 297 | - | ||
| 298 | TEST_F(TApp, IniLayered) { | 290 | TEST_F(TApp, IniLayered) { |
| 299 | 291 | ||
| 300 | TempFile tmpini{"TestIniTmp.ini"}; | 292 | TempFile tmpini{"TestIniTmp.ini"}; |
| @@ -310,7 +302,7 @@ TEST_F(TApp, IniLayered) { | @@ -310,7 +302,7 @@ TEST_F(TApp, IniLayered) { | ||
| 310 | out << "subsubcom.val=3" << std::endl; | 302 | out << "subsubcom.val=3" << std::endl; |
| 311 | } | 303 | } |
| 312 | 304 | ||
| 313 | - int one=0, two=0, three=0; | 305 | + int one = 0, two = 0, three = 0; |
| 314 | app.add_option("--val", one); | 306 | app.add_option("--val", one); |
| 315 | auto subcom = app.add_subcommand("subcom"); | 307 | auto subcom = app.add_subcommand("subcom"); |
| 316 | subcom->add_option("--val", two); | 308 | subcom->add_option("--val", two); |
| @@ -322,12 +314,11 @@ TEST_F(TApp, IniLayered) { | @@ -322,12 +314,11 @@ TEST_F(TApp, IniLayered) { | ||
| 322 | EXPECT_EQ(1, one); | 314 | EXPECT_EQ(1, one); |
| 323 | EXPECT_EQ(2, two); | 315 | EXPECT_EQ(2, two); |
| 324 | EXPECT_EQ(3, three); | 316 | EXPECT_EQ(3, three); |
| 325 | - | ||
| 326 | } | 317 | } |
| 327 | 318 | ||
| 328 | TEST_F(TApp, IniFailure) { | 319 | TEST_F(TApp, IniFailure) { |
| 329 | 320 | ||
| 330 | - TempFile tmpini{"TestIniTmp.ini"}; | 321 | + TempFile tmpini{"TestIniTmp.ini"}; |
| 331 | 322 | ||
| 332 | app.add_config("--config", tmpini); | 323 | app.add_config("--config", tmpini); |
| 333 | 324 | ||
| @@ -340,7 +331,6 @@ TEST_F(TApp, IniFailure) { | @@ -340,7 +331,6 @@ TEST_F(TApp, IniFailure) { | ||
| 340 | EXPECT_THROW(run(), CLI::ExtrasINIError); | 331 | EXPECT_THROW(run(), CLI::ExtrasINIError); |
| 341 | } | 332 | } |
| 342 | 333 | ||
| 343 | - | ||
| 344 | TEST_F(TApp, IniSubFailure) { | 334 | TEST_F(TApp, IniSubFailure) { |
| 345 | 335 | ||
| 346 | TempFile tmpini{"TestIniTmp.ini"}; | 336 | TempFile tmpini{"TestIniTmp.ini"}; |
| @@ -357,7 +347,6 @@ TEST_F(TApp, IniSubFailure) { | @@ -357,7 +347,6 @@ TEST_F(TApp, IniSubFailure) { | ||
| 357 | EXPECT_THROW(run(), CLI::ExtrasINIError); | 347 | EXPECT_THROW(run(), CLI::ExtrasINIError); |
| 358 | } | 348 | } |
| 359 | 349 | ||
| 360 | - | ||
| 361 | TEST_F(TApp, IniNoSubFailure) { | 350 | TEST_F(TApp, IniNoSubFailure) { |
| 362 | 351 | ||
| 363 | TempFile tmpini{"TestIniTmp.ini"}; | 352 | TempFile tmpini{"TestIniTmp.ini"}; |
| @@ -388,7 +377,6 @@ TEST_F(TApp, IniFlagConvertFailure) { | @@ -388,7 +377,6 @@ TEST_F(TApp, IniFlagConvertFailure) { | ||
| 388 | EXPECT_THROW(run(), CLI::ConversionError); | 377 | EXPECT_THROW(run(), CLI::ConversionError); |
| 389 | } | 378 | } |
| 390 | 379 | ||
| 391 | - | ||
| 392 | TEST_F(TApp, IniFlagNumbers) { | 380 | TEST_F(TApp, IniFlagNumbers) { |
| 393 | 381 | ||
| 394 | TempFile tmpini{"TestIniTmp.ini"}; | 382 | TempFile tmpini{"TestIniTmp.ini"}; |
| @@ -474,7 +462,6 @@ TEST_F(TApp, IniFlags) { | @@ -474,7 +462,6 @@ TEST_F(TApp, IniFlags) { | ||
| 474 | EXPECT_EQ(true, three); | 462 | EXPECT_EQ(true, three); |
| 475 | EXPECT_EQ(true, four); | 463 | EXPECT_EQ(true, four); |
| 476 | EXPECT_EQ(true, five); | 464 | EXPECT_EQ(true, five); |
| 477 | - | ||
| 478 | } | 465 | } |
| 479 | 466 | ||
| 480 | TEST_F(TApp, IniOutputSimple) { | 467 | TEST_F(TApp, IniOutputSimple) { |
| @@ -528,7 +515,7 @@ TEST_F(TApp, IniOutputFlag) { | @@ -528,7 +515,7 @@ TEST_F(TApp, IniOutputFlag) { | ||
| 528 | TEST_F(TApp, IniOutputSet) { | 515 | TEST_F(TApp, IniOutputSet) { |
| 529 | 516 | ||
| 530 | int v; | 517 | int v; |
| 531 | - app.add_set("--simple", v, {1,2,3}); | 518 | + app.add_set("--simple", v, {1, 2, 3}); |
| 532 | 519 | ||
| 533 | args = {"--simple=2"}; | 520 | args = {"--simple=2"}; |
| 534 | 521 | ||
| @@ -538,10 +525,9 @@ TEST_F(TApp, IniOutputSet) { | @@ -538,10 +525,9 @@ TEST_F(TApp, IniOutputSet) { | ||
| 538 | EXPECT_THAT(str, HasSubstr("simple=2")); | 525 | EXPECT_THAT(str, HasSubstr("simple=2")); |
| 539 | } | 526 | } |
| 540 | 527 | ||
| 541 | - | ||
| 542 | TEST_F(TApp, IniOutputDefault) { | 528 | TEST_F(TApp, IniOutputDefault) { |
| 543 | 529 | ||
| 544 | - int v=7; | 530 | + int v = 7; |
| 545 | app.add_option("--simple", v, "", true); | 531 | app.add_option("--simple", v, "", true); |
| 546 | 532 | ||
| 547 | run(); | 533 | run(); |
| @@ -574,7 +560,7 @@ TEST_F(TApp, IniQuotedOutput) { | @@ -574,7 +560,7 @@ TEST_F(TApp, IniQuotedOutput) { | ||
| 574 | 560 | ||
| 575 | std::string val2; | 561 | std::string val2; |
| 576 | app.add_option("--val2", val2); | 562 | app.add_option("--val2", val2); |
| 577 | - | 563 | + |
| 578 | args = {"--val1", "I am a string", "--val2", R"(I am a "confusing" string)"}; | 564 | args = {"--val1", "I am a string", "--val2", R"(I am a "confusing" string)"}; |
| 579 | 565 | ||
| 580 | run(); | 566 | run(); |
| @@ -585,5 +571,4 @@ TEST_F(TApp, IniQuotedOutput) { | @@ -585,5 +571,4 @@ TEST_F(TApp, IniQuotedOutput) { | ||
| 585 | std::string str = app.config_to_str(); | 571 | std::string str = app.config_to_str(); |
| 586 | EXPECT_THAT(str, HasSubstr("val1=\"I am a string\"")); | 572 | EXPECT_THAT(str, HasSubstr("val1=\"I am a string\"")); |
| 587 | EXPECT_THAT(str, HasSubstr("val2='I am a \"confusing\" string'")); | 573 | EXPECT_THAT(str, HasSubstr("val2='I am a \"confusing\" string'")); |
| 588 | - | ||
| 589 | } | 574 | } |
tests/NewParseTest.cpp
| @@ -6,21 +6,19 @@ using ::testing::HasSubstr; | @@ -6,21 +6,19 @@ using ::testing::HasSubstr; | ||
| 6 | 6 | ||
| 7 | using cx = std::complex<double>; | 7 | using cx = std::complex<double>; |
| 8 | 8 | ||
| 9 | -CLI::Option* add_option(CLI::App& app, | ||
| 10 | - std::string name, cx& variable, | ||
| 11 | - std::string description="", bool defaulted=false) { | ||
| 12 | - CLI::callback_t fun = [&variable](CLI::results_t res){ | ||
| 13 | - if(res.size()!=2) | 9 | +CLI::Option * |
| 10 | +add_option(CLI::App &app, std::string name, cx &variable, std::string description = "", bool defaulted = false) { | ||
| 11 | + CLI::callback_t fun = [&variable](CLI::results_t res) { | ||
| 12 | + if(res.size() != 2) | ||
| 14 | return false; | 13 | return false; |
| 15 | - double x,y; | ||
| 16 | - bool worked = CLI::detail::lexical_cast(res[0], x) | ||
| 17 | - && CLI::detail::lexical_cast(res[1], y); | 14 | + double x, y; |
| 15 | + bool worked = CLI::detail::lexical_cast(res[0], x) && CLI::detail::lexical_cast(res[1], y); | ||
| 18 | if(worked) | 16 | if(worked) |
| 19 | - variable = cx(x,y); | 17 | + variable = cx(x, y); |
| 20 | return worked; | 18 | return worked; |
| 21 | }; | 19 | }; |
| 22 | 20 | ||
| 23 | - CLI::Option* opt = app.add_option(name, fun, description, defaulted); | 21 | + CLI::Option *opt = app.add_option(name, fun, description, defaulted); |
| 24 | opt->set_custom_option("COMPLEX", 2); | 22 | opt->set_custom_option("COMPLEX", 2); |
| 25 | if(defaulted) { | 23 | if(defaulted) { |
| 26 | std::stringstream out; | 24 | std::stringstream out; |
| @@ -32,19 +30,18 @@ CLI::Option* add_option(CLI::App& app, | @@ -32,19 +30,18 @@ CLI::Option* add_option(CLI::App& app, | ||
| 32 | 30 | ||
| 33 | TEST_F(TApp, AddingComplexParser) { | 31 | TEST_F(TApp, AddingComplexParser) { |
| 34 | 32 | ||
| 35 | - | ||
| 36 | - cx comp {0, 0}; | 33 | + cx comp{0, 0}; |
| 37 | add_option(app, "-c,--complex", comp); | 34 | add_option(app, "-c,--complex", comp); |
| 38 | args = {"-c", "1.5", "2.5"}; | 35 | args = {"-c", "1.5", "2.5"}; |
| 39 | 36 | ||
| 40 | run(); | 37 | run(); |
| 41 | 38 | ||
| 42 | - EXPECT_EQ(cx(1.5,2.5), comp); | 39 | + EXPECT_EQ(cx(1.5, 2.5), comp); |
| 43 | } | 40 | } |
| 44 | 41 | ||
| 45 | TEST_F(TApp, DefaultComplex) { | 42 | TEST_F(TApp, DefaultComplex) { |
| 46 | 43 | ||
| 47 | - cx comp {1, 2}; | 44 | + cx comp{1, 2}; |
| 48 | add_option(app, "-c,--complex", comp, "", true); | 45 | add_option(app, "-c,--complex", comp, "", true); |
| 49 | args = {"-c", "4", "3"}; | 46 | args = {"-c", "4", "3"}; |
| 50 | 47 | ||
| @@ -52,15 +49,15 @@ TEST_F(TApp, DefaultComplex) { | @@ -52,15 +49,15 @@ TEST_F(TApp, DefaultComplex) { | ||
| 52 | EXPECT_THAT(help, HasSubstr("1")); | 49 | EXPECT_THAT(help, HasSubstr("1")); |
| 53 | EXPECT_THAT(help, HasSubstr("2")); | 50 | EXPECT_THAT(help, HasSubstr("2")); |
| 54 | 51 | ||
| 55 | - EXPECT_EQ(cx(1,2), comp); | 52 | + EXPECT_EQ(cx(1, 2), comp); |
| 56 | 53 | ||
| 57 | run(); | 54 | run(); |
| 58 | 55 | ||
| 59 | - EXPECT_EQ(cx(4,3), comp); | 56 | + EXPECT_EQ(cx(4, 3), comp); |
| 60 | } | 57 | } |
| 61 | 58 | ||
| 62 | TEST_F(TApp, BuiltinComplex) { | 59 | TEST_F(TApp, BuiltinComplex) { |
| 63 | - cx comp {1, 2}; | 60 | + cx comp{1, 2}; |
| 64 | app.add_complex("-c,--complex", comp, "", true); | 61 | app.add_complex("-c,--complex", comp, "", true); |
| 65 | 62 | ||
| 66 | args = {"-c", "4", "3"}; | 63 | args = {"-c", "4", "3"}; |
| @@ -70,28 +67,26 @@ TEST_F(TApp, BuiltinComplex) { | @@ -70,28 +67,26 @@ TEST_F(TApp, BuiltinComplex) { | ||
| 70 | EXPECT_THAT(help, HasSubstr("2")); | 67 | EXPECT_THAT(help, HasSubstr("2")); |
| 71 | EXPECT_THAT(help, HasSubstr("COMPLEX")); | 68 | EXPECT_THAT(help, HasSubstr("COMPLEX")); |
| 72 | 69 | ||
| 73 | - EXPECT_EQ(cx(1,2), comp); | 70 | + EXPECT_EQ(cx(1, 2), comp); |
| 74 | 71 | ||
| 75 | run(); | 72 | run(); |
| 76 | 73 | ||
| 77 | - EXPECT_EQ(cx(4,3), comp); | ||
| 78 | - | 74 | + EXPECT_EQ(cx(4, 3), comp); |
| 79 | } | 75 | } |
| 80 | 76 | ||
| 81 | - | ||
| 82 | TEST_F(TApp, BuiltinComplexIgnoreI) { | 77 | TEST_F(TApp, BuiltinComplexIgnoreI) { |
| 83 | - cx comp {1, 2}; | 78 | + cx comp{1, 2}; |
| 84 | app.add_complex("-c,--complex", comp); | 79 | app.add_complex("-c,--complex", comp); |
| 85 | 80 | ||
| 86 | args = {"-c", "4", "3i"}; | 81 | args = {"-c", "4", "3i"}; |
| 87 | 82 | ||
| 88 | run(); | 83 | run(); |
| 89 | 84 | ||
| 90 | - EXPECT_EQ(cx(4,3), comp); | 85 | + EXPECT_EQ(cx(4, 3), comp); |
| 91 | } | 86 | } |
| 92 | 87 | ||
| 93 | TEST_F(TApp, BuiltinComplexFail) { | 88 | TEST_F(TApp, BuiltinComplexFail) { |
| 94 | - cx comp {1, 2}; | 89 | + cx comp{1, 2}; |
| 95 | app.add_complex("-c,--complex", comp); | 90 | app.add_complex("-c,--complex", comp); |
| 96 | 91 | ||
| 97 | args = {"-c", "4"}; | 92 | args = {"-c", "4"}; |
tests/SimpleTest.cpp
| @@ -11,20 +11,18 @@ using input_t = std::vector<std::string>; | @@ -11,20 +11,18 @@ using input_t = std::vector<std::string>; | ||
| 11 | TEST(Basic, Empty) { | 11 | TEST(Basic, Empty) { |
| 12 | 12 | ||
| 13 | { | 13 | { |
| 14 | - CLI::App app; | ||
| 15 | - input_t simpleput; | ||
| 16 | - app.parse(simpleput); | 14 | + CLI::App app; |
| 15 | + input_t simpleput; | ||
| 16 | + app.parse(simpleput); | ||
| 17 | } | 17 | } |
| 18 | { | 18 | { |
| 19 | - CLI::App app; | ||
| 20 | - input_t spare = {"spare"}; | ||
| 21 | - EXPECT_THROW(app.parse(spare), CLI::ExtrasError); | 19 | + CLI::App app; |
| 20 | + input_t spare = {"spare"}; | ||
| 21 | + EXPECT_THROW(app.parse(spare), CLI::ExtrasError); | ||
| 22 | } | 22 | } |
| 23 | { | 23 | { |
| 24 | - CLI::App app; | ||
| 25 | - input_t simpleput; | ||
| 26 | - app.parse(simpleput); | 24 | + CLI::App app; |
| 25 | + input_t simpleput; | ||
| 26 | + app.parse(simpleput); | ||
| 27 | } | 27 | } |
| 28 | } | 28 | } |
| 29 | - | ||
| 30 | - |
tests/SubcommandTest.cpp
| @@ -9,15 +9,15 @@ TEST_F(TApp, BasicSubcommands) { | @@ -9,15 +9,15 @@ TEST_F(TApp, BasicSubcommands) { | ||
| 9 | EXPECT_THROW(app.get_subcommand("sub3"), CLI::OptionNotFound); | 9 | EXPECT_THROW(app.get_subcommand("sub3"), CLI::OptionNotFound); |
| 10 | 10 | ||
| 11 | run(); | 11 | run(); |
| 12 | - EXPECT_EQ((size_t) 0, app.get_subcommands().size()); | ||
| 13 | - | 12 | + EXPECT_EQ((size_t)0, app.get_subcommands().size()); |
| 13 | + | ||
| 14 | app.reset(); | 14 | app.reset(); |
| 15 | args = {"sub1"}; | 15 | args = {"sub1"}; |
| 16 | run(); | 16 | run(); |
| 17 | EXPECT_EQ(sub1, app.get_subcommands().at(0)); | 17 | EXPECT_EQ(sub1, app.get_subcommands().at(0)); |
| 18 | 18 | ||
| 19 | app.reset(); | 19 | app.reset(); |
| 20 | - EXPECT_EQ((size_t) 0, app.get_subcommands().size()); | 20 | + EXPECT_EQ((size_t)0, app.get_subcommands().size()); |
| 21 | 21 | ||
| 22 | args = {"sub2"}; | 22 | args = {"sub2"}; |
| 23 | run(); | 23 | run(); |
| @@ -34,7 +34,6 @@ TEST_F(TApp, MultiSubFallthrough) { | @@ -34,7 +34,6 @@ TEST_F(TApp, MultiSubFallthrough) { | ||
| 34 | auto sub1 = app.add_subcommand("sub1"); | 34 | auto sub1 = app.add_subcommand("sub1"); |
| 35 | auto sub2 = app.add_subcommand("sub2"); | 35 | auto sub2 = app.add_subcommand("sub2"); |
| 36 | 36 | ||
| 37 | - | ||
| 38 | args = {"sub1", "sub2"}; | 37 | args = {"sub1", "sub2"}; |
| 39 | run(); | 38 | run(); |
| 40 | EXPECT_TRUE(app.got_subcommand("sub1")); | 39 | EXPECT_TRUE(app.got_subcommand("sub1")); |
| @@ -71,27 +70,21 @@ TEST_F(TApp, MultiSubFallthrough) { | @@ -71,27 +70,21 @@ TEST_F(TApp, MultiSubFallthrough) { | ||
| 71 | EXPECT_TRUE(*sub1); | 70 | EXPECT_TRUE(*sub1); |
| 72 | EXPECT_FALSE(*sub2); | 71 | EXPECT_FALSE(*sub2); |
| 73 | EXPECT_FALSE(sub2->parsed()); | 72 | EXPECT_FALSE(sub2->parsed()); |
| 74 | - | 73 | + |
| 75 | EXPECT_THROW(app.got_subcommand("sub3"), CLI::OptionNotFound); | 74 | EXPECT_THROW(app.got_subcommand("sub3"), CLI::OptionNotFound); |
| 76 | } | 75 | } |
| 77 | 76 | ||
| 78 | - | ||
| 79 | TEST_F(TApp, Callbacks) { | 77 | TEST_F(TApp, Callbacks) { |
| 80 | auto sub1 = app.add_subcommand("sub1"); | 78 | auto sub1 = app.add_subcommand("sub1"); |
| 81 | - sub1->set_callback([](){ | ||
| 82 | - throw CLI::Success(); | ||
| 83 | - }); | 79 | + sub1->set_callback([]() { throw CLI::Success(); }); |
| 84 | auto sub2 = app.add_subcommand("sub2"); | 80 | auto sub2 = app.add_subcommand("sub2"); |
| 85 | bool val = false; | 81 | bool val = false; |
| 86 | - sub2->set_callback([&val](){ | ||
| 87 | - val = true; | ||
| 88 | - }); | ||
| 89 | - | 82 | + sub2->set_callback([&val]() { val = true; }); |
| 83 | + | ||
| 90 | args = {"sub2"}; | 84 | args = {"sub2"}; |
| 91 | EXPECT_FALSE(val); | 85 | EXPECT_FALSE(val); |
| 92 | run(); | 86 | run(); |
| 93 | EXPECT_TRUE(val); | 87 | EXPECT_TRUE(val); |
| 94 | - | ||
| 95 | } | 88 | } |
| 96 | 89 | ||
| 97 | TEST_F(TApp, NoFallThroughOpts) { | 90 | TEST_F(TApp, NoFallThroughOpts) { |
| @@ -99,18 +92,17 @@ TEST_F(TApp, NoFallThroughOpts) { | @@ -99,18 +92,17 @@ TEST_F(TApp, NoFallThroughOpts) { | ||
| 99 | app.add_option("--val", val); | 92 | app.add_option("--val", val); |
| 100 | 93 | ||
| 101 | app.add_subcommand("sub"); | 94 | app.add_subcommand("sub"); |
| 102 | - | 95 | + |
| 103 | args = {"sub", "--val", "2"}; | 96 | args = {"sub", "--val", "2"}; |
| 104 | EXPECT_THROW(run(), CLI::ExtrasError); | 97 | EXPECT_THROW(run(), CLI::ExtrasError); |
| 105 | } | 98 | } |
| 106 | 99 | ||
| 107 | - | ||
| 108 | TEST_F(TApp, NoFallThroughPositionals) { | 100 | TEST_F(TApp, NoFallThroughPositionals) { |
| 109 | int val = 1; | 101 | int val = 1; |
| 110 | app.add_option("val", val); | 102 | app.add_option("val", val); |
| 111 | 103 | ||
| 112 | app.add_subcommand("sub"); | 104 | app.add_subcommand("sub"); |
| 113 | - | 105 | + |
| 114 | args = {"sub", "2"}; | 106 | args = {"sub", "2"}; |
| 115 | EXPECT_THROW(run(), CLI::ExtrasError); | 107 | EXPECT_THROW(run(), CLI::ExtrasError); |
| 116 | } | 108 | } |
| @@ -121,33 +113,31 @@ TEST_F(TApp, FallThroughRegular) { | @@ -121,33 +113,31 @@ TEST_F(TApp, FallThroughRegular) { | ||
| 121 | app.add_option("--val", val); | 113 | app.add_option("--val", val); |
| 122 | 114 | ||
| 123 | app.add_subcommand("sub"); | 115 | app.add_subcommand("sub"); |
| 124 | - | 116 | + |
| 125 | args = {"sub", "--val", "2"}; | 117 | args = {"sub", "--val", "2"}; |
| 126 | // Should not throw | 118 | // Should not throw |
| 127 | run(); | 119 | run(); |
| 128 | } | 120 | } |
| 129 | 121 | ||
| 130 | - | ||
| 131 | TEST_F(TApp, FallThroughShort) { | 122 | TEST_F(TApp, FallThroughShort) { |
| 132 | app.fallthrough(); | 123 | app.fallthrough(); |
| 133 | int val = 1; | 124 | int val = 1; |
| 134 | app.add_option("-v", val); | 125 | app.add_option("-v", val); |
| 135 | 126 | ||
| 136 | app.add_subcommand("sub"); | 127 | app.add_subcommand("sub"); |
| 137 | - | 128 | + |
| 138 | args = {"sub", "-v", "2"}; | 129 | args = {"sub", "-v", "2"}; |
| 139 | // Should not throw | 130 | // Should not throw |
| 140 | run(); | 131 | run(); |
| 141 | } | 132 | } |
| 142 | 133 | ||
| 143 | - | ||
| 144 | TEST_F(TApp, FallThroughPositional) { | 134 | TEST_F(TApp, FallThroughPositional) { |
| 145 | app.fallthrough(); | 135 | app.fallthrough(); |
| 146 | int val = 1; | 136 | int val = 1; |
| 147 | app.add_option("val", val); | 137 | app.add_option("val", val); |
| 148 | 138 | ||
| 149 | app.add_subcommand("sub"); | 139 | app.add_subcommand("sub"); |
| 150 | - | 140 | + |
| 151 | args = {"sub", "2"}; | 141 | args = {"sub", "2"}; |
| 152 | // Should not throw | 142 | // Should not throw |
| 153 | run(); | 143 | run(); |
| @@ -159,13 +149,12 @@ TEST_F(TApp, FallThroughEquals) { | @@ -159,13 +149,12 @@ TEST_F(TApp, FallThroughEquals) { | ||
| 159 | app.add_option("--val", val); | 149 | app.add_option("--val", val); |
| 160 | 150 | ||
| 161 | app.add_subcommand("sub"); | 151 | app.add_subcommand("sub"); |
| 162 | - | 152 | + |
| 163 | args = {"sub", "--val=2"}; | 153 | args = {"sub", "--val=2"}; |
| 164 | // Should not throw | 154 | // Should not throw |
| 165 | run(); | 155 | run(); |
| 166 | } | 156 | } |
| 167 | 157 | ||
| 168 | - | ||
| 169 | TEST_F(TApp, EvilParseFallthrough) { | 158 | TEST_F(TApp, EvilParseFallthrough) { |
| 170 | app.fallthrough(); | 159 | app.fallthrough(); |
| 171 | int val1 = 0, val2 = 0; | 160 | int val1 = 0, val2 = 0; |
| @@ -173,7 +162,7 @@ TEST_F(TApp, EvilParseFallthrough) { | @@ -173,7 +162,7 @@ TEST_F(TApp, EvilParseFallthrough) { | ||
| 173 | 162 | ||
| 174 | auto sub = app.add_subcommand("sub"); | 163 | auto sub = app.add_subcommand("sub"); |
| 175 | sub->add_option("val2", val2); | 164 | sub->add_option("val2", val2); |
| 176 | - | 165 | + |
| 177 | args = {"sub", "--val1", "1", "2"}; | 166 | args = {"sub", "--val1", "1", "2"}; |
| 178 | // Should not throw | 167 | // Should not throw |
| 179 | run(); | 168 | run(); |
| @@ -188,10 +177,8 @@ TEST_F(TApp, CallbackOrdering) { | @@ -188,10 +177,8 @@ TEST_F(TApp, CallbackOrdering) { | ||
| 188 | app.add_option("--val", val); | 177 | app.add_option("--val", val); |
| 189 | 178 | ||
| 190 | auto sub = app.add_subcommand("sub"); | 179 | auto sub = app.add_subcommand("sub"); |
| 191 | - sub->set_callback([&val, &sub_val](){ | ||
| 192 | - sub_val = val; | ||
| 193 | - }); | ||
| 194 | - | 180 | + sub->set_callback([&val, &sub_val]() { sub_val = val; }); |
| 181 | + | ||
| 195 | args = {"sub", "--val=2"}; | 182 | args = {"sub", "--val=2"}; |
| 196 | run(); | 183 | run(); |
| 197 | EXPECT_EQ(2, val); | 184 | EXPECT_EQ(2, val); |
| @@ -202,7 +189,6 @@ TEST_F(TApp, CallbackOrdering) { | @@ -202,7 +189,6 @@ TEST_F(TApp, CallbackOrdering) { | ||
| 202 | run(); | 189 | run(); |
| 203 | EXPECT_EQ(2, val); | 190 | EXPECT_EQ(2, val); |
| 204 | EXPECT_EQ(2, sub_val); | 191 | EXPECT_EQ(2, sub_val); |
| 205 | - | ||
| 206 | } | 192 | } |
| 207 | 193 | ||
| 208 | TEST_F(TApp, RequiredSubCom) { | 194 | TEST_F(TApp, RequiredSubCom) { |
| @@ -218,7 +204,6 @@ TEST_F(TApp, RequiredSubCom) { | @@ -218,7 +204,6 @@ TEST_F(TApp, RequiredSubCom) { | ||
| 218 | args = {"sub1"}; | 204 | args = {"sub1"}; |
| 219 | 205 | ||
| 220 | run(); | 206 | run(); |
| 221 | - | ||
| 222 | } | 207 | } |
| 223 | 208 | ||
| 224 | TEST_F(TApp, Required1SubCom) { | 209 | TEST_F(TApp, Required1SubCom) { |
| @@ -248,8 +233,8 @@ TEST_F(TApp, BadSubcomSearch) { | @@ -248,8 +233,8 @@ TEST_F(TApp, BadSubcomSearch) { | ||
| 248 | 233 | ||
| 249 | struct SubcommandProgram : public TApp { | 234 | struct SubcommandProgram : public TApp { |
| 250 | 235 | ||
| 251 | - CLI::App* start; | ||
| 252 | - CLI::App* stop; | 236 | + CLI::App *start; |
| 237 | + CLI::App *stop; | ||
| 253 | 238 | ||
| 254 | int dummy; | 239 | int dummy; |
| 255 | std::string file; | 240 | std::string file; |
| @@ -258,7 +243,7 @@ struct SubcommandProgram : public TApp { | @@ -258,7 +243,7 @@ struct SubcommandProgram : public TApp { | ||
| 258 | SubcommandProgram() { | 243 | SubcommandProgram() { |
| 259 | start = app.add_subcommand("start", "Start prog"); | 244 | start = app.add_subcommand("start", "Start prog"); |
| 260 | stop = app.add_subcommand("stop", "Stop prog"); | 245 | stop = app.add_subcommand("stop", "Stop prog"); |
| 261 | - | 246 | + |
| 262 | app.add_flag("-d", dummy, "My dummy var"); | 247 | app.add_flag("-d", dummy, "My dummy var"); |
| 263 | start->add_option("-f,--file", file, "File name"); | 248 | start->add_option("-f,--file", file, "File name"); |
| 264 | stop->add_flag("-c,--count", count, "Some flag opt"); | 249 | stop->add_flag("-c,--count", count, "Some flag opt"); |
| @@ -275,7 +260,6 @@ TEST_F(SubcommandProgram, Working) { | @@ -275,7 +260,6 @@ TEST_F(SubcommandProgram, Working) { | ||
| 275 | EXPECT_EQ("filename", file); | 260 | EXPECT_EQ("filename", file); |
| 276 | } | 261 | } |
| 277 | 262 | ||
| 278 | - | ||
| 279 | TEST_F(SubcommandProgram, Spare) { | 263 | TEST_F(SubcommandProgram, Spare) { |
| 280 | args = {"extra", "-d", "start", "-ffilename"}; | 264 | args = {"extra", "-d", "start", "-ffilename"}; |
| 281 | 265 | ||
| @@ -292,7 +276,7 @@ TEST_F(SubcommandProgram, Multiple) { | @@ -292,7 +276,7 @@ TEST_F(SubcommandProgram, Multiple) { | ||
| 292 | args = {"-d", "start", "-ffilename", "stop"}; | 276 | args = {"-d", "start", "-ffilename", "stop"}; |
| 293 | 277 | ||
| 294 | run(); | 278 | run(); |
| 295 | - EXPECT_EQ((size_t) 2, app.get_subcommands().size()); | 279 | + EXPECT_EQ((size_t)2, app.get_subcommands().size()); |
| 296 | EXPECT_EQ(1, dummy); | 280 | EXPECT_EQ(1, dummy); |
| 297 | EXPECT_EQ("filename", file); | 281 | EXPECT_EQ("filename", file); |
| 298 | } | 282 | } |
| @@ -308,20 +292,17 @@ TEST_F(SubcommandProgram, MultipleArgs) { | @@ -308,20 +292,17 @@ TEST_F(SubcommandProgram, MultipleArgs) { | ||
| 308 | 292 | ||
| 309 | run(); | 293 | run(); |
| 310 | 294 | ||
| 311 | - EXPECT_EQ((size_t) 2, app.get_subcommands().size()); | ||
| 312 | - | 295 | + EXPECT_EQ((size_t)2, app.get_subcommands().size()); |
| 313 | } | 296 | } |
| 314 | 297 | ||
| 315 | TEST_F(SubcommandProgram, CaseCheck) { | 298 | TEST_F(SubcommandProgram, CaseCheck) { |
| 316 | args = {"Start"}; | 299 | args = {"Start"}; |
| 317 | EXPECT_THROW(run(), CLI::ExtrasError); | 300 | EXPECT_THROW(run(), CLI::ExtrasError); |
| 318 | 301 | ||
| 319 | - | ||
| 320 | app.reset(); | 302 | app.reset(); |
| 321 | args = {"start"}; | 303 | args = {"start"}; |
| 322 | run(); | 304 | run(); |
| 323 | 305 | ||
| 324 | - | ||
| 325 | app.reset(); | 306 | app.reset(); |
| 326 | start->ignore_case(); | 307 | start->ignore_case(); |
| 327 | run(); | 308 | run(); |
| @@ -337,15 +318,15 @@ TEST_F(TApp, SubcomInheritCaseCheck) { | @@ -337,15 +318,15 @@ TEST_F(TApp, SubcomInheritCaseCheck) { | ||
| 337 | auto sub2 = app.add_subcommand("sub2"); | 318 | auto sub2 = app.add_subcommand("sub2"); |
| 338 | 319 | ||
| 339 | run(); | 320 | run(); |
| 340 | - EXPECT_EQ((size_t) 0, app.get_subcommands().size()); | ||
| 341 | - | 321 | + EXPECT_EQ((size_t)0, app.get_subcommands().size()); |
| 322 | + | ||
| 342 | app.reset(); | 323 | app.reset(); |
| 343 | args = {"SuB1"}; | 324 | args = {"SuB1"}; |
| 344 | run(); | 325 | run(); |
| 345 | EXPECT_EQ(sub1, app.get_subcommands().at(0)); | 326 | EXPECT_EQ(sub1, app.get_subcommands().at(0)); |
| 346 | 327 | ||
| 347 | app.reset(); | 328 | app.reset(); |
| 348 | - EXPECT_EQ((size_t) 0, app.get_subcommands().size()); | 329 | + EXPECT_EQ((size_t)0, app.get_subcommands().size()); |
| 349 | 330 | ||
| 350 | args = {"sUb2"}; | 331 | args = {"sUb2"}; |
| 351 | run(); | 332 | run(); |
| @@ -366,9 +347,7 @@ TEST_F(SubcommandProgram, HelpOrder) { | @@ -366,9 +347,7 @@ TEST_F(SubcommandProgram, HelpOrder) { | ||
| 366 | 347 | ||
| 367 | TEST_F(SubcommandProgram, Callbacks) { | 348 | TEST_F(SubcommandProgram, Callbacks) { |
| 368 | 349 | ||
| 369 | - start->set_callback([](){ | ||
| 370 | - throw CLI::Success(); | ||
| 371 | - }); | 350 | + start->set_callback([]() { throw CLI::Success(); }); |
| 372 | 351 | ||
| 373 | run(); | 352 | run(); |
| 374 | 353 | ||
| @@ -377,5 +356,4 @@ TEST_F(SubcommandProgram, Callbacks) { | @@ -377,5 +356,4 @@ TEST_F(SubcommandProgram, Callbacks) { | ||
| 377 | args = {"start"}; | 356 | args = {"start"}; |
| 378 | 357 | ||
| 379 | EXPECT_THROW(run(), CLI::Success); | 358 | EXPECT_THROW(run(), CLI::Success); |
| 380 | - | ||
| 381 | } | 359 | } |
tests/TimerTest.cpp
| @@ -28,7 +28,7 @@ TEST(Timer, STimes) { | @@ -28,7 +28,7 @@ TEST(Timer, STimes) { | ||
| 28 | */ | 28 | */ |
| 29 | 29 | ||
| 30 | // Fails on Windows | 30 | // Fails on Windows |
| 31 | -//TEST(Timer, UStimes) { | 31 | +// TEST(Timer, UStimes) { |
| 32 | // CLI::Timer timer; | 32 | // CLI::Timer timer; |
| 33 | // std::this_thread::sleep_for(std::chrono::microseconds(2)); | 33 | // std::this_thread::sleep_for(std::chrono::microseconds(2)); |
| 34 | // std::string output = timer.to_string(); | 34 | // std::string output = timer.to_string(); |
| @@ -58,8 +58,7 @@ TEST(Timer, PrintTimer) { | @@ -58,8 +58,7 @@ TEST(Timer, PrintTimer) { | ||
| 58 | 58 | ||
| 59 | TEST(Timer, TimeItTimer) { | 59 | TEST(Timer, TimeItTimer) { |
| 60 | CLI::Timer timer; | 60 | CLI::Timer timer; |
| 61 | - std::string output = timer.time_it([](){ | ||
| 62 | - std::this_thread::sleep_for(std::chrono::milliseconds(10));}, .1); | 61 | + std::string output = timer.time_it([]() { std::this_thread::sleep_for(std::chrono::milliseconds(10)); }, .1); |
| 63 | std::cout << output << std::endl; | 62 | std::cout << output << std::endl; |
| 64 | EXPECT_THAT(output, HasSubstr("ms")); | 63 | EXPECT_THAT(output, HasSubstr("ms")); |
| 65 | } | 64 | } |
tests/app_helper.hpp
| @@ -20,27 +20,23 @@ struct TApp : public ::testing::Test { | @@ -20,27 +20,23 @@ struct TApp : public ::testing::Test { | ||
| 20 | std::reverse(std::begin(newargs), std::end(newargs)); | 20 | std::reverse(std::begin(newargs), std::end(newargs)); |
| 21 | return app.parse(newargs); | 21 | return app.parse(newargs); |
| 22 | } | 22 | } |
| 23 | - | ||
| 24 | }; | 23 | }; |
| 25 | 24 | ||
| 26 | - | ||
| 27 | class TempFile { | 25 | class TempFile { |
| 28 | std::string _name; | 26 | std::string _name; |
| 29 | 27 | ||
| 30 | -public: | ||
| 31 | - | 28 | + public: |
| 32 | TempFile(std::string name) : _name(name) { | 29 | TempFile(std::string name) : _name(name) { |
| 33 | if(!CLI::NonexistentPath(_name)) | 30 | if(!CLI::NonexistentPath(_name)) |
| 34 | throw std::runtime_error(_name); | 31 | throw std::runtime_error(_name); |
| 35 | - | ||
| 36 | } | 32 | } |
| 37 | 33 | ||
| 38 | ~TempFile() { | 34 | ~TempFile() { |
| 39 | std::remove(_name.c_str()); // Doesn't matter if returns 0 or not | 35 | std::remove(_name.c_str()); // Doesn't matter if returns 0 or not |
| 40 | } | 36 | } |
| 41 | 37 | ||
| 42 | - operator const std::string& () const {return _name;} | ||
| 43 | - const char* c_str() const {return _name.c_str();} | 38 | + operator const std::string &() const { return _name; } |
| 39 | + const char *c_str() const { return _name.c_str(); } | ||
| 44 | }; | 40 | }; |
| 45 | 41 | ||
| 46 | inline void put_env(std::string name, std::string value) { | 42 | inline void put_env(std::string name, std::string value) { |
| @@ -58,4 +54,3 @@ inline void unset_env(std::string name) { | @@ -58,4 +54,3 @@ inline void unset_env(std::string name) { | ||
| 58 | unsetenv(name.c_str()); | 54 | unsetenv(name.c_str()); |
| 59 | #endif | 55 | #endif |
| 60 | } | 56 | } |
| 61 | - |
tests/link_test_1.cpp