Prettify OptionParser usage printing.

GitOrigin-RevId: f0b7c833e91d9eea49483bb93124babc190fa670
This commit is contained in:
levlam 2020-06-17 06:09:53 +03:00
parent ce137620f5
commit 4f8fd1781d
2 changed files with 34 additions and 10 deletions

View File

@ -4335,7 +4335,7 @@ void main(int argc, char **argv) {
use_test_dc = true;
return Status::OK();
});
options.add_option('v', "verbosity", "Verbosity level", [&](Slice level) {
options.add_option('v', "verbosity", "Set verbosity level", [&](Slice level) {
int new_verbosity = 1;
while (begins_with(level, "v")) {
new_verbosity++;
@ -4347,7 +4347,7 @@ void main(int argc, char **argv) {
new_verbosity_level = VERBOSITY_NAME(FATAL) + new_verbosity;
return Status::OK();
});
options.add_option('l', "log", "Log file", [&](Slice file_name) {
options.add_option('l', "log", "Log to file", [&](Slice file_name) {
if (file_log.init(file_name.str()).is_ok() && file_log.init(file_name.str()).is_ok() &&
file_log.init(file_name.str(), 1000 << 20).is_ok()) {
log_interface = &ts_log;
@ -4362,19 +4362,19 @@ void main(int argc, char **argv) {
disable_network = true;
return Status::OK();
});
options.add_option('\0', "api-id", "Telegram API ID", [&](Slice parameter) {
options.add_option('\0', "api-id", "Set Telegram API ID", [&](Slice parameter) {
api_id = to_integer<int32>(parameter);
return Status::OK();
});
options.add_option('\0', "api_id", "Telegram API ID", [&](Slice parameter) {
options.add_option('\0', "api_id", "Set Telegram API ID", [&](Slice parameter) {
api_id = to_integer<int32>(parameter);
return Status::OK();
});
options.add_option('\0', "api-hash", "Telegram API hash", [&](Slice parameter) {
options.add_option('\0', "api-hash", "Set Telegram API hash", [&](Slice parameter) {
api_hash = parameter.str();
return Status::OK();
});
options.add_option('\0', "api_hash", "Telegram API hash", [&](Slice parameter) {
options.add_option('\0', "api_hash", "Set Telegram API hash", [&](Slice parameter) {
api_hash = parameter.str();
return Status::OK();
});

View File

@ -135,22 +135,46 @@ Result<vector<char *>> OptionParser::run(int argc, char *argv[]) {
}
StringBuilder &operator<<(StringBuilder &sb, const OptionParser &o) {
sb << o.description_ << "\n";
if (!o.description_.empty()) {
sb << o.description_ << ". ";
}
sb << "Options:\n";
size_t max_length = 0;
for (auto &opt : o.options_) {
bool has_short_key = opt.short_key != '\0';
bool has_long_key = !opt.long_key.empty();
size_t length = (has_short_key ? 2 : 0) + (has_long_key ? 2 + opt.long_key.size() + 2 * has_short_key : 0);
if (opt.type != OptionParser::Option::Type::NoArg) {
length += 5;
}
if (length > max_length) {
max_length = length;
}
}
max_length++;
for (auto &opt : o.options_) {
bool has_short_key = opt.short_key != '\0';
sb << " ";
size_t length = max_length;
if (has_short_key) {
sb << "-" << opt.short_key;
sb << '-' << opt.short_key;
length -= 2;
}
if (!opt.long_key.empty()) {
if (has_short_key) {
sb << '|';
sb << ", ";
length -= 2;
}
sb << "--" << opt.long_key;
length -= 2 + opt.long_key.size();
}
if (opt.type != OptionParser::Option::Type::NoArg) {
sb << "<arg>";
length -= 5;
}
sb << " " << opt.description;
sb << string(length, ' ') << opt.description;
sb << '\n';
}
return sb;