Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to print help in markdown form #769

Merged
merged 1 commit into from
Feb 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions src/utils/optionsparser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ bool OptionsParser::ProcessFlags(const std::vector<std::string>& args) {
ShowHelp();
return false;
}
if (param == "--help-md") {
ShowHelpMd();
return false;
}
if (param == "--show-hidden") {
ShowHidden();
continue;
Expand Down Expand Up @@ -264,6 +268,88 @@ void OptionsParser::ShowHelp() const {
}
}

namespace {
std::string EscapeMd(const std::string& input) {
const std::string kSpecial = "~#<>&*_\\[]+-`|:\n\r";
std::string s = input;
size_t pos = 0;
while ((pos = s.find_first_of(kSpecial, pos)) != std::string::npos) {
switch (s[pos]) {
case '<':
s.replace(pos, 1, "&lt;");
pos += 4;
break;
case '>':
s.replace(pos, 1, "&gt;");
pos += 4;
break;
case '&':
s.replace(pos, 1, "&amp;");
pos += 5;
break;
case '\n':
s.replace(pos, 1, "<br/>");
pos += 5;
break;
case '\r':
s.erase(pos, 1);
break;
default:
s.insert(pos, "\\");
pos += 2;
}
}
return s;
}
} // namespace

void OptionsParser::ShowHelpMd() const {
std::cout << "\n# Lc0 options\n";
std::cout << "\n*Flag*|*UCI option*|Description\n---|---|------\n";
std::cout << "**--help**, **-h**||Show help and exit.\n";
for (const auto& option : options_) {
if (option->hidden_) continue;
if (!option->GetLongFlag().empty()) {
std::cout << "**--" << option->GetLongFlag() << "**";
}
if (option->GetShortFlag()) {
std::cout << ", **-" << option->GetShortFlag() << "**";
}
std::cout << '|';
if (!option->GetUciOption().empty()) {
std::cout << "**" << option->GetUciOption() << "**";
}
std::cout << '|' << EscapeMd(option->GetHelpText());
std::string help = option->GetHelp(defaults_);
size_t idx = help.rfind("DEFAULT:");
if (idx != std::string::npos) {
help.replace(idx, 8, "*Default value:* `");
size_t idx2 = help.rfind("MIN:");
if (idx2 != std::string::npos) {
help.replace(idx2, 4, "`<br/>*Minimum value:* `");
}
idx2 = help.rfind("MAX:");
if (idx2 != std::string::npos) {
help.replace(idx2, 4, "`<br/>*Maximum value:* `");
}
idx2 = help.rfind("VALUES:");
if (idx2 != std::string::npos) {
help.replace(idx2, 7, "`<br/>*Allowed values:* `");
while ((idx2 = help.find(",", idx2)) != std::string::npos) {
help.replace(idx2, 1, "`, `");
idx2 += 4;
}
}
idx2 = help.rfind("]");
if (idx2 != std::string::npos) {
help.erase(idx2);
}
std::cout << "<br/>" << help.substr(idx) << "`";
}
std::cout << "\n";
}
}

void OptionsParser::ShowHidden() const {
for (const auto& option : options_) option->hidden_ = false;
}
Expand Down
4 changes: 3 additions & 1 deletion src/utils/optionsparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,10 @@ class OptionsParser {
void AddContext(const std::string&);

private:
// Prints help to std::cerr.
// Prints help to std::cout.
void ShowHelp() const;
// Prints markdown formatted help to std::cout.
void ShowHelpMd() const;
// Make all hidden options visible.
void ShowHidden() const;
// Returns an option based on the long flag.
Expand Down