Debian Codegraph @

tests/src/cli2/test_InterfaceHelpPrinter.cpp

raw
1 #include <pbcopper/cli2/internal/InterfaceHelpPrinter.h>
2
3 #include <sstream>
4 #include <string>
5 #include <vector>
6
7 #include <gtest/gtest.h>
8
9 #include <pbcopper/cli2/Interface.h>
10 #include <pbcopper/cli2/MultiToolInterface.h>
11 #include <pbcopper/cli2/internal/OptionData.h>
12 #include <pbcopper/cli2/internal/OptionTranslator.h>
13 #include <pbcopper/cli2/internal/PositionalArgumentTranslator.h>
14
15 #include "../../../src/cli2/PbBoilerplateDisclaimer.h"
16
17 using HiddenOptionMode = PacBio::CLI_v2::internal::HiddenOptionMode;
18 using Interface = PacBio::CLI_v2::Interface;
19 using InterfaceHelpPrinter = PacBio::CLI_v2::internal::InterfaceHelpPrinter;
20 using MultiToolInterface = PacBio::CLI_v2::MultiToolInterface;
21 using Option = PacBio::CLI_v2::Option;
22 using OptionData = PacBio::CLI_v2::internal::OptionData;
23 using OptionGroupData = PacBio::CLI_v2::internal::OptionGroupData;
24 using OptionTranslator = PacBio::CLI_v2::internal::OptionTranslator;
25 using OptionValue = PacBio::CLI_v2::OptionValue;
26 using OptionValueType = PacBio::CLI_v2::OptionValueType;
27 using PositionalArgument = PacBio::CLI_v2::PositionalArgument;
28 using PositionalArgumentTranslator = PacBio::CLI_v2::internal::PositionalArgumentTranslator;
29 using Tool = PacBio::CLI_v2::Tool;
30
31 // clang-format off
32
33 namespace CLI_v2_InterfaceHelpPrinterTests {
34 namespace Options {
35
36 const Option TargetDirectory
37 {
38 R"({
39 "names" : ["t", "target-dir"],
40 "description" : "Copy all source files into <DIR>.",
41 "type" : "dir",
42 "default" : "my/default/dir"
43 })"
44 };
45
46 const Option Force
47 {
48 R"({
49 "names" : ["f", "force"],
50 "description" : "Overwrite things.",
51 "type" : "boolean"
52 })"
53 };
54
55 const Option Progress
56 {
57 R"({
58 "names" : ["p"],
59 "description" : "Show progress during copy.",
60 "type" : "boolean"
61 })"
62 };
63
64 const Option DryRun
65 {
66 R"({
67 "names" : ["n", "no-op"],
68 "description" : "Dry run. Report actions that would be taken but do not perform them.",
69 "type" : "boolean"
70 })"
71 };
72
73 const Option Timeout
74 {
75 R"({
76 "names" : ["timeout"],
77 "description" : "Abort execution after <INT> milliseconds.",
78 "type" : "integer",
79 "default" : 5000
80 })"
81 };
82
83 const Option Delta
84 {
85 R"({
86 "names" : ["delta"],
87 "description" : "Some delta for things.",
88 "type" : "float",
89 "default" : 0.01
90 })"
91 };
92
93 const Option Ploidy
94 {
95 R"({
96 "names" : ["ploidy"],
97 "description" : "Genome ploidy.",
98 "type" : "string",
99 "default" : "haploid",
100 "choices" : ["haploid", "diploid"]
101 })"
102 };
103
104 const Option Element
105 {
106 R"({
107 "names" : ["e", "element"],
108 "names.hidden" : ["elementChoice"],
109 "description" : "Choice of element indicates mood. Science.",
110 "type" : "string",
111 "default" : "fire",
112 "choices" : ["earth", "wind", "fire", "water"]
113 })"
114 };
115
116 const Option Seed
117 {
118 R"({
119 "names" : ["seed"],
120 "description" : "Seed for random-number generator.",
121 "type" : "int",
122 "default" : -1,
123 "default.hidden" : true
124 })"
125 };
126
127 const PositionalArgument Source
128 {
129 R"({
130 "name" : "source",
131 "description" : "Source file to copy.",
132 "type" : "file"
133 })"
134 };
135
136 const PositionalArgument Dest
137 {
138 R"({
139 "name" : "dest",
140 "description" : "Destination directory. Essentially where we want to drop things, but really just making a long description.",
141 "type" : "dir"
142 })"
143 };
144
145 const PositionalArgument OptionalStats
146 {
147 R"({
148 "name" : "stats",
149 "description" : "Write stats to file, rather than to stdout.",
150 "type" : "FILE",
151 "required" : false
152 })"
153 };
154
155 } // namespace Options
156 } // namespace CLI_v2_InterfaceHelpPrinterTests
157
158 TEST(CLI2_InterfaceHelpPrinter, formats_usage)
159 {
160 const std::string expectedText{"Usage:\n frobber [options]"};
161
162 const Interface i{"frobber"};
163
164 InterfaceHelpPrinter help{i, HiddenOptionMode::HIDE};
165 const auto formattedText = help.Usage();
166 EXPECT_EQ(expectedText, formattedText);
167 }
168
169 TEST(CLI2_InterfaceHelpPrinter, formats_usage_with_pos_args)
170 {
171 const std::string expectedText{"Usage:\n frobber [options] <source> <dest> [stats]"};
172
173 Interface i{"frobber"};
174 i.AddPositionalArguments({
175 CLI_v2_InterfaceHelpPrinterTests::Options::Source,
176 CLI_v2_InterfaceHelpPrinterTests::Options::Dest,
177 CLI_v2_InterfaceHelpPrinterTests::Options::OptionalStats
178 });
179
180 InterfaceHelpPrinter help{i, HiddenOptionMode::HIDE};
181 const auto formattedText = help.Usage();
182 EXPECT_EQ(expectedText, formattedText);
183 }
184
185 TEST(CLI2_InterfaceHelpPrinter, usage_with_optional_pos_args_always_places_them_after_required_args)
186 {
187 const std::string expectedText{"Usage:\n frobber [options] <source> <dest> [stats]"};
188
189 Interface i{"frobber"};
190 i.AddPositionalArguments({
191 CLI_v2_InterfaceHelpPrinterTests::Options::Source,
192 CLI_v2_InterfaceHelpPrinterTests::Options::OptionalStats,
193 CLI_v2_InterfaceHelpPrinterTests::Options::Dest
194 });
195
196 InterfaceHelpPrinter help{i, HiddenOptionMode::HIDE};
197 const auto formattedText = help.Usage();
198 EXPECT_EQ(expectedText, formattedText);
199 }
200
201 TEST(CLI2_InterfaceHelpPrinter, formats_description)
202 {
203 const std::string expectedText{"some description"};
204
205 Interface i{"frobber", "some description"};
206
207 InterfaceHelpPrinter help{i, HiddenOptionMode::HIDE};
208 const auto formattedText = help.Description();
209 EXPECT_EQ(expectedText, formattedText);
210 }
211
212 TEST(CLI2_InterfaceHelpPrinter, formats_empty_description)
213 {
214 const std::string expectedText;
215
216 Interface i{"frobber", ""};
217
218 InterfaceHelpPrinter help{i, HiddenOptionMode::HIDE};
219 const auto formattedText = help.Description();
220 EXPECT_EQ(expectedText, formattedText);
221 }
222
223 TEST(CLI2_InterfaceHelpPrinter, no_formatted_option_names_for_hidden_option)
224 {
225 OptionData option;
226 option.IsHidden = true;
227
228 const auto formattedText = PacBio::CLI_v2::internal::HelpMetrics::OptionNames(option);
229 EXPECT_TRUE(formattedText.empty());
230 }
231
232 TEST(CLI2_InterfaceHelpPrinter, can_calculate_metrics_from_builtins_only)
233 {
234 Interface i{"frobber"};
235 InterfaceHelpPrinter help{i, HiddenOptionMode::HIDE};
236
237 const auto& metrics = help.Metrics();
238
239 const std::string longestTest{"-j,--num-threads"};
240 EXPECT_EQ(longestTest.size(), metrics.MaxNameLength);
241 }
242
243 TEST(CLI2_InterfaceHelpPrinter, can_calculate_metrics_with_long_option)
244 {
245 const Option testOption{
246 R"({
247 "names" : ["super-long-option-name-that-is-longer-than-loglevel"],
248 "description" : "foo"
249 })"};
250
251 const std::string longestText{
252 "--super-long-option-name-that-is-longer-than-loglevel",
253 };
254
255 const std::string helpText{"-h,--help"};
256 const std::string logLevelText{"--log-level"};
257 const std::string versionText{"--version"};
258
259 Interface i{"frobber"};
260 i.AddOption(testOption);
261
262 InterfaceHelpPrinter help{i, HiddenOptionMode::HIDE};
263 const auto& metrics = help.Metrics();
264 EXPECT_EQ(longestText.size(), metrics.MaxNameLength); // include spacer
265
266 const auto testOptionData = OptionTranslator::Translate(testOption);
267 const auto& helpOption = i.HelpOption();
268 const auto& logLevelOption = *i.LogLevelOption();
269 const auto& versionOption = i.VersionOption();
270 EXPECT_EQ(longestText, metrics.FormattedOptionNames.at(testOptionData).nameString);
271 EXPECT_EQ(helpText, metrics.FormattedOptionNames.at(helpOption).nameString);
272 EXPECT_EQ(logLevelText, metrics.FormattedOptionNames.at(logLevelOption).nameString);
273 EXPECT_EQ(versionText, metrics.FormattedOptionNames.at(versionOption).nameString);
274 }
275
276 TEST(CLI2_InterfaceHelpPrinter, formats_int_choices)
277 {
278 const std::string expectedText{"0, 5, 10, 15"};
279
280 OptionValue choice1 = 0;
281 OptionValue choice2 = 5;
282 OptionValue choice3 = 10;
283 OptionValue choice4 = 15;
284
285 OptionData option;
286 option.Type = OptionValueType::INT;
287 option.Choices = {choice1, choice2, choice3, choice4};
288
289 Interface i{"frobber"};
290 InterfaceHelpPrinter help{i, HiddenOptionMode::HIDE};
291
292 const auto formattedText = help.Choices(option);
293 EXPECT_EQ(expectedText, formattedText);
294 }
295
296 TEST(CLI2_InterfaceHelpPrinter, formats_float_choices)
297 {
298 const std::string expectedText{"0.1, 0.5, 1.05, 1.5"};
299
300 OptionValue choice1 = 0.1f;
301 OptionValue choice2 = 0.5f;
302 OptionValue choice3 = 1.05f;
303 OptionValue choice4 = 1.5f;
304
305 OptionData option;
306 option.Type = OptionValueType::FLOAT;
307 option.Choices = {choice1, choice2, choice3, choice4};
308
309 Interface i{"frobber"};
310 InterfaceHelpPrinter help{i, HiddenOptionMode::HIDE};
311
312 const auto formattedText = help.Choices(option);
313 EXPECT_EQ(expectedText, formattedText);
314 }
315
316 TEST(CLI2_InterfaceHelpPrinter, formats_string_choices)
317 {
318 const std::string expectedText{"fire, water, air, earth"};
319
320 OptionValue choice1 = std::string{"fire"};
321 OptionValue choice2 = std::string{"water"};
322 OptionValue choice3 = std::string{"air"};
323 OptionValue choice4 = std::string{"earth"};
324
325 OptionData option;
326 option.Type = OptionValueType::STRING;
327 option.Choices = {choice1, choice2, choice3, choice4};
328
329 Interface i{"frobber"};
330 InterfaceHelpPrinter help{i, HiddenOptionMode::HIDE};
331
332 const auto formattedText = help.Choices(option);
333 EXPECT_EQ(expectedText, formattedText);
334 }
335
336 TEST(CLI2_InterfaceHelpPrinter, should_not_show_default_value_for_boolean)
337 {
338 OptionData option;
339 Interface i{"frobber"};
340 InterfaceHelpPrinter help{i, HiddenOptionMode::HIDE};
341 EXPECT_FALSE(help.ShouldShowDefaultValue(option));
342 }
343
344 TEST(CLI2_InterfaceHelpPrinter, should_not_show_default_value_for_empty_string)
345 {
346 OptionData option;
347 option.Type = OptionValueType::STRING;
348 option.DefaultValue = std::string{};
349 Interface i{"frobber"};
350 InterfaceHelpPrinter help{i, HiddenOptionMode::HIDE};
351 EXPECT_FALSE(help.ShouldShowDefaultValue(option));
352 }
353
354 TEST(CLI2_InterfaceHelpPrinter, should_show_default_value_for_string)
355 {
356 OptionData option;
357 option.Type = OptionValueType::STRING;
358 option.DefaultValue = std::string{"foo"};
359 Interface i{"frobber"};
360 InterfaceHelpPrinter help{i, HiddenOptionMode::HIDE};
361 EXPECT_TRUE(help.ShouldShowDefaultValue(option));
362 }
363
364 TEST(CLI2_InterfaceHelpPrinter, should_show_default_value_for_number)
365 {
366 OptionData option;
367 option.Type = OptionValueType::INT;
368 option.DefaultValue = 42;
369 Interface i{"frobber"};
370 InterfaceHelpPrinter help{i, HiddenOptionMode::HIDE};
371 EXPECT_TRUE(help.ShouldShowDefaultValue(option));
372 }
373
374 TEST(CLI2_InterfaceHelpPrinter, should_not_show_default_value_for_number_with_no_default)
375 {
376 OptionData option;
377 option.Type = OptionValueType::INT;
378 Interface i{"frobber"};
379 InterfaceHelpPrinter help{i, HiddenOptionMode::HIDE};
380 EXPECT_FALSE(help.ShouldShowDefaultValue(option));
381 }
382
383 TEST(CLI2_InterfaceHelpPrinter, formats_default_value)
384 {
385 OptionData option;
386 option.Type = OptionValueType::INT;
387 option.DefaultValue = 42;
388
389 const std::string expectedText{"42"};
390
391 Interface i{"frobber"};
392 InterfaceHelpPrinter help{i, HiddenOptionMode::HIDE};
393 const auto defaultValue = help.DefaultValue(option);
394 EXPECT_EQ(expectedText, defaultValue);
395 }
396
397 TEST(CLI2_InterfaceHelpPrinter, formats_option_with_wordwrapped_description)
398 {
399 // 80
400 // v
401 const std::string expectedText{
402 " -x Lorem ipsum dolor sit amet, consectetur adipiscing\n"
403 " elit, sed do eiusmod tempor incididunt ut labore et\n"
404 " dolore magna aliqua."};
405 /*
406 " -j,--num-threads INT Number of threads to use, 0 means autodetection. [0] <-- initial spacing to description based on this built-in
407 */
408 const Option optionWithLongDescription{
409 R"({
410 "names" : ["x"],
411 "description" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
412 })"};
413
414 const auto option = OptionTranslator::Translate(optionWithLongDescription);
415
416 Interface i{"frobber"};
417 i.AddOption(optionWithLongDescription);
418
419 InterfaceHelpPrinter help{i, 80, HiddenOptionMode::HIDE};
420 const auto formattedText = help.Option(option);
421 EXPECT_EQ(expectedText, formattedText);
422 }
423
424 TEST(CLI2_InterfaceHelpPrinter, formats_empty_option_group)
425 {
426 const std::string expectedText;
427
428 Interface i{"frobber"};
429 InterfaceHelpPrinter help{i, 80, HiddenOptionMode::HIDE};
430 const auto formattedText = help.OptionGroup(OptionGroupData{});
431 EXPECT_EQ(expectedText, formattedText);
432 }
433
434 TEST(CLI2_InterfaceHelpPrinter, formats_builtin_option_group)
435 {
436 const std::string expectedText{
437 "Options:\n"
438 " -h,--help Show this help and exit.\n"
439 " --version Show application version and exit.\n"
440 " -j,--num-threads INT Number of threads to use, 0 means autodetection. [0]\n"
441 " --log-level STR Set log level. Valid choices: (TRACE, DEBUG, INFO,\n"
442 " WARN, FATAL). [WARN]\n"
443 " --log-file FILE Log to a file, instead of stderr.\n"};
444
445 Interface i{"frobber"};
446 InterfaceHelpPrinter help{i, 80, HiddenOptionMode::HIDE};
447 const auto formattedText = help.Options();
448 EXPECT_EQ(expectedText, formattedText);
449 }
450
451 TEST(CLI2_InterfaceHelpPrinter, prints_log_level_override)
452 {
453 const std::string expectedText{
454 "Options:\n"
455 " -h,--help Show this help and exit.\n"
456 " --version Show application version and exit.\n"
457 " -j,--num-threads INT Number of threads to use, 0 means autodetection. [0]\n"
458 " --log-level STR Set log level. Valid choices: (TRACE, DEBUG, INFO,\n"
459 " WARN, FATAL). [DEBUG]\n"
460 " --log-file FILE Log to a file, instead of stderr.\n"};
461
462 Interface i{"frobber"};
463 i.DefaultLogLevel(PacBio::Logging::LogLevel::DEBUG);
464 InterfaceHelpPrinter help{i, 80, HiddenOptionMode::HIDE};
465 const auto formattedText = help.Options();
466 EXPECT_EQ(expectedText, formattedText);
467 }
468
469 TEST(CLI2_InterfaceHelpPrinter, formats_option_group)
470 {
471 // 80
472 // v
473 const std::string expectedText{
474 "Test Group:\n"
475 " -f,--force Overwrite things.\n"
476 " -p Show progress during copy.\n"
477 " -n,--no-op Dry run. Report actions that would be taken but do not\n"
478 " perform them.\n"
479 /*
480 " -j,--num-threads INT Number of threads to use, 0 means autodetection. [0] <-- initial spacing to description based on this built-in
481 */
482 };
483
484 const std::vector<Option> options{
485 CLI_v2_InterfaceHelpPrinterTests::Options::Force,
486 CLI_v2_InterfaceHelpPrinterTests::Options::Progress,
487 CLI_v2_InterfaceHelpPrinterTests::Options::DryRun};
488
489 auto optionsData = OptionTranslator::Translate(options);
490
491 Interface i{"frobber"};
492 i.AddOptionGroup("Test Group", options);
493 InterfaceHelpPrinter help{i, 80, HiddenOptionMode::HIDE};
494 const auto formattedText = help.OptionGroup({"Test Group", optionsData});
495 EXPECT_EQ(expectedText, formattedText);
496 }
497
498 TEST(CLI2_InterfaceHelpPrinter, formats_positional_argument)
499 {
500 const std::string expectedText{
501 " source FILE Source file to copy."
502 };
503 /*
504 " -j,--num-threads INT Number of threads to use, 0 means autodetection. [0] <-- initial spacing to type based on this built-in
505 */
506
507 Interface i{"frobber"};
508 i.AddPositionalArgument(CLI_v2_InterfaceHelpPrinterTests::Options::Source);
509
510 const auto posArgData = PositionalArgumentTranslator::Translate(CLI_v2_InterfaceHelpPrinterTests::Options::Source);
511
512 InterfaceHelpPrinter help{i, 80, HiddenOptionMode::HIDE};
513 const auto formattedText = help.PositionalArgument(posArgData);
514 EXPECT_EQ(expectedText, formattedText);
515 }
516
517 TEST(CLI2_InterfaceHelpPrinter, formats_positional_argument_with_wordwrapped_description)
518 {
519 // 80
520 // v
521 const std::string expectedText{
522 " source FILE Source file to copy.\n"
523 " dest DIR Destination directory. Essentially where we want to\n"
524 " drop things, but really just making a long\n"
525 " description.\n"
526 };
527 /*
528 " -j,--num-threads INT Number of threads to use, 0 means autodetection. [0] <-- initial spacing to description based on this built-in
529 */
530
531 Interface i{"frobber"};
532 i.AddPositionalArguments({
533 CLI_v2_InterfaceHelpPrinterTests::Options::Source,
534 CLI_v2_InterfaceHelpPrinterTests::Options::Dest
535 });
536
537 const auto posArgData = PositionalArgumentTranslator::Translate(CLI_v2_InterfaceHelpPrinterTests::Options::Source);
538
539 InterfaceHelpPrinter help{i, 80, HiddenOptionMode::HIDE};
540 const auto formattedText = help.PositionalArguments();
541 EXPECT_EQ(expectedText, formattedText);
542 }
543
544 TEST(CLI2_InterfaceHelpPrinter, prints_expected_full_interface_help)
545 {
546 const std::string expectedText{R"(frobber - Frobb your files in a most delightful, nobbly way
547
548 Usage:
549 frobber [options] <source> <dest> [stats]
550
551 source FILE Source file to copy.
552 dest DIR Destination directory. Essentially where we want to
553 drop things, but really just making a long
554 description.
555 stats FILE Write stats to file, rather than to stdout.
556
557 Output Options:
558 -p Show progress during copy.
559 -f,--force Overwrite things.
560 -t,--target-dir DIR Copy all source files into <DIR>. [my/default/dir]
561 -n,--no-op Dry run. Report actions that would be taken but do
562 not perform them.
563 --timeout INT Abort execution after <INT> milliseconds. [5000]
564 --seed INT Seed for random-number generator.
565
566 Algorithm Options:
567 --delta FLOAT Some delta for things. [0.01]
568 -e,--element STR Choice of element indicates mood. Science. Valid
569 choices: (earth, wind, fire, water). [fire]
570 --ploidy STR Genome ploidy. Valid choices: (haploid, diploid).
571 [haploid]
572
573 -h,--help Show this help and exit.
574 --version Show application version and exit.
575 -j,--num-threads INT Number of threads to use, 0 means autodetection. [0]
576 --log-level STR Set log level. Valid choices: (TRACE, DEBUG, INFO,
577 WARN, FATAL). [WARN]
578 --log-file FILE Log to a file, instead of stderr.
579
580 )"
581 PB_BOILERPLATE_DISCLAIMER
582 };
583
584 Interface i {
585 "frobber",
586 "Frobb your files in a most delightful, nobbly way",
587 "3.14"
588 };
589
590 i.AddOptionGroup("Output Options", {
591 CLI_v2_InterfaceHelpPrinterTests::Options::Progress,
592 CLI_v2_InterfaceHelpPrinterTests::Options::Force,
593 CLI_v2_InterfaceHelpPrinterTests::Options::TargetDirectory,
594 CLI_v2_InterfaceHelpPrinterTests::Options::DryRun,
595 CLI_v2_InterfaceHelpPrinterTests::Options::Timeout,
596 CLI_v2_InterfaceHelpPrinterTests::Options::Seed,
597 });
598 i.AddOptionGroup("Algorithm Options",{
599 CLI_v2_InterfaceHelpPrinterTests::Options::Delta,
600 CLI_v2_InterfaceHelpPrinterTests::Options::Element,
601 CLI_v2_InterfaceHelpPrinterTests::Options::Ploidy
602 });
603
604 i.AddPositionalArguments({
605 CLI_v2_InterfaceHelpPrinterTests::Options::Source,
606 CLI_v2_InterfaceHelpPrinterTests::Options::Dest,
607 CLI_v2_InterfaceHelpPrinterTests::Options::OptionalStats
608 });
609
610 const InterfaceHelpPrinter help{i, 80, HiddenOptionMode::HIDE};
611 std::ostringstream out;
612 help.Print(out);
613 EXPECT_EQ(expectedText, out.str());
614
615 // reset ostream and check operator<<
616 out.str("");
617 out << help;
618 EXPECT_EQ(expectedText, out.str());
619 }
620
621 TEST(CLI2_InterfaceHelpPrinter, word_wraps_application_description)
622 {
623 // 80
624 // v
625 const std::string expectedText {
626 "frobber - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do\n"
627 " eiusmod tempor incididunt ut labore et dolore magna aliqua.\n"
628 "\n"
629 "Usage:\n"
630 " frobber [options]\n"
631 "\n"
632 "Options:\n"
633 " -h,--help Show this help and exit.\n"
634 " --version Show application version and exit.\n"
635 " -j,--num-threads INT Number of threads to use, 0 means autodetection. [0]\n"
636 " --log-level STR Set log level. Valid choices: (TRACE, DEBUG, INFO,\n"
637 " WARN, FATAL). [WARN]\n"
638 " --log-file FILE Log to a file, instead of stderr.\n"
639 "\n"
640 PB_BOILERPLATE_DISCLAIMER
641 };
642
643 Interface i {
644 "frobber",
645 "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
646 "3.14"
647 };
648 InterfaceHelpPrinter help{i, 80, HiddenOptionMode::HIDE};
649 std::ostringstream out;
650 out << help;
651 EXPECT_EQ(expectedText, out.str());
652 }
653
654 TEST(CLI2_InterfaceHelpPrinter, displays_footer_text)
655 {
656 const std::string expectedText {R"(frobber - Frobb your files in a most delightful, nobbly way.
657
658 Usage:
659 frobber [options]
660
661 Options:
662 -h,--help Show this help and exit.
663 --version Show application version and exit.
664 -j,--num-threads INT Number of threads to use, 0 means autodetection. [0]
665 --log-level STR Set log level. Valid choices: (TRACE, DEBUG, INFO,
666 WARN, FATAL). [WARN]
667 --log-file FILE Log to a file, instead of stderr.
668
669 Typical workflow:
670 1. Do a thing to the thing.
671 $ frobber
672
673 2. Do the frobbing again, because why not?
674 $ frobber
675
676 3. Lather, rinse, repeat.
677
678 So long, and thanks for all the fish.
679
680 )"
681 PB_BOILERPLATE_DISCLAIMER
682 };
683
684 Interface i {
685 "frobber",
686 "Frobb your files in a most delightful, nobbly way.",
687 "3.14"
688 };
689 i.HelpFooter(R"(Typical workflow:
690 1. Do a thing to the thing.
691 $ frobber
692
693 2. Do the frobbing again, because why not?
694 $ frobber
695
696 3. Lather, rinse, repeat.
697
698 So long, and thanks for all the fish.)");
699
700 InterfaceHelpPrinter help{i, 80, HiddenOptionMode::HIDE};
701 std::ostringstream out;
702 out << help;
703 EXPECT_EQ(expectedText, out.str());
704 }
705
706 TEST(CLI2_InterfaceHelpPrinter, can_disable_builtins)
707 {
708 const std::string expectedText{
709 "Options:\n"
710 " -h,--help Show this help and exit.\n"
711 " --version Show application version and exit.\n"};
712
713 Interface i{"frobber"};
714 i.DisableLogFileOption()
715 .DisableLogLevelOption()
716 .DisableNumThreadsOption();
717
718 InterfaceHelpPrinter help{i, 80, HiddenOptionMode::HIDE};
719 const auto formattedText = help.Options();
720 EXPECT_EQ(expectedText, formattedText);
721 }
722
723 TEST(CLI2_InterfaceHelpPrinter, can_enable_verbose_option)
724 {
725 const std::string expectedText{
726 "Options:\n"
727 " -h,--help Show this help and exit.\n"
728 " --version Show application version and exit.\n"
729 " -j,--num-threads INT Number of threads to use, 0 means autodetection. [0]\n"
730 " --log-level STR Set log level. Valid choices: (TRACE, DEBUG, INFO,\n"
731 " WARN, FATAL). [WARN]\n"
732 " --log-file FILE Log to a file, instead of stderr.\n"
733 " -v,--verbose Use verbose output.\n"};
734
735 Interface i{"frobber"};
736 i.EnableVerboseOption();
737
738 InterfaceHelpPrinter help{i, 80, HiddenOptionMode::HIDE};
739 const auto formattedText = help.Options();
740 EXPECT_EQ(expectedText, formattedText);
741 }
742
743 TEST(CLI2_InterfaceHelpPrinter, can_handle_hidden_options)
744 {
745 const Option triforceOption {
746 R"({
747 "names" : ["t", "triforce"],
748 "description" : "Choose wisely.",
749 "type" : "string",
750 "choices" : ["power", "wisdom", "courage"],
751 "choices.hidden" : true,
752 "default" : "power"
753 })"};
754
755 const Option doomOption {
756 R"({
757 "names" : ["iddqd"],
758 "description" : "You know.",
759 "hidden" : true
760 })"};
761
762 const std::string hiddenAlarms{"--alarms"};
763 const std::string hiddenExceptionsPassthrough{"--allow-exceptions-passthrough"};
764 const std::string hiddenShowAllHelp{"--show-all-help"};
765 // stopping short for choice list text, in case of wrap-around
766 const std::string hiddenTriforceChoices{"Choose wisely. Valid choices: ("};
767 const std::string hiddenDoom{"--iddqd"};
768
769 Interface i{"frobber"};
770 i.AddOptionGroup("Test Group", {triforceOption, doomOption});
771
772 // ensure hidden
773 {
774 InterfaceHelpPrinter help{i, 80, HiddenOptionMode::HIDE};
775 std::ostringstream helpOutput;
776 help.Print(helpOutput);
777 const std::string helpText = helpOutput.str();
778
779 EXPECT_EQ(std::string::npos, helpText.find(hiddenAlarms));
780 EXPECT_EQ(std::string::npos, helpText.find(hiddenExceptionsPassthrough));
781 EXPECT_EQ(std::string::npos, helpText.find(hiddenShowAllHelp));
782 EXPECT_EQ(std::string::npos, helpText.find(hiddenTriforceChoices));
783 EXPECT_EQ(std::string::npos, helpText.find(hiddenDoom));
784 }
785
786 // expose via --show-all-help
787 {
788 InterfaceHelpPrinter help{i, 80, HiddenOptionMode::SHOW};
789 std::ostringstream helpOutput;
790 help.Print(helpOutput);
791 const std::string helpText = helpOutput.str();
792
793 EXPECT_NE(std::string::npos, helpText.find(hiddenAlarms));
794 EXPECT_NE(std::string::npos, helpText.find(hiddenExceptionsPassthrough));
795 EXPECT_NE(std::string::npos, helpText.find(hiddenShowAllHelp));
796 EXPECT_NE(std::string::npos, helpText.find(hiddenTriforceChoices));
797 EXPECT_NE(std::string::npos, helpText.find(hiddenDoom));
798 }
799 }
800
801 // clang-format on