start help
This commit is contained in:
parent
95af125bf6
commit
bd48ccfcbd
@ -35,6 +35,38 @@ namespace cxxopts
|
|||||||
|
|
||||||
std::basic_regex<char> option_specifier
|
std::basic_regex<char> option_specifier
|
||||||
("(([a-zA-Z]),)?([a-zA-Z][-_a-zA-Z]+)");
|
("(([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
|
OptionAdder
|
||||||
@ -270,6 +302,8 @@ Options::add_option
|
|||||||
}
|
}
|
||||||
|
|
||||||
//add the help details
|
//add the help details
|
||||||
|
auto& options = m_help[""];
|
||||||
|
options.push_back(HelpDetails{s, l, desc, value->has_arg()});
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -290,13 +324,29 @@ Options::add_one_option
|
|||||||
std::string
|
std::string
|
||||||
Options::help() const
|
Options::help() const
|
||||||
{
|
{
|
||||||
|
typedef std::vector<std::pair<std::string, std::string>> OptionHelp;
|
||||||
|
|
||||||
auto group = m_help.find("");
|
auto group = m_help.find("");
|
||||||
if (group == m_help.end())
|
if (group == m_help.end())
|
||||||
{
|
{
|
||||||
return "";
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -286,16 +286,12 @@ namespace cxxopts
|
|||||||
int m_count;
|
int m_count;
|
||||||
};
|
};
|
||||||
|
|
||||||
class HelpDetails
|
struct HelpDetails
|
||||||
{
|
{
|
||||||
public:
|
std::string s;
|
||||||
HelpDetails();
|
std::string l;
|
||||||
|
std::string desc;
|
||||||
private:
|
bool has_arg;
|
||||||
std::string m_short;
|
|
||||||
std::string m_long;
|
|
||||||
std::string m_description;
|
|
||||||
bool m_arg;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class Options
|
class Options
|
||||||
|
|||||||
@ -38,6 +38,7 @@ int main(int argc, char* argv[])
|
|||||||
("b,bob", "Bob")
|
("b,bob", "Bob")
|
||||||
("f,file", "File", cxxopts::value<std::vector<std::string>>())
|
("f,file", "File", cxxopts::value<std::vector<std::string>>())
|
||||||
("positional", "Positional arguments", cxxopts::value<std::string>())
|
("positional", "Positional arguments", cxxopts::value<std::string>())
|
||||||
|
("help", "Print help")
|
||||||
;
|
;
|
||||||
|
|
||||||
options.parse_positional("positional");
|
options.parse_positional("positional");
|
||||||
@ -66,7 +67,7 @@ int main(int argc, char* argv[])
|
|||||||
|
|
||||||
if (options.count("help"))
|
if (options.count("help"))
|
||||||
{
|
{
|
||||||
//std::cout << options.print_help();
|
std::cout << options.help() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.count("positional"))
|
if (options.count("positional"))
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user