Replace cxxopts with CLI11

- Simiplifies CLI code for future modification
- filesystem library linked in cmake check_for_std_filesystem file
This commit is contained in:
dan 2023-01-05 13:05:18 -08:00
parent bf0dc52df7
commit dc7f3cee22
10 changed files with 687 additions and 670 deletions

6
.gitmodules vendored
View File

@ -1,9 +1,6 @@
[submodule "external/nlohmann"] [submodule "external/nlohmann"]
path = external/nlohmann path = external/nlohmann
url = https://github.com/nlohmann/json.git url = https://github.com/nlohmann/json.git
[submodule "external/cxxopts"]
path = external/cxxopts
url = https://github.com/jarro2783/cxxopts.git
[submodule "external/ghc-filesystem"] [submodule "external/ghc-filesystem"]
path = external/ghc-filesystem path = external/ghc-filesystem
url = https://github.com/gulrak/filesystem.git url = https://github.com/gulrak/filesystem.git
@ -39,3 +36,6 @@
[submodule "gui"] [submodule "gui"]
path = gui path = gui
url = https://github.com/oxen-io/lokinet-gui.git url = https://github.com/oxen-io/lokinet-gui.git
[submodule "external/CLI11"]
path = external/CLI11
url = https://github.com/CLIUtils/CLI11.git

View File

@ -47,5 +47,5 @@ else()
set(GHC_FILESYSTEM_WITH_INSTALL OFF CACHE INTERNAL "") set(GHC_FILESYSTEM_WITH_INSTALL OFF CACHE INTERNAL "")
add_subdirectory(external/ghc-filesystem) add_subdirectory(external/ghc-filesystem)
target_link_libraries(filesystem INTERFACE ghc_filesystem) target_link_libraries(filesystem INTERFACE ghc_filesystem)
target_compile_definitions(filesystem INTERFACE USE_GHC_FILESYSTEM) target_compile_definitions(filesystem INTERFACE USE_GHC_FILESYSTEM CLI11_HAS_FILESYSTEM=0)
endif() endif()

View File

@ -1,12 +1,15 @@
#include <oxenmq/oxenmq.h> #include <oxenmq/oxenmq.h>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <fmt/core.h> #include <fmt/core.h>
#include <cxxopts.hpp>
#include <future> #include <future>
#include <vector> #include <vector>
#include <array> #include <array>
#include <llarp/net/net.hpp> #include <llarp/net/net.hpp>
#include <CLI/App.hpp>
#include <CLI/Formatter.hpp>
#include <CLI/Config.hpp>
#ifdef _WIN32 #ifdef _WIN32
// add the unholy windows headers for iphlpapi // add the unholy windows headers for iphlpapi
#include <winsock2.h> #include <winsock2.h>
@ -53,25 +56,28 @@ OMQ_Request(
namespace namespace
{ {
template <typename T>
constexpr bool is_optional = false;
template <typename T>
constexpr bool is_optional<std::optional<T>> = true;
// Extracts a value from a cxxopts result and assigns it into `value` if present. The value can struct command_line_options
// either be a plain value or a std::optional. If not present, `value` is not touched.
template <typename T>
void
extract_option(const cxxopts::ParseResult& r, const std::string& name, T& value)
{ {
if (r.count(name)) // bool options
{ bool verbose = false;
if constexpr (is_optional<T>) bool help = false;
value = r[name].as<typename T::value_type>(); bool vpnUp = false;
else bool vpnDown = false;
value = r[name].as<T>(); bool printStatus = false;
} bool killDaemon = false;
}
// string options
std::string exitAddress;
std::string rpc;
std::string endpoint = "default";
std::string token;
std::optional<std::string> range;
// oxenmq
oxenmq::address rpcURL{"tcp://127.0.0.1:1190"};
oxenmq::LogLevel logLevel = oxenmq::LogLevel::warn;
};
// Takes a code, prints a message, and returns the code. Intended use is: // Takes a code, prints a message, and returns the code. Intended use is:
// return exit_error(1, "blah: {}", 42); // return exit_error(1, "blah: {}", 42);
@ -98,119 +104,104 @@ namespace
int int
main(int argc, char* argv[]) main(int argc, char* argv[])
{ {
cxxopts::Options opts("lokinet-vpn", "LokiNET vpn control utility"); CLI::App cli{"lokiNET vpn control utility", "lokinet-vpn"};
command_line_options options{};
// flags: boolean values in command_line_options struct
cli.add_flag("v,--verbose", options.verbose, "Verbose");
cli.add_flag("--up", options.vpnUp, "Put VPN up");
cli.add_flag("--down", options.vpnDown, "Put VPN down");
cli.add_flag("--status", options.printStatus, "Print VPN status and exit");
cli.add_flag("k,--kill", options.killDaemon, "Kill lokinet daemon");
// options: string values in command_line_options struct
cli.add_option("--exit", options.exitAddress, "Specify exit node address")->capture_default_str();
cli.add_option("--endpoint", options.endpoint, "Endpoint to use")->capture_default_str();
cli.add_option("--token", options.token, "Exit auth token to use")->capture_default_str();
// options: oxenmq values in command_line_options struct
cli.add_option("--rpc", options.rpc, "Specify RPC URL for lokinet")->capture_default_str();
cli.add_option(
"--log-level", options.logLevel, "Log verbosity level, see log levels for accepted values")
->type_name("LEVEL")
->capture_default_str();
// clang-format off
opts.add_options()
("v,verbose", "Verbose", cxxopts::value<bool>())
("h,help", "help", cxxopts::value<bool>())
("kill", "kill the daemon", cxxopts::value<bool>())
("up", "put vpn up", cxxopts::value<bool>())
("down", "put vpn down", cxxopts::value<bool>())
("exit", "specify exit node address", cxxopts::value<std::string>())
("rpc", "rpc url for lokinet", cxxopts::value<std::string>())
("endpoint", "endpoint to use", cxxopts::value<std::string>())
("token", "exit auth token to use", cxxopts::value<std::string>())
("auth", "exit auth token to use", cxxopts::value<std::string>())
("status", "print status and exit", cxxopts::value<bool>())
("range", "ip range to map", cxxopts::value<std::string>())
;
// clang-format on
oxenmq::address rpcURL("tcp://127.0.0.1:1190");
std::string exitAddress;
std::string endpoint = "default";
std::string token;
std::optional<std::string> range;
oxenmq::LogLevel logLevel = oxenmq::LogLevel::warn;
bool goUp = false;
bool goDown = false;
bool printStatus = false;
bool killDaemon = false;
try try
{ {
const auto result = opts.parse(argc, argv); cli.parse(argc, argv);
if (result.count("help") > 0)
{
std::cout << opts.help() << std::endl;
return 0;
}
if (result.count("verbose") > 0)
{
logLevel = oxenmq::LogLevel::debug;
}
goUp = result.count("up") > 0;
goDown = result.count("down") > 0;
printStatus = result.count("status") > 0;
killDaemon = result.count("kill") > 0;
extract_option(result, "rpc", rpcURL);
extract_option(result, "exit", exitAddress);
extract_option(result, "endpoint", endpoint);
extract_option(result, "token", token);
extract_option(result, "auth", token);
extract_option(result, "range", range);
} }
catch (const cxxopts::option_not_exists_exception& ex) catch (const CLI::ParseError& e)
{ {
return exit_error(2, "{}\n{}", ex.what(), opts.help()); return cli.exit(e);
} }
catch (std::exception& ex)
try
{ {
return exit_error(2, "{}", ex.what()); if (options.verbose)
options.logLevel = oxenmq::LogLevel::debug;
}
catch (const CLI::OptionNotFound& e)
{
cli.exit(e);
}
catch (const CLI::Error& e)
{
cli.exit(e);
};
int numCommands = options.vpnUp + options.vpnDown + options.printStatus + options.killDaemon;
switch (numCommands)
{
case 0:
return exit_error(3, "One of --up/--down/--status/--kill must be specified");
case 1:
return exit_error(3, "Only one of --up/--down/--status/--kill may be specified");
default:
break;
} }
int num_commands = goUp + goDown + printStatus + killDaemon; if (options.vpnUp and options.exitAddress.empty())
return exit_error("No exit address provided, must specify --exit <address>");
if (num_commands == 0)
return exit_error(3, "One of --up/--down/--status/--kill must be specified");
if (num_commands != 1)
return exit_error(3, "Only one of --up/--down/--status/--kill may be specified");
if (goUp and exitAddress.empty())
return exit_error("no exit address provided");
oxenmq::OxenMQ omq{ oxenmq::OxenMQ omq{
[](oxenmq::LogLevel lvl, const char* file, int line, std::string msg) { [](oxenmq::LogLevel lvl, const char* file, int line, std::string msg) {
std::cout << lvl << " [" << file << ":" << line << "] " << msg << std::endl; std::cout << lvl << " [" << file << ":" << line << "] " << msg << std::endl;
}, },
logLevel}; options.logLevel};
omq.start(); omq.start();
std::promise<bool> connectPromise; std::promise<bool> connectPromise;
const auto connID = omq.connect_remote( const auto connectionID = omq.connect_remote(
rpcURL, options.rpc,
[&connectPromise](auto) { connectPromise.set_value(true); }, [&connectPromise](auto) { connectPromise.set_value(true); },
[&connectPromise](auto, std::string_view msg) { [&connectPromise](auto, std::string_view msg) {
std::cout << "failed to connect to lokinet RPC: " << msg << std::endl; std::cout << "Failed to connect to lokinet RPC: " << msg << std::endl;
connectPromise.set_value(false); connectPromise.set_value(false);
}); });
auto ftr = connectPromise.get_future(); auto ftr = connectPromise.get_future();
if (not ftr.get()) if (not ftr.get())
{
return 1; return 1;
}
if (killDaemon) if (options.killDaemon)
{ {
if (not OMQ_Request(omq, connID, "llarp.halt")) if (not OMQ_Request(omq, connectionID, "llarp.halt"))
return exit_error("call to llarp.halt failed"); return exit_error("Call to llarp.halt failed");
return 0; return 0;
} }
if (printStatus) if (options.printStatus)
{ {
const auto maybe_status = OMQ_Request(omq, connID, "llarp.status"); const auto maybe_status = OMQ_Request(omq, connectionID, "llarp.status");
if (not maybe_status) if (not maybe_status)
return exit_error("call to llarp.status failed"); return exit_error("call to llarp.status failed");
try try
{ {
const auto& ep = maybe_status->at("result").at("services").at(endpoint); const auto& ep = maybe_status->at("result").at("services").at(options.endpoint);
const auto exitMap = ep.at("exitMap"); const auto exitMap = ep.at("exitMap");
if (exitMap.empty()) if (exitMap.empty())
{ {
@ -230,13 +221,13 @@ main(int argc, char* argv[])
} }
return 0; return 0;
} }
if (goUp) if (options.vpnUp)
{ {
nlohmann::json opts{{"exit", exitAddress}, {"token", token}}; nlohmann::json opts{{"exit", options.exitAddress}, {"token", options.token}};
if (range) if (options.range)
opts["range"] = *range; opts["range"] = *options.range;
auto maybe_result = OMQ_Request(omq, connID, "llarp.exit", std::move(opts)); auto maybe_result = OMQ_Request(omq, connectionID, "llarp.exit", std::move(opts));
if (not maybe_result) if (not maybe_result)
return exit_error("could not add exit"); return exit_error("could not add exit");
@ -247,12 +238,12 @@ main(int argc, char* argv[])
return exit_error("{}", err_it.value()); return exit_error("{}", err_it.value());
} }
} }
if (goDown) if (options.vpnDown)
{ {
nlohmann::json opts{{"unmap", true}}; nlohmann::json opts{{"unmap", true}};
if (range) if (options.range)
opts["range"] = *range; opts["range"] = *options.range;
if (not OMQ_Request(omq, connID, "llarp.exit", std::move(opts))) if (not OMQ_Request(omq, connectionID, "llarp.exit", std::move(opts)))
return exit_error("failed to unmap exit"); return exit_error("failed to unmap exit");
} }

File diff suppressed because it is too large Load Diff

1
external/CLI11 vendored Submodule

@ -0,0 +1 @@
Subproject commit 4c7c8ddc45d2ef74584e5cd945f7a4d27c987748

View File

@ -26,7 +26,6 @@ if(SUBMODULE_CHECK)
message(STATUS "Checking submodules") message(STATUS "Checking submodules")
check_submodule(nlohmann) check_submodule(nlohmann)
check_submodule(cxxopts)
check_submodule(ghc-filesystem) check_submodule(ghc-filesystem)
check_submodule(oxen-logging fmt spdlog) check_submodule(oxen-logging fmt spdlog)
check_submodule(pybind11) check_submodule(pybind11)
@ -36,6 +35,7 @@ if(SUBMODULE_CHECK)
check_submodule(uvw) check_submodule(uvw)
check_submodule(cpr) check_submodule(cpr)
check_submodule(ngtcp2) check_submodule(ngtcp2)
check_submodule(CLI11)
endif() endif()
endif() endif()
@ -78,7 +78,7 @@ if(WITH_HIVE)
add_subdirectory(pybind11 EXCLUDE_FROM_ALL) add_subdirectory(pybind11 EXCLUDE_FROM_ALL)
endif() endif()
add_subdirectory(cxxopts EXCLUDE_FROM_ALL) system_or_submodule(CLI11 CLI11 CLI11>=2.2.0 CLI11)
if(WITH_PEERSTATS) if(WITH_PEERSTATS)
add_library(sqlite_orm INTERFACE) add_library(sqlite_orm INTERFACE)

1
external/cxxopts vendored

@ -1 +0,0 @@
Subproject commit c74846a891b3cc3bfa992d588b1295f528d43039

2
external/readme.md vendored
View File

@ -1,7 +1,7 @@
directory for git submodules directory for git submodules
* CLI11: cli argument parser
* cpr: curl for people, used by lokinet-bootstrap toolchain (to be removed) * cpr: curl for people, used by lokinet-bootstrap toolchain (to be removed)
* cxxopts: cli argument parser (to be removed)
* ghc-filesystem: `std::filesystem` shim lib for older platforms (like macos) * ghc-filesystem: `std::filesystem` shim lib for older platforms (like macos)
* ngtcp2: quic implementation * ngtcp2: quic implementation
* nlohmann: json parser * nlohmann: json parser

View File

@ -278,7 +278,7 @@ endif()
target_link_libraries(lokinet-amalgum PRIVATE libunbound) target_link_libraries(lokinet-amalgum PRIVATE libunbound)
target_link_libraries(lokinet-amalgum PUBLIC target_link_libraries(lokinet-amalgum PUBLIC
cxxopts CLI11
oxenc::oxenc oxenc::oxenc
lokinet-platform lokinet-platform
lokinet-config lokinet-config

View File

@ -14,7 +14,10 @@
#include <llarp/util/service_manager.hpp> #include <llarp/util/service_manager.hpp>
#include <cxxopts.hpp> #include <CLI/App.hpp>
#include <CLI/Formatter.hpp>
#include <CLI/Config.hpp>
#include <csignal> #include <csignal>
#include <stdexcept> #include <stdexcept>