Debian Codegraph @

src/cli2/MultiToolInterface.cpp

raw
1 #include <pbcopper/cli2/MultiToolInterface.h>
2
3 #include <pbcopper/cli2/internal/BuiltinOptions.h>
4 #include <pbcopper/cli2/internal/InterfaceData.h>
5 #include <pbcopper/cli2/internal/OptionTranslator.h>
6
7 #include <type_traits>
8 #include <utility>
9
10 #include <cassert>
11
12 using OptionTranslator = PacBio::CLI_v2::internal::OptionTranslator;
13
14 namespace PacBio {
15 namespace CLI_v2 {
16
17 MultiToolInterface::MultiToolInterface(std::string name, std::string description,
18 std::string version)
19 : data_{std::move(name),
20 std::move(description),
21 std::move(version),
22 OptionTranslator::Translate(Builtin::Help),
23 OptionTranslator::Translate(Builtin::Version),
24 OptionTranslator::Translate(Builtin::ShowAllHelp)}
25 {}
26
27 MultiToolInterface& MultiToolInterface::AddTool(Tool tool)
28 {
29 // ensure new subtools use the multi-tool's config
30 if (data_.LogConfig) {
31 tool.interface.LogConfig(*data_.LogConfig);
32 }
33 data_.Tools.emplace_back(std::move(tool));
34 return *this;
35 }
36
37 MultiToolInterface& MultiToolInterface::AddTools(std::vector<Tool> tools)
38 {
39 for (auto&& tool : tools) {
40 AddTool(std::move(tool));
41 }
42 return *this;
43 }
44
45 const std::string& MultiToolInterface::ApplicationDescription() const
46 {
47 return data_.AppDescription;
48 }
49
50 const std::string& MultiToolInterface::ApplicationName() const { return data_.AppName; }
51
52 const std::string& MultiToolInterface::ApplicationVersion() const { return data_.AppVersion; }
53
54 bool MultiToolInterface::HasTool(const std::string& toolName) const
55 {
56 for (const auto& tool : data_.Tools) {
57 if (tool.name == toolName) {
58 return true;
59 }
60 }
61 return false;
62 }
63
64 const std::string& MultiToolInterface::HelpFooter() const { return data_.HelpFooter; }
65
66 MultiToolInterface& MultiToolInterface::HelpFooter(std::string footer)
67 {
68 data_.HelpFooter = std::move(footer);
69 return *this;
70 }
71
72 const std::optional<Logging::LogConfig>& MultiToolInterface::LogConfig() const
73 {
74 return data_.LogConfig;
75 }
76
77 MultiToolInterface& MultiToolInterface::LogConfig(Logging::LogConfig config)
78 {
79 // ensure any current subtools use the new multi-tool config
80 data_.LogConfig = config;
81 for (auto& tool : data_.Tools) {
82 tool.interface.LogConfig(config);
83 }
84 return *this;
85 }
86
87 const internal::OptionData& MultiToolInterface::HelpOption() const { return data_.HelpOption; }
88
89 void MultiToolInterface::PrintVersion() const { data_.VersionPrinter(*this); }
90
91 MultiToolInterface& MultiToolInterface::RegisterVersionPrinter(
92 MultiToolVersionPrinterCallback printer)
93 {
94 data_.VersionPrinter = printer;
95 return *this;
96 }
97
98 const internal::OptionData& MultiToolInterface::ShowAllHelpOption() const
99 {
100 return data_.ShowAllHelpOption;
101 }
102
103 const Tool& MultiToolInterface::ToolFor(const std::string& toolName) const
104 {
105 for (const auto& tool : data_.Tools) {
106 if (tool.name == toolName) {
107 return tool;
108 }
109 }
110 throw std::runtime_error{"[pbcopper] command line ERROR: unknown tool '" + toolName +
111 "' requested"};
112 }
113
114 const std::vector<Tool>& MultiToolInterface::Tools() const { return data_.Tools; }
115
116 const internal::OptionData& MultiToolInterface::VersionOption() const
117 {
118 return data_.VersionOption;
119 }
120
121 } // namespace CLI_v2
122 } // namespace PacBio