Bump version of cxxopts

pull/771/head
Michael 5 years ago
parent 8c8af7465e
commit 8a8833d0fd
No known key found for this signature in database
GPG Key ID: 2D51757B47E2434C

@ -3,16 +3,29 @@
This is the changelog for `cxxopts`, a C++11 library for parsing command line
options. The project adheres to semantic versioning.
## Next version
### Changed
* Only search for a C++ compiler in CMakeLists.txt.
* Allow for exceptions to be disabled.
* Fix duplicate default options when there is a short and long option.
* Add `CXXOPTS_NO_EXCEPTIONS` to disable exceptions.
## 2.2
### Changed
* Allow integers to have leading zeroes.
* Build the tests by default.
* Don't check for container when showing positional help.
### Added
* Iterator inputs to `parse_positional`.
* Throw an exception if the option in `parse_positional` doesn't exist.
* Parse a delimited list in a single argument for vector options.
* Add an option to disable implicit value on booleans.
### Bug Fixes
@ -22,6 +35,7 @@ options. The project adheres to semantic versioning.
* Throw on invalid option syntax when beginning with a `-`.
* Throw in `as` when option wasn't present.
* Fix catching exceptions by reference.
* Fix out of bounds errors parsing integers.
## 2.1.1

@ -30,12 +30,13 @@ endforeach()
set(VERSION ${CXXOPTS__VERSION_MAJOR}.${CXXOPTS__VERSION_MINOR}.${CXXOPTS__VERSION_PATCH})
message(STATUS "cxxopts version ${VERSION}")
project(cxxopts VERSION "${VERSION}")
project(cxxopts VERSION "${VERSION}" LANGUAGES CXX)
enable_testing()
option(CXXOPTS_BUILD_EXAMPLES "Set to ON to build examples" ON)
option(CXXOPTS_BUILD_TESTS "Set to ON to build tests" ON)
option(CXXOPTS_ENABLE_INSTALL "Generate the install target" ON)
# request c++11 without gnu extension for the whole project and enable more warnings
if (CXXOPTS_CXX_STANDARD)
@ -53,6 +54,7 @@ elseif(CMAKE_CXX_COMPILER_ID MATCHES "[Cc]lang" OR CMAKE_CXX_COMPILER_ID MATCHES
endif()
add_library(cxxopts INTERFACE)
add_library(cxxopts::cxxopts ALIAS cxxopts)
# optionally, enable unicode support using the ICU library
set(CXXOPTS_USE_UNICODE_HELP FALSE CACHE BOOL "Use ICU Unicode library")
@ -70,35 +72,37 @@ target_include_directories(cxxopts INTERFACE
$<INSTALL_INTERFACE:include>
)
include(CMakePackageConfigHelpers)
set(CXXOPTS_CMAKE_DIR "lib/cmake/cxxopts" CACHE STRING
"Installation directory for cmake files, relative to ${CMAKE_INSTALL_PREFIX}.")
set(version_config "${PROJECT_BINARY_DIR}/cxxopts-config-version.cmake")
set(project_config "${PROJECT_BINARY_DIR}/cxxopts-config.cmake")
set(targets_export_name cxxopts-targets)
# Generate the version, config and target files into the build directory.
write_basic_package_version_file(
${version_config}
VERSION ${VERSION}
COMPATIBILITY AnyNewerVersion)
configure_package_config_file(
${PROJECT_SOURCE_DIR}/cxxopts-config.cmake.in
${project_config}
INSTALL_DESTINATION ${CXXOPTS_CMAKE_DIR})
export(TARGETS cxxopts NAMESPACE cxxopts::
FILE ${PROJECT_BINARY_DIR}/${targets_export_name}.cmake)
# Install version, config and target files.
#install(
# FILES ${project_config} ${version_config}
# DESTINATION ${CXXOPTS_CMAKE_DIR})
#install(EXPORT ${targets_export_name} DESTINATION ${CXXOPTS_CMAKE_DIR}
# NAMESPACE cxxopts::)
# Install the header file and export the target
#install(TARGETS cxxopts EXPORT ${targets_export_name} DESTINATION lib)
#install(FILES ${PROJECT_SOURCE_DIR}/include/cxxopts.hpp DESTINATION include)
if(CXXOPTS_ENABLE_INSTALL)
include(CMakePackageConfigHelpers)
set(CXXOPTS_CMAKE_DIR "lib/cmake/cxxopts" CACHE STRING
"Installation directory for cmake files, relative to ${CMAKE_INSTALL_PREFIX}.")
set(version_config "${PROJECT_BINARY_DIR}/cxxopts-config-version.cmake")
set(project_config "${PROJECT_BINARY_DIR}/cxxopts-config.cmake")
set(targets_export_name cxxopts-targets)
# Generate the version, config and target files into the build directory.
write_basic_package_version_file(
${version_config}
VERSION ${VERSION}
COMPATIBILITY AnyNewerVersion)
configure_package_config_file(
${PROJECT_SOURCE_DIR}/cxxopts-config.cmake.in
${project_config}
INSTALL_DESTINATION ${CXXOPTS_CMAKE_DIR})
export(TARGETS cxxopts NAMESPACE cxxopts::
FILE ${PROJECT_BINARY_DIR}/${targets_export_name}.cmake)
# Install version, config and target files.
install(
FILES ${project_config} ${version_config}
DESTINATION ${CXXOPTS_CMAKE_DIR})
install(EXPORT ${targets_export_name} DESTINATION ${CXXOPTS_CMAKE_DIR}
NAMESPACE cxxopts::)
# Install the header file and export the target
install(TARGETS cxxopts EXPORT ${targets_export_name} DESTINATION lib)
install(FILES ${PROJECT_SOURCE_DIR}/include/cxxopts.hpp DESTINATION include)
endif()
add_subdirectory(src)
add_subdirectory(test)

@ -116,6 +116,18 @@ There is no way to disambiguate positional arguments from the value following
a boolean, so we have chosen that they will be positional arguments, and
therefore, `-o false` does not work.
## `std::vector<T>` values
Parsing of list of values in form of an `std::vector<T>` is also supported, as long as `T`
can be parsed. To separate single values in a list the definition `CXXOPTS_VECTOR_DELIMITER`
is used, which is ',' by default. Ensure that you use no whitespaces between values because
those would be interpreted as the next command line option. Example for a command line option
that can be parsed as a `std::vector<double>`:
~~~
--my_list=1,-2.1,3,4.5
~~~
## Custom help
The string after the program name on the first line of the help can be

@ -29,6 +29,7 @@ THE SOFTWARE.
#include <cctype>
#include <exception>
#include <iostream>
#include <limits>
#include <map>
#include <memory>
#include <regex>
@ -43,6 +44,10 @@ THE SOFTWARE.
#define CXXOPTS_HAS_OPTIONAL
#endif
#ifndef CXXOPTS_VECTOR_DELIMITER
#define CXXOPTS_VECTOR_DELIMITER ','
#endif
#define CXXOPTS__VERSION_MAJOR 2
#define CXXOPTS__VERSION_MINOR 2
#define CXXOPTS__VERSION_PATCH 0
@ -309,6 +314,9 @@ namespace cxxopts
virtual std::shared_ptr<Value>
implicit_value(const std::string& value) = 0;
virtual std::shared_ptr<Value>
no_implicit_value() = 0;
virtual bool
is_boolean() const = 0;
};
@ -354,7 +362,7 @@ namespace cxxopts
{
public:
option_exists_error(const std::string& option)
: OptionSpecException(u8"Option " + LQUOTE + option + RQUOTE + u8" already exists")
: OptionSpecException("Option " + LQUOTE + option + RQUOTE + " already exists")
{
}
};
@ -363,7 +371,7 @@ namespace cxxopts
{
public:
invalid_option_format_error(const std::string& format)
: OptionSpecException(u8"Invalid option format " + LQUOTE + format + RQUOTE)
: OptionSpecException("Invalid option format " + LQUOTE + format + RQUOTE)
{
}
};
@ -371,8 +379,8 @@ namespace cxxopts
class option_syntax_exception : public OptionParseException {
public:
option_syntax_exception(const std::string& text)
: OptionParseException(u8"Argument " + LQUOTE + text + RQUOTE +
u8" starts with a - but has incorrect syntax")
: OptionParseException("Argument " + LQUOTE + text + RQUOTE +
" starts with a - but has incorrect syntax")
{
}
};
@ -381,7 +389,7 @@ namespace cxxopts
{
public:
option_not_exists_exception(const std::string& option)
: OptionParseException(u8"Option " + LQUOTE + option + RQUOTE + u8" does not exist")
: OptionParseException("Option " + LQUOTE + option + RQUOTE + " does not exist")
{
}
};
@ -391,7 +399,7 @@ namespace cxxopts
public:
missing_argument_exception(const std::string& option)
: OptionParseException(
u8"Option " + LQUOTE + option + RQUOTE + u8" is missing an argument"
"Option " + LQUOTE + option + RQUOTE + " is missing an argument"
)
{
}
@ -402,7 +410,7 @@ namespace cxxopts
public:
option_requires_argument_exception(const std::string& option)
: OptionParseException(
u8"Option " + LQUOTE + option + RQUOTE + u8" requires an argument"
"Option " + LQUOTE + option + RQUOTE + " requires an argument"
)
{
}
@ -417,8 +425,8 @@ namespace cxxopts
const std::string& arg
)
: OptionParseException(
u8"Option " + LQUOTE + option + RQUOTE +
u8" does not take an argument, but argument " +
"Option " + LQUOTE + option + RQUOTE +
" does not take an argument, but argument " +
LQUOTE + arg + RQUOTE + " given"
)
{
@ -429,7 +437,7 @@ namespace cxxopts
{
public:
option_not_present_exception(const std::string& option)
: OptionParseException(u8"Option " + LQUOTE + option + RQUOTE + u8" not present")
: OptionParseException("Option " + LQUOTE + option + RQUOTE + " not present")
{
}
};
@ -442,7 +450,7 @@ namespace cxxopts
const std::string& arg
)
: OptionParseException(
u8"Argument " + LQUOTE + arg + RQUOTE + u8" failed to parse"
"Argument " + LQUOTE + arg + RQUOTE + " failed to parse"
)
{
}
@ -453,12 +461,32 @@ namespace cxxopts
public:
option_required_exception(const std::string& option)
: OptionParseException(
u8"Option " + LQUOTE + option + RQUOTE + u8" is required but not present"
"Option " + LQUOTE + option + RQUOTE + " is required but not present"
)
{
}
};
template <typename T>
void throw_or_mimic(const std::string& text)
{
static_assert(std::is_base_of<std::exception, T>::value,
"throw_or_mimic only works on std::exception and "
"deriving classes");
#ifndef CXXOPTS_NO_EXCEPTIONS
// If CXXOPTS_NO_EXCEPTIONS is not defined, just throw
throw T{text};
#else
// Otherwise manually instantiate the exception, print what() to stderr,
// and abort
T exception{text};
std::cerr << exception.what() << std::endl;
std::cerr << "Aborting (exceptions disabled)..." << std::endl;
std::abort();
#endif
}
namespace values
{
namespace
@ -466,9 +494,9 @@ namespace cxxopts
std::basic_regex<char> integer_pattern
("(-)?(0x)?([0-9a-zA-Z]+)|((0x)?0)");
std::basic_regex<char> truthy_pattern
("(t|T)(rue)?");
("(t|T)(rue)?|1");
std::basic_regex<char> falsy_pattern
("((f|F)(alse)?)?");
("(f|F)(alse)?|0");
}
namespace detail
@ -485,16 +513,16 @@ namespace cxxopts
{
if (negative)
{
if (u > static_cast<U>(-(std::numeric_limits<T>::min)()))
if (u > static_cast<U>((std::numeric_limits<T>::min)()))
{
throw argument_incorrect_type(text);
throw_or_mimic<argument_incorrect_type>(text);
}
}
else
{
if (u > static_cast<U>((std::numeric_limits<T>::max)()))
{
throw argument_incorrect_type(text);
throw_or_mimic<argument_incorrect_type>(text);
}
}
}
@ -523,14 +551,15 @@ namespace cxxopts
// if we got to here, then `t` is a positive number that fits into
// `R`. So to avoid MSVC C4146, we first cast it to `R`.
// See https://github.com/jarro2783/cxxopts/issues/62 for more details.
return -static_cast<R>(t);
return -static_cast<R>(t-1)-1;
}
template <typename R, typename T>
T
checked_negate(T&&, const std::string& text, std::false_type)
checked_negate(T&& t, const std::string& text, std::false_type)
{
throw argument_incorrect_type(text);
throw_or_mimic<argument_incorrect_type>(text);
return t;
}
template <typename T>
@ -542,7 +571,7 @@ namespace cxxopts
if (match.length() == 0)
{
throw argument_incorrect_type(text);
throw_or_mimic<argument_incorrect_type>(text);
}
if (match.length(4) > 0)
@ -553,7 +582,6 @@ namespace cxxopts
using US = typename std::make_unsigned<T>::type;
constexpr auto umax = (std::numeric_limits<US>::max)();
constexpr bool is_signed = std::numeric_limits<T>::is_signed;
const bool negative = match.length(1) > 0;
const uint8_t base = match.length(2) > 0 ? 16 : 10;
@ -568,27 +596,28 @@ namespace cxxopts
if (*iter >= '0' && *iter <= '9')
{
digit = *iter - '0';
digit = static_cast<US>(*iter - '0');
}
else if (base == 16 && *iter >= 'a' && *iter <= 'f')
{
digit = *iter - 'a' + 10;
digit = static_cast<US>(*iter - 'a' + 10);
}
else if (base == 16 && *iter >= 'A' && *iter <= 'F')
{
digit = *iter - 'A' + 10;
digit = static_cast<US>(*iter - 'A' + 10);
}
else
{
throw argument_incorrect_type(text);
throw_or_mimic<argument_incorrect_type>(text);
}
if (umax - digit < result * base)
US next = result * base + digit;
if (result > next)
{
throw argument_incorrect_type(text);
throw_or_mimic<argument_incorrect_type>(text);
}
result = result * base + digit;
result = next;
}
detail::check_signed_range<T>(negative, result, text);
@ -601,7 +630,7 @@ namespace cxxopts
}
else
{
value = result;
value = static_cast<T>(result);
}
}
@ -611,7 +640,7 @@ namespace cxxopts
std::stringstream in(text);
in >> value;
if (!in) {
throw argument_incorrect_type(text);
throw_or_mimic<argument_incorrect_type>(text);
}
}
@ -691,7 +720,7 @@ namespace cxxopts
return;
}
throw argument_incorrect_type(text);
throw_or_mimic<argument_incorrect_type>(text);
}
inline
@ -714,9 +743,13 @@ namespace cxxopts
void
parse_value(const std::string& text, std::vector<T>& value)
{
T v;
parse_value(text, v);
value.push_back(v);
std::stringstream in(text);
std::string token;
while(in.eof() == false && std::getline(in, token, CXXOPTS_VECTOR_DELIMITER)) {
T v;
parse_value(token, v);
value.emplace_back(std::move(v));
}
}
#ifdef CXXOPTS_HAS_OPTIONAL
@ -825,6 +858,13 @@ namespace cxxopts
return shared_from_this();
}
std::shared_ptr<Value>
no_implicit_value()
{
m_implicit = false;
return shared_from_this();
}
std::string
get_default_value() const
{
@ -1035,21 +1075,29 @@ namespace cxxopts
parse_default(std::shared_ptr<const OptionDetails> details)
{
ensure_value(details);
m_default = true;
m_value->parse();
}
size_t
count() const
count() const noexcept
{
return m_count;
}
// TODO: maybe default options should count towards the number of arguments
bool
has_default() const noexcept
{
return m_default;
}
template <typename T>
const T&
as() const
{
if (m_value == nullptr) {
throw std::domain_error("No value");
throw_or_mimic<std::domain_error>("No value");
}
#ifdef CXXOPTS_NO_RTTI
@ -1071,6 +1119,7 @@ namespace cxxopts
std::shared_ptr<Value> m_value;
size_t m_count = 0;
bool m_default = false;
};
class KeyValue
@ -1143,7 +1192,7 @@ namespace cxxopts
if (iter == m_options->end())
{
throw option_not_present_exception(option);
throw_or_mimic<option_not_present_exception>(option);
}
auto riter = m_results.find(iter->second);
@ -1202,6 +1251,28 @@ namespace cxxopts
std::vector<KeyValue> m_sequential;
};
struct Option
{
Option
(
const std::string& opts,
const std::string& desc,
const std::shared_ptr<const Value>& value = ::cxxopts::value<bool>(),
const std::string& arg_help = ""
)
: opts_(opts)
, desc_(desc)
, value_(value)
, arg_help_(arg_help)
{
}
std::string opts_;
std::string desc_;
std::shared_ptr<const Value> value_;
std::string arg_help_;
};
class Options
{
typedef std::unordered_map<std::string, std::shared_ptr<OptionDetails>>
@ -1254,6 +1325,20 @@ namespace cxxopts
OptionAdder
add_options(std::string group = "");
void
add_options
(
const std::string& group,
std::initializer_list<Option> options
);
void
add_option
(
const std::string& group,
const Option& option
);
void
add_option
(
@ -1492,6 +1577,21 @@ ParseResult::ParseResult
parse(argc, argv);
}
inline
void
Options::add_options
(
const std::string &group,
std::initializer_list<Option> options
)
{
OptionAdder option_adder(*this, group);
for (const auto &option: options)
{
option_adder(option.opts_, option.desc_, option.value_, option.arg_help_);
}
}
inline
OptionAdder
Options::add_options(std::string group)
@ -1514,7 +1614,7 @@ OptionAdder::operator()
if (result.empty())
{
throw invalid_option_format_error(opts);
throw_or_mimic<invalid_option_format_error>(opts);
}
const auto& short_match = result[2];
@ -1522,10 +1622,10 @@ OptionAdder::operator()
if (!short_match.length() && !long_match.length())
{
throw invalid_option_format_error(opts);
throw_or_mimic<invalid_option_format_error>(opts);
} else if (long_match.length() == 1 && short_match.length())
{
throw invalid_option_format_error(opts);
throw_or_mimic<invalid_option_format_error>(opts);
}
auto option_names = []
@ -1598,7 +1698,7 @@ ParseResult::checked_parse_arg
}
else
{
throw missing_argument_exception(name);
throw_or_mimic<missing_argument_exception>(name);
}
}
else
@ -1623,7 +1723,7 @@ ParseResult::add_to_option(const std::string& option, const std::string& arg)
if (iter == m_options->end())
{
throw option_not_exists_exception(option);
throw_or_mimic<option_not_exists_exception>(option);
}
parse_option(iter->second, option, arg);
@ -1659,7 +1759,10 @@ ParseResult::consume_positional(std::string a)
return true;
}
}
++m_next_positional;
else
{
throw_or_mimic<option_not_exists_exception>(*m_next_positional);
}
}
return false;
@ -1725,7 +1828,9 @@ ParseResult::parse(int& argc, char**& argv)
// but if it starts with a `-`, then it's an error
if (argv[current][0] == '-' && argv[current][1] != '\0') {
throw option_syntax_exception(argv[current]);
if (!m_allow_unrecognised) {
throw_or_mimic<option_syntax_exception>(argv[current]);
}
}
//if true is returned here then it was consumed, otherwise it is
@ -1761,7 +1866,7 @@ ParseResult::parse(int& argc, char**& argv)
else
{
//error
throw option_not_exists_exception(name);
throw_or_mimic<option_not_exists_exception>(name);
}
}
@ -1779,7 +1884,7 @@ ParseResult::parse(int& argc, char**& argv)
else
{
//error
throw option_requires_argument_exception(name);
throw_or_mimic<option_requires_argument_exception>(name);
}
}
}
@ -1802,7 +1907,7 @@ ParseResult::parse(int& argc, char**& argv)
else
{
//error
throw option_not_exists_exception(name);
throw_or_mimic<option_not_exists_exception>(name);
}
}
@ -1834,7 +1939,7 @@ ParseResult::parse(int& argc, char**& argv)
auto& store = m_results[detail];
if(!store.count() && value.has_default()){
if(value.has_default() && !store.count() && !store.has_default()){
parse_default(detail);
}
}
@ -1861,6 +1966,17 @@ ParseResult::parse(int& argc, char**& argv)
}
inline
void
Options::add_option
(
const std::string& group,
const Option& option
)
{
add_options(group, {option});
}
inline
void
Options::add_option
@ -1909,7 +2025,7 @@ Options::add_one_option
if (!in.second)
{
throw option_exists_error(option);
throw_or_mimic<option_exists_error>(option);
}
}
@ -1938,8 +2054,7 @@ Options::help_one_group(const std::string& g) const
for (const auto& o : group->second.options)
{
if (o.is_container &&
m_positional_set.find(o.l) != m_positional_set.end() &&
if (m_positional_set.find(o.l) != m_positional_set.end() &&
!m_show_positional)
{
continue;
@ -1958,8 +2073,7 @@ Options::help_one_group(const std::string& g) const
auto fiter = format.begin();
for (const auto& o : group->second.options)
{
if (o.is_container &&
m_positional_set.find(o.l) != m_positional_set.end() &&
if (m_positional_set.find(o.l) != m_positional_set.end() &&
!m_show_positional)
{
continue;

@ -56,6 +56,7 @@ parse(int argc, char* argv[])
("help", "Print help")
("int", "An integer", cxxopts::value<int>(), "N")
("float", "A floating point number", cxxopts::value<float>())
("vector", "A list of doubles", cxxopts::value<std::vector<double>>())
("option_that_is_too_long_for_the_help", "A very long option")
#ifdef CXXOPTS_USE_UNICODE
("unicode", u8"A help option with non-ascii: à. Here the size of the"
@ -130,6 +131,16 @@ parse(int argc, char* argv[])
std::cout << "float = " << result["float"].as<float>() << std::endl;
}
if (result.count("vector"))
{
std::cout << "vector = ";
const auto values = result["vector"].as<std::vector<double>>();
for (const auto& v : values) {
std::cout << v << ", ";
}
std::cout << std::endl;
}
std::cout << "Arguments remain = " << argc << std::endl;
return result;

@ -93,7 +93,7 @@ TEST_CASE("Basic options", "[options]")
CHECK(arguments[1].key() == "short");
CHECK(arguments[2].key() == "value");
CHECK(arguments[3].key() == "av");
CHECK_THROWS_AS(result["nothing"].as<std::string>(), std::domain_error&);
}
@ -216,6 +216,22 @@ TEST_CASE("No positional with extras", "[positional]")
CHECK(argv[1] == std::string("a"));
}
TEST_CASE("Positional not valid", "[positional]") {
cxxopts::Options options("positional_invalid", "invalid positional argument");
options.add_options()
("long", "a long option", cxxopts::value<std::string>())
;
options.parse_positional("something");
Argv av({"foobar", "bar", "baz"});
char** argv = av.argv();
auto argc = av.argc();
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::option_not_exists_exception&);
}
TEST_CASE("Empty with implicit value", "[implicit]")
{
cxxopts::Options options("empty_implicit", "doesn't handle empty");
@ -234,12 +250,75 @@ TEST_CASE("Empty with implicit value", "[implicit]")
REQUIRE(result["implicit"].as<std::string>() == "");
}
TEST_CASE("Boolean without implicit value", "[implicit]")
{
cxxopts::Options options("no_implicit", "bool without an implicit value");
options.add_options()
("bool", "Boolean without implicit", cxxopts::value<bool>()
->no_implicit_value());
SECTION("When no value provided") {
Argv av({"no_implicit", "--bool"});
char** argv = av.argv();
auto argc = av.argc();
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::missing_argument_exception&);
}
SECTION("With equal-separated true") {
Argv av({"no_implicit", "--bool=true"});
char** argv = av.argv();
auto argc = av.argc();
auto result = options.parse(argc, argv);
CHECK(result.count("bool") == 1);
CHECK(result["bool"].as<bool>() == true);
}
SECTION("With equal-separated false") {
Argv av({"no_implicit", "--bool=false"});
char** argv = av.argv();
auto argc = av.argc();
auto result = options.parse(argc, argv);
CHECK(result.count("bool") == 1);
CHECK(result["bool"].as<bool>() == false);
}
SECTION("With space-separated true") {
Argv av({"no_implicit", "--bool", "true"});
char** argv = av.argv();
auto argc = av.argc();
auto result = options.parse(argc, argv);
CHECK(result.count("bool") == 1);
CHECK(result["bool"].as<bool>() == true);
}
SECTION("With space-separated false") {
Argv av({"no_implicit", "--bool", "false"});
char** argv = av.argv();
auto argc = av.argc();
auto result = options.parse(argc, argv);
CHECK(result.count("bool") == 1);
CHECK(result["bool"].as<bool>() == false);
}
}
TEST_CASE("Default values", "[default]")
{
cxxopts::Options options("defaults", "has defaults");
options.add_options()
("default", "Has implicit", cxxopts::value<int>()
->default_value("42"));
("default", "Has implicit", cxxopts::value<int>()->default_value("42"))
("v,vector", "Default vector", cxxopts::value<std::vector<int>>()
->default_value("1,4"))
;
SECTION("Sets defaults") {
Argv av({"implicit"});
@ -250,6 +329,11 @@ TEST_CASE("Default values", "[default]")
auto result = options.parse(argc, argv);
CHECK(result.count("default") == 0);
CHECK(result["default"].as<int>() == 42);
auto& v = result["vector"].as<std::vector<int>>();
REQUIRE(v.size() == 2);
CHECK(v[0] == 1);
CHECK(v[1] == 4);
}
SECTION("When values provided") {
@ -392,6 +476,8 @@ TEST_CASE("Overflow on boundary", "[integer]")
TEST_CASE("Integer overflow", "[options]")
{
using namespace cxxopts::values;
cxxopts::Options options("reject_overflow", "rejects overflowing integers");
options.add_options()
("positional", "Integers", cxxopts::value<std::vector<int8_t>>());
@ -403,6 +489,10 @@ TEST_CASE("Integer overflow", "[options]")
options.parse_positional("positional");
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::argument_incorrect_type&);
int integer = 0;
CHECK_THROWS_AS((integer_parser("23423423423", integer)), cxxopts::argument_incorrect_type&);
CHECK_THROWS_AS((integer_parser("234234234234", integer)), cxxopts::argument_incorrect_type&);
}
TEST_CASE("Floats", "[options]")
@ -452,6 +542,8 @@ TEST_CASE("Booleans", "[boolean]") {
("bool", "A Boolean", cxxopts::value<bool>())
("debug", "Debugging", cxxopts::value<bool>())
("timing", "Timing", cxxopts::value<bool>())
("verbose", "Verbose", cxxopts::value<bool>())
("dry-run", "Dry Run", cxxopts::value<bool>())
("noExplicitDefault", "No Explicit Default", cxxopts::value<bool>())
("defaultTrue", "Timing", cxxopts::value<bool>()->default_value("true"))
("defaultFalse", "Timing", cxxopts::value<bool>()->default_value("false"))
@ -460,7 +552,7 @@ TEST_CASE("Booleans", "[boolean]") {
options.parse_positional("others");
Argv av({"booleans", "--bool=false", "--debug=true", "--timing", "extra"});
Argv av({"booleans", "--bool=false", "--debug=true", "--timing", "--verbose=1", "--dry-run=0", "extra"});
char** argv = av.argv();
auto argc = av.argc();
@ -470,6 +562,8 @@ TEST_CASE("Booleans", "[boolean]") {
REQUIRE(result.count("bool") == 1);
REQUIRE(result.count("debug") == 1);
REQUIRE(result.count("timing") == 1);
REQUIRE(result.count("verbose") == 1);
REQUIRE(result.count("dry-run") == 1);
REQUIRE(result.count("noExplicitDefault") == 0);
REQUIRE(result.count("defaultTrue") == 0);
REQUIRE(result.count("defaultFalse") == 0);
@ -477,6 +571,8 @@ TEST_CASE("Booleans", "[boolean]") {
CHECK(result["bool"].as<bool>() == false);
CHECK(result["debug"].as<bool>() == true);
CHECK(result["timing"].as<bool>() == true);
CHECK(result["verbose"].as<bool>() == true);
CHECK(result["dry-run"].as<bool>() == false);
CHECK(result["noExplicitDefault"].as<bool>() == false);
CHECK(result["defaultTrue"].as<bool>() == true);
CHECK(result["defaultFalse"].as<bool>() == false);
@ -484,6 +580,26 @@ TEST_CASE("Booleans", "[boolean]") {
REQUIRE(result.count("others") == 1);
}
TEST_CASE("std::vector", "[vector]") {
std::vector<double> vector;
cxxopts::Options options("vector", " - tests vector");
options.add_options()
("vector", "an vector option", cxxopts::value<std::vector<double>>(vector));
Argv av({"vector", "--vector", "1,-2.1,3,4.5"});
char** argv = av.argv();
auto argc = av.argc();
options.parse(argc, argv);
REQUIRE(vector.size() == 4);
CHECK(vector[0] == 1);
CHECK(vector[1] == -2.1);
CHECK(vector[2] == 3);
CHECK(vector[3] == 4.5);
}
#ifdef CXXOPTS_HAS_OPTIONAL
TEST_CASE("std::optional", "[optional]") {
std::optional<std::string> optional;
@ -533,6 +649,33 @@ TEST_CASE("Unrecognised options", "[options]") {
}
}
TEST_CASE("Allow bad short syntax", "[options]") {
cxxopts::Options options("unknown_options", " - test unknown options");
options.add_options()
("long", "a long option")
("s,short", "a short option");
Argv av({
"unknown_options",
"-some_bad_short",
});
char** argv = av.argv();
auto argc = av.argc();
SECTION("Default behaviour") {
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::option_syntax_exception&);
}
SECTION("After allowing unrecognised options") {
options.allow_unrecognised_options();
CHECK_NOTHROW(options.parse(argc, argv));
REQUIRE(argc == 2);
CHECK_THAT(argv[1], Catch::Equals("-some_bad_short"));
}
}
TEST_CASE("Invalid option syntax", "[options]") {
cxxopts::Options options("invalid_syntax", " - test invalid syntax");
@ -548,3 +691,86 @@ TEST_CASE("Invalid option syntax", "[options]") {
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::option_syntax_exception&);
}
}
TEST_CASE("Options empty", "[options]") {
cxxopts::Options options("Options list empty", " - test empty option list");
options.add_options();
options.add_options("");
options.add_options("", {});
options.add_options("test");
Argv argv_({
"test",
"--unknown"
});
auto argc = argv_.argc();
char** argv = argv_.argv();
CHECK(options.groups().empty());
CHECK_THROWS_AS(options.parse(argc, argv), cxxopts::option_not_exists_exception&);
}
TEST_CASE("Initializer list with group", "[options]") {
cxxopts::Options options("Initializer list group", " - test initializer list with group");
options.add_options("", {
{"a, address", "server address", cxxopts::value<std::string>()->default_value("127.0.0.1")},
{"p, port", "server port", cxxopts::value<std::string>()->default_value("7110"), "PORT"},
});
cxxopts::Option help{"h,help", "Help"};
options.add_options("TEST_GROUP", {
{"t, test", "test option"},
help
});
Argv argv({
"test",
"--address",
"10.0.0.1",
"-p",
"8000",
"-t",
});
char** actual_argv = argv.argv();
auto argc = argv.argc();
auto result = options.parse(argc, actual_argv);
CHECK(options.groups().size() == 2);
CHECK(result.count("address") == 1);
CHECK(result.count("port") == 1);
CHECK(result.count("test") == 1);
CHECK(result.count("help") == 0);
CHECK(result["address"].as<std::string>() == "10.0.0.1");
CHECK(result["port"].as<std::string>() == "8000");
CHECK(result["test"].as<bool>() == true);
}
TEST_CASE("Option add with add_option(string, Option)", "[options]") {
cxxopts::Options options("Option add with add_option", " - test Option add with add_option(string, Option)");
cxxopts::Option option_1("t,test", "test option", cxxopts::value<int>()->default_value("7"), "TEST");
options.add_option("", option_1);
options.add_option("TEST", {"a,aggregate", "test option 2", cxxopts::value<int>(), "AGGREGATE"});
Argv argv_({
"test",
"--test",
"5",
"-a",
"4"
});
auto argc = argv_.argc();
char** argv = argv_.argv();
auto result = options.parse(argc, argv);
CHECK(result.arguments().size()==2);
CHECK(options.groups().size() == 2);
CHECK(result.count("address") == 0);
CHECK(result.count("aggregate") == 1);
CHECK(result.count("test") == 1);
CHECK(result["aggregate"].as<int>() == 4);
CHECK(result["test"].as<int>() == 5);
}

Loading…
Cancel
Save