diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..7b1bec3 --- /dev/null +++ b/.clang-format @@ -0,0 +1,20 @@ +--- +AccessModifierOffset: '-4' +AllowShortBlocksOnASingleLine: 'false' +AllowShortCaseLabelsOnASingleLine: 'false' +AllowShortFunctionsOnASingleLine: None +AllowShortIfStatementsOnASingleLine: 'false' +AllowShortLoopsOnASingleLine: 'false' +AlwaysBreakTemplateDeclarations: 'true' +BreakBeforeBraces: Allman +ColumnLimit: '160' +IndentCaseLabels: 'true' +IndentWidth: '4' +Language: Cpp +MaxEmptyLinesToKeep: '3' +PenaltyBreakComment: '100000' +PointerAlignment: Left +Standard: Cpp11 +UseTab: Never + +... diff --git a/CMakeLists.txt b/CMakeLists.txt index 2d67f7a..2d4c85e 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,7 @@ # (__) \__/(__) \____/ # This file is part of popl (program options parser lib) -# Copyright (C) 2015-2018 Johannes Pohl +# Copyright (C) 2015-2019 Johannes Pohl # This software may be modified and distributed under the terms # of the MIT license. See the LICENSE file for details. @@ -18,7 +18,7 @@ if (CMAKE_VERSION VERSION_LESS "3.1") else () set (CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_EXTENSIONS OFF) - set(PROJECT_VERSION "1.2.0") + set(PROJECT_VERSION "1.2.90") endif () diff --git a/Makefile b/Makefile index de31efb..5d04977 100644 --- a/Makefile +++ b/Makefile @@ -15,6 +15,9 @@ BIN = popl_example all: $(TARGET) +reformat: + clang-format -i include/popl.hpp + $(TARGET): $(OBJ) $(CXX) $(CXXFLAGS) -o $(BIN) $(OBJ) $(LDFLAGS) strip $(BIN) diff --git a/include/popl.hpp b/include/popl.hpp index fad23b4..6b91e78 100644 --- a/include/popl.hpp +++ b/include/popl.hpp @@ -1,20 +1,21 @@ /*** - ____ __ ____ __ - ( _ \ / \( _ \( ) + ____ __ ____ __ + ( _ \ / \( _ \( ) ) __/( O )) __// (_/\ (__) \__/(__) \____/ - version 1.2.0 + version 1.2.90 https://github.com/badaix/popl This file is part of popl (program options parser lib) - Copyright (C) 2015-2018 Johannes Pohl - + Copyright (C) 2015-2019 Johannes Pohl + This software may be modified and distributed under the terms of the MIT license. See the LICENSE file for details. ***/ /// checked with clang-tidy: -/// run-clang-tidy-3.8.py -header-filter='.*' -checks='*,-misc-definitions-in-headers,-google-readability-braces-around-statements,-readability-braces-around-statements,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-google-build-using-namespace,-google-build-using-namespace' +/// run-clang-tidy-3.8.py -header-filter='.*' +/// -checks='*,-misc-definitions-in-headers,-google-readability-braces-around-statements,-readability-braces-around-statements,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-google-build-using-namespace,-google-build-using-namespace' #ifndef POPL_HPP #define POPL_HPP @@ -26,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -36,7 +38,7 @@ namespace popl { -#define POPL_VERSION "1.2.0" +#define POPL_VERSION "1.2.90" /// Option's argument type @@ -47,9 +49,9 @@ namespace popl */ enum class Argument { - no = 0, // option never takes an argument - required, // option always requires an argument - optional // option may take an argument + no = 0, // option never takes an argument + required, // option always requires an argument + optional // option may take an argument }; @@ -64,12 +66,12 @@ enum class Argument */ enum class Attribute { - inactive = 0, - hidden = 1, - required = 2, - optional = 3, - advanced = 4, - expert = 5 + inactive = 0, + hidden = 1, + required = 2, + optional = 3, + advanced = 4, + expert = 5 }; @@ -81,9 +83,9 @@ enum class Attribute */ enum class OptionName { - unspecified, - short_name, - long_name + unspecified, + short_name, + long_name }; @@ -95,159 +97,165 @@ enum class OptionName */ class Option { -friend class OptionParser; + friend class OptionParser; + public: - /// Construct an Option - /// @param short_name the options's short name. Must be empty or one character. - /// @param long_name the option's long name. Can be empty. - /// @param description the Option's description that will be shown in the help message - Option(const std::string& short_name, const std::string& long_name, std::string description); + /// Construct an Option + /// @param short_name the options's short name. Must be empty or one character. + /// @param long_name the option's long name. Can be empty. + /// @param description the Option's description that will be shown in the help message + Option(const std::string& short_name, const std::string& long_name, std::string description); + + /// Destructor + virtual ~Option() = default; - /// Destructor - virtual ~Option() = default; + /// default copy constructor + Option(const Option&) = default; - /// default copy constructor - Option(const Option&) = default; + /// default move constructor + Option(Option&&) = default; - /// default move constructor - Option(Option&&) = default; + /// default assignement operator + Option& operator=(const Option&) = default; - /// default assignement operator - Option& operator=(const Option&) = default; + /// default move assignement operator + Option& operator=(Option&&) = default; - /// default move assignement operator - Option& operator=(Option&&) = default; + /// Get the Option's short name + /// @return character of the options's short name or 0 if no short name is defined + char short_name() const; - /// Get the Option's short name - /// @return character of the options's short name or 0 if no short name is defined - char short_name() const; + /// Get the Option's long name + /// @return the long name of the Option. Empty string if no long name is defined + std::string long_name() const; - /// Get the Option's long name - /// @return the long name of the Option. Empty string if no long name is defined - std::string long_name() const; + /// Get the Option's long or short name + /// @param what_name the option's name to return + /// @param what_hyphen preced the returned name with (double-)hypen + /// @return the requested name of the Option. Empty string if not defined. + std::string name(OptionName what_name, bool with_hypen = false) const; - /// Get the Option's long or short name - /// @param what_name the option's name to return - /// @param what_hyphen preced the returned name with (double-)hypen - /// @return the requested name of the Option. Empty string if not defined. - std::string name(OptionName what_name, bool with_hypen = false) const; + /// Get the Option's description + /// @return the description + std::string description() const; - /// Get the Option's description - /// @return the description - std::string description() const; + /// Get the Option's default value + /// @param out stream to write the default value to + /// @return true if a default value is available, false if not + virtual bool get_default(std::ostream& out) const = 0; - /// Get the Option's default value - /// @param out stream to write the default value to - /// @return true if a default value is available, false if not - virtual bool get_default(std::ostream& out) const = 0; + /// Set the Option's attribute + /// @param attribute + void set_attribute(const Attribute& attribute); - /// Set the Option's attribute - /// @param attribute - void set_attribute(const Attribute& attribute); + /// Get the Option's attribute + /// @return the Options's attribute + Attribute attribute() const; - /// Get the Option's attribute - /// @return the Options's attribute - Attribute attribute() const; + /// Get the Option's argument type + /// @return argument type (no, required, optional) + virtual Argument argument_type() const = 0; - /// Get the Option's argument type - /// @return argument type (no, required, optional) - virtual Argument argument_type() const = 0; + /// Check how often the Option is set on command line + /// @return the Option's count on command line + virtual size_t count() const = 0; - /// Check how often the Option is set on command line - /// @return the Option's count on command line - virtual size_t count() const = 0; + /// Check if the Option is set + /// @return true if set at least once + virtual bool is_set() const = 0; - /// Check if the Option is set - /// @return true if set at least once - virtual bool is_set() const = 0; protected: - /// Parse the command line option and fill the internal data structure - /// @param what_name short or long option name - /// @param value the value as given on command line - virtual void parse(OptionName what_name, const char* value) = 0; - - /// Clear the internal data structure - virtual void clear() = 0; - - std::string short_name_; - std::string long_name_; - std::string description_; - Attribute attribute_; + /// Parse the command line option and fill the internal data structure + /// @param what_name short or long option name + /// @param value the value as given on command line + virtual void parse(OptionName what_name, const char* value) = 0; + + /// Clear the internal data structure + virtual void clear() = 0; + + std::string short_name_; + std::string long_name_; + std::string description_; + Attribute attribute_; }; - /// Value option with optional default value /** * Value option with optional default value * If set, it requires an argument */ -template +template class Value : public Option { public: - /// Construct an Value Option - /// @param short_name the option's short name. Must be empty or one character. - /// @param long_name the option's long name. Can be empty. - /// @param description the Option's description that will be shown in the help message - Value(const std::string& short_name, const std::string& long_name, const std::string& description); - - /// Construct an Value Option - /// @param short_name the option's short name. Must be empty or one character. - /// @param long_name the option's long name. Can be empty. - /// @param description the Option's description that will be shown in the help message - /// @param default_val the Option's default value - /// @param assign_to pointer to a variable to assign the parsed command line value to - Value(const std::string& short_name, const std::string& long_name, const std::string& description, const T& default_val, T* assign_to = nullptr); - - size_t count() const override; - bool is_set() const override; - - /// Assign the last parsed command line value to "var" - /// @param var pointer to the variable where is value is written to - void assign_to(T* var); - - /// Manually set the Option's value. Deletes current value(s) - /// @param value the new value of the option - void set_value(const T& value); - - /// Get the Option's value. Will throw if option at index idx is not available - /// @param idx the zero based index of the value (if set multiple times) - /// @return the Option's value at index "idx" - T value(size_t idx = 0) const; - - /// Set the Option's default value - /// @param value the default value if not specified on command line - void set_default(const T& value); - - /// Check if the Option has a default value - /// @return true if the Option has a default value - bool has_default() const; - - /// Get the Option's default value. Will throw if no default is set. - /// @return the Option's default value - T get_default() const; - bool get_default(std::ostream& out) const override; - - Argument argument_type() const override; + /// Construct an Value Option + /// @param short_name the option's short name. Must be empty or one character. + /// @param long_name the option's long name. Can be empty. + /// @param description the Option's description that will be shown in the help message + Value(const std::string& short_name, const std::string& long_name, const std::string& description); + + /// Construct an Value Option + /// @param short_name the option's short name. Must be empty or one character. + /// @param long_name the option's long name. Can be empty. + /// @param description the Option's description that will be shown in the help message + /// @param default_val the Option's default value + /// @param assign_to pointer to a variable to assign the parsed command line value to + Value(const std::string& short_name, const std::string& long_name, const std::string& description, const T& default_val, T* assign_to = nullptr); + + size_t count() const override; + bool is_set() const override; + + /// Assign the last parsed command line value to "var" + /// @param var pointer to the variable where is value is written to + void assign_to(T* var); + + /// Manually set the Option's value. Deletes current value(s) + /// @param value the new value of the option + void set_value(const T& value); + + /// Get the Option's value. Will throw if option at index idx is not available + /// @param idx the zero based index of the value (if set multiple times) + /// @return the Option's value at index "idx" + T value(size_t idx = 0) const; + + /// Get the Option's value, return default_value if not set. + /// @param default_value return value if value is not set + /// @param idx the zero based index of the value (if set multiple times) + /// @return the Option's value at index "idx" or the default value or default_value + T value_or(const T& default_value, size_t idx = 0) const; + + /// Set the Option's default value + /// @param value the default value if not specified on command line + void set_default(const T& value); + + /// Check if the Option has a default value + /// @return true if the Option has a default value + bool has_default() const; + + /// Get the Option's default value. Will throw if no default is set. + /// @return the Option's default value + T get_default() const; + bool get_default(std::ostream& out) const override; + + Argument argument_type() const override; protected: - void parse(OptionName what_name, const char* value) override; - std::unique_ptr default_; + void parse(OptionName what_name, const char* value) override; + std::unique_ptr default_; - virtual void update_reference(); - virtual void add_value(const T& value); - void clear() override; + virtual void update_reference(); + virtual void add_value(const T& value); + void clear() override; - T* assign_to_; - std::vector values_; + T* assign_to_; + std::vector values_; }; - /// Value option with implicit default value /** * Value option with implicit default value @@ -255,21 +263,20 @@ class Value : public Option * -without argument it carries the implicit default value * -with argument it carries the explicit value */ -template +template class Implicit : public Value { public: - Implicit(const std::string& short_name, const std::string& long_name, const std::string& description, const T& implicit_val, T* assign_to = nullptr); + Implicit(const std::string& short_name, const std::string& long_name, const std::string& description, const T& implicit_val, T* assign_to = nullptr); - Argument argument_type() const override; + Argument argument_type() const override; protected: - void parse(OptionName what_name, const char* value) override; + void parse(OptionName what_name, const char* value) override; }; - /// Value option without value /** * Value option without value @@ -279,100 +286,102 @@ class Implicit : public Value class Switch : public Value { public: - Switch(const std::string& short_name, const std::string& long_name, const std::string& description, bool* assign_to = nullptr); + Switch(const std::string& short_name, const std::string& long_name, const std::string& description, bool* assign_to = nullptr); - void set_default(const bool& value) = delete; - Argument argument_type() const override; + void set_default(const bool& value) = delete; + Argument argument_type() const override; protected: - void parse(OptionName what_name, const char* value) override; + void parse(OptionName what_name, const char* value) override; }; - using Option_ptr = std::shared_ptr