diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2017-05-17 06:00:20 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2017-05-17 06:00:20 +0300 |
commit | c67055bad3cdfc93e2ac57d87f36c6e0993af690 (patch) | |
tree | da8759c66f1af00415784e5824e6bfc2195aee92 /utils/command_line.hpp | |
download | winapi-debug-c67055bad3cdfc93e2ac57d87f36c6e0993af690.tar.gz winapi-debug-c67055bad3cdfc93e2ac57d87f36c6e0993af690.zip |
initial commit
Diffstat (limited to '')
-rw-r--r-- | utils/command_line.hpp | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/utils/command_line.hpp b/utils/command_line.hpp new file mode 100644 index 0000000..79fb673 --- /dev/null +++ b/utils/command_line.hpp @@ -0,0 +1,86 @@ +// Copyright (c) 2017 Egor Tensin <Egor.Tensin@gmail.com> +// This file is part of the "PDB repository" project. +// For details, see https://github.com/egor-tensin/pdb-repo. +// Distributed under the MIT License. + +#pragma once + +#include <boost/filesystem.hpp> +#include <boost/program_options.hpp> + +#include <exception> +#include <iostream> +#include <ostream> +#include <string> + +namespace +{ + class SettingsParser + { + public: + typedef boost::program_options::options_description Options; + typedef boost::program_options::positional_options_description Arguments; + + explicit SettingsParser(const std::string& argv0) + : prog_name{extract_filename(argv0)} + { } + + SettingsParser(const std::string& argv0, const Options& options) + : prog_name{extract_filename(argv0)} + , options{options} + { } + + SettingsParser(const std::string& argv0, const Options& options, const Arguments& args) + : prog_name{extract_filename(argv0)} + , options{options} + , args{args} + { } + + virtual ~SettingsParser() = default; + + virtual const char* get_short_description() const { return "[OPTION]..."; } + + void parse(int argc, char* argv[]) const + { + boost::program_options::variables_map vm; + boost::program_options::store( + boost::program_options::command_line_parser{argc, argv} + .options(options) + .positional(args) + .run(), + vm); + boost::program_options::notify(vm); + } + + void usage() const + { + std::cout << *this; + } + + void usage_error(const std::exception& e) const + { + std::cerr << "usage error: " << e.what() << '\n'; + std::cerr << *this; + } + + private: + static std::string extract_filename(const std::string& path) + { + return boost::filesystem::path{path}.filename().string(); + } + + const std::string prog_name; + const Options options; + const Arguments args; + + friend std::ostream& operator<<(std::ostream&, const SettingsParser&); + }; + + std::ostream& operator<<(std::ostream& os, const SettingsParser& cmd_parser) + { + const auto short_descr = cmd_parser.get_short_description(); + os << "usage: " << cmd_parser.prog_name << ' ' << short_descr << '\n'; + os << cmd_parser.options; + return os; + } +} |