-
Notifications
You must be signed in to change notification settings - Fork 9
feat!: Improve command line argument handling #56
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
ba2977a
3c5e18b
ff98aaf
3611eaa
a686188
e80ffa8
8bddb5e
bc894a8
1d6ee5c
52d0503
6b75974
b2b315d
9d3ec38
a365d7b
df31fe2
f113c29
fd2e90d
08a3d5d
8f2d8ad
2788feb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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> | ||
|
|
@@ -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") | ||
sitaowang1998 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ( | ||
| "host", | ||
|
||
| boost::program_options::value<std::string>(), | ||
| boost::program_options::value<std::string>(&host)->required(), | ||
| "scheduler host address" | ||
sitaowang1998 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ); | ||
| desc.add_options()( | ||
| ) | ||
| ( | ||
| "port", | ||
| boost::program_options::value<unsigned short>(), | ||
| boost::program_options::value<unsigned short>(&port)->required(), | ||
| "port to listen on" | ||
sitaowang1998 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ); | ||
| desc.add_options()( | ||
| ) | ||
| ( | ||
| "storage_url", | ||
|
||
| boost::program_options::value<std::string>(), | ||
| boost::program_options::value<std::string>(&storage_url)->required(), | ||
| "storage server url" | ||
sitaowang1998 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ); | ||
|
|
||
| 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"; | ||
sitaowang1998 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return false; | ||
| } | ||
| } | ||
|
|
||
| auto heartbeat_loop( | ||
|
|
@@ -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)) { | ||
sitaowang1998 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return cCmdArgParseErr; | ||
| } | ||
|
|
||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar comments as on the other file. |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |||||||||||||||||||||||||
| #include <cstddef> | ||||||||||||||||||||||||||
| #include <cstdlib> | ||||||||||||||||||||||||||
| #include <functional> | ||||||||||||||||||||||||||
| #include <iostream> | ||||||||||||||||||||||||||
| #include <memory> | ||||||||||||||||||||||||||
| #include <optional> | ||||||||||||||||||||||||||
| #include <stdexcept> | ||||||||||||||||||||||||||
|
|
@@ -11,7 +12,6 @@ | |||||||||||||||||||||||||
| #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> | ||||||||||||||||||||||||||
|
|
@@ -50,29 +50,62 @@ 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; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| boost::program_options::notify(variables); | ||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Move notify() call before validation checks. The + boost::program_options::notify(variables);
+
if (!variables.contains(std::string(spider::core::cHostOption))
&& !variables.contains(std::string(spider::core::cStorageUrlOption))
&& !variables.contains(std::string(spider::core::cLibsOption)))
{
std::cout << spider::core::cWorkerUsage << "\n";
std::cout << desc << "\n";
return false;
}
-
- boost::program_options::notify(variables);📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||
| 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< | ||||||||||||||||||||||||||
|
|
@@ -329,32 +362,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; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.