2012-05-02 16:46:10 +04:00
|
|
|
/*============================================================================
|
|
|
|
KWSys - Kitware System Library
|
|
|
|
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
|
|
|
|
|
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
|
|
|
|
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even the
|
|
|
|
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the License for more information.
|
|
|
|
============================================================================*/
|
|
|
|
#include "kwsysPrivate.h"
|
|
|
|
#include KWSYS_HEADER(CommandLineArguments.hxx)
|
|
|
|
|
|
|
|
// Work-around CMake dependency scanning limitation. This must
|
|
|
|
// duplicate the above list of headers.
|
|
|
|
#if 0
|
|
|
|
# include "CommandLineArguments.hxx.in"
|
|
|
|
#endif
|
|
|
|
|
2015-08-28 21:50:49 +03:00
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
|
|
|
|
2014-08-07 17:55:30 +04:00
|
|
|
#include <assert.h> /* assert */
|
2012-05-02 16:46:10 +04:00
|
|
|
#include <string.h> /* strcmp */
|
|
|
|
|
|
|
|
int testCommandLineArguments1(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
kwsys::CommandLineArguments arg;
|
|
|
|
arg.Initialize(argc, argv);
|
|
|
|
|
|
|
|
int n = 0;
|
|
|
|
char* m = 0;
|
2015-08-28 21:50:49 +03:00
|
|
|
std::string p;
|
2012-05-02 16:46:10 +04:00
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
typedef kwsys::CommandLineArguments argT;
|
|
|
|
arg.AddArgument("-n", argT::SPACE_ARGUMENT, &n, "Argument N");
|
|
|
|
arg.AddArgument("-m", argT::EQUAL_ARGUMENT, &m, "Argument M");
|
|
|
|
arg.AddBooleanArgument("-p", &p, "Argument P");
|
|
|
|
|
|
|
|
arg.StoreUnusedArguments(true);
|
|
|
|
|
|
|
|
if ( !arg.Parse() )
|
|
|
|
{
|
2015-08-28 21:50:49 +03:00
|
|
|
std::cerr << "Problem parsing arguments" << std::endl;
|
2012-05-02 16:46:10 +04:00
|
|
|
res = 1;
|
|
|
|
}
|
|
|
|
if ( n != 24 )
|
|
|
|
{
|
2015-08-28 21:50:49 +03:00
|
|
|
std::cout << "Problem setting N. Value of N: " << n << std::endl;
|
2012-05-02 16:46:10 +04:00
|
|
|
res = 1;
|
|
|
|
}
|
|
|
|
if ( !m || strcmp(m, "test value") != 0 )
|
|
|
|
{
|
2015-08-28 21:50:49 +03:00
|
|
|
std::cout << "Problem setting M. Value of M: " << m << std::endl;
|
2012-05-02 16:46:10 +04:00
|
|
|
res = 1;
|
|
|
|
}
|
|
|
|
if ( p != "1" )
|
|
|
|
{
|
2015-08-28 21:50:49 +03:00
|
|
|
std::cout << "Problem setting P. Value of P: " << p << std::endl;
|
2012-05-02 16:46:10 +04:00
|
|
|
res = 1;
|
|
|
|
}
|
2015-08-28 21:50:49 +03:00
|
|
|
std::cout << "Value of N: " << n << std::endl;
|
|
|
|
std::cout << "Value of M: " << m << std::endl;
|
|
|
|
std::cout << "Value of P: " << p << std::endl;
|
2012-05-02 16:46:10 +04:00
|
|
|
if ( m )
|
|
|
|
{
|
|
|
|
delete [] m;
|
|
|
|
}
|
|
|
|
|
|
|
|
char** newArgv = 0;
|
|
|
|
int newArgc = 0;
|
|
|
|
arg.GetUnusedArguments(&newArgc, &newArgv);
|
|
|
|
int cc;
|
|
|
|
const char* valid_unused_args[9] = {
|
|
|
|
0, "--ignored", "--second-ignored", "third-ignored",
|
|
|
|
"some", "junk", "at", "the", "end"
|
|
|
|
};
|
|
|
|
if ( newArgc != 9 )
|
|
|
|
{
|
2015-08-28 21:50:49 +03:00
|
|
|
std::cerr << "Bad number of unused arguments: " << newArgc << std::endl;
|
2012-05-02 16:46:10 +04:00
|
|
|
res = 1;
|
|
|
|
}
|
|
|
|
for ( cc = 0; cc < newArgc; ++ cc )
|
|
|
|
{
|
2014-08-07 17:55:30 +04:00
|
|
|
assert(newArgv[cc]); /* Quiet Clang scan-build. */
|
2015-08-28 21:50:49 +03:00
|
|
|
std::cout << "Unused argument[" << cc << "] = [" << newArgv[cc] << "]"
|
|
|
|
<< std::endl;
|
2012-05-02 16:46:10 +04:00
|
|
|
if ( cc >= 9 )
|
|
|
|
{
|
2015-08-28 21:50:49 +03:00
|
|
|
std::cerr << "Too many unused arguments: " << cc << std::endl;
|
2012-05-02 16:46:10 +04:00
|
|
|
res = 1;
|
|
|
|
}
|
|
|
|
else if ( valid_unused_args[cc] &&
|
|
|
|
strcmp(valid_unused_args[cc], newArgv[cc]) != 0 )
|
|
|
|
{
|
2015-08-28 21:50:49 +03:00
|
|
|
std::cerr << "Bad unused argument [" << cc << "] \""
|
2012-05-02 16:46:10 +04:00
|
|
|
<< newArgv[cc] << "\" should be: \"" << valid_unused_args[cc] << "\""
|
2015-08-28 21:50:49 +03:00
|
|
|
<< std::endl;
|
2012-05-02 16:46:10 +04:00
|
|
|
res = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
arg.DeleteRemainingArguments(newArgc, &newArgv);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|