Repository navigation
Expand file tree
/
Copy pathtest_usage_custom.cpp
More file actions
122 lines (110 loc) · 4.64 KB
/
Copy pathtest_usage_custom.cpp
File metadata and controls
122 lines (110 loc) · 4.64 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
#include <gtest/gtest.h>
#include <rfl.hpp>
#include <rfl/cli.hpp>
#include <optional>
#include <string>
#include <variant>
#include <vector>
namespace test_usage_custom {
struct CatOptions {
rfl::Group<"COMMAND OPTIONS",
rfl::Description<"Buffer data in memory", rfl::DefaultVal<bool>>>
buffer_data = false;
rfl::Group<"COMMON OPTIONS",
rfl::Short<"v", rfl::Description<"Set verbose mode",
rfl::DefaultVal<bool>>>>
verbose = false;
rfl::Group<"INPUT OPTIONS",
rfl::Short<"F", rfl::Description<"Format of input files",
std::optional<std::string>>>>
input_format;
rfl::Hidden<rfl::Positional<std::optional<std::vector<std::string>>>>
input_filenames;
};
using SubApp =
rfl::cli::App<"", "", "", rfl::cli::BuiltinHelp::short_and_long>;
TEST(cli, test_usage_custom_synopsis_description_footer_and_help_group) {
const rfl::cli::UsageOptions opts{
.detailed = true,
.synopsis = "osmium cat [OPTIONS] [OSM-FILE...]",
.description = "Concatenate OSM files and convert to different formats.",
.footer = "Use 'osmium help cat' to display the manual page.",
.help_group = "COMMON OPTIONS",
};
const std::vector<std::string> args = {"-h"};
const auto res =
rfl::cli::read_app<CatOptions, SubApp>("osmium cat", args, opts);
ASSERT_TRUE(res) << res.error().what();
ASSERT_TRUE(std::holds_alternative<rfl::cli::HelpOrVersion>(*res));
const auto& text = std::get<rfl::cli::HelpOrVersion>(*res).text;
EXPECT_TRUE(text.starts_with(
"Usage: osmium cat [OPTIONS] [OSM-FILE...]\n\n"
"Concatenate OSM files and convert to different formats.\n"));
const auto cmd_pos = text.find("\nCOMMAND OPTIONS:\n");
const auto common_pos = text.find("\nCOMMON OPTIONS:\n");
const auto help_pos = text.find(" -h, --help");
const auto verbose_pos = text.find(" -v, --verbose");
const auto input_pos = text.find("\nINPUT OPTIONS:\n");
const auto footer_pos =
text.find("\nUse 'osmium help cat' to display the manual page.\n");
ASSERT_NE(cmd_pos, std::string::npos) << text;
ASSERT_NE(common_pos, std::string::npos) << text;
ASSERT_NE(help_pos, std::string::npos) << text;
ASSERT_NE(verbose_pos, std::string::npos) << text;
ASSERT_NE(input_pos, std::string::npos) << text;
ASSERT_NE(footer_pos, std::string::npos) << text;
// Built-in -h/--help is placed at the start of COMMON OPTIONS without
// reordering COMMAND OPTIONS ahead of it.
EXPECT_LT(cmd_pos, common_pos);
EXPECT_LT(common_pos, help_pos);
EXPECT_LT(help_pos, verbose_pos);
EXPECT_LT(verbose_pos, input_pos);
EXPECT_LT(input_pos, footer_pos);
EXPECT_EQ(text.find("input-filenames"), std::string::npos);
}
TEST(cli, test_usage_custom_header_before_usage) {
const rfl::cli::UsageOptions opts{
.detailed = true,
.header = "solc, the Solidity commandline compiler.\n\nNo warranty.",
.synopsis = "solc [options] [input_file...]",
};
const auto text = rfl::cli::usage<CatOptions>("solc", opts);
ASSERT_TRUE(text) << text.error().what();
EXPECT_TRUE(text->starts_with(
"solc, the Solidity commandline compiler.\n\nNo warranty.\n\n"
"Usage: solc [options] [input_file...]\n"))
<< *text;
}
TEST(cli, test_usage_max_option_width_wraps_wide_options) {
struct WideOptions {
rfl::Description<"Short flag", rfl::DefaultVal<bool>> verbose = false;
rfl::Description<"Very wide option label", std::optional<std::string>>
very_long_option_name_exceeding_forty_characters;
};
const auto text =
rfl::cli::usage<WideOptions, rfl::cli::PureFlags>("tool", true);
ASSERT_TRUE(text) << text.error().what();
// Short option should not be padded past the 40-char cap.
EXPECT_EQ(text->find("--verbose "),
std::string::npos)
<< *text;
// Wide option wraps its description onto the next line.
EXPECT_NE(
text->find(
"--very-long-option-name-exceeding-forty-characters=<value>\n"),
std::string::npos)
<< *text;
}
TEST(cli, test_usage_hidden_groups) {
const rfl::cli::UsageOptions opts{
.detailed = true,
.hidden_groups = {"INPUT OPTIONS"},
};
const auto text = rfl::cli::usage<CatOptions>("osmium cat", opts);
ASSERT_TRUE(text) << text.error().what();
EXPECT_NE(text->find("COMMAND OPTIONS:"), std::string::npos) << *text;
EXPECT_NE(text->find("COMMON OPTIONS:"), std::string::npos) << *text;
EXPECT_EQ(text->find("INPUT OPTIONS:"), std::string::npos) << *text;
EXPECT_EQ(text->find("--input-format"), std::string::npos) << *text;
}
} // namespace test_usage_custom