Commit 915c68703f2778869348e9a634663b18b70c26c5

Authored by Henry Fredrick Schreiner
1 parent 200d0f27

Nicer help printing

examples/try.cpp
... ... @@ -6,7 +6,7 @@ int main (int argc, char** argv) {
6 6 CLI::App app("K3Pi goofit fitter");
7 7  
8 8 std::string file;
9   - app.add_option("-f,--file", file, "File name");
  9 + app.add_option("-f,--file,file", file, "File name");
10 10  
11 11 int count;
12 12 app.add_flag("-c,--count", count, "Counter");
... ...
include/CLI.hpp
... ... @@ -470,6 +470,11 @@ public:
470 470 return out;
471 471 }
472 472  
  473 + // Just the pname
  474 + std::string get_pname() const {
  475 + return pname;
  476 + }
  477 +
473 478 /// Process the callback
474 479 bool run_callback() const {
475 480 if(opts.validators.size()>0) {
... ... @@ -1299,10 +1304,10 @@ public:
1299 1304 std::cerr << "ERROR: ";
1300 1305 std::cerr << e.what() << std::endl;
1301 1306 if(e.print_help)
1302   - std::cerr << help() << std::endl;
  1307 + std::cerr << help();
1303 1308 } else {
1304 1309 if(e.print_help)
1305   - std::cout << help() << std::endl;
  1310 + std::cout << help();
1306 1311 }
1307 1312 return e.exit_code;
1308 1313 }
... ... @@ -1323,6 +1328,18 @@ public:
1323 1328 out << "Subcommand: " << name << " ";
1324 1329 out << prog_discription << std::endl;
1325 1330 out << "Usage: " << progname;
  1331 +
  1332 + // Check for options
  1333 + bool npos = false;
  1334 + for(const Option &opt : options) {
  1335 + if(opt.nonpositional()) {
  1336 + npos = true;
  1337 + break;
  1338 + }
  1339 + }
  1340 +
  1341 + if(npos)
  1342 + out << " [OPTIONS...]";
1326 1343  
1327 1344 // Positionals
1328 1345 bool pos=false;
... ... @@ -1332,6 +1349,8 @@ public:
1332 1349 if(opt.has_discription())
1333 1350 pos=true;
1334 1351 }
  1352 +
  1353 +
1335 1354 out << std::endl << std::endl;
1336 1355  
1337 1356 // Positional discriptions
... ... @@ -1339,7 +1358,7 @@ public:
1339 1358 out << "Positionals:" << std::endl;
1340 1359 for(const Option &opt : options)
1341 1360 if(opt.positional() && opt.has_discription()) {
1342   - out << " " << std::setw(30) << std::right << opt.help_positional();
  1361 + out << std::setw(30) << std::left << " " + opt.get_pname();
1343 1362 out << opt.get_discription() << std::endl;
1344 1363 }
1345 1364 out << std::endl;
... ... @@ -1348,21 +1367,14 @@ public:
1348 1367  
1349 1368  
1350 1369 // Options
1351   - int len = std::accumulate(std::begin(options), std::end(options), 0,
1352   - [](int val, const Option &opt){
1353   - return std::max(opt.help_len()+3, val);});
1354   -
1355   - bool npos = false;
1356   - for(const Option &opt : options) {
1357   - if(opt.nonpositional()) {
1358   - if(!npos) {
1359   - out << "Options:" << std::endl;
1360   - npos=true;
  1370 + if(npos) {
  1371 + out << "Options:" << std::endl;
  1372 + for(const Option &opt : options) {
  1373 + if(opt.nonpositional()) {
  1374 + out << opt.help(30) << std::endl;
1361 1375 }
1362   - out << opt.help(len) << std::endl;
1363 1376 }
1364   - if(npos)
1365   - out << std::endl;
  1377 + out << std::endl;
1366 1378 }
1367 1379  
1368 1380 // Subcommands
... ...