forked from microsoft/cppwinrt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_reader.cpp
More file actions
154 lines (131 loc) · 4.8 KB
/
cmd_reader.cpp
File metadata and controls
154 lines (131 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "pch.h"
#include "cmd_reader.h"
#include <fstream>
using namespace cppwinrt;
class response_file
{
const char* resp_file_name = "respfile.txt";
void write_response_file(const char* input)
{
std::ofstream resp_file(resp_file_name);
if (!resp_file.is_open())
FAIL("Response file could not be created");
resp_file << input;
resp_file.close();
}
void remove_response_file()
{
std::remove(resp_file_name);
}
public:
response_file(const char* input)
{
write_response_file(input);
}
template<size_t numOptions>
reader create_reader(size_t const argc, const char* argv[], const option(&options)[numOptions])
{
return reader{ argc, argv, options };
}
~response_file()
{
remove_response_file();
}
};
TEST_CASE("cmd_reader")
{
static constexpr option options[]
{
{ "input", 1 },
{ "reference", 0 },
{ "output", 0, 1 },
{ "component", 0, 1 },
{ "filter", 0 },
{ "name", 0, 1 },
{ "verbose", 0, 0 },
};
// input and output
{
const char* argv[] = { "progname", "-in", "example_file.in", "-out", "example_file.out" };
const size_t argc = 5;
reader args{ argc, argv, options };
REQUIRE(args.exists("input"));
REQUIRE(args.value("input") == "example_file.in");
REQUIRE_FALSE(args.exists("reference"));
REQUIRE(args.exists("output"));
REQUIRE(args.value("output") == "example_file.out");
REQUIRE_FALSE(args.exists("filter"));
REQUIRE_FALSE(args.exists("name"));
REQUIRE_FALSE(args.exists("verbose"));
}
// response file #1: filename no quotes
{
const char* argv[] = { "progname", "@respfile.txt" };
const size_t argc = _countof(argv);
response_file rf{ R"(-in example_file.in -out example_file.out)" };
reader args = rf.create_reader(argc, argv, options);
REQUIRE(args.exists("input"));
REQUIRE(args.value("input") == "example_file.in");
REQUIRE_FALSE(args.exists("reference"));
REQUIRE(args.exists("output"));
REQUIRE(args.value("output") == "example_file.out");
REQUIRE_FALSE(args.exists("filter"));
REQUIRE_FALSE(args.exists("name"));
REQUIRE_FALSE(args.exists("verbose"));
}
// response file #2: filename with quotes
{
const char* argv[] = { "progname", "@respfile.txt" };
const size_t argc = _countof(argv);
response_file rf{ R"(-in "example file.in" -out "example file.out")" };
reader args = rf.create_reader(argc, argv, options);
REQUIRE(args.exists("input"));
REQUIRE(args.value("input") == "example file.in");
REQUIRE_FALSE(args.exists("reference"));
REQUIRE(args.exists("output"));
REQUIRE(args.value("output") == "example file.out");
REQUIRE_FALSE(args.exists("filter"));
REQUIRE_FALSE(args.exists("name"));
REQUIRE_FALSE(args.exists("verbose"));
}
// response file #3: filename with quote within name
{
const char* argv[] = { "progname", "@respfile.txt" };
const size_t argc = _countof(argv);
response_file rf{ R"(-in example\"file.in -out example\"file.out)" };
reader args = rf.create_reader(argc, argv, options);
REQUIRE(args.exists("input"));
REQUIRE(args.value("input") == R"(example"file.in)");
REQUIRE_FALSE(args.exists("reference"));
REQUIRE(args.exists("output"));
REQUIRE(args.value("output") == R"(example"file.out)");
REQUIRE_FALSE(args.exists("filter"));
REQUIRE_FALSE(args.exists("name"));
REQUIRE_FALSE(args.exists("verbose"));
}
// response file #4: really, really, long path
{
const char* argv[] = { "progname", "@respfile.txt" };
const size_t argc = _countof(argv);
std::string file_name_in(R"(C:\)");
std::string file_name_out(R"(C:\)");
std::string input_str("-in ");
for (int i = 0; i < 500; i++) {
file_name_in.append(R"(dirname\)");
file_name_out.append(R"(dirname\)");
}
file_name_in.append("example_file.in");
file_name_out.append("example_file.out");
input_str.append(file_name_in).append(" -out ").append(file_name_out);
response_file rf{ input_str.data() };
reader args = rf.create_reader(argc, argv, options);
REQUIRE(args.exists("input"));
REQUIRE(args.value("input") == file_name_in);
REQUIRE_FALSE(args.exists("reference"));
REQUIRE(args.exists("output"));
REQUIRE(args.value("output") == file_name_out);
REQUIRE_FALSE(args.exists("filter"));
REQUIRE_FALSE(args.exists("name"));
REQUIRE_FALSE(args.exists("verbose"));
}
}