Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Improve command line argument handling #56

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 51 additions & 42 deletions src/spider/scheduler/scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
#include <chrono>
#include <cstddef>
#include <functional>
#include <iostream>
#include <memory>
#include <string>
#include <system_error>
#include <thread>

#include <boost/any/bad_any_cast.hpp>
#include <boost/program_options/errors.hpp>
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/parsers.hpp>
Expand Down Expand Up @@ -38,33 +38,62 @@ constexpr int cCleanupInterval = 5;
constexpr int cRetryCount = 5;

namespace {
auto parse_args(int const argc, char** argv) -> boost::program_options::variables_map {

char const* const cUsage
= "Usage: spider_scheduler --host <host> --port <port> --storage_url <url>";

auto parse_args(
int const argc,
char** argv,
std::string& host,
unsigned short& port,
std::string& storage_url
) -> bool {
boost::program_options::options_description desc;
desc.add_options()("help", "spider scheduler");
desc.add_options()(
// clang-format off
desc.add_options()
("help", "spider scheduler")
(
"host",
boost::program_options::value<std::string>(),
boost::program_options::value<std::string>(&host)->required(),
"scheduler host address"
);
desc.add_options()(
)
(
"port",
boost::program_options::value<unsigned short>(),
boost::program_options::value<unsigned short>(&port)->required(),
"port to listen on"
);
desc.add_options()(
)
(
"storage_url",
boost::program_options::value<std::string>(),
boost::program_options::value<std::string>(&storage_url)->required(),
"storage server url"
);

boost::program_options::variables_map variables;
boost::program_options::store(
// NOLINTNEXTLINE(misc-include-cleaner)
boost::program_options::parse_command_line(argc, argv, desc),
variables
);
boost::program_options::notify(variables);
return variables;
);
// clang-format on

try {
boost::program_options::variables_map variables;
boost::program_options::store(
// NOLINTNEXTLINE(misc-include-cleaner)
boost::program_options::parse_command_line(argc, argv, desc),
variables
);

if (!variables.contains("host") && !variables.contains("port")
&& !variables.contains("storage_url"))
{
std::cout << cUsage << "\n";
std::cout << desc << "\n";
return false;
}

boost::program_options::notify(variables);
return true;
} catch (boost::program_options::error& e) {
std::cerr << "spider_scheduler: " << e.what() << "\n";
std::cerr << cUsage << "\n";
std::cerr << "Try 'spider_scheduler --help' for more information.\n";
return false;
}
}

auto heartbeat_loop(
Expand Down Expand Up @@ -137,30 +166,10 @@ auto main(int argc, char** argv) -> int {
spdlog::set_level(spdlog::level::trace);
#endif

boost::program_options::variables_map const args = parse_args(argc, argv);

unsigned short port = 0;
std::string scheduler_addr;
std::string storage_url;
try {
if (!args.contains("port")) {
spdlog::error("port is required");
return cCmdArgParseErr;
}
port = args["port"].as<unsigned short>();
if (!args.contains("host")) {
spdlog::error("host is required");
return cCmdArgParseErr;
}
scheduler_addr = args["host"].as<std::string>();
if (!args.contains("storage_url")) {
spdlog::error("storage_url is required");
return cCmdArgParseErr;
}
storage_url = args["storage_url"].as<std::string>();
} catch (boost::bad_any_cast& e) {
return cCmdArgParseErr;
} catch (boost::program_options::error& e) {
if (!parse_args(argc, argv, scheduler_addr, port, storage_url)) {
return cCmdArgParseErr;
}

Expand Down
89 changes: 46 additions & 43 deletions src/spider/worker/worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
#include <functional>
#include <memory>
#include <optional>
#include <iostream>
#include <stdexcept>
#include <string>
#include <thread>
#include <vector>

#include <absl/container/flat_hash_map.h>
#include <boost/any/bad_any_cast.hpp>
#include <boost/dll/runtime_symbol_info.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/process/v2/environment.hpp>
Expand Down Expand Up @@ -50,29 +50,54 @@ constexpr int cTaskErr = 5;
constexpr int cRetryCount = 5;

namespace {
auto parse_args(int const argc, char** argv) -> boost::program_options::variables_map {

char const* const cUsage = "Usage: spider_worker --host <host> --storage_url <storage_url> --libs <libs>";

auto parse_args(int const argc, char** argv, std::string& host, std::string& storage_url, std::vector<std::string>& libs) -> bool {
boost::program_options::options_description desc;
desc.add_options()("help", "spider scheduler");
desc.add_options()(
"storage_url",
boost::program_options::value<std::string>(),
// clang-format off
desc.add_options()
("help", "spider scheduler")
(
"host",
boost::program_options::value<std::string>(&host)->required(),
"worker host address"
)
(
"storage_url",
boost::program_options::value<std::string>(&storage_url)->required(),
"storage server url"
);
desc.add_options()(
)
(
"libs",
boost::program_options::value<std::vector<std::string>>(),
boost::program_options::value<std::vector<std::string>>(&libs),
"dynamic libraries that include the spider tasks"
);
desc.add_options()("host", boost::program_options::value<std::string>(), "worker host address");

boost::program_options::variables_map variables;
boost::program_options::store(
// NOLINTNEXTLINE(misc-include-cleaner)
boost::program_options::parse_command_line(argc, argv, desc),
variables
);
boost::program_options::notify(variables);
return variables;
);
// clang-format on

try {
boost::program_options::variables_map variables;
boost::program_options::store(
// NOLINTNEXTLINE(misc-include-cleaner)
boost::program_options::parse_command_line(argc, argv, desc),
variables
);

if (!variables.contains("host") && !variables.contains("storage_url") && !variables.contains("libs"))
{
std::cout << cUsage << "\n";
std::cout << desc << "\n";
return false;
}

boost::program_options::notify(variables);
return true;
} catch (boost::program_options::error& e) {
std::cerr << "spider_worker: " << e.what() << "\n";
std::cerr << cUsage << "\n";
std::cerr << "Try 'spider_worker --help' for more information.\n";
return false;
}
}

auto get_environment_variable() -> absl::flat_hash_map<
Expand Down Expand Up @@ -329,32 +354,10 @@ auto main(int argc, char** argv) -> int {
spdlog::set_level(spdlog::level::trace);
#endif

boost::program_options::variables_map const args = parse_args(argc, argv);

std::string storage_url;
std::vector<std::string> libs;
std::string worker_addr;
try {
if (!args.contains("storage_url")) {
spdlog::error("Missing storage_url");
return cCmdArgParseErr;
}
storage_url = args["storage_url"].as<std::string>();
if (!args.contains("host")) {
spdlog::error("Missing host");
return cCmdArgParseErr;
}
worker_addr = args["host"].as<std::string>();
if (!args.contains("libs") || args["libs"].empty()) {
spdlog::error("Missing libs");
return cCmdArgParseErr;
}
libs = args["libs"].as<std::vector<std::string>>();
} catch (boost::bad_any_cast const& e) {
spdlog::error("Error: {}", e.what());
return cCmdArgParseErr;
} catch (boost::program_options::error const& e) {
spdlog::error("Error: {}", e.what());
if (!parse_args(argc, argv, worker_addr, storage_url, libs)) {
return cCmdArgParseErr;
}

Expand Down
Loading