ENH: improve error handling for foamHelp

- Catch any leading option (the incorrect location).
- Catch initialization error for cleaner result.
This commit is contained in:
Mark Olesen
2018-11-21 20:18:39 +01:00
parent 98b5914198
commit 6dcc46b187
3 changed files with 41 additions and 23 deletions

View File

@ -1,10 +1,12 @@
argList::addArgument("tool"); argList::addArgument("tool");
const wordList opts(helpType::dictionaryConstructorTablePtr_->sortedToc());
string note = "Valid <tool> options include:"; argList::notes.append("Valid <tool> options include:");
forAll(opts, i) for (const word& tool : helpType::dictionaryConstructorTablePtr_->sortedToc())
{ {
note = note + ' ' + opts[i]; argList::notes.append(" " + tool);
} }
argList::notes.append(note); argList::notes.append
(
"\nNOTE the <tool> must actually appear *before* any options"
);

View File

@ -44,24 +44,36 @@ int main(int argc, char *argv[])
#include "addRegionOption.H" #include "addRegionOption.H"
#include "addToolOption.H" #include "addToolOption.H"
// Intercept request for help // Intercept request for any -option (eg, -doc, -help)
if ((argc > 1) && (strcmp(argv[1], "-help") == 0)) // when it is the first argument
if (argc > 1 && '-' == *argv[1])
{ {
#include "setRootCase.H" #include "setRootCase.H"
} }
else if (argc < 2)
if (argc < 2)
{ {
FatalError FatalError
<< "No help utility has been supplied" << nl << "No help utility has been supplied" << nl
<< exit(FatalError); << exit(FatalError);
} }
word utilityName = argv[1]; word utilityName(argv[1]);
Foam::autoPtr<Foam::helpType> utility autoPtr<helpType> utility;
(
helpType::New(utilityName) const bool throwing = FatalError.throwExceptions();
); try
{
utility.reset(helpType::New(utilityName));
}
catch (Foam::error& err)
{
utility.clear();
FatalError
<< err.message().c_str() << nl
<< exit(FatalError);
}
FatalError.throwExceptions(throwing);
utility().init(); utility().init();
@ -71,7 +83,7 @@ int main(int argc, char *argv[])
utility().execute(args, mesh); utility().execute(args, mesh);
Info<< "End\n" << endl; Info<< "\nEnd\n" << endl;
return 0; return 0;
} }

View File

@ -32,31 +32,35 @@ Foam::autoPtr<Foam::helpType> Foam::helpType::New
const word& helpTypeName const word& helpTypeName
) )
{ {
Info<< "Selecting helpType " << helpTypeName << endl;
auto cstrIter = dictionaryConstructorTablePtr_->cfind(helpTypeName); auto cstrIter = dictionaryConstructorTablePtr_->cfind(helpTypeName);
if (!cstrIter.found()) if (!cstrIter.found())
{ {
// special treatment for -help // special treatment for -help
// exit without stack trace // exit without stack trace
if (helpTypeName == "-help") if (helpTypeName.startsWith("-help"))
{ {
FatalErrorInFunction FatalErrorInFunction
<< "Valid helpType selections are:" << nl << "Valid helpType selections:" << nl
<< dictionaryConstructorTablePtr_->sortedToc() << nl << " "
<< flatOutput(dictionaryConstructorTablePtr_->sortedToc())
<< endl
<< exit(FatalError); << exit(FatalError);
} }
else else
{ {
FatalErrorInFunction FatalErrorInFunction
<< "Unknown helpType type " << helpTypeName << nl << "Unknown helpType type '" << helpTypeName << "'" << nl << nl
<< "Valid helpType selections are:" << nl << "Valid helpType selections:" << nl
<< dictionaryConstructorTablePtr_->sortedToc() << nl << " "
<< flatOutput(dictionaryConstructorTablePtr_->sortedToc())
<< endl
<< abort(FatalError); << abort(FatalError);
} }
} }
Info<< "Selecting helpType '" << helpTypeName << "'" << endl;
return autoPtr<helpType>(cstrIter()()); return autoPtr<helpType>(cstrIter()());
} }