ENH: generalize argList help output

This commit is contained in:
Mark Olesen 2020-11-11 16:59:01 +01:00
parent ef2ea9629c
commit 67b3c01e13

View File

@ -41,6 +41,7 @@ static inline int apiYear()
return 2000 + (foamVersion::api / 100); return 2000 + (foamVersion::api / 100);
} }
// Footer for manpage // Footer for manpage
static inline void printManFooter() static inline void printManFooter()
{ {
@ -52,31 +53,55 @@ static inline void printManFooter()
} }
// Regular option // Option output (manpage formatted)
static void printManOption(const word& optName) static inline void printManOption
(
const word& optName,
const string& optArg,
const string& optUsage
)
{ {
Info<< ".TP\n\\fB\\-" << optName << "\\fR"; Info<< ".TP\n\\fB\\-" << optName << "\\fR";
// Option has arg? if (optArg.size())
const auto optIter = argList::validOptions.cfind(optName);
if (optIter.found() && optIter().size())
{ {
Info<< " \\fI" << optIter().c_str() << "\\fR"; Info<< " \\fI" << optArg.c_str() << "\\fR";
} }
Info<< nl; Info<< nl;
// Option has usage information? if (optUsage.size())
const auto usageIter = argList::optionUsage.cfind(optName);
if (usageIter.found())
{ {
stringOps::writeWrapped(Info, *usageIter, argList::usageMax, 0, true); stringOps::writeWrapped(Info, optUsage, argList::usageMax, 0, true);
} }
else else
{ {
Info<< nl; Info<< nl;
} }
}
// Bool option output (manpage formatted)
static inline void printManOption
(
const word& optName,
const string& optUsage
)
{
printManOption(optName, string::null, optUsage);
}
// Option output (manpage formatted)
// - uses static HashTables to obtain values
static void printManOption(const word& optName)
{
printManOption
(
optName,
argList::validOptions.lookup(optName, string::null),
argList::optionUsage.lookup(optName, string::null)
);
if (argList::validParOptions.found(optName)) if (argList::validParOptions.found(optName))
{ {
Info<< "\\fB[Parallel option]\\fR" << nl; Info<< "\\fB[Parallel option]\\fR" << nl;
@ -84,14 +109,7 @@ static void printManOption(const word& optName)
} }
// Simple, hard-coded option // Wrapped output with initial start column
static inline void printManOption(const char* optName, const char* optUsage)
{
Info<< ".TP\n\\fB\\-" << optName << "\\fR" << nl
<< optUsage << nl;
}
static void printOptionUsage static void printOptionUsage
( (
std::string::size_type start, std::string::size_type start,
@ -112,7 +130,7 @@ static void printOptionUsage
} }
while (start < argList::usageMin) while (start < argList::usageMin)
{ {
Info<<' '; Info<< ' ';
++start; ++start;
} }
@ -126,39 +144,52 @@ static void printOptionUsage
} }
// Regular option // Option output (usage formatted)
static void printOption(const word& optName) static inline void printOption
(
const word& optName,
const string& optArg,
const string& optUsage
)
{ {
Info<< " -" << optName; Info<< " -" << optName;
// Length includes leading ' -' // Length with leading ' -'
label len = optName.size() + 3; std::string::size_type len = optName.size() + 3;
const auto optIter = argList::validOptions.cfind(optName); if (optArg.size())
if (optIter.found() && optIter().size())
{ {
// Length includes space between option/param and '<>' Info<< " <" << optArg.c_str() << '>';
len += optIter().size() + 3;
Info<< " <" << optIter().c_str() << '>'; // Length with space between option/param and '<>'
len += optArg.size() + 3;
} }
const auto usageIter = argList::optionUsage.cfind(optName); printOptionUsage(len, optUsage);
if (usageIter.found())
{
printOptionUsage(len, usageIter());
}
else
{
Info<< nl;
}
} }
// Simple, hard-coded option // Bool option output (usage formatted)
static inline void printOption(const char* optName, const char* optUsage) static inline void printOption
(
const word& optName,
const string& optUsage
)
{ {
Info<< " -" << optName; printOption(optName, string::null, optUsage);
printOptionUsage(3 + strlen(optName), optUsage); }
// Option output (usage formatted)
// - uses static HashTables to obtain values
static void printOption(const word& optName)
{
printOption
(
optName,
argList::validOptions.lookup(optName, string::null),
argList::optionUsage.lookup(optName, string::null)
);
} }
} // End namespace Foam } // End namespace Foam
@ -262,12 +293,12 @@ void Foam::argList::printMan() const
Info<< ".TP\n\\fI" << argName.c_str() << "\\fR"; Info<< ".TP\n\\fI" << argName.c_str() << "\\fR";
Info<< nl; Info<< nl;
// Arg has usage information? const string& text =
argList::argUsage.lookup(argIndex, string::null);
const auto usageIter = argList::argUsage.cfind(argIndex); if (text.size())
if (usageIter.found())
{ {
stringOps::writeWrapped(Info, *usageIter, usageMax, 0, true); stringOps::writeWrapped(Info, text, usageMax, 0, true);
} }
else else
{ {
@ -373,17 +404,12 @@ void Foam::argList::printUsage(bool full) const
Info<< " <" << argName.c_str() << '>'; Info<< " <" << argName.c_str() << '>';
const auto usageIter = argList::argUsage.cfind(argIndex); printOptionUsage
if (usageIter.found()) (
{ // Length with leading spaces and surround '<>'
const label len = argName.size() + 4; (argName.size() + 4),
argList::argUsage.lookup(argIndex, string::null)
printOptionUsage(len, usageIter()); );
}
else
{
Info<< nl;
}
} }
} }