testCommandLineArguments1.cxx 3.0 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. // Work-around CMake dependency scanning limitation. This must
  13. // duplicate the above list of headers.
  14. #if 0
  15. # include "CommandLineArguments.hxx.in"
  16. #endif
  17. #include <iostream>
  18. #include <vector>
  19. #include <assert.h> /* assert */
  20. #include <string.h> /* strcmp */
  21. int testCommandLineArguments1(int argc, char* argv[])
  22. {
  23. kwsys::CommandLineArguments arg;
  24. arg.Initialize(argc, argv);
  25. int n = 0;
  26. char* m = 0;
  27. std::string p;
  28. int res = 0;
  29. typedef kwsys::CommandLineArguments argT;
  30. arg.AddArgument("-n", argT::SPACE_ARGUMENT, &n, "Argument N");
  31. arg.AddArgument("-m", argT::EQUAL_ARGUMENT, &m, "Argument M");
  32. arg.AddBooleanArgument("-p", &p, "Argument P");
  33. arg.StoreUnusedArguments(true);
  34. if ( !arg.Parse() )
  35. {
  36. std::cerr << "Problem parsing arguments" << std::endl;
  37. res = 1;
  38. }
  39. if ( n != 24 )
  40. {
  41. std::cout << "Problem setting N. Value of N: " << n << std::endl;
  42. res = 1;
  43. }
  44. if ( !m || strcmp(m, "test value") != 0 )
  45. {
  46. std::cout << "Problem setting M. Value of M: " << m << std::endl;
  47. res = 1;
  48. }
  49. if ( p != "1" )
  50. {
  51. std::cout << "Problem setting P. Value of P: " << p << std::endl;
  52. res = 1;
  53. }
  54. std::cout << "Value of N: " << n << std::endl;
  55. std::cout << "Value of M: " << m << std::endl;
  56. std::cout << "Value of P: " << p << std::endl;
  57. if ( m )
  58. {
  59. delete [] m;
  60. }
  61. char** newArgv = 0;
  62. int newArgc = 0;
  63. arg.GetUnusedArguments(&newArgc, &newArgv);
  64. int cc;
  65. const char* valid_unused_args[9] = {
  66. 0, "--ignored", "--second-ignored", "third-ignored",
  67. "some", "junk", "at", "the", "end"
  68. };
  69. if ( newArgc != 9 )
  70. {
  71. std::cerr << "Bad number of unused arguments: " << newArgc << std::endl;
  72. res = 1;
  73. }
  74. for ( cc = 0; cc < newArgc; ++ cc )
  75. {
  76. assert(newArgv[cc]); /* Quiet Clang scan-build. */
  77. std::cout << "Unused argument[" << cc << "] = [" << newArgv[cc] << "]"
  78. << std::endl;
  79. if ( cc >= 9 )
  80. {
  81. std::cerr << "Too many unused arguments: " << cc << std::endl;
  82. res = 1;
  83. }
  84. else if ( valid_unused_args[cc] &&
  85. strcmp(valid_unused_args[cc], newArgv[cc]) != 0 )
  86. {
  87. std::cerr << "Bad unused argument [" << cc << "] \""
  88. << newArgv[cc] << "\" should be: \"" << valid_unused_args[cc] << "\""
  89. << std::endl;
  90. res = 1;
  91. }
  92. }
  93. arg.DeleteRemainingArguments(newArgc, &newArgv);
  94. return res;
  95. }