summaryrefslogtreecommitdiff
path: root/tools/cmdline.h
diff options
context:
space:
mode:
Diffstat (limited to 'tools/cmdline.h')
-rw-r--r--tools/cmdline.h58
1 files changed, 52 insertions, 6 deletions
diff --git a/tools/cmdline.h b/tools/cmdline.h
index 9b730e6..994341d 100644
--- a/tools/cmdline.h
+++ b/tools/cmdline.h
@@ -6,6 +6,7 @@
#ifndef TOOLS_CMDLINE_H_
#define TOOLS_CMDLINE_H_
+#include <stdarg.h>
#include <stdio.h>
#include <string.h>
@@ -19,7 +20,7 @@ namespace tools {
class CommandLineParser {
public:
- typedef size_t OptionId;
+ typedef int OptionId;
// An abstract class for defining command line options.
class CmdOptionInterface {
@@ -53,16 +54,24 @@ class CommandLineParser {
// Returns whether the option should be displayed as required in the help
// output. No effect on validation.
virtual bool required() const = 0;
+
+ // Returns whether the option is not really an option but just help text
+ virtual bool help_only() const = 0;
};
+ // Add help text
+ void AddHelpText(const char* help_text, int verbosity_level = 0) {
+ options_.emplace_back(new CmdHelpText(help_text, verbosity_level));
+ }
+
// Add a positional argument. Returns the id of the added option or
// kOptionError on error.
// The "required" flag indicates whether the parameter is mandatory or
// optional, but is only used for how it is displayed in the command line
// help.
OptionId AddPositionalOption(const char* name, bool required,
- const char* help_text, const char** storage,
- int verbosity_level = 0) {
+ const std::string& help_text,
+ const char** storage, int verbosity_level = 0) {
options_.emplace_back(new CmdOptionPositional(name, help_text, storage,
verbosity_level, required));
return options_.size() - 1;
@@ -113,11 +122,44 @@ class CommandLineParser {
// Return the remaining positional args
std::vector<const char*> PositionalArgs() const;
+ // Conditionally print a message to stderr
+ void VerbosePrintf(int min_verbosity, const char* format, ...) const;
+
private:
+ // Help text only.
+ class CmdHelpText : public CmdOptionInterface {
+ public:
+ CmdHelpText(const char* help_text, int verbosity_level)
+ : help_text_(help_text), verbosity_level_(verbosity_level) {}
+
+ std::string help_flags() const override { return ""; }
+ const char* help_text() const override { return help_text_; }
+ int verbosity_level() const override { return verbosity_level_; }
+ bool matched() const override { return false; }
+
+ bool Match(const char* arg, bool parse_options) const override {
+ return false;
+ }
+
+ bool Parse(const int argc, const char* argv[], int* i) override {
+ return true;
+ }
+
+ bool positional() const override { return false; }
+
+ bool required() const override { return false; }
+
+ bool help_only() const override { return true; }
+
+ private:
+ const char* help_text_;
+ const int verbosity_level_;
+ };
+
// A positional argument.
class CmdOptionPositional : public CmdOptionInterface {
public:
- CmdOptionPositional(const char* name, const char* help_text,
+ CmdOptionPositional(const char* name, const std::string& help_text,
const char** storage, int verbosity_level,
bool required)
: name_(name),
@@ -127,7 +169,7 @@ class CommandLineParser {
required_(required) {}
std::string help_flags() const override { return name_; }
- const char* help_text() const override { return help_text_; }
+ const char* help_text() const override { return help_text_.c_str(); }
int verbosity_level() const override { return verbosity_level_; }
bool matched() const override { return matched_; }
@@ -150,9 +192,11 @@ class CommandLineParser {
bool required() const override { return required_; }
+ bool help_only() const override { return false; }
+
private:
const char* name_;
- const char* help_text_;
+ const std::string help_text_;
const char** storage_;
const int verbosity_level_;
const bool required_;
@@ -252,6 +296,8 @@ class CommandLineParser {
return false;
}
+ bool help_only() const override { return false; }
+
private:
// Returns whether arg matches the short_name flag of this option.
bool MatchShort(const char* arg) const {