testCommandLineArguments1.cxx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*============================================================================
  2. KWSys - Kitware System Library
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "kwsysPrivate.h"
  11. #include KWSYS_HEADER(CommandLineArguments.hxx)
  12. #include KWSYS_HEADER(ios/iostream)
  13. #include KWSYS_HEADER(stl/vector)
  14. // Work-around CMake dependency scanning limitation. This must
  15. // duplicate the above list of headers.
  16. #if 0
  17. # include "CommandLineArguments.hxx.in"
  18. # include "kwsys_ios_iostream.h.in"
  19. #endif
  20. #include <assert.h> /* assert */
  21. #include <string.h> /* strcmp */
  22. int testCommandLineArguments1(int argc, char* argv[])
  23. {
  24. kwsys::CommandLineArguments arg;
  25. arg.Initialize(argc, argv);
  26. int n = 0;
  27. char* m = 0;
  28. kwsys_stl::string p;
  29. int res = 0;
  30. typedef kwsys::CommandLineArguments argT;
  31. arg.AddArgument("-n", argT::SPACE_ARGUMENT, &n, "Argument N");
  32. arg.AddArgument("-m", argT::EQUAL_ARGUMENT, &m, "Argument M");
  33. arg.AddBooleanArgument("-p", &p, "Argument P");
  34. arg.StoreUnusedArguments(true);
  35. if ( !arg.Parse() )
  36. {
  37. kwsys_ios::cerr << "Problem parsing arguments" << kwsys_ios::endl;
  38. res = 1;
  39. }
  40. if ( n != 24 )
  41. {
  42. kwsys_ios::cout << "Problem setting N. Value of N: " << n << kwsys_ios::endl;
  43. res = 1;
  44. }
  45. if ( !m || strcmp(m, "test value") != 0 )
  46. {
  47. kwsys_ios::cout << "Problem setting M. Value of M: " << m << kwsys_ios::endl;
  48. res = 1;
  49. }
  50. if ( p != "1" )
  51. {
  52. kwsys_ios::cout << "Problem setting P. Value of P: " << p << kwsys_ios::endl;
  53. res = 1;
  54. }
  55. kwsys_ios::cout << "Value of N: " << n << kwsys_ios::endl;
  56. kwsys_ios::cout << "Value of M: " << m << kwsys_ios::endl;
  57. kwsys_ios::cout << "Value of P: " << p << kwsys_ios::endl;
  58. if ( m )
  59. {
  60. delete [] m;
  61. }
  62. char** newArgv = 0;
  63. int newArgc = 0;
  64. arg.GetUnusedArguments(&newArgc, &newArgv);
  65. int cc;
  66. const char* valid_unused_args[9] = {
  67. 0, "--ignored", "--second-ignored", "third-ignored",
  68. "some", "junk", "at", "the", "end"
  69. };
  70. if ( newArgc != 9 )
  71. {
  72. kwsys_ios::cerr << "Bad number of unused arguments: " << newArgc << kwsys_ios::endl;
  73. res = 1;
  74. }
  75. for ( cc = 0; cc < newArgc; ++ cc )
  76. {
  77. assert(newArgv[cc]); /* Quiet Clang scan-build. */
  78. kwsys_ios::cout << "Unused argument[" << cc << "] = [" << newArgv[cc] << "]"
  79. << kwsys_ios::endl;
  80. if ( cc >= 9 )
  81. {
  82. kwsys_ios::cerr << "Too many unused arguments: " << cc << kwsys_ios::endl;
  83. res = 1;
  84. }
  85. else if ( valid_unused_args[cc] &&
  86. strcmp(valid_unused_args[cc], newArgv[cc]) != 0 )
  87. {
  88. kwsys_ios::cerr << "Bad unused argument [" << cc << "] \""
  89. << newArgv[cc] << "\" should be: \"" << valid_unused_args[cc] << "\""
  90. << kwsys_ios::endl;
  91. res = 1;
  92. }
  93. }
  94. arg.DeleteRemainingArguments(newArgc, &newArgv);
  95. return res;
  96. }