start help

This commit is contained in:
Jarryd Beck 2014-10-10 18:03:18 +11:00
parent 95af125bf6
commit bd48ccfcbd
3 changed files with 58 additions and 11 deletions

View File

@ -35,6 +35,38 @@ namespace cxxopts
std::basic_regex<char> option_specifier
("(([a-zA-Z]),)?([a-zA-Z][-_a-zA-Z]+)");
std::string
format_option
(
const std::string& s,
const std::string& l,
bool has_arg
)
{
std::string result = " ";
if (s.size() > 0)
{
result += "-" + s + ",";
}
else
{
result += " ";
}
if (l.size() > 0)
{
result += " --" + l;
}
if (has_arg)
{
result += " arg";
}
return result;
}
}
OptionAdder
@ -270,6 +302,8 @@ Options::add_option
}
//add the help details
auto& options = m_help[""];
options.push_back(HelpDetails{s, l, desc, value->has_arg()});
}
void
@ -290,13 +324,29 @@ Options::add_one_option
std::string
Options::help() const
{
typedef std::vector<std::pair<std::string, std::string>> OptionHelp;
auto group = m_help.find("");
if (group == m_help.end())
{
return "";
}
return "";
OptionHelp format;
size_t longest = 0;
std::string result;
for (const auto& o : group->second)
{
auto s = format_option(o.s, o.l, o.has_arg);
longest = std::max(longest, s.size());
format.push_back(std::make_pair(s, std::string()));
result += s + "\n";
}
return result;
}
}

View File

@ -286,16 +286,12 @@ namespace cxxopts
int m_count;
};
class HelpDetails
struct HelpDetails
{
public:
HelpDetails();
private:
std::string m_short;
std::string m_long;
std::string m_description;
bool m_arg;
std::string s;
std::string l;
std::string desc;
bool has_arg;
};
class Options

View File

@ -38,6 +38,7 @@ int main(int argc, char* argv[])
("b,bob", "Bob")
("f,file", "File", cxxopts::value<std::vector<std::string>>())
("positional", "Positional arguments", cxxopts::value<std::string>())
("help", "Print help")
;
options.parse_positional("positional");
@ -66,7 +67,7 @@ int main(int argc, char* argv[])
if (options.count("help"))
{
//std::cout << options.print_help();
std::cout << options.help() << std::endl;
}
if (options.count("positional"))