Debian Codegraph @

src/cli2/MultiToolInterfaceHelpPrinter.cpp

raw
1 #include <pbcopper/cli2/internal/MultiToolInterfaceHelpPrinter.h>
2
3 #include <pbcopper/cli2/internal/OptionData.h>
4 #include "PbBoilerplateDisclaimer.h"
5
6 #include <iomanip>
7 #include <ios>
8 #include <ostream>
9 #include <sstream>
10
11 namespace PacBio {
12 namespace CLI_v2 {
13 namespace internal {
14
15 MultiToolInterfaceHelpPrinter::MultiToolInterfaceHelpPrinter(MultiToolInterface multiToolInterface,
16 HiddenOptionMode hiddenOptionMode)
17 : metrics_{multiToolInterface, hiddenOptionMode}
18 , interface_{std::move(multiToolInterface)}
19 , showHiddenOptions_{hiddenOptionMode == HiddenOptionMode::SHOW}
20 {
21 MakeHelpText();
22 }
23
24 MultiToolInterfaceHelpPrinter::MultiToolInterfaceHelpPrinter(MultiToolInterface multiToolInterface,
25 const size_t maxColumn,
26 HiddenOptionMode hiddenOptionMode)
27 : metrics_{multiToolInterface, maxColumn, hiddenOptionMode}
28 , interface_{std::move(multiToolInterface)}
29 , showHiddenOptions_{hiddenOptionMode == HiddenOptionMode::SHOW}
30 {
31 MakeHelpText();
32 }
33
34 void MultiToolInterfaceHelpPrinter::MakeHelpText()
35 {
36 std::ostringstream result;
37 result << interface_.ApplicationName();
38
39 const std::string description = interface_.ApplicationDescription();
40 if (!description.empty()) {
41 result << " - " << description;
42 }
43 result << '\n';
44
45 result << '\n'
46 << "Usage:\n"
47 << " " << interface_.ApplicationName() << " <tool>\n"
48 << '\n';
49
50 result << metrics_.HelpEntry(interface_.HelpOption()) << '\n'
51 << metrics_.HelpEntry(interface_.VersionOption()) << "\n\n";
52
53 bool hasExamples = false;
54 result << "Tools:\n";
55
56 const auto& tools = interface_.Tools();
57 for (const auto& tool : tools) {
58 if ((tool.visibility == ToolVisibility::HIDDEN) && !showHiddenOptions_) {
59 continue;
60 }
61 const auto& i = tool.interface;
62 result << " " << std::setw(10) << std::left << tool.name << " "
63 << i.ApplicationDescription() << '\n';
64 if (!tool.interface.Example().empty()) {
65 hasExamples = true;
66 }
67 }
68 result << '\n';
69
70 if (hasExamples) {
71 result << "Examples:\n";
72 for (const auto& tool : tools) {
73 if (tool.visibility == ToolVisibility::HIDDEN) {
74 continue;
75 }
76 const auto& example = tool.interface.Example();
77 if (!example.empty()) {
78 result << " " << example << '\n';
79 }
80 }
81 result << '\n';
82 }
83
84 const auto& footer = interface_.HelpFooter();
85 if (!footer.empty()) {
86 result << footer << '\n';
87
88 // Some application footers contain trailing newlines, some do not.
89 // Ensure we have at least one empty line between footer and disclaimer.
90 if (footer.back() != '\n') {
91 result << '\n';
92 }
93 }
94
95 result << PB_BOILERPLATE_DISCLAIMER;
96
97 text_ = result.str();
98 }
99
100 void MultiToolInterfaceHelpPrinter::Print(std::ostream& out) const { out << text_; }
101
102 std::ostream& operator<<(std::ostream& out, const MultiToolInterfaceHelpPrinter& printer)
103 {
104 printer.Print(out);
105 return out;
106 }
107
108 } // namespace internal
109 } // namespace CLI_v2
110 } // namespace PacBio