mirror of
https://github.com/CLIUtils/CLI11.git
synced 2025-04-30 20:53:52 +00:00
Adding bool flag
This commit is contained in:
parent
c10bece495
commit
2fae7e2cdf
@ -104,6 +104,16 @@ struct is_vector<std::vector<T, A> > {
|
|||||||
static bool const value = true;
|
static bool const value = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct is_bool {
|
||||||
|
static const bool value = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct is_bool<bool> {
|
||||||
|
static bool const value = true;
|
||||||
|
};
|
||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
// Based generally on https://rmf.io/cxx11/almost-static-if
|
// Based generally on https://rmf.io/cxx11/almost-static-if
|
||||||
/// Simple empty scoped class
|
/// Simple empty scoped class
|
||||||
@ -748,10 +758,11 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Add option for flag
|
/// Add option for flag
|
||||||
template<typename T, enable_if_t<std::is_integral<T>::value, detail::enabler> = detail::dummy>
|
template<typename T,
|
||||||
|
enable_if_t<std::is_integral<T>::value && !is_bool<T>::value, detail::enabler> = detail::dummy>
|
||||||
Option* add_flag(
|
Option* add_flag(
|
||||||
std::string name, ///< The name, short,long
|
std::string name, ///< The name, short,long
|
||||||
T &count, ///< A varaible holding the count
|
T &count, ///< A varaible holding the count
|
||||||
std::string discription="" ///< Discription string
|
std::string discription="" ///< Discription string
|
||||||
) {
|
) {
|
||||||
|
|
||||||
@ -764,6 +775,25 @@ public:
|
|||||||
return add_option(name, fun, discription, NOTHING);
|
return add_option(name, fun, discription, NOTHING);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Bool version only allows the flag once
|
||||||
|
template<typename T,
|
||||||
|
enable_if_t<is_bool<T>::value, detail::enabler> = detail::dummy>
|
||||||
|
Option* add_flag(
|
||||||
|
std::string name, ///< The name, short,long
|
||||||
|
T &count, ///< A varaible holding true if passed
|
||||||
|
std::string discription="" ///< Discription string
|
||||||
|
) {
|
||||||
|
|
||||||
|
count = false;
|
||||||
|
CLI::callback_t fun = [&count](CLI::results_t res){
|
||||||
|
count = true;
|
||||||
|
return res.size() == 1;
|
||||||
|
};
|
||||||
|
|
||||||
|
return add_option(name, fun, discription, NOTHING);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Add set of options
|
/// Add set of options
|
||||||
template<typename T>
|
template<typename T>
|
||||||
Option* add_set(
|
Option* add_set(
|
||||||
|
@ -141,6 +141,38 @@ TEST_F(TApp, LotsOfFlags) {
|
|||||||
EXPECT_EQ(1, app.count("A"));
|
EXPECT_EQ(1, app.count("A"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
TEST_F(TApp, BoolAndIntFlags) {
|
||||||
|
|
||||||
|
bool bflag;
|
||||||
|
int iflag;
|
||||||
|
unsigned int uflag;
|
||||||
|
|
||||||
|
app.add_flag("b", bflag);
|
||||||
|
app.add_flag("i", iflag);
|
||||||
|
app.add_flag("u", uflag);
|
||||||
|
|
||||||
|
args = {"-b", "-i", "-u"};
|
||||||
|
EXPECT_NO_THROW(run());
|
||||||
|
EXPECT_TRUE(bflag);
|
||||||
|
EXPECT_EQ(1, iflag);
|
||||||
|
EXPECT_EQ((unsigned int) 1, uflag);
|
||||||
|
|
||||||
|
app.reset();
|
||||||
|
|
||||||
|
args = {"-b", "-b"};
|
||||||
|
EXPECT_THROW(run(), CLI::ParseError);
|
||||||
|
|
||||||
|
app.reset();
|
||||||
|
bflag = false;
|
||||||
|
|
||||||
|
args = {"-iiiuu"};
|
||||||
|
EXPECT_NO_THROW(run());
|
||||||
|
EXPECT_FALSE(bflag);
|
||||||
|
EXPECT_EQ(3, iflag);
|
||||||
|
EXPECT_EQ((unsigned int) 2, uflag);
|
||||||
|
}
|
||||||
|
|
||||||
TEST_F(TApp, ShortOpts) {
|
TEST_F(TApp, ShortOpts) {
|
||||||
|
|
||||||
unsigned long long funnyint;
|
unsigned long long funnyint;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user