Allow help string to be customised

Fixes #82. This allows the help string after the program name to be
completely replaced by the user.
This commit is contained in:
Jarryd Beck 2017-11-28 18:02:35 +11:00
parent 2ca68adeaf
commit 5b774d7a7e
2 changed files with 17 additions and 2 deletions

View File

@ -95,6 +95,12 @@ overridden. The effect is that writing `-o` by itself will set option `o` to
`true`. However, they can also be written with various strings using either
`=value` or the next argument.
## Custom help
The string after the program name on the first line of the help can be
completely replaced by calling `options.custom_help`. Note that you might
also want to override the positional help by calling `options.positional_help`.
# Linking
This is a header only library.

View File

@ -1167,6 +1167,7 @@ namespace cxxopts
Options(std::string program, std::string help_string = "")
: m_program(std::move(program))
, m_help_string(toLocalString(std::move(help_string)))
, m_custom_help("[OPTION...]")
, m_positional_help("positional parameters")
, m_show_positional(false)
, m_next_positional(m_positional.end())
@ -1180,6 +1181,13 @@ namespace cxxopts
return *this;
}
Options&
custom_help(std::string help_text)
{
m_custom_help = std::move(help_text);
return *this;
}
Options&
show_positional_help()
{
@ -1247,6 +1255,7 @@ namespace cxxopts
std::string m_program;
String m_help_string;
std::string m_custom_help;
std::string m_positional_help;
bool m_show_positional;
@ -1928,9 +1937,9 @@ std::string
Options::help(const std::vector<std::string>& help_groups) const
{
String result = m_help_string + "\nUsage:\n " +
toLocalString(m_program) + " [OPTION...]";
toLocalString(m_program) + " " + toLocalString(m_custom_help);
if (m_positional.size() > 0) {
if (m_positional.size() > 0 && m_positional_help.size() > 0) {
result += " " + toLocalString(m_positional_help);
}