testCommandLineArguments1.cxx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing#kwsys for details. */
  3. #include "kwsysPrivate.h"
  4. #include KWSYS_HEADER(CommandLineArguments.hxx)
  5. // Work-around CMake dependency scanning limitation. This must
  6. // duplicate the above list of headers.
  7. #if 0
  8. #include "CommandLineArguments.hxx.in"
  9. #endif
  10. #include <iostream>
  11. #include <vector>
  12. #include <assert.h> /* assert */
  13. #include <string.h> /* strcmp */
  14. int testCommandLineArguments1(int argc, char* argv[])
  15. {
  16. kwsys::CommandLineArguments arg;
  17. arg.Initialize(argc, argv);
  18. int n = 0;
  19. char* m = KWSYS_NULLPTR;
  20. std::string p;
  21. int res = 0;
  22. typedef kwsys::CommandLineArguments argT;
  23. arg.AddArgument("-n", argT::SPACE_ARGUMENT, &n, "Argument N");
  24. arg.AddArgument("-m", argT::EQUAL_ARGUMENT, &m, "Argument M");
  25. arg.AddBooleanArgument("-p", &p, "Argument P");
  26. arg.StoreUnusedArguments(true);
  27. if (!arg.Parse()) {
  28. std::cerr << "Problem parsing arguments" << std::endl;
  29. res = 1;
  30. }
  31. if (n != 24) {
  32. std::cout << "Problem setting N. Value of N: " << n << std::endl;
  33. res = 1;
  34. }
  35. if (!m || strcmp(m, "test value") != 0) {
  36. std::cout << "Problem setting M. Value of M: " << m << std::endl;
  37. res = 1;
  38. }
  39. if (p != "1") {
  40. std::cout << "Problem setting P. Value of P: " << p << std::endl;
  41. res = 1;
  42. }
  43. std::cout << "Value of N: " << n << std::endl;
  44. std::cout << "Value of M: " << m << std::endl;
  45. std::cout << "Value of P: " << p << std::endl;
  46. if (m) {
  47. delete[] m;
  48. }
  49. char** newArgv = KWSYS_NULLPTR;
  50. int newArgc = 0;
  51. arg.GetUnusedArguments(&newArgc, &newArgv);
  52. int cc;
  53. const char* valid_unused_args[9] = { KWSYS_NULLPTR,
  54. "--ignored",
  55. "--second-ignored",
  56. "third-ignored",
  57. "some",
  58. "junk",
  59. "at",
  60. "the",
  61. "end" };
  62. if (newArgc != 9) {
  63. std::cerr << "Bad number of unused arguments: " << newArgc << std::endl;
  64. res = 1;
  65. }
  66. for (cc = 0; cc < newArgc; ++cc) {
  67. assert(newArgv[cc]); /* Quiet Clang scan-build. */
  68. std::cout << "Unused argument[" << cc << "] = [" << newArgv[cc] << "]"
  69. << std::endl;
  70. if (cc >= 9) {
  71. std::cerr << "Too many unused arguments: " << cc << std::endl;
  72. res = 1;
  73. } else if (valid_unused_args[cc] &&
  74. strcmp(valid_unused_args[cc], newArgv[cc]) != 0) {
  75. std::cerr << "Bad unused argument [" << cc << "] \"" << newArgv[cc]
  76. << "\" should be: \"" << valid_unused_args[cc] << "\""
  77. << std::endl;
  78. res = 1;
  79. }
  80. }
  81. arg.DeleteRemainingArguments(newArgc, &newArgv);
  82. return res;
  83. }