99#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
1010#include < catch2/catch.hpp>
1111
12+ #include < charconv> // std::from_chars()
1213#include < filesystem> // std::filesystem::*
1314#include < ranges> // std::ranges::views::empty<>
15+
1416namespace {
1517
1618//
@@ -19,6 +21,7 @@ namespace {
1921constexpr const char *const MYARGS[] = {" FOO" , " X" , " Y" , " Z" , " AAA" };
2022
2123} // namespace
24+
2225TEST_CASE (" Null parse" , " [Z]" )
2326{
2427 static constinit const char *const ARGV[]{" foo" };
@@ -30,8 +33,8 @@ TEST_CASE("Null help", "[Z]")
3033 static constinit const char *const ARGV[]{" foo" , " -h" };
3134 auto ret = bux::C_EZArgs{}.parse (std::size (ARGV), ARGV);
3235 REQUIRE (!ret);
33- REQUIRE (ret.message () ==
34- " USAGE: ./ foo [-h]\n "
36+ CHECK (ret.message () ==
37+ " USAGE: foo [-h]\n "
3538 " \n "
3639 " VALID FLAGS:\n "
3740 " -h, --help\n "
@@ -83,16 +86,37 @@ TEST_CASE("Scenario: argv[0] with -E -h", "[S]")
8386 const char *const argv[]{arg0.c_str (), " -h" };
8487 auto ret = ezargs.parse (2 , argv);
8588 REQUIRE (!ret);
86- const auto help = fmt::format (
87- " USAGE: .{} test1.exe (bar| foo) ... [-E] [-h]\n "
89+ CHECK (ret. message () ==
90+ " USAGE: test1.exe (foo|bar ) ... [-E] [-h]\n "
8891 " VALID ACTIONS:\n "
89- " bar\n "
90- " foo\n " ,
91- #ifdef _WIN32
92- ' \\ '
93- #else
94- ' /'
95- #endif
96- );
97- REQUIRE (ret.message () == help);
92+ " foo\n "
93+ " bar\n " );
94+ }
95+
96+ TEST_CASE (" Scenario: Parse negative number as flag value" , " [S]" )
97+ {
98+ double x{};
99+ bux::C_EZArgs ezargs;
100+ ezargs.add_flag (' x' , " foobar" , [&](auto v){ // parse
101+ std::from_chars (v.data (), v.data ()+v.size (), x);
102+ });
103+ const std::string arg0 = std::filesystem::current_path () / " test1.exe" ;
104+ const char *argv[]{arg0.c_str (), " -x" , " -.5" };
105+ REQUIRE (ezargs.parse (3 , argv));
106+ CHECK (x == -.5 );
107+ // -------------------------------------------------------------------------
108+ ezargs.add_flag (' .' , " contrived" , []{}); // trigger
109+ x = 0 ;
110+ REQUIRE (ezargs.parse (3 , argv));
111+ CHECK (x == -.5 );
112+ // -------------------------------------------------------------------------
113+ ezargs.add_flag (' 5' , " contrived" , []{}); // trigger
114+ CHECK (!ezargs.parse (3 , argv));
115+ // -------------------------------------------------------------------------
116+ std::string s6;
117+ ezargs.add_flag (' 6' , " contrived" , [&](auto v){ s6 = v; }); // parse
118+ argv[2 ] = " -.6" ;
119+ x = 0 ;
120+ REQUIRE (ezargs.parse (3 , argv));
121+ CHECK (x == -.6 );
98122}
0 commit comments