99#include < eggs/test/cli.hpp>
1010#include < eggs/test/detail/print.hpp>
1111
12+ #include < charconv>
1213#include < cstddef>
1314#include < cstdio>
1415#include < cstdlib>
1516#include < filesystem>
17+ #include < optional>
1618#include < string_view>
1719
1820namespace eggs ::test {
@@ -32,12 +34,22 @@ void print_help(std::FILE* out, std::string_view const usage)
3234 test::print_options (out, /* ns:*/ {}, k_desc_col);
3335}
3436
37+ std::optional<int > parse_int (std::string_view sv) noexcept
38+ {
39+ int value = 0 ;
40+ auto [end, ec] = std::from_chars (sv.data (), sv.data () + sv.size (), value);
41+ if (ec != std::errc{} || end != sv.data () + sv.size ()) return std::nullopt ;
42+ return value;
43+ }
44+
3545} // namespace
3646
3747int main (int argc, char const * argv[])
3848{
3949 run_options const opts = parse_cli (argc, argv);
4050
51+ std::optional<int > exit_code;
52+
4153 for (int i = 1 ; i < argc; ++i) {
4254 std::string_view const arg = argv[i];
4355
@@ -49,11 +61,22 @@ int main(int argc, char const* argv[])
4961 return EXIT_SUCCESS ;
5062 }
5163
64+ if (arg.starts_with (" --exit-code=" )) {
65+ std::string_view const val = arg.substr (12 );
66+
67+ if (exit_code = test::parse_int (val); !exit_code) {
68+ detail::println (stderr, " error: invalid exit code '{}'" , val);
69+ return EXIT_FAILURE ;
70+ }
71+ continue ;
72+ }
73+
5274 detail::println (stderr, " error: unknown argument '{}'" , arg);
5375 return EXIT_FAILURE ;
5476 }
5577
56- return test::run (opts);
78+ int const result = test::run (opts);
79+ return exit_code.value_or (result);
5780}
5881
5982} // namespace eggs::test
0 commit comments