From edef8cf773693439024b508df5f0936c6970c013 Mon Sep 17 00:00:00 2001 From: badaix Date: Mon, 2 Aug 2021 14:38:28 +0200 Subject: [PATCH] Fix Windows compilation, add "reset" of Options --- CMakeLists.txt | 4 ++-- README.md | 1 + example/popl_example.cpp | 2 +- include/popl.hpp | 34 ++++++++++++++++++++++------------ test/test_main.cpp | 2 +- 5 files changed, 27 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f920383..026efec 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,7 @@ # (__) \__/(__) \____/ # This file is part of popl (program options parser lib) -# Copyright (C) 2015-2019 Johannes Pohl +# Copyright (C) 2015-2021 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.90") + set(PROJECT_VERSION "1.3.0") endif () diff --git a/README.md b/README.md index 42cccaf..dc614f7 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ popl is a C++ command line arguments parser that supports the same set of option * No external dependencies, just C++11 * Platform independent * Supports the same set of options as GNU's `getopt`: short options, long options, non-option arguments, ... +* Supports parsing of `ini` files * Templatized option parsing: arguments are directly casted into the desired target type * Automatic creation of a usage message * Console help message diff --git a/example/popl_example.cpp b/example/popl_example.cpp index 173a052..142666c 100644 --- a/example/popl_example.cpp +++ b/example/popl_example.cpp @@ -1,6 +1,6 @@ /*** This file is part of popl (program options parser lib) - Copyright (C) 2015-2019 Johannes Pohl + Copyright (C) 2015-2021 Johannes Pohl This software may be modified and distributed under the terms of the MIT license. See the LICENSE file for details. diff --git a/include/popl.hpp b/include/popl.hpp index 6321a14..8fd668b 100644 --- a/include/popl.hpp +++ b/include/popl.hpp @@ -3,11 +3,11 @@ ( _ \ / \( _ \( ) ) __/( O )) __// (_/\ (__) \__/(__) \____/ - version 1.2.90 + version 1.3.0 https://github.com/badaix/popl This file is part of popl (program options parser lib) - Copyright (C) 2015-2019 Johannes Pohl + Copyright (C) 2015-2021 Johannes Pohl This software may be modified and distributed under the terms of the MIT license. See the LICENSE file for details. @@ -33,12 +33,15 @@ #include #include #include +#ifdef WINDOWS +#include +#endif namespace popl { -#define POPL_VERSION "1.2.90" +#define POPL_VERSION "1.3.0" /// Option's argument type @@ -338,6 +341,9 @@ class OptionParser /// @param argv command line arguments void parse(int argc, const char* const argv[]); + /// Delete all parsed options + void reset(); + /// Produce a help message /// @param max_attribute show options up to this level (optional, advanced, expert) /// @return the help message @@ -761,8 +767,10 @@ inline void Value::update_reference() { if (this->assign_to_) { - if (this->is_set() || default_) - *this->assign_to_ = value(); + if (!this->is_set() && default_) + *this->assign_to_ = *default_; + else if (this->is_set()) + *this->assign_to_ = values_.back(); } } @@ -977,8 +985,6 @@ inline void OptionParser::parse(const std::string& ini_filename) Option_ptr option = find_option(key); if (option && (option->attribute() == Attribute::inactive)) option = nullptr; - // if (option && (option->argument_type() != Argument::required)) - // option = nullptr; if (option) option->parse(OptionName::long_name, key_value.second.c_str()); @@ -989,11 +995,6 @@ inline void OptionParser::parse(const std::string& ini_filename) inline void OptionParser::parse(int argc, const char* const argv[]) { - unknown_options_.clear(); - non_option_args_.clear(); - for (auto& opt : options_) - opt->clear(); - for (int n = 1; n < argc; ++n) { const std::string arg(argv[n]); @@ -1094,6 +1095,15 @@ inline void OptionParser::parse(int argc, const char* const argv[]) } +inline void OptionParser::reset() +{ + unknown_options_.clear(); + non_option_args_.clear(); + for (auto& opt : options_) + opt->clear(); +} + + inline std::string OptionParser::help(const Attribute& max_attribute) const { ConsoleOptionPrinter option_printer(this); diff --git a/test/test_main.cpp b/test/test_main.cpp index e9b8280..120552c 100644 --- a/test/test_main.cpp +++ b/test/test_main.cpp @@ -1,6 +1,6 @@ /*** This file is part of popl (program options parser lib) - Copyright (C) 2015-2019 Johannes Pohl + Copyright (C) 2015-2021 Johannes Pohl This software may be modified and distributed under the terms of the MIT license. See the LICENSE file for details.