Add usage in OptionParser.

GitOrigin-RevId: 0a15b69eda345c0fbc503afc26c6787f5fbcaa8e
This commit is contained in:
levlam 2020-09-14 17:04:45 +03:00
parent 6e08881d81
commit 0ad6ff0599
2 changed files with 12 additions and 0 deletions

View File

@ -7,12 +7,18 @@
#include "td/utils/OptionParser.h"
#include "td/utils/logging.h"
#include "td/utils/PathView.h"
#include <cstring>
#include <unordered_map>
namespace td {
void OptionParser::set_usage(Slice executable_name, Slice usage) {
PathView path_view(executable_name);
usage_ = PSTRING() << path_view.file_name() << " " << usage;
}
void OptionParser::set_description(string description) {
description_ = std::move(description);
}
@ -170,6 +176,9 @@ Result<vector<char *>> OptionParser::run(int argc, char *argv[], int expected_no
}
StringBuilder &operator<<(StringBuilder &sb, const OptionParser &o) {
if (!o.usage_.empty()) {
sb << "Usage: " << o.usage_ << "\n\n";
}
if (!o.description_.empty()) {
sb << o.description_ << ". ";
}

View File

@ -45,6 +45,8 @@ class OptionParser {
};
}
void set_usage(Slice executable_name, Slice usage);
void set_description(string description);
void add_checked_option(char short_key, Slice long_key, Slice description, std::function<Status(Slice)> callback);
@ -69,6 +71,7 @@ class OptionParser {
private:
vector<Option> options_;
vector<std::function<Status()>> checks_;
string usage_;
string description_;
};