Skip to content

Commit

Permalink
Merge pull request #12640 from trilinos/12639-Teuchos-Handle-non-matc…
Browse files Browse the repository at this point in the history
…hing-quotes-in-CLP-on-Mac

Teuchos: Handle non-matching quotes in CLP on Mac
  • Loading branch information
ccober6 authored Jan 5, 2024
2 parents 09880b2 + 20db5ec commit 471c324
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
10 changes: 7 additions & 3 deletions packages/teuchos/core/src/Teuchos_CommandLineProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,13 @@ inline int my_max( int a, int b ) { return a > b ? a : b; }

std::string remove_quotes( const std::string& str )
{
if(str[0] != '\"')
return str;
return str.substr(1,str.size()-2);
if (str[0] == '\"' && str[str.size()-1] == '\"')
return str.substr(1,str.size()-2);
else if (str[0] == '\"')
return str.substr(1,str.size()-1);
else if (str[str.size()-1] == '\"')
return str.substr(0,str.size()-1);
return str;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@ TRIBITS_ADD_EXECUTABLE_AND_TEST(
COMM serial mpi
NUM_MPI_PROCS 1
)

TRIBITS_ADD_EXECUTABLE_AND_TEST(
CLP_Remove_Quotes
SOURCES Teuchos_CLP_Remove_Quotes.cpp
ARGS "--method=TEST"
COMM serial mpi
NUM_MPI_PROCS 1
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "Teuchos_GlobalMPISession.hpp"
#include "Teuchos_CommandLineProcessor.hpp"


int main( int argc, char* argv[] )
{
using Teuchos::CommandLineProcessor;

Teuchos::GlobalMPISession mpiSession(&argc, &argv);

bool verbose = true;
bool parse_successful = true;

std::string test_arg[4];
std::string test_title[4];
test_arg[0] = "--method=Name_of_Method"; test_title[0] = "without Quotes";
test_arg[1] = "--method=\"Name_of_Method\""; test_title[1] = "with Quotes";
test_arg[2] = "--method=\"Name_of_Method"; test_title[2] = "with Leading Quote";
test_arg[3] = "--method=Name_of_Method\""; test_title[3] = "with Trailing Quote";

for (int i=0; i<4; i++) {
try {
if (verbose)
std::cout << "Test "<<i<<" : CLP - remove_quotes() " << test_title[i] << std::endl;

argv[1] = &(test_arg[i][0]);
CommandLineProcessor CLP(true, true); // Recognize all options AND throw exceptions

std::string method_name = "";

CLP.setOption("method", &method_name, "Name of Method");
CLP.parse(argc, argv);

if (verbose)
std::cout << "Test "<<i<<" : CLP - remove_quotes() " << test_title[i] << ": ";
if (method_name != "Name_of_Method")
{
parse_successful = false;
if (verbose) std::cout << "FAILED" << std::endl;
}
else
if (verbose) std::cout << "PASSED" << std::endl;
}
catch( CommandLineProcessor::UnrecognizedOption &excpt ) {
if(verbose)
std::cout << "*** Caught EXPECTED standard exception : " << excpt.what() << std::endl
<< "Test "<<i<<" : CLP - remove_quotes() " << test_title[i] << ": PASSED" << std::endl;
}
catch( ... ) {
if(verbose)
std::cout << "*** Caught UNEXPECTED unknown exception" << std::endl
<< "Test "<<i<<" : CLP - remove_quotes() " << test_title[i] << ": FAILED" << std::endl;
parse_successful = false; // No exceptions should be thrown for this command line processor.
}
}

// Return whether the command line processor tests passed.
if (parse_successful) {
std::cout << "End Result: TEST PASSED" << std::endl;
return 0;
}
else {
std::cout << "End Result: TEST FAILED" << std::endl;
return 1;
}
}

0 comments on commit 471c324

Please sign in to comment.