vmtester: Replace CLI11 with own command line args parsing

This commit is contained in:
Paweł Bylica 2019-03-16 23:43:19 +01:00
parent e3dab00a3d
commit 0328d36288
No known key found for this signature in database
GPG Key ID: 7A0C037434FE77EF
2 changed files with 72 additions and 13 deletions

View File

@ -19,7 +19,7 @@ add_test(NAME vmtester-help
COMMAND evmc-vmtester --help
)
set_tests_properties(vmtester-help PROPERTIES
PASS_REGULAR_EXPRESSION "Options:"
PASS_REGULAR_EXPRESSION "Usage:"
)
add_test(NAME vmtester-nonexistingvm

View File

@ -6,8 +6,6 @@
#include <evmc/loader.h>
#include <CLI/CLI.hpp>
#include <iostream>
#include <memory>
@ -32,28 +30,89 @@ int main(int argc, char* argv[])
{
try
{
std::string vm_path;
const auto app_name = "EVMC VM Tester " PROJECT_VERSION;
CLI::App app{"EVMC VM Tester"};
app.add_option("vm", vm_path, "Path to the VM shared library to be tested");
testing::InitGoogleTest(&argc, argv);
CLI11_PARSE(app, argc, argv);
std::cout << "Testing " << vm_path << "\n";
auto help = false;
auto version = false;
std::string evmc_module;
int num_args = 0;
for (int i = 1; i < argc; ++i)
{
auto arg = std::string{argv[i]};
auto x = arg.find_first_not_of('-');
if (x == 0) // Argument.
{
++num_args;
if (num_args > 1)
{
std::cerr << "Unexpected argument \"" << arg << "\"\n";
return -1;
}
evmc_module = std::move(arg);
continue;
}
else if (x <= 2)
{
arg.erase(0, x);
if (arg == "version")
{
version = true;
continue;
}
if (arg == "help")
{
help = true;
continue;
}
}
std::cerr << "Unknown option \"" << argv[i] << "\"\n";
return -1;
}
if (help)
{
std::cout << "\n"
<< app_name << "\n"
<< "Usage: " << argv[0] << " MODULE\n";
return 0;
}
if (version)
{
std::cout << app_name << "\n";
return 0;
}
if (num_args < 1)
{
std::cerr << "The MODULE argument is required.\n"
<< "Run with --help for more information.\n";
return -1;
}
std::cout << app_name << "\n";
std::cout << "Testing " << evmc_module << "\n";
evmc_loader_error_code ec;
create_fn = evmc_load(vm_path.c_str(), &ec);
create_fn = evmc_load(evmc_module.c_str(), &ec);
switch (ec)
{
case EVMC_LOADER_SUCCESS:
break;
case EVMC_LOADER_CANNOT_OPEN:
std::cerr << "Cannot open " << vm_path << "\n";
std::cerr << "Cannot open " << evmc_module << "\n";
return static_cast<int>(ec);
case EVMC_LOADER_SYMBOL_NOT_FOUND:
std::cerr << "EVMC create function not found in " << vm_path << "\n";
std::cerr << "EVMC create function not found in " << evmc_module << "\n";
return static_cast<int>(ec);
case EVMC_LOADER_INVALID_ARGUMENT:
std::cerr << "Invalid argument: \"" << vm_path << "\"\n";
std::cerr << "Invalid argument: \"" << evmc_module << "\"\n";
return static_cast<int>(ec);
default:
std::cerr << "Unexpected error in evmc_load(): " << ec << "\n";
@ -66,6 +125,6 @@ int main(int argc, char* argv[])
catch (const std::exception& ex)
{
std::cerr << ex.what() << "\n";
return 1;
return -2;
}
}