1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-30 20:53:52 +00:00

Format cleanup, using standard fn

This commit is contained in:
Henry Fredrick Schreiner 2017-02-04 14:21:02 -05:00
parent c5cab1feb6
commit 34b92f6894
2 changed files with 19 additions and 38 deletions

View File

@ -4,10 +4,10 @@
int main (int argc, char** argv) { int main (int argc, char** argv) {
CLI::App app("K3Pi goofit fitter"); CLI::App app("K3Pi goofit fitter");
CLI::App* start = app.add_subcommand("start"); app.add_flag("--random", "Some random flag");
CLI::App* stop = app.add_subcommand("stop"); CLI::App* start = app.add_subcommand("start", "A great subcommand");
CLI::App* stop = app.add_subcommand("stop", "Do you really want to stop?");
std::cout << app.help();
std::string file; std::string file;
start->add_option("-f,--file", file, "File name"); start->add_option("-f,--file", file, "File name");

View File

@ -174,7 +174,13 @@ namespace detail {
return "STRING"; return "STRING";
} }
void format_help(std::stringstream &out, std::string name, std::string discription, size_t wid) {
name = " " + name;
out << std::setw(wid) << std::left << name;
if(name.length()>=wid)
out << std::endl << std::setw(wid) << "";
out << discription << std::endl;
}
struct Combiner { struct Combiner {
int num; int num;
@ -567,7 +573,7 @@ public:
/// The first half of the help print, name plus default, etc /// The first half of the help print, name plus default, etc
std::string help_name() const { std::string help_name() const {
std::stringstream out; std::stringstream out;
out << " " << get_name(); out << get_name();
if(expected() != 0) { if(expected() != 0) {
if(typeval != "") if(typeval != "")
out << " " << typeval; out << " " << typeval;
@ -581,25 +587,6 @@ public:
return out.str(); return out.str();
} }
/// The length of the name part of the help, for formatting
int help_len() const {
return help_name().length();
}
/// Make a help string, adjustable len.
std::string help(int len = 0) const {
std::stringstream out;
if(help_len() > len) {
out << help_name() << "\n";
out << std::setw(len) << " ";
} else {
out << std::setw(len) << std::left << help_name();
}
out << discription;
return out.str();
}
/// Produce a flattened vector of results, vs. a vector of vectors. /// Produce a flattened vector of results, vs. a vector of vectors.
std::vector<std::string> flatten_results() const { std::vector<std::string> flatten_results() const {
std::vector<std::string> output; std::vector<std::string> output;
@ -1326,7 +1313,7 @@ public:
throw OptionNotFound(name); throw OptionNotFound(name);
} }
std::string help() const { std::string help(size_t wid=30) const {
std::stringstream out; std::stringstream out;
if(name != "") if(name != "")
out << "Subcommand: " << name << " "; out << "Subcommand: " << name << " ";
@ -1354,17 +1341,14 @@ public:
pos=true; pos=true;
} }
out << std::endl << std::endl; out << std::endl << std::endl;
// Positional discriptions // Positional discriptions
if(pos) { if(pos) {
out << "Positionals:" << std::endl; out << "Positionals:" << std::endl;
for(const Option &opt : options) for(const Option &opt : options)
if(opt.positional() && opt.has_discription()) { if(opt.positional() && opt.has_discription())
out << std::setw(30) << std::left << " " + opt.get_pname(); detail::format_help(out, opt.get_pname(), opt.get_discription(), wid);
out << opt.get_discription() << std::endl;
}
out << std::endl; out << std::endl;
} }
@ -1374,9 +1358,9 @@ public:
if(npos) { if(npos) {
out << "Options:" << std::endl; out << "Options:" << std::endl;
for(const Option &opt : options) { for(const Option &opt : options) {
if(opt.nonpositional()) { if(opt.nonpositional())
out << opt.help(30) << std::endl; detail::format_help(out, opt.help_name(), opt.get_discription(), wid);
}
} }
out << std::endl; out << std::endl;
} }
@ -1384,11 +1368,8 @@ public:
// Subcommands // Subcommands
if(subcommands.size()> 0) { if(subcommands.size()> 0) {
out << "Subcommands:" << std::endl; out << "Subcommands:" << std::endl;
int max = std::accumulate(std::begin(subcommands), std::end(subcommands), 0, for(const std::unique_ptr<App> &com : subcommands)
[](int i, const std::unique_ptr<App> &j){return std::max(i, (int) j->get_name().length()+3);}); detail::format_help(out, com->get_name(), com->prog_discription, wid);
for(const std::unique_ptr<App> &com : subcommands) {
out << std::setw(max) << std::left << com->get_name() << " " << com->prog_discription << std::endl;
}
} }
return out.str(); return out.str();
} }