Allow positional arguments to show in help

Fixes #72. This adds an option for positional arguments to be
shown in the help output.
This commit is contained in:
Jarryd Beck 2017-11-13 18:47:12 +11:00
parent 4ebfa25915
commit 24450d183a
2 changed files with 18 additions and 3 deletions

View File

@ -1118,6 +1118,7 @@ namespace cxxopts
: m_program(std::move(program))
, m_help_string(toLocalString(std::move(help_string)))
, m_positional_help("positional parameters")
, m_show_positional(false)
, m_next_positional(m_positional.end())
{
}
@ -1129,6 +1130,13 @@ namespace cxxopts
return *this;
}
Options&
show_positional_help()
{
m_show_positional = true;
return *this;
}
ParseResult
parse(int& argc, char**& argv);
@ -1187,6 +1195,7 @@ namespace cxxopts
std::string m_program;
String m_help_string;
std::string m_positional_help;
bool m_show_positional;
std::unordered_map<std::string, std::shared_ptr<OptionDetails>> m_options;
std::vector<std::string> m_positional;
@ -1788,7 +1797,9 @@ Options::help_one_group(const std::string& g) const
for (const auto& o : group->second.options)
{
if (o.is_container && m_positional_set.find(o.l) != m_positional_set.end())
if (o.is_container &&
m_positional_set.find(o.l) != m_positional_set.end() &&
!m_show_positional)
{
continue;
}
@ -1806,7 +1817,9 @@ Options::help_one_group(const std::string& g) const
auto fiter = format.begin();
for (const auto& o : group->second.options)
{
if (o.is_container && m_positional_set.find(o.l) != m_positional_set.end())
if (o.is_container &&
m_positional_set.find(o.l) != m_positional_set.end() &&
!m_show_positional)
{
continue;
}

View File

@ -31,7 +31,9 @@ int main(int argc, char* argv[])
try
{
cxxopts::Options options(argv[0], " - example command line options");
options.positional_help("[optional args]");
options
.positional_help("[optional args]")
.show_positional_help();
bool apple = false;