add text wrapping to the argList::printUsage output

This commit is contained in:
Mark Olesen
2009-12-03 16:16:56 +01:00
parent 67b79d9206
commit 587401643c
3 changed files with 97 additions and 36 deletions

View File

@ -41,8 +41,18 @@ using namespace Foam;
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
argList::noParallel(); argList::noParallel();
argList::addBoolOption("new"); argList::addBoolOption
argList::addBoolOption("old"); (
"new",
"output switches that are known from the libraries "
"but that do not seem to be known in the current etc/controlDict"
);
argList::addBoolOption
(
"old",
"output switches that appear to be unknown in "
"the current etc/controlDict"
);
argList args(argc, argv); argList args(argc, argv);

View File

@ -37,11 +37,13 @@ License
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * // // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
bool Foam::argList::bannerEnabled = true;
Foam::SLList<Foam::string> Foam::argList::validArgs; Foam::SLList<Foam::string> Foam::argList::validArgs;
Foam::HashTable<Foam::string> Foam::argList::validOptions; Foam::HashTable<Foam::string> Foam::argList::validOptions;
Foam::HashTable<Foam::string> Foam::argList::validParOptions; Foam::HashTable<Foam::string> Foam::argList::validParOptions;
Foam::HashTable<Foam::string> Foam::argList::optionUsage; Foam::HashTable<Foam::string> Foam::argList::optionUsage;
bool Foam::argList::bannerEnabled = true; Foam::string::size_type Foam::argList::usageMin = 16;
Foam::string::size_type Foam::argList::usageMax = 80;
Foam::argList::initValidTables::initValidTables() Foam::argList::initValidTables::initValidTables()
@ -129,20 +131,89 @@ void Foam::argList::noParallel()
void Foam::argList::printOptionUsage void Foam::argList::printOptionUsage
( (
const label location, const label location,
const label padWidth,
const string& str const string& str
) )
{ {
if (!str.empty()) const string::size_type textWidth = usageMax - usageMin;
const string::size_type strLen = str.size();
if (strLen)
{ {
for (label i = location; i < padWidth; ++i) // minimum of 2 spaces between option and usage:
if (string::size_type(location) + 2 <= usageMin)
{
for (string::size_type i = location; i < usageMin; ++i)
{ {
Info<<' '; Info<<' ';
} }
// we could also add text wrapping if desired
Info<<" " << str.c_str();
} }
else
{
// or start a new line
Info<< nl; Info<< nl;
for (string::size_type i = 0; i < usageMin; ++i)
{
Info<<' ';
}
}
// text wrap - this could probably be made more efficient
string::size_type pos = 0;
while (pos != string::npos && strLen - pos > textWidth)
{
string::size_type prev = pos;
string::size_type wordEnd = str.find_first_of(" \t\n", pos);
string::size_type next = string::npos;
while (wordEnd != string::npos && (wordEnd - pos) < textWidth)
{
prev = wordEnd;
next = str.find_first_not_of(" \t\n", wordEnd);
if (next == string::npos)
{
wordEnd = string::npos;
}
else
{
wordEnd = str.find_first_of(" \t\n", next);
}
}
if (pos != string::npos)
{
// indent next line
if (pos)
{
for (string::size_type i = 0; i < usageMin; ++i)
{
Info<<' ';
}
}
Info<< str.substr(pos, (prev - pos)).c_str() << nl;
pos = next;
}
}
if (pos != string::npos)
{
// indent next line
if (pos)
{
for (string::size_type i = 0; i < usageMin; ++i)
{
Info<<' ';
}
}
Info<< str.substr(pos).c_str() << nl;
}
}
else
{
Info<< nl;
}
} }
@ -675,27 +746,6 @@ void Foam::argList::printUsage() const
Info<< "\noptions:\n"; Info<< "\noptions:\n";
// min is length of the -srcDoc option
// first get the length of option + param
label padWidth = 6;
forAllConstIter(HashTable<string>, validOptions, iter)
{
label len = iter().size();
if (len)
{
len++; // space between option and param
}
len += iter.key().size();
if (padWidth < len)
{
padWidth = len;
}
}
padWidth += 3; // include leading " -"
wordList opts = validOptions.sortedToc(); wordList opts = validOptions.sortedToc();
forAll(opts, optI) forAll(opts, optI)
{ {
@ -719,7 +769,6 @@ void Foam::argList::printUsage() const
printOptionUsage printOptionUsage
( (
len, len,
padWidth,
usageIter() usageIter()
); );
} }
@ -736,7 +785,6 @@ void Foam::argList::printUsage() const
printOptionUsage printOptionUsage
( (
9, 9,
padWidth,
"display source code in browser" "display source code in browser"
); );
@ -744,7 +792,6 @@ void Foam::argList::printUsage() const
printOptionUsage printOptionUsage
( (
6, 6,
padWidth,
"display application documentation in browser" "display application documentation in browser"
); );
@ -752,7 +799,6 @@ void Foam::argList::printUsage() const
printOptionUsage printOptionUsage
( (
7, 7,
padWidth,
"print the usage" "print the usage"
); );
Info<< endl; Info<< endl;

View File

@ -121,7 +121,6 @@ class argList
static void printOptionUsage static void printOptionUsage
( (
const label location, const label location,
const label padWidth,
const string& str const string& str
); );
@ -154,6 +153,12 @@ public:
//- Short usage information for validOptions //- Short usage information for validOptions
static HashTable<string> optionUsage; static HashTable<string> optionUsage;
//- Min offset for displaying usage (default: 16)
static string::size_type usageMin;
//- Max screen width for displaying usage (default: 80)
static string::size_type usageMax;
//! @cond ignoreDocumentation //! @cond ignoreDocumentation
class initValidTables class initValidTables
{ {