summaryrefslogtreecommitdiffstatshomepage
path: root/src/utils/command_line.hpp
diff options
context:
space:
mode:
authorEgor Tensin <egor@tensin.name>2026-06-12 23:49:48 +0200
committerEgor Tensin <egor@tensin.name>2026-06-12 23:49:48 +0200
commitb00525a10746de26040d201fa2ca7fb075d1f83b (patch)
tree5ef06ab9fe9f1e49040a7bb76f74a62acdf4bed5 /src/utils/command_line.hpp
parentcmake: move main library code to src/ (diff)
downloadwinapi-debug-b00525a10746de26040d201fa2ca7fb075d1f83b.tar.gz
winapi-debug-b00525a10746de26040d201fa2ca7fb075d1f83b.zip
utils/ -> src/utils/
Diffstat (limited to 'src/utils/command_line.hpp')
-rw-r--r--src/utils/command_line.hpp73
1 files changed, 73 insertions, 0 deletions
diff --git a/src/utils/command_line.hpp b/src/utils/command_line.hpp
new file mode 100644
index 0000000..d3178f1
--- /dev/null
+++ b/src/utils/command_line.hpp
@@ -0,0 +1,73 @@
+// Copyright (c) 2017 Egor Tensin <Egor.Tensin@gmail.com>
+// This file is part of the "winapi-debug" project.
+// For details, see https://github.com/egor-tensin/winapi-debug.
+// Distributed under the MIT License.
+
+#pragma once
+
+#include <boost/program_options.hpp>
+
+#include <exception>
+#include <filesystem>
+#include <format>
+#include <iostream>
+#include <ostream>
+#include <string>
+
+class SettingsParser {
+public:
+ explicit SettingsParser(int, char** argv) : prog_name{extract_filename(argv[0])} {
+ visible.add_options()("help,h", "show this message and exit");
+ }
+
+ virtual ~SettingsParser() = default;
+
+ virtual const char* get_short_description() const {
+ return "[--option VALUE]...";
+ }
+
+ virtual void parse(int argc, char* argv[]) {
+ boost::program_options::options_description all;
+ all.add(hidden).add(visible);
+ boost::program_options::variables_map vm;
+ boost::program_options::store(boost::program_options::command_line_parser{argc, argv}
+ .options(all)
+ .positional(positional)
+ .run(),
+ vm);
+ if (vm.count("help"))
+ exit_with_usage = true;
+ else
+ boost::program_options::notify(vm);
+ }
+
+ bool exit_with_usage = false;
+
+ void usage() const {
+ std::cout << *this;
+ }
+
+ void usage_error(const std::exception& e) const {
+ std::cerr << std::format("usage error: {}\n", e.what());
+ std::cerr << *this;
+ }
+
+protected:
+ boost::program_options::options_description hidden;
+ boost::program_options::options_description visible;
+ boost::program_options::positional_options_description positional;
+
+private:
+ static std::string extract_filename(char* path) {
+ return std::filesystem::path{path}.filename().string();
+ }
+
+ const std::string prog_name;
+
+ friend std::ostream& operator<<(std::ostream& os, const SettingsParser& parser) {
+ const auto short_descr = parser.get_short_description();
+ os << std::format("usage: {} {}\n", parser.prog_name, short_descr);
+ os << parser.visible;
+ return os;
+ }
+};