Improve readability of usage message

Former-commit-id: 7919b8e95e57518ee5dd60c6de8b6524a1749998
This commit is contained in:
poljvd 2014-12-17 16:43:14 +01:00
parent b74036b61d
commit 6248489aed

View File

@ -17,6 +17,9 @@
#include "getoptpp.h" #include "getoptpp.h"
#include <stdexcept> #include <stdexcept>
#include <cassert> #include <cassert>
#include <cstdio>
#include <string>
#include <sys/ioctl.h>
using namespace std; using namespace std;
@ -116,15 +119,32 @@ void OptionsParser::usage() const {
cerr << "Parameters: " << endl; cerr << "Parameters: " << endl;
int totalWidth = 80;
int usageWidth = 33;
// read total width from the terminal
struct winsize w;
if (ioctl(0, TIOCGWINSZ, &w) == 0)
{
if (w.ws_col > totalWidth)
totalWidth = w.ws_col;
}
std::list<Parameter*>::const_iterator i; std::list<Parameter*>::const_iterator i;
for(i = parameters.parameters.begin(); for(i = parameters.parameters.begin();
i != parameters.parameters.end(); i++) i != parameters.parameters.end(); i++)
{ {
cerr.width(33); cerr.width(usageWidth);
cerr << std::left << " " + (*i)->usageLine(); cerr << std::left << " " + (*i)->usageLine();
cerr.width(40); std::string description = (*i)->description();
cerr << std::left << (*i)->description() << endl; while (int(description.length()) > (totalWidth - usageWidth))
{
size_t pos = description.find_last_of(' ', totalWidth - usageWidth);
cerr << description.substr(0, pos) << std::endl << std::string(usageWidth - 1, ' ');
description = description.substr(pos);
}
cerr << description << endl;
} }
} }