diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2017-06-22 01:55:54 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2017-06-22 01:55:54 +0300 |
commit | 58abcc99fcd663435d32c71c90816c8a07081a47 (patch) | |
tree | b32ab9b60c01ac860a66369923c0953843f935d3 /utils/command_line.hpp | |
parent | fix a CMake warning (diff) | |
download | winapi-debug-58abcc99fcd663435d32c71c90816c8a07081a47.tar.gz winapi-debug-58abcc99fcd663435d32c71c90816c8a07081a47.zip |
refactor command-line parameters parsing
Diffstat (limited to 'utils/command_line.hpp')
-rw-r--r-- | utils/command_line.hpp | 63 |
1 files changed, 32 insertions, 31 deletions
diff --git a/utils/command_line.hpp b/utils/command_line.hpp index 83cf8d6..8a5cf93 100644 --- a/utils/command_line.hpp +++ b/utils/command_line.hpp @@ -20,40 +20,40 @@ 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} - { } + { + visible.add_options() + ("help,h", + "show this message and exit"); + } virtual ~SettingsParser() = default; - virtual const char* get_short_description() const { return "[OPTION]..."; } + virtual const char* get_short_description() const + { + return "[--option VALUE]..."; + } - void parse(int argc, char* argv[]) const + 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(options) - .positional(args) + .options(all) + .positional(positional) .run(), vm); - boost::program_options::notify(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; @@ -65,6 +65,11 @@ namespace 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(const std::string& path) { @@ -72,17 +77,13 @@ namespace } const std::string prog_name; - const Options options; - const Arguments args; - friend std::ostream& operator<<(std::ostream&, const SettingsParser&); + friend std::ostream& operator<<(std::ostream& os, const SettingsParser& parser) + { + const auto short_descr = parser.get_short_description(); + os << "usage: " << parser.prog_name << ' ' << short_descr << '\n'; + os << parser.visible; + return os; + } }; - - 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; - } } |