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

Improved help #97

Closed
wants to merge 3 commits into from
Closed
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
40 changes: 23 additions & 17 deletions data/single_include/lyra/lyra.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1661,7 +1661,11 @@ class parser
[[deprecated]] std::string get_usage_text() const { return ""; }
[[deprecated]] std::string get_description_text() const { return ""; }

virtual help_text get_help_text(const option_style &) const { return {}; }
virtual help_text get_help_text(const option_style &, size_t indent) const
{
(void) indent;
return {};
}
virtual std::string get_usage_text(const option_style &) const
{
return "";
Expand Down Expand Up @@ -1719,7 +1723,7 @@ class parser
printer & p, const option_style & style) const
{
p.heading("OPTIONS, ARGUMENTS:");
auto rows = get_help_text(style);
auto rows = get_help_text(style, 0);
if (style.options_print_order
!= option_style::opt_print_order::per_declaration)
std::stable_sort(rows.begin(), rows.end(),
Expand Down Expand Up @@ -1770,7 +1774,7 @@ The set of help texts for any options in the sub-parsers to this one, if any.

[source]
----
virtual help_text get_help_text(const option_style &) const;
virtual help_text get_help_text(const option_style &, size_t indent) const;
----

Collects, and returns, the set of help items for the sub-parser arguments in
Expand Down Expand Up @@ -2228,9 +2232,9 @@ class arg : public bound_parser<arg>
return text;
}

help_text get_help_text(const option_style & style) const override
help_text get_help_text(const option_style & style, size_t indent) const override
{
return { { get_usage_text(style), m_description } };
return { { std::string(indent, ' ') + get_usage_text(style), m_description } };
}

using parser::parse;
Expand Down Expand Up @@ -2409,12 +2413,12 @@ class arguments : public parser
return text;
}

help_text get_help_text(const option_style & style) const override
help_text get_help_text(const option_style & style, size_t indent) const override
{
help_text text;
for (auto const & p : parsers)
{
auto child_help = p->get_help_text(style);
auto child_help = p->get_help_text(style, indent + 2);
text.insert(text.end(), child_help.begin(), child_help.end());
}
return text;
Expand Down Expand Up @@ -3799,9 +3803,11 @@ class literal : public parser
return description;
}

help_text get_help_text(const option_style &) const override
help_text get_help_text(const option_style &, size_t indent) const override
{
return { { name, description } };
std::string name_str = std::string(indent, ' ') + "\033[1m" + name + "\033[0m";
std::string description_str = "\033[3m" + description + "\033[0m";
return { { name_str, description_str } };
}

using parser::parse;
Expand Down Expand Up @@ -3979,21 +3985,21 @@ class command : public group
+ parsers[1]->get_usage_text(style);
}

help_text get_help_text(const option_style & style) const override
help_text get_help_text(const option_style & style, size_t indent) const override
{
if (expanded_help_details)
{
help_text text;
text.push_back({ "", "" });
auto c = parsers[0]->get_help_text(style);
auto c = parsers[0]->get_help_text(style, indent + 2);
text.insert(text.end(), c.begin(), c.end());
text.push_back({ "", "" });
auto o = parsers[1]->get_help_text(style);
auto o = parsers[1]->get_help_text(style, indent + 2);
text.insert(text.end(), o.begin(), o.end());
return text;
}
else
return parsers[0]->get_help_text(style);
return parsers[0]->get_help_text(style, indent + 2);
}

protected:
Expand All @@ -4003,7 +4009,7 @@ class command : public group
printer & p, const option_style & style) const override
{
p.heading("OPTIONS, ARGUMENTS:");
for (auto const & cols : parsers[1]->get_help_text(style))
for (auto const & cols : parsers[1]->get_help_text(style, 0))
{
p.option(cols.option, cols.description, 2);
}
Expand Down Expand Up @@ -4217,12 +4223,12 @@ class opt : public bound_parser<opt>
return usage;
}

help_text get_help_text(const option_style & style) const override
help_text get_help_text(const option_style & style, size_t indent) const override
{
std::string text;
std::string text = std::string(indent, ' ');
for (auto const & opt_name : opt_names)
{
if (!text.empty()) text += ", ";
if (!text.empty() && !(text.back() == ' ')) text += ", ";
text += format_opt(opt_name, style);
}
if (!m_hint.empty()) ((text += " <") += m_hint) += ">";
Expand Down
4 changes: 2 additions & 2 deletions include/lyra/arg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ class arg : public bound_parser<arg>
return text;
}

help_text get_help_text(const option_style & style) const override
help_text get_help_text(const option_style & style, size_t indent) const override
{
return { { get_usage_text(style), m_description } };
return { { std::string(indent, ' ') + get_usage_text(style), m_description } };
}

using parser::parse;
Expand Down
4 changes: 2 additions & 2 deletions include/lyra/arguments.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,12 @@ class arguments : public parser
}

// Return a container of the individual help text for the composed parsers.
help_text get_help_text(const option_style & style) const override
help_text get_help_text(const option_style & style, size_t indent) const override
{
help_text text;
for (auto const & p : parsers)
{
auto child_help = p->get_help_text(style);
auto child_help = p->get_help_text(style, indent + 2);
text.insert(text.end(), child_help.begin(), child_help.end());
}
return text;
Expand Down
10 changes: 5 additions & 5 deletions include/lyra/command.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,21 @@ class command : public group
+ parsers[1]->get_usage_text(style);
}

help_text get_help_text(const option_style & style) const override
help_text get_help_text(const option_style & style, size_t indent) const override
{
if (expanded_help_details)
{
help_text text;
text.push_back({ "", "" });
auto c = parsers[0]->get_help_text(style);
auto c = parsers[0]->get_help_text(style, indent + 2);
text.insert(text.end(), c.begin(), c.end());
text.push_back({ "", "" });
auto o = parsers[1]->get_help_text(style);
auto o = parsers[1]->get_help_text(style, indent + 2);
text.insert(text.end(), o.begin(), o.end());
return text;
}
else
return parsers[0]->get_help_text(style);
return parsers[0]->get_help_text(style, indent + 2);
}

protected:
Expand All @@ -125,7 +125,7 @@ class command : public group
// This avoid printing out the "internal" group brackets "{}" for the
// command arguments.
p.heading("OPTIONS, ARGUMENTS:");
for (auto const & cols : parsers[1]->get_help_text(style))
for (auto const & cols : parsers[1]->get_help_text(style, 0))
{
p.option(cols.option, cols.description, 2);
}
Expand Down
6 changes: 4 additions & 2 deletions include/lyra/literal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ class literal : public parser
return description;
}

help_text get_help_text(const option_style &) const override
help_text get_help_text(const option_style &, size_t indent) const override
{
return { { name, description } };
std::string name_str = std::string(indent, ' ') + "\033[1m" + name + "\033[0m";
std::string description_str = "\033[3m" + description + "\033[0m";
return { { name_str, description_str } };
}

using parser::parse;
Expand Down
6 changes: 3 additions & 3 deletions include/lyra/opt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ class opt : public bound_parser<opt>
return usage;
}

help_text get_help_text(const option_style & style) const override
help_text get_help_text(const option_style & style, size_t indent) const override
{
std::string text;
std::string text = std::string(indent, ' ');
for (auto const & opt_name : opt_names)
{
if (!text.empty()) text += ", ";
if (!text.empty() && !(text.back() == ' ')) text += ", ";
text += format_opt(opt_name, style);
}
if (!m_hint.empty()) ((text += " <") += m_hint) += ">";
Expand Down
10 changes: 7 additions & 3 deletions include/lyra/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,11 @@ class parser
[[deprecated]] std::string get_usage_text() const { return ""; }
[[deprecated]] std::string get_description_text() const { return ""; }

virtual help_text get_help_text(const option_style &) const { return {}; }
virtual help_text get_help_text(const option_style &, size_t indent) const
{
(void) indent;
return {};
}
virtual std::string get_usage_text(const option_style &) const
{
return "";
Expand Down Expand Up @@ -198,7 +202,7 @@ class parser
printer & p, const option_style & style) const
{
p.heading("OPTIONS, ARGUMENTS:");
auto rows = get_help_text(style);
auto rows = get_help_text(style, 0);
if (style.options_print_order
!= option_style::opt_print_order::per_declaration)
std::stable_sort(rows.begin(), rows.end(),
Expand Down Expand Up @@ -249,7 +253,7 @@ The set of help texts for any options in the sub-parsers to this one, if any.

[source]
----
virtual help_text get_help_text(const option_style &) const;
virtual help_text get_help_text(const option_style &, size_t indent) const;
----

Collects, and returns, the set of help items for the sub-parser arguments in
Expand Down