Skip to content

Commit 0e28e41

Browse files
committed
ENH: Return error compile-time if required longflag property is not set
TCLAP always requires specification of a "longflag" (even if a shortflag is specified) - see spf13/pflag#139. If longflag is not set in the module description file for more than one parameter then invalid code is generated (that has longflag set to empty) that fails at runtime. This commit detects the missing "longflag" element and reports the error at compile-time. fixes #140
1 parent 4325526 commit 0e28e41

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

GenerateCLP/GenerateCLP.cxx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,6 +1339,7 @@ void GenerateTCLAPParse(std::ostream &sout, ModuleDescription &module)
13391339
sout << " std::ostringstream msg;" << EOL << std::endl;
13401340

13411341
// Second pass generates TCLAP declarations
1342+
int longflagMissingCount = 0;
13421343
std::vector<ModuleParameterGroup>::const_iterator git;
13431344
std::vector<ModuleParameter>::const_iterator pit;
13441345
for (git = module.GetParameterGroups().begin();
@@ -1356,6 +1357,8 @@ void GenerateTCLAPParse(std::ostream &sout, ModuleDescription &module)
13561357
continue;
13571358
}
13581359

1360+
bool longflagMissing = false;
1361+
13591362
sout << " msg.str(\"\");";
13601363
sout << "msg << "
13611364
<< "\""
@@ -1389,6 +1392,10 @@ void GenerateTCLAPParse(std::ostream &sout, ModuleDescription &module)
13891392

13901393
if (pit->GetCPPType() == "bool")
13911394
{
1395+
if (pit->GetLongFlag().empty())
1396+
{
1397+
longflagMissing = true;
1398+
}
13921399
sout << " TCLAP::SwitchArg "
13931400
<< pit->GetName()
13941401
<< "Arg" << "(\""
@@ -1440,6 +1447,10 @@ void GenerateTCLAPParse(std::ostream &sout, ModuleDescription &module)
14401447
}
14411448
else if (IsEnumeration(*pit))
14421449
{
1450+
if (pit->GetLongFlag().empty())
1451+
{
1452+
longflagMissing = true;
1453+
}
14431454
sout << " TCLAP::ValueArg<";
14441455
sout << pit->GetCPPType();
14451456
sout << "> "
@@ -1460,6 +1471,10 @@ void GenerateTCLAPParse(std::ostream &sout, ModuleDescription &module)
14601471
}
14611472
else
14621473
{
1474+
if (pit->GetLongFlag().empty())
1475+
{
1476+
longflagMissing = true;
1477+
}
14631478
if (pit->GetMultiple() == "true")
14641479
{
14651480
sout << " TCLAP::MultiArg<";
@@ -1510,6 +1525,19 @@ void GenerateTCLAPParse(std::ostream &sout, ModuleDescription &module)
15101525
<< EOL << std::endl << EOL << std::endl;
15111526
}
15121527
}
1528+
if (longflagMissing)
1529+
{
1530+
std::cerr << "GenerateCLP: Warning: Required 'longflag' is not specified for parameter '" << pit->GetName() << "'." << std::endl;
1531+
longflagMissingCount++;
1532+
}
1533+
if (longflagMissingCount > 1)
1534+
{
1535+
// If only one parameter is missing a longflag, then TCLAP can still be used.
1536+
// However, multiple parameters missing longflags will cause TCLAP to throw an exception at runtime.
1537+
// It is better to report the error here, at compile time.
1538+
std::cerr << "GenerateCLP: Error: multiple parameters found with missing 'longflag'. Code generation aborted." << std::endl;
1539+
exit(EXIT_FAILURE);
1540+
}
15131541
}
15141542
}
15151543

0 commit comments

Comments
 (0)