Debian Codegraph @

Utilities/KWSys/testCommandLineArguments1.cxx

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