Commit 093838f0 authored by Andy Cedilnik's avatar Andy Cedilnik
Browse files

ENH: Add support for multi-arguments: -f arg1 arg2 arg3 ... and support for...

ENH: Add support for multi-arguments: -f arg1 arg2 arg3 ... and support for lists: -f arg1 -f arg2 -f arg3 ... and for boolean to be stored as strings and doubles
parent d1199302
......@@ -819,6 +819,24 @@ IF(KWSYS_STANDALONE OR CMake_SOURCE_DIR)
ENDIF(NOT CYGWIN)
ADD_TEST(kwsys.testHashSTL ${EXEC_DIR}/testHashSTL)
ADD_TEST(kwsys.testRegistry ${EXEC_DIR}/testRegistry)
ADD_TEST(kwsys.testCommandLineArguments ${EXEC_DIR}/testCommandLineArguments
--another-bool-variable
--long3=opt
--set-bool-arg1
-SSS ken brad bill andy
--some-bool-variable=true
--some-double-variable12.5
--some-int-variable 14
"--some-string-variable=test string with space"
--some-multi-argument 5 1 8 3 7 1 3 9 7 1
-N 12.5 -SS=andy -N 1.31 -N 22
-SS=bill -BBtrue -SS=brad
-BBtrue
-BBfalse
-SS=ken
-A
-C=test
--long2 hello)
IF(COMMAND SET_TESTS_PROPERTIES AND COMMAND GET_TEST_PROPERTY AND KWSYS_STANDALONE)
ADD_TEST(kwsys.testFail ${EXEC_DIR}/testFail)
# We expect test to fail
......
This diff is collapsed.
......@@ -18,6 +18,7 @@
#include <@KWSYS_NAMESPACE@/Configure.hxx>
#include <@KWSYS_NAMESPACE@/stl/string>
#include <@KWSYS_NAMESPACE@/stl/vector>
/* Define this macro temporarily to keep the code readable. */
#if !defined (KWSYS_NAMESPACE) && !@KWSYS_NAMESPACE@_NAME_IS_KWSYS
......@@ -28,6 +29,7 @@ namespace @KWSYS_NAMESPACE@
{
class CommandLineArgumentsInternal;
class CommandLineArgumentsCallbackStructure;
/** \class CommandLineArguments
* \brief Command line arguments processing code.
......@@ -53,6 +55,7 @@ class CommandLineArgumentsInternal;
* CONCAT_ARGUMENT - The argument takes value after no space : --Aval
* SPACE_ARGUMENT - The argument takes value after space : --A val
* EQUAL_ARGUMENT - The argument takes value after equal : --A=val
* MULTI_ARGUMENT - The argument takes values after space : --A val1 val2 val3 ...
*
* Example use:
*
......@@ -82,7 +85,8 @@ public:
NO_ARGUMENT,
CONCAT_ARGUMENT,
SPACE_ARGUMENT,
EQUAL_ARGUMENT
EQUAL_ARGUMENT,
MULTI_ARGUMENT
};
/**
......@@ -95,7 +99,13 @@ public:
BOOL_TYPE, // The vairable is boolean (bool)
DOUBLE_TYPE, // The variable is float (double)
STRING_TYPE, // The variable is string (char*)
STL_STRING_TYPE // The variable is string (char*)
STL_STRING_TYPE, // The variable is string (char*)
VECTOR_INT_TYPE, // The variable is integer (int)
VECTOR_BOOL_TYPE, // The vairable is boolean (bool)
VECTOR_DOUBLE_TYPE, // The variable is float (double)
VECTOR_STRING_TYPE, // The variable is string (char*)
VECTOR_STL_STRING_TYPE, // The variable is string (char*)
LAST_VARIABLE_TYPE
};
/**
......@@ -138,10 +148,10 @@ public:
* specified value. If the argument is specified, the option is casted to the
* apropriate type.
*/
void AddArgument(const char* argument, ArgumentTypeEnum type, bool* variable,
const char* help);
void AddArgument(const char* argument, ArgumentTypeEnum type, int* variable,
const char* help);
void AddArgument(const char* argument, ArgumentTypeEnum type,
bool* variable, const char* help);
void AddArgument(const char* argument, ArgumentTypeEnum type,
int* variable, const char* help);
void AddArgument(const char* argument, ArgumentTypeEnum type,
double* variable, const char* help);
void AddArgument(const char* argument, ArgumentTypeEnum type,
......@@ -149,15 +159,53 @@ public:
void AddArgument(const char* argument, ArgumentTypeEnum type,
kwsys_stl::string* variable, const char* help);
/**
* Add handler for argument which is going to set the variable to the
* specified value. If the argument is specified, the option is casted to the
* apropriate type. This will handle the multi argument values.
*/
void AddArgument(const char* argument, ArgumentTypeEnum type,
kwsys_stl::vector<bool>* variable, const char* help);
void AddArgument(const char* argument, ArgumentTypeEnum type,
kwsys_stl::vector<int>* variable, const char* help);
void AddArgument(const char* argument, ArgumentTypeEnum type,
kwsys_stl::vector<double>* variable, const char* help);
void AddArgument(const char* argument, ArgumentTypeEnum type,
kwsys_stl::vector<char*>* variable, const char* help);
void AddArgument(const char* argument, ArgumentTypeEnum type,
kwsys_stl::vector<kwsys_stl::string>* variable, const char* help);
/**
* Add handler for boolean argument. The argument does not take any option
* and if it is specified, the value of the variable is true/1, otherwise it
* is false/0.
*/
void AddBooleanArgument(const char* argument, bool* variable, const char*
help);
void AddBooleanArgument(const char* argument, int* variable, const char*
help);
void AddBooleanArgument(const char* argument,
bool* variable, const char* help);
void AddBooleanArgument(const char* argument,
int* variable, const char* help);
void AddBooleanArgument(const char* argument,
double* variable, const char* help);
void AddBooleanArgument(const char* argument,
char** variable, const char* help);
void AddBooleanArgument(const char* argument,
kwsys_stl::string* variable, const char* help);
/**
* Add handler for boolean argument. The argument does not take any option
* and if it is specified, the value of the variable is true/1, otherwise it
* is false/0. This will handle the multi argument values.
*/
void AddBooleanArgument(const char* argument,
kwsys_stl::vector<bool>* variable, const char* help);
void AddBooleanArgument(const char* argument,
kwsys_stl::vector<int>* variable, const char* help);
void AddBooleanArgument(const char* argument,
kwsys_stl::vector<double>* variable, const char* help);
void AddBooleanArgument(const char* argument,
kwsys_stl::vector<char*>* variable, const char* help);
void AddBooleanArgument(const char* argument,
kwsys_stl::vector<kwsys_stl::string>* variable, const char* help);
/**
* Set the callbacks for error handling.
......@@ -205,6 +253,25 @@ protected:
void AddArgument(const char* argument, ArgumentTypeEnum type,
VariableTypeEnum vtype, void* variable, const char* help);
bool GetMatchedArguments(kwsys_stl::vector<kwsys_stl::string>* matches,
const kwsys_stl::string& arg);
//! Populate individual variables
bool PopulateVariable(CommandLineArgumentsCallbackStructure* cs,
const char* value);
//! Populate individual variables of type ...
void PopulateVariable(bool* variable, const kwsys_stl::string& value);
void PopulateVariable(int* variable, const kwsys_stl::string& value);
void PopulateVariable(double* variable, const kwsys_stl::string& value);
void PopulateVariable(char** variable, const kwsys_stl::string& value);
void PopulateVariable(kwsys_stl::string* variable, const kwsys_stl::string& value);
void PopulateVariable(kwsys_stl::vector<bool>* variable, const kwsys_stl::string& value);
void PopulateVariable(kwsys_stl::vector<int>* variable, const kwsys_stl::string& value);
void PopulateVariable(kwsys_stl::vector<double>* variable, const kwsys_stl::string& value);
void PopulateVariable(kwsys_stl::vector<char*>* variable, const kwsys_stl::string& value);
void PopulateVariable(kwsys_stl::vector<kwsys_stl::string>* variable, const kwsys_stl::string& value);
typedef CommandLineArgumentsInternal Internal;
Internal* Internals;
kwsys_stl::string Help;
......
......@@ -3157,7 +3157,7 @@ bool SystemTools::LocateFileInDir(const char *filename,
{
filename_dir = SystemTools::GetFilenamePath(filename_dir);
filename_dir_base = SystemTools::GetFilenameName(filename_dir);
#if _WIN32
#if defined( _WIN32 )
if (!filename_dir_base.size() ||
filename_dir_base[filename_dir_base.size() - 1] == ':')
#else
......
......@@ -14,6 +14,7 @@
#include "kwsysPrivate.h"
#include KWSYS_HEADER(CommandLineArguments.hxx)
#include KWSYS_HEADER(ios/iostream)
#include KWSYS_HEADER(stl/vector)
// Work-around CMake dependency scanning limitation. This must
// duplicate the above list of headers.
......@@ -46,6 +47,12 @@ int unknown_argument(const char* argument, void* call_data)
return 1;
}
bool CompareTwoItemsOnList(bool i1, bool i2) { return i1 == i2; }
bool CompareTwoItemsOnList(int i1, int i2) { return i1 == i2; }
bool CompareTwoItemsOnList(double i1, double i2) { return i1 == i2; }
bool CompareTwoItemsOnList(const kwsys_stl::string& i1,
const kwsys_stl::string& i2) { return i1 == i2; }
int main(int argc, char* argv[])
{
// Example run: ./testCommandLineArguments --some-int-variable 4
......@@ -70,6 +77,21 @@ int main(int argc, char* argv[])
bool bool_arg1 = false;
int bool_arg2 = 0;
kwsys_stl::vector<int> numbers_argument;
int valid_numbers[] = { 5, 1, 8, 3, 7, 1, 3, 9, 7, 1 };
kwsys_stl::vector<double> doubles_argument;
double valid_doubles[] = { 12.5, 1.31, 22 };
kwsys_stl::vector<bool> bools_argument;
bool valid_bools[] = { true, true, false };
kwsys_stl::vector<char*> strings_argument;
char* valid_strings[] = { "andy", "bill", "brad", "ken" };
kwsys_stl::vector<kwsys_stl::string> stl_strings_argument;
kwsys_stl::string valid_stl_strings[] = { "ken", "brad", "bill", "andy" };
typedef kwsys::CommandLineArguments argT;
arg.AddArgument("--some-int-variable", argT::SPACE_ARGUMENT, &some_int_variable, "Set some random int variable");
......@@ -80,6 +102,11 @@ int main(int argc, char* argv[])
arg.AddArgument("--another-bool-variable", argT::NO_ARGUMENT, &some_bool_variable1, "Set some random bool variable 1");
arg.AddBooleanArgument("--set-bool-arg1", &bool_arg1, "Test AddBooleanArgument 1");
arg.AddBooleanArgument("--set-bool-arg2", &bool_arg2, "Test AddBooleanArgument 2");
arg.AddArgument("--some-multi-argument", argT::MULTI_ARGUMENT, &numbers_argument, "Some multiple values variable");
arg.AddArgument("-N", argT::SPACE_ARGUMENT, &doubles_argument, "Some explicit multiple values variable");
arg.AddArgument("-BB", argT::CONCAT_ARGUMENT, &bools_argument, "Some explicit multiple values variable");
arg.AddArgument("-SS", argT::EQUAL_ARGUMENT, &strings_argument, "Some explicit multiple values variable");
arg.AddArgument("-SSS", argT::MULTI_ARGUMENT, &stl_strings_argument, "Some explicit multiple values variable");
arg.AddCallback("-A", argT::NO_ARGUMENT, argument, random_ptr, "Some option -A. This option has a multiline comment. It should demonstrate how the code splits lines.");
arg.AddCallback("-B", argT::SPACE_ARGUMENT, argument, random_ptr, "Option -B takes argument with space");
......@@ -99,7 +126,7 @@ int main(int argc, char* argv[])
kwsys_ios::cout << "Some int variable was set to: " << some_int_variable << kwsys_ios::endl;
kwsys_ios::cout << "Some double variable was set to: " << some_double_variable << kwsys_ios::endl;
if ( some_string_variable )
if ( some_string_variable && strcmp(some_string_variable, "test string with space") == 0)
{
kwsys_ios::cout << "Some string variable was set to: " << some_string_variable << kwsys_ios::endl;
delete [] some_string_variable;
......@@ -109,6 +136,38 @@ int main(int argc, char* argv[])
kwsys_ios::cerr << "Problem setting string variable" << kwsys_ios::endl;
res = 1;
}
size_t cc;
#define CompareTwoLists(list1, list_valid, lsize) \
if ( list1.size() != lsize ) \
{ \
kwsys_ios::cerr << "Problem setting " #list1 ". Size is: " << list1.size() \
<< " should be: " << lsize << kwsys_ios::endl; \
res = 1; \
} \
else \
{ \
kwsys_ios::cout << #list1 " argument set:"; \
for ( cc =0; cc < lsize; ++ cc ) \
{ \
kwsys_ios::cout << " " << list1[cc]; \
if ( !CompareTwoItemsOnList(list1[cc], list_valid[cc]) ) \
{ \
kwsys_ios::cerr << "Problem setting " #list1 ". Value of " \
<< cc << " is: [" << list1[cc] << "] <> [" \
<< list_valid[cc] << "]" << kwsys_ios::endl; \
res = 1; \
break; \
} \
} \
kwsys_ios::cout << kwsys_ios::endl; \
}
CompareTwoLists(numbers_argument, valid_numbers, 10);
CompareTwoLists(doubles_argument, valid_doubles, 3);
CompareTwoLists(bools_argument, valid_bools, 3);
CompareTwoLists(strings_argument, valid_strings, 4);
CompareTwoLists(stl_strings_argument, valid_stl_strings, 4);
kwsys_ios::cout << "Some STL String variable was set to: " << some_stl_string_variable.c_str() << kwsys_ios::endl;
kwsys_ios::cout << "Some bool variable was set to: " << some_bool_variable << kwsys_ios::endl;
kwsys_ios::cout << "Some bool variable was set to: " << some_bool_variable1 << kwsys_ios::endl;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment