aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/utils
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2017-06-22 01:55:54 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2017-06-22 01:55:54 +0300
commit58abcc99fcd663435d32c71c90816c8a07081a47 (patch)
treeb32ab9b60c01ac860a66369923c0953843f935d3 /utils
parentfix a CMake warning (diff)
downloadwinapi-debug-58abcc99fcd663435d32c71c90816c8a07081a47.tar.gz
winapi-debug-58abcc99fcd663435d32c71c90816c8a07081a47.zip
refactor command-line parameters parsing
Diffstat (limited to '')
-rw-r--r--utils/addr2name.cpp53
-rw-r--r--utils/command_line.hpp63
-rw-r--r--utils/enum_symbols.cpp43
-rw-r--r--utils/name2addr.cpp45
4 files changed, 77 insertions, 127 deletions
diff --git a/utils/addr2name.cpp b/utils/addr2name.cpp
index 76e781d..87206f2 100644
--- a/utils/addr2name.cpp
+++ b/utils/addr2name.cpp
@@ -24,51 +24,32 @@ namespace
{
public:
explicit Addr2Name(const std::string& argv0)
- : SettingsParser{argv0, build_options(), build_args()}
- { }
-
- bool exit_with_usage() const { return help_flag; }
-
- const char* get_short_description() const override
- {
- return "[-h|--help] [--pdb ADDR,PATH]... [--] [ADDR]...";
- }
-
- std::vector<PDB> pdbs;
- std::vector<pdb::Address> addresses;
- bool lines = false;
-
- private:
- Options build_options()
+ : SettingsParser{argv0}
{
- namespace program_options = boost::program_options;
- Options descr{"options"};
- descr.add_options()
- ("help,h",
- program_options::bool_switch(&help_flag),
- "show this message and exit")
+ visible.add_options()
("pdb",
- program_options::value<std::vector<PDB>>(&pdbs)
+ boost::program_options::value<std::vector<PDB>>(&pdbs)
->value_name("ADDR,PATH"),
"load a PDB file")
- ("address",
- program_options::value<std::vector<pdb::Address>>(&addresses)
- ->value_name("ADDR"),
- "add an address to resolve")
("lines,l",
- program_options::bool_switch(&lines),
+ boost::program_options::bool_switch(&lines),
"try to resolve source files & line numbers");
- return descr;
+ hidden.add_options()
+ ("address",
+ boost::program_options::value<std::vector<pdb::Address>>(&addresses)
+ ->value_name("ADDR"),
+ "add an address to resolve");
+ positional.add("address", -1);
}
- static Arguments build_args()
+ const char* get_short_description() const override
{
- Arguments descr;
- descr.add("address", -1);
- return descr;
+ return "[-h|--help] [--pdb ADDR,PATH]... [--] [ADDR]...";
}
- bool help_flag = false;
+ std::vector<PDB> pdbs;
+ std::vector<pdb::Address> addresses;
+ bool lines = false;
};
std::string format_symbol(const pdb::Module& module, const pdb::Symbol& symbol)
@@ -130,7 +111,7 @@ int main(int argc, char* argv[])
{
try
{
- const Addr2Name settings{argv[0]};
+ Addr2Name settings{argv[0]};
try
{
@@ -142,7 +123,7 @@ int main(int argc, char* argv[])
return 1;
}
- if (settings.exit_with_usage())
+ if (settings.exit_with_usage)
{
settings.usage();
return 0;
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;
- }
}
diff --git a/utils/enum_symbols.cpp b/utils/enum_symbols.cpp
index 848c523..8fa0960 100644
--- a/utils/enum_symbols.cpp
+++ b/utils/enum_symbols.cpp
@@ -23,10 +23,19 @@ namespace
{
public:
explicit EnumSymbols(const std::string& argv0)
- : SettingsParser{argv0, build_options()}
- { }
-
- bool exit_with_usage() const { return help_flag; }
+ : SettingsParser{argv0}
+ {
+ visible.add_options()
+ ("pdb",
+ boost::program_options::value<std::vector<PDB>>(&pdbs)
+ ->value_name("ADDR,PATH"),
+ "load a PDB file")
+ ("functions",
+ boost::program_options::value<pdb::Symbol::Tag>(&tag)
+ ->implicit_value(function_tag)
+ ->zero_tokens(),
+ "only list functions");
+ }
const char* get_short_description() const override
{
@@ -46,28 +55,6 @@ namespace
}
private:
- Options build_options()
- {
- namespace program_options = boost::program_options;
- Options descr{"options"};
- descr.add_options()
- ("help,h",
- program_options::bool_switch(&help_flag),
- "show this message and exit")
- ("pdb",
- program_options::value<std::vector<PDB>>(&pdbs)
- ->value_name("ADDR,PATH"),
- "load a PDB file")
- ("functions",
- program_options::value<pdb::Symbol::Tag>(&tag)
- ->implicit_value(function_tag)
- ->zero_tokens(),
- "only list functions");
- return descr;
- }
-
- bool help_flag = false;
-
static const auto reserved_tag = static_cast<pdb::Symbol::Tag>(pdb::Symbol::Type::RESERVED);
static const auto function_tag = static_cast<pdb::Symbol::Tag>(pdb::Symbol::Type::Function);
@@ -79,7 +66,7 @@ int main(int argc, char* argv[])
{
try
{
- const EnumSymbols settings{argv[0]};
+ EnumSymbols settings{argv[0]};
try
{
@@ -91,7 +78,7 @@ int main(int argc, char* argv[])
return 1;
}
- if (settings.exit_with_usage())
+ if (settings.exit_with_usage)
{
settings.usage();
return 0;
diff --git a/utils/name2addr.cpp b/utils/name2addr.cpp
index f177c86..22c637b 100644
--- a/utils/name2addr.cpp
+++ b/utils/name2addr.cpp
@@ -23,47 +23,28 @@ namespace
{
public:
explicit Name2Addr(const std::string& argv0)
- : SettingsParser{argv0, build_options(), build_args()}
- { }
-
- bool exit_with_usage() const { return help_flag; }
-
- const char* get_short_description() const override
- {
- return "[-h|--help] [--pdb ADDR,PATH]... [--] [NAME]...";
- }
-
- std::vector<PDB> pdbs;
- std::vector<std::string> names;
-
- private:
- Options build_options()
+ : SettingsParser{argv0}
{
- namespace program_options = boost::program_options;
- Options descr{"options"};
- descr.add_options()
- ("help,h",
- program_options::bool_switch(&help_flag),
- "show this message and exit")
+ visible.add_options()
("pdb",
- program_options::value<std::vector<PDB>>(&pdbs)
+ boost::program_options::value<std::vector<PDB>>(&pdbs)
->value_name("ADDR,PATH"),
- "load a PDB file")
+ "load a PDB file");
+ hidden.add_options()
("name",
- program_options::value<std::vector<std::string>>(&names)
+ boost::program_options::value<std::vector<std::string>>(&names)
->value_name("NAME"),
"add a name to resolve");
- return descr;
+ positional.add("name", -1);
}
- static Arguments build_args()
+ const char* get_short_description() const override
{
- Arguments descr;
- descr.add("name", -1);
- return descr;
+ return "[-h|--help] [--pdb ADDR,PATH]... [--] [NAME]...";
}
- bool help_flag = false;
+ std::vector<PDB> pdbs;
+ std::vector<std::string> names;
};
void dump_error(const std::exception& e)
@@ -90,7 +71,7 @@ int main(int argc, char* argv[])
{
try
{
- const Name2Addr settings{argv[0]};
+ Name2Addr settings{argv[0]};
try
{
@@ -102,7 +83,7 @@ int main(int argc, char* argv[])
return 1;
}
- if (settings.exit_with_usage())
+ if (settings.exit_with_usage)
{
settings.usage();
return 0;