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(); controlDict_.watchIndices().clear();
// Installing the new handler // Installing the new handler
autoPtr<fileOperation> handler auto handler = fileOperation::New(fileHandlerName, true);
(
fileOperation::New
(
fileHandlerName,
true
)
);
Foam::fileHandler(handler); Foam::fileHandler(handler);
// Reinstall old watches // Reinstall old watches

View File

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

View File

@ -157,6 +157,12 @@ class argList
const string& str 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, //- Read a List of values from ITstream,
//- treating a single entry like a list of size 1. //- treating a single entry like a list of size 1.
template<class T> template<class T>

View File

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

View File

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

View File

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

View File

@ -125,7 +125,7 @@ Foam::labelList Foam::fileOperations::hostCollatedFileOperation::subRanks
Foam::fileOperations::hostCollatedFileOperation::hostCollatedFileOperation Foam::fileOperations::hostCollatedFileOperation::hostCollatedFileOperation
( (
const bool verbose bool verbose
) )
: :
collatedFileOperation collatedFileOperation
@ -140,6 +140,8 @@ Foam::fileOperations::hostCollatedFileOperation::hostCollatedFileOperation
verbose verbose
) )
{ {
verbose = (verbose && Foam::infoDetailLevel > 0);
if (verbose) if (verbose)
{ {
// Print a bit of information // Print a bit of information
@ -150,12 +152,12 @@ Foam::fileOperations::hostCollatedFileOperation::hostCollatedFileOperation
} }
Pstream::gatherList(ioRanks); Pstream::gatherList(ioRanks);
Info<< " IO nodes:" << endl; Info<< " IO nodes:" << nl;
forAll(ioRanks, proci) 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 //- Destructor
virtual ~hostCollatedFileOperationInitialise() virtual ~hostCollatedFileOperationInitialise() = default;
{}
}; };

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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