ENH: minor improvements for command-line handling

- check for excess input on command-line arguments

- reduce fileHandler warning verbosity when the output banner is
  disabled
This commit is contained in:
Mark Olesen
2018-07-19 14:52:27 +02:00
parent 6e2ba37963
commit cdcbcf4c78
17 changed files with 115 additions and 82 deletions

View File

@ -259,14 +259,7 @@ void Foam::Time::readDict()
controlDict_.watchIndices().clear();
// Installing the new handler
autoPtr<fileOperation> handler
(
fileOperation::New
(
fileHandlerName,
true
)
);
auto handler = fileOperation::New(fileHandlerName, true);
Foam::fileHandler(handler);
// Reinstall old watches

View File

@ -194,6 +194,36 @@ static void printBuildInfo(const bool full=true)
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
void Foam::argList::warnTrailing(const ITstream& is, const label index)
{
const label nExcess = is.nRemainingTokens();
if (nExcess)
{
std::cerr
<< nl
<< "--> FOAM WARNING:" << nl
<< "argument " << index << " has "
<< nExcess << " excess tokens" << nl << nl;
}
}
void Foam::argList::warnTrailing(const ITstream& is, const word& optName)
{
const label nExcess = is.nRemainingTokens();
if (nExcess)
{
std::cerr
<< nl
<< "--> FOAM WARNING:" << nl
<< "option -" << optName << " has "
<< nExcess << " excess tokens" << nl << nl;
}
}
void Foam::argList::addArgument(const string& argName)
{
validArgs.append(argName);
@ -637,7 +667,7 @@ void Foam::argList::getRootCase()
fileName casePath;
// [-case dir] specified
auto optIter = options_.cfind("case");
const auto optIter = options_.cfind("case");
if (optIter.found())
{
@ -704,16 +734,17 @@ Foam::argList::argList
options_(argc),
distributed_(false)
{
// Check for fileHandler
// Check for -fileHandler, which requires an argument.
word handlerType(getEnv("FOAM_FILEHANDLER"));
for (int argI = 0; argI < argc; ++argI)
for (int argi = argc-2; argi > 0; --argi)
{
if (argv[argI][0] == '-')
if (argv[argi][0] == '-')
{
const char *optionName = &argv[argI][1];
if (string(optionName) == "fileHandler")
const char *optName = &argv[argi][1];
if (strcmp(optName, "fileHandler") == 0)
{
handlerType = argv[argI+1];
handlerType = argv[argi+1];
break;
}
}
@ -757,7 +788,6 @@ Foam::argList::argList
// Check arguments and options, argv[0] was already handled
int nArgs = 1;
HashTable<string>::const_iterator optIter;
for (int argi = 1; argi < args_.size(); ++argi)
{
argListStr_ += ' ';
@ -774,14 +804,8 @@ Foam::argList::argList
}
else if
(
(
(optIter = validOptions.cfind(optName)).found()
&& !optIter.object().empty()
)
|| (
(optIter = validParOptions.cfind(optName)).found()
&& !optIter.object().empty()
)
validOptions.lookup(optName, "").size()
|| validParOptions.lookup(optName, "").size()
)
{
// If the option is known to require an argument,
@ -966,15 +990,15 @@ void Foam::argList::parse
// 5. '-fileHandler' commmand-line option
{
word handlerType =
word fileHandlerName =
options_.lookup("fileHandler", getEnv("FOAM_FILEHANDLER"));
if (handlerType.empty())
if (fileHandlerName.empty())
{
handlerType = fileOperation::defaultFileHandler;
fileHandlerName = fileOperation::defaultFileHandler;
}
auto handler = fileOperation::New(handlerType, bannerEnabled());
auto handler = fileOperation::New(fileHandlerName, bannerEnabled());
Foam::fileHandler(handler);
}

View File

@ -157,6 +157,12 @@ class argList
const string& str
);
//- Warn if there are input tokens remaining on the stream
static void warnTrailing(const ITstream& is, const label index);
//- Warn if there are input tokens remaining on the stream
static void warnTrailing(const ITstream& is, const word& optName);
//- Read a List of values from ITstream,
//- treating a single entry like a list of size 1.
template<class T>

View File

@ -25,7 +25,6 @@ License
#include "argList.H"
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T>
@ -34,8 +33,8 @@ inline void Foam::argList::readList(ITstream& is, List<T>& list)
if (is.size() == 1)
{
// Single token - treat like List with one entry
list.setSize(1);
is >> list[0];
list.resize(1);
is >> list.first();
}
else
{
@ -226,7 +225,8 @@ inline T Foam::argList::read(const label index) const
T val;
is >> val;
// Could also check is.nRemainingTokens() to detect trailing rubbish
warnTrailing(is, index);
return val;
}
@ -239,7 +239,8 @@ inline T Foam::argList::opt(const word& optName) const
T val;
is >> val;
// Could also check is.nRemainingTokens() to detect trailing rubbish
warnTrailing(is, optName);
return val;
}
@ -303,6 +304,8 @@ inline Foam::List<T> Foam::argList::readList(const label index) const
List<T> list;
readList(is, list);
warnTrailing(is, index);
return list;
}
@ -315,6 +318,8 @@ inline Foam::List<T> Foam::argList::readList(const word& optName) const
List<T> list;
readList(is, list);
warnTrailing(is, optName);
return list;
}
@ -331,6 +336,8 @@ inline bool Foam::argList::readListIfPresent
ITstream is(optName, options_[optName]);
readList(is, list);
warnTrailing(is, optName);
return true;
}

View File

@ -242,7 +242,7 @@ bool Foam::fileOperations::collatedFileOperation::appendObject
Foam::fileOperations::collatedFileOperation::collatedFileOperation
(
const bool verbose
bool verbose
)
:
masterUncollatedFileOperation
@ -263,6 +263,8 @@ Foam::fileOperations::collatedFileOperation::collatedFileOperation
nProcs_(Pstream::nProcs()),
ioRanks_(ioRanks())
{
verbose = (verbose && Foam::infoDetailLevel > 0);
if (verbose)
{
Info<< "I/O : " << typeName
@ -300,12 +302,12 @@ Foam::fileOperations::collatedFileOperation::collatedFileOperation
}
Pstream::gatherList(ioRanks);
Info<< " IO nodes:" << endl;
forAll(ioRanks, proci)
Info<< " IO nodes:" << nl;
for (const string& ranks : ioRanks)
{
if (!ioRanks[proci].empty())
if (!ranks.empty())
{
Info<< " " << ioRanks[proci] << endl;
Info<< " " << ranks << nl;
}
}
}
@ -339,7 +341,7 @@ Foam::fileOperations::collatedFileOperation::collatedFileOperation
const label comm,
const labelList& ioRanks,
const word& typeName,
const bool verbose
bool verbose
)
:
masterUncollatedFileOperation(comm, false),
@ -348,6 +350,8 @@ Foam::fileOperations::collatedFileOperation::collatedFileOperation
nProcs_(Pstream::nProcs()),
ioRanks_(ioRanks)
{
verbose = (verbose && Foam::infoDetailLevel > 0);
if (verbose)
{
Info<< "I/O : " << typeName

View File

@ -115,7 +115,7 @@ public:
// Constructors
//- Construct null
collatedFileOperation(const bool verbose);
collatedFileOperation(bool verbose);
//- Construct from user communicator
collatedFileOperation
@ -123,7 +123,7 @@ public:
const label comm,
const labelList& ioRanks,
const word& typeName,
const bool verbose
bool verbose
);

View File

@ -125,7 +125,7 @@ Foam::labelList Foam::fileOperations::hostCollatedFileOperation::subRanks
Foam::fileOperations::hostCollatedFileOperation::hostCollatedFileOperation
(
const bool verbose
bool verbose
)
:
collatedFileOperation
@ -140,6 +140,8 @@ Foam::fileOperations::hostCollatedFileOperation::hostCollatedFileOperation
verbose
)
{
verbose = (verbose && Foam::infoDetailLevel > 0);
if (verbose)
{
// Print a bit of information
@ -150,12 +152,12 @@ Foam::fileOperations::hostCollatedFileOperation::hostCollatedFileOperation
}
Pstream::gatherList(ioRanks);
Info<< " IO nodes:" << endl;
forAll(ioRanks, proci)
Info<< " IO nodes:" << nl;
for (const string& ranks : ioRanks)
{
if (!ioRanks[proci].empty())
if (!ranks.empty())
{
Info<< " " << ioRanks[proci] << endl;
Info<< " " << ranks << nl;
}
}
}

View File

@ -117,8 +117,7 @@ public:
//- Destructor
virtual ~hostCollatedFileOperationInitialise()
{}
virtual ~hostCollatedFileOperationInitialise() = default;
};

View File

@ -428,7 +428,7 @@ Foam::fileOperation::fileOperation(label comm)
Foam::autoPtr<Foam::fileOperation> Foam::fileOperation::New
(
const word& handlerType,
const bool verbose
bool verbose
)
{
if (debug)
@ -1173,6 +1173,8 @@ Foam::label Foam::fileOperation::detectProcessorPath(const fileName& fName)
}
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
const Foam::fileOperation& Foam::fileHandler()
{
if (!fileOperation::fileHandlerPtr_.valid())
@ -1193,17 +1195,15 @@ const Foam::fileOperation& Foam::fileHandler()
void Foam::fileHandler(autoPtr<fileOperation>& newHandler)
{
if (fileOperation::fileHandlerPtr_.valid())
if
(
newHandler.valid() && fileOperation::fileHandlerPtr_.valid()
&& newHandler->type() == fileOperation::fileHandlerPtr_->type()
)
{
if
(
newHandler.valid()
&& newHandler->type() == fileOperation::fileHandlerPtr_->type()
)
{
return;
}
return;
}
fileOperation::fileHandlerPtr_.clear();
if (newHandler.valid())

View File

@ -186,7 +186,7 @@ public:
fileOperation,
word,
(
const bool verbose
bool verbose
),
(verbose)
);
@ -198,7 +198,7 @@ public:
static autoPtr<fileOperation> New
(
const word& handlerType,
const bool verbose
bool verbose = false
);

View File

@ -25,7 +25,6 @@ License
#include "fileOperationInitialise.H"
#include "addToRunTimeSelectionTable.H"
#include "OSspecific.H"
/* * * * * * * * * * * * * * * Static Member Data * * * * * * * * * * * * * */
@ -62,10 +61,9 @@ Foam::fileOperations::fileOperationInitialise::New
InfoInFunction << "Constructing fileOperationInitialise" << endl;
}
wordConstructorTable::iterator cstrIter =
wordConstructorTablePtr_->find(type);
auto cstrIter = wordConstructorTablePtr_->cfind(type);
if (cstrIter == wordConstructorTablePtr_->end())
if (!cstrIter.found())
{
FatalErrorInFunction
<< "Unknown fileOperationInitialise type "
@ -79,10 +77,4 @@ Foam::fileOperations::fileOperationInitialise::New
}
// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
Foam::fileOperations::fileOperationInitialise::~fileOperationInitialise()
{}
// ************************************************************************* //

View File

@ -80,7 +80,7 @@ public:
//- Destructor
virtual ~fileOperationInitialise();
virtual ~fileOperationInitialise() = default;
// Member Functions

View File

@ -58,8 +58,7 @@ public:
//- Destructor
virtual ~unthreadedInitialise()
{}
virtual ~unthreadedInitialise() = default;
// Member Functions

View File

@ -751,7 +751,7 @@ Foam::fileOperations::masterUncollatedFileOperation::read
Foam::fileOperations::masterUncollatedFileOperation::
masterUncollatedFileOperation
(
const bool verbose
bool verbose
)
:
fileOperation
@ -764,9 +764,12 @@ masterUncollatedFileOperation
),
myComm_(comm_)
{
verbose = (verbose && Foam::infoDetailLevel > 0);
if (verbose)
{
Info<< "I/O : " << typeName
Info
<< "I/O : " << typeName
<< " (maxMasterFileBufferSize " << maxMasterFileBufferSize << ')'
<< endl;
}
@ -801,15 +804,18 @@ Foam::fileOperations::masterUncollatedFileOperation::
masterUncollatedFileOperation
(
const label comm,
const bool verbose
bool verbose
)
:
fileOperation(comm),
myComm_(-1)
{
verbose = (verbose && Foam::infoDetailLevel > 0);
if (verbose)
{
Info<< "I/O : " << typeName
Info
<< "I/O : " << typeName
<< " (maxMasterFileBufferSize " << maxMasterFileBufferSize << ')'
<< endl;
}

View File

@ -494,10 +494,10 @@ public:
// Constructors
//- Construct null
masterUncollatedFileOperation(const bool verbose);
masterUncollatedFileOperation(bool verbose);
//- Construct from communicator
masterUncollatedFileOperation(const label comm, const bool verbose);
masterUncollatedFileOperation(const label comm, bool verbose);
//- Destructor

View File

@ -178,14 +178,15 @@ Foam::fileOperations::uncollatedFileOperation::lookupProcessorsPath
Foam::fileOperations::uncollatedFileOperation::uncollatedFileOperation
(
const bool verbose
bool verbose
)
:
fileOperation(Pstream::worldComm)
{
if (verbose)
{
Info<< "I/O : " << typeName << endl;
DetailInfo
<< "I/O : " << typeName << endl;
}
}

View File

@ -82,7 +82,7 @@ public:
// Constructors
//- Construct null
uncollatedFileOperation(const bool verbose);
uncollatedFileOperation(bool verbose);
//- Destructor