summaryrefslogtreecommitdiffstats
path: root/Source/kwsys/testCommandLineArguments.cxx
diff options
context:
space:
mode:
authorAndy Cedilnik <andy.cedilnik@kitware.com>2004-09-13 20:15:02 (GMT)
committerAndy Cedilnik <andy.cedilnik@kitware.com>2004-09-13 20:15:02 (GMT)
commitc8fa6581428dd1899298034f93720882ac95488c (patch)
tree241d0956027d4e93e40eb60911f21945d6aa42be /Source/kwsys/testCommandLineArguments.cxx
parent3e03bed0acf45313c7eee351ab5047e926a5ee44 (diff)
downloadCMake-c8fa6581428dd1899298034f93720882ac95488c.zip
CMake-c8fa6581428dd1899298034f93720882ac95488c.tar.gz
CMake-c8fa6581428dd1899298034f93720882ac95488c.tar.bz2
ENH: Move command line argument parsing code to kwsys
Diffstat (limited to 'Source/kwsys/testCommandLineArguments.cxx')
-rw-r--r--Source/kwsys/testCommandLineArguments.cxx110
1 files changed, 110 insertions, 0 deletions
diff --git a/Source/kwsys/testCommandLineArguments.cxx b/Source/kwsys/testCommandLineArguments.cxx
new file mode 100644
index 0000000..3ac099a
--- /dev/null
+++ b/Source/kwsys/testCommandLineArguments.cxx
@@ -0,0 +1,110 @@
+/*=========================================================================
+
+ Program: KWSys - Kitware System Library
+ Module: $RCSfile$
+
+ Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved.
+ See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
+
+ This software is distributed WITHOUT ANY WARRANTY; without even
+ the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ PURPOSE. See the above copyright notices for more information.
+
+=========================================================================*/
+#include <kwsys/CommandLineArguments.hxx>
+
+void* random_ptr = (void*)0x123;
+
+int argument(const char* arg, const char* value, void* call_data)
+{
+ cout << "Got argument: \"" << arg << "\" value: \"" << (value?value:"(null)") << "\"" << endl;
+ if ( call_data != random_ptr )
+ {
+ cerr << "Problem processing call_data" << endl;
+ return 0;
+ }
+ return 1;
+}
+
+int unknown_argument(const char* argument, void* call_data)
+{
+ cout << "Got unknown argument: \"" << argument << "\"" << endl;
+ if ( call_data != random_ptr )
+ {
+ cerr << "Problem processing call_data" << endl;
+ return 0;
+ }
+ return 1;
+}
+
+int main(int argc, char* argv[])
+{
+ // Example run: ./testCommandLineArguments --some-int-variable 4
+ // --another-bool-variable --some-bool-variable=yes
+ // --some-stl-string-variable=foobar --set-bool-arg1 --set-bool-arg2
+ // --some-string-variable=hello
+
+ int res = 0;
+ kwsys::CommandLineArguments arg;
+ arg.Initialize(argc, argv);
+
+ // For error handling
+ arg.SetClientData(random_ptr);
+ arg.SetUnknownArgumentCallback(unknown_argument);
+
+ int some_int_variable = 10;
+ double some_double_variable = 10.10;
+ char* some_string_variable = 0;
+ kwsys_stl::string some_stl_string_variable = "";
+ bool some_bool_variable = false;
+ bool some_bool_variable1 = false;
+ bool bool_arg1 = false;
+ int bool_arg2 = 0;
+
+ typedef kwsys::CommandLineArguments argT;
+
+ arg.AddArgument("--some-int-variable", argT::SPACE_ARGUMENT, &some_int_variable, "Set some random int variable");
+ arg.AddArgument("--some-double-variable", argT::CONCAT_ARGUMENT, &some_double_variable, "Set some random double variable");
+ arg.AddArgument("--some-string-variable", argT::EQUAL_ARGUMENT, &some_string_variable, "Set some random string variable");
+ arg.AddArgument("--some-stl-string-variable", argT::EQUAL_ARGUMENT, &some_stl_string_variable, "Set some random stl string variable");
+ arg.AddArgument("--some-bool-variable", argT::EQUAL_ARGUMENT, &some_bool_variable, "Set some random bool variable");
+ 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.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");
+ arg.AddCallback("-C", argT::EQUAL_ARGUMENT, argument, random_ptr, "Option -C takes argument after =");
+ arg.AddCallback("-D", argT::CONCAT_ARGUMENT, argument, random_ptr, "This option takes concatinated argument");
+ arg.AddCallback("--long1", argT::NO_ARGUMENT, argument, random_ptr, "-A");
+ arg.AddCallback("--long2", argT::SPACE_ARGUMENT, argument, random_ptr, "-B");
+ arg.AddCallback("--long3", argT::EQUAL_ARGUMENT, argument, random_ptr, "Same as -C but a bit different");
+ arg.AddCallback("--long4", argT::CONCAT_ARGUMENT, argument, random_ptr, "-C");
+
+ if ( !arg.Parse() )
+ {
+ cerr << "Problem parsing arguments" << endl;
+ res = 1;
+ }
+ cout << "Help: " << arg.GetHelp() << endl;
+
+ cout << "Some int variable was set to: " << some_int_variable << endl;
+ cout << "Some double variable was set to: " << some_double_variable << endl;
+ if ( some_string_variable )
+ {
+ cout << "Some string variable was set to: " << some_string_variable << endl;
+ delete [] some_string_variable;
+ }
+ else
+ {
+ cerr << "Problem setting string variable" << endl;
+ res = 1;
+ }
+ cout << "Some STL String variable was set to: " << some_stl_string_variable.c_str() << endl;
+ cout << "Some bool variable was set to: " << some_bool_variable << endl;
+ cout << "Some bool variable was set to: " << some_bool_variable1 << endl;
+ cout << "bool_arg1 variable was set to: " << bool_arg1 << endl;
+ cout << "bool_arg2 variable was set to: " << bool_arg2 << endl;
+ cout << endl;
+ return res;
+}