Repository navigation
Expand file tree
/
Copy pathcommand-line.cpp
More file actions
140 lines (113 loc) · 3.96 KB
/
Copy pathcommand-line.cpp
File metadata and controls
140 lines (113 loc) · 3.96 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
/* ========================================================================
Copyright (C) 2021 Eaton
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
========================================================================
*/
#include "fty/command-line.h"
#include <catch2/catch.hpp>
#include <iostream>
TEST_CASE("Command line help")
{
bool help = false;
bool optionBool = false;
std::string optionString = "Default value";
// clang-format off
fty::CommandLine cmd("Test command line", {
{"--bool", optionBool, "Option bool"},
{"--string", optionString, "Option string"},
{"--help", help, "Show this help"}
});
CHECK(cmd.help() ==
R"(Test command line
--bool Option bool
--string Option string [default: Default value]
--help Show this help
)");
// clang-format on
}
TEST_CASE("Command line without arg")
{
bool help = false;
bool optionBool = false;
std::string optionString = "Default value";
// clang-format off
fty::CommandLine cmd("Test command line", {
{"--bool", optionBool, "Option bool"},
{"--string", optionString, "Option string"},
{"--help", help, "Show this help"}
});
// clang-format on
char arg0[] = "./test";
char* argv1[] = {arg0};
if (!cmd.parse(1, argv1)) {
FAIL(cmd.error());
}
CHECK(cmd.positionalArgs().size() == 0);
CHECK(help == false);
CHECK(optionBool == false);
CHECK(optionString == "Default value");
help = true;
optionBool = true;
optionString = "Default value";
}
TEST_CASE("Command line with arg")
{
bool help = false;
bool optionBool = false;
std::string optionString = "Default value";
// clang-format off
fty::CommandLine cmd("Test command line", {
{"--bool", optionBool, "Option bool"},
{"--string", optionString, "Option string"},
{"--help", help, "Show this help"}
});
// clang-format on
char arg0[] = "./test";
char arg1[] = "--bool";
char arg2[] = "--string=Test string";
char arg3[] = "--help";
char* argv1[] = {arg0, arg1, arg2, arg3};
if (!cmd.parse(4, argv1)) {
FAIL(cmd.error());
}
CHECK(cmd.positionalArgs().size() == 0);
CHECK(help == true);
CHECK(optionBool == true);
CHECK(optionString == "Test string");
}
TEST_CASE("Argument parse")
{
bool help = false;
bool optionBool = false;
std::string optionString = "Default value";
// clang-format off
fty::CommandLine cmd("Test command line", {
{"--bool", optionBool, "Option bool"},
{"--string", optionString, "Option string"},
{"--help", help, "Show this help"}
});
// clang-format on
char arg0[] = "./test";
char arg1[] = "hello";
char arg2[] = "--bool";
char arg3[] = "--string=Test string";
char arg4[] = "--help";
char arg5[] = "end";
char* argv1[] = {arg0, arg1, arg2, arg3, arg4, arg5};
if (!cmd.parse(6, argv1)) {
FAIL(cmd.error());
}
CHECK(cmd.positionalArgs().size() == 2);
CHECK(cmd.positionalArgs()[0] == "hello");
CHECK(cmd.positionalArgs()[1] == "end");
}