Refactor parsing of shard index/count in cmdline handling

This worsens the message for negative numbers a bit, but
simplifies the code enough that this is still a win.
This commit is contained in:
Martin Hořeňovský 2022-10-23 00:09:17 +02:00
parent f1361ef624
commit a43f67962e
No known key found for this signature in database
GPG key ID: DE48307B8B0D381A
10 changed files with 65 additions and 76 deletions

View file

@ -7,7 +7,6 @@
// SPDX-License-Identifier: BSL-1.0
#include <catch2/internal/catch_commandline.hpp>
#include <catch2/internal/catch_compiler_capabilities.hpp>
#include <catch2/catch_config.hpp>
#include <catch2/internal/catch_string_manip.hpp>
#include <catch2/interfaces/catch_interfaces_registry_hub.hpp>
@ -177,42 +176,29 @@ namespace Catch {
return ParserResult::ok( ParseResultType::Matched );
};
auto const setShardCount = [&]( std::string const& shardCount ) {
CATCH_TRY{
std::size_t parsedTo = 0;
int64_t parsedCount = std::stoll(shardCount, &parsedTo, 0);
if (parsedTo != shardCount.size()) {
return ParserResult::runtimeError("Could not parse '" + shardCount + "' as shard count");
}
if (parsedCount <= 0) {
return ParserResult::runtimeError("Shard count must be a positive number");
}
config.shardCount = static_cast<unsigned int>(parsedCount);
return ParserResult::ok(ParseResultType::Matched);
} CATCH_CATCH_ANON(std::exception const&) {
return ParserResult::runtimeError("Could not parse '" + shardCount + "' as shard count");
auto parsedCount = parseUInt( shardCount );
if ( !parsedCount ) {
return ParserResult::runtimeError(
"Could not parse '" + shardCount + "' as shard count" );
}
if ( *parsedCount == 0 ) {
return ParserResult::runtimeError(
"Shard count must be positive" );
}
config.shardCount = *parsedCount;
return ParserResult::ok( ParseResultType::Matched );
};
auto const setShardIndex = [&](std::string const& shardIndex) {
CATCH_TRY{
std::size_t parsedTo = 0;
int64_t parsedIndex = std::stoll(shardIndex, &parsedTo, 0);
if (parsedTo != shardIndex.size()) {
return ParserResult::runtimeError("Could not parse '" + shardIndex + "' as shard index");
}
if (parsedIndex < 0) {
return ParserResult::runtimeError("Shard index must be a non-negative number");
}
config.shardIndex = static_cast<unsigned int>(parsedIndex);
return ParserResult::ok(ParseResultType::Matched);
} CATCH_CATCH_ANON(std::exception const&) {
return ParserResult::runtimeError("Could not parse '" + shardIndex + "' as shard index");
auto parsedIndex = parseUInt( shardIndex );
if ( !parsedIndex ) {
return ParserResult::runtimeError(
"Could not parse '" + shardIndex + "' as shard index" );
}
config.shardIndex = *parsedIndex;
return ParserResult::ok( ParseResultType::Matched );
};
auto cli
= ExeName( config.processName )
| Help( config.showHelp )