Debian Codegraph @

src/cli2/Results.cpp

raw
1 #include <pbcopper/cli2/Results.h>
2
3 #include <pbcopper/cli2/internal/BuiltinOptions.h>
4 #include <pbcopper/cli2/internal/OptionTranslator.h>
5 #include <pbcopper/cli2/internal/PositionalArgumentTranslator.h>
6 #include <pbcopper/parallel/ThreadCount.h>
7 #include <pbcopper/utility/StringUtils.h>
8
9 #include <algorithm>
10 #include <sstream>
11 #include <stdexcept>
12
13 namespace PacBio {
14 namespace CLI_v2 {
15
16 Results& Results::AddPositionalArgument(std::string arg)
17 {
18 posArgValues_.emplace_back(std::move(arg));
19 return *this;
20 }
21
22 Results& Results::AddDefaultOption(const internal::OptionData& opt)
23 {
24 // make default value
25 auto result = std::make_shared<Result>(*opt.DefaultValue, SetByMode::DEFAULT);
26
27 // add for all option names
28 for (const auto& name : opt.Names) {
29 results_.insert({name, result});
30 }
31 for (const auto& hiddenName : opt.HiddenNames) {
32 results_.insert({hiddenName, result});
33 }
34 return *this;
35 }
36
37 Results& Results::AddObservedFlag(const std::string& name, const SetByMode setBy)
38 {
39 AddObservedValue(name, OptionValue{true}, setBy);
40 return *this;
41 }
42
43 Results& Results::AddObservedValue(const std::string& name, OptionValue value,
44 const SetByMode setBy)
45 {
46 const auto found = results_.find(name);
47 if (found == results_.cend()) {
48 auto result = std::make_shared<Result>(value, setBy);
49 results_.insert({name, std::move(result)});
50 } else {
51 *found->second = Result{value, setBy};
52 }
53
54 return *this;
55 }
56
57 std::string Results::EffectiveCommandLine() const
58 {
59 std::ostringstream out;
60
61 // application name
62 const auto tokens = PacBio::Utility::Split(inputCommandLine_, ' ');
63 const auto& appName = tokens.front();
64 out << appName;
65
66 // Options can have multiple names ("h", "help"). Limit to unique
67 // result entries, keeping the longest name.
68 std::unordered_map<std::shared_ptr<Result>, std::string> values;
69 for (const auto& entry : results_) {
70 const auto& name = entry.first;
71 const auto& value = entry.second;
72
73 auto found = values.find(value);
74 if (found == values.cend()) {
75 values.insert({value, name});
76 } else {
77 if (found->second.length() < name.length()) {
78 found->second = name;
79 }
80 }
81 }
82
83 // Option parameter output
84 for (const auto& entry : values) {
85 const auto* result = entry.first.get();
86 const auto& name = entry.second;
87 const auto typeIndex = result->which();
88
89 const bool shortOption = (name.size() == 1);
90 const std::string dashStyle = (shortOption ? " -" : " --");
91
92 // print enabled switches
93 if (typeIndex == 9) {
94 const bool isSet = *result;
95 if (isSet) {
96 out << dashStyle << name;
97 }
98 }
99
100 // print non-empty string parameters
101 else if (typeIndex == 10) {
102 const std::string stringValue = *result;
103 if (!stringValue.empty()) {
104 out << dashStyle << name << '=' << stringValue;
105 }
106 }
107
108 // for all numeric types
109 else {
110 out << dashStyle << name << '=' << *result;
111 }
112 }
113
114 // Positional arg output
115 for (const auto& posArg : posArgValues_) {
116 out << ' ' << posArg;
117 }
118
119 return out.str();
120 }
121
122 const std::string& Results::InputCommandLine() const { return inputCommandLine_; }
123
124 Results& Results::InputCommandLine(std::string cmdLine)
125 {
126 inputCommandLine_ = std::move(cmdLine);
127 return *this;
128 }
129
130 std::string Results::LogFile() const
131 {
132 const auto& logFileOpt = (*this)[Builtin::LogFile];
133 const std::string logFileStr = logFileOpt;
134 return logFileStr;
135 }
136
137 PacBio::Logging::LogLevel Results::LogLevel() const
138 {
139 const auto& logLevelOpt = (*this)[Builtin::LogLevel];
140 const std::string logLevelStr = logLevelOpt;
141 return PacBio::Logging::LogLevel(logLevelStr);
142 }
143
144 size_t Results::NumThreads() const
145 {
146 const unsigned int requestedNumThreads = (*this)[Builtin::NumThreads];
147 return static_cast<size_t>(Parallel::NormalizedThreadCount(requestedNumThreads));
148 }
149
150 std::filesystem::path Results::AlarmsFile() const
151 {
152 return static_cast<std::string>((*this)[Builtin::Alarms]);
153 }
154
155 const std::vector<std::string>& Results::PositionalArguments() const { return posArgValues_; }
156
157 Results& Results::PositionalArguments(const std::vector<internal::PositionalArgumentData>& posArgs)
158 {
159 for (const auto& posArg : posArgs) {
160 posArgNames_.push_back(posArg.Name);
161 }
162 return *this;
163 }
164
165 bool Results::Verbose() const
166 {
167 const auto found = results_.find("verbose");
168 if (found != results_.cend()) {
169 return *(found->second.get());
170 }
171 return false; // verbose option not enabled
172 }
173
174 bool Results::ExceptionPassthrough() const
175 {
176 const auto found = results_.find("allow-exceptions-passthrough");
177 if (found != results_.cend()) {
178 return *(found->second.get());
179 }
180 return false; // allow-exceptions-passthrough option not enabled
181 }
182
183 bool Results::ShowAllHelp() const
184 {
185 const auto found = results_.find("show-all-help");
186 if (found != std::cend(results_)) {
187 return *(found->second.get());
188 }
189 return false; // show-all-help option not enabled
190 }
191
192 const Result& Results::operator[](const Option& opt) const
193 {
194 const auto optionNames = internal::OptionTranslator::OptionNames(opt);
195 for (const auto& name : optionNames) {
196 const auto found = results_.find(name);
197 if (found != results_.cend()) {
198 return *(found->second.get());
199 }
200 }
201
202 // not found
203 std::ostringstream out;
204 out << "[pbcopper] command line results ERROR: unknown option, with name(s):\n";
205 for (const auto& name : optionNames) {
206 out << " " << name << '\n';
207 }
208 throw std::invalid_argument{out.str()};
209 }
210
211 const std::string& Results::operator[](const PositionalArgument& posArg) const
212 {
213 const auto name = internal::PositionalArgumentTranslator::PositionalArgName(posArg);
214 for (size_t i = 0; i < posArgNames_.size(); ++i) {
215 if (posArgNames_.at(i) == name) {
216 if (i >= posArgValues_.size()) {
217 throw std::runtime_error{
218 "[pbcopper] command line results ERROR: missing value for positional "
219 "argument: " +
220 name};
221 }
222 return posArgValues_.at(i);
223 }
224 }
225
226 // not found
227 std::ostringstream out;
228 out << "[pbcopper] command line results ERROR: unknown positional argument, with name: " << name
229 << '\n';
230 throw std::invalid_argument{out.str()};
231 }
232
233 } // namespace CLI_v2
234 } // namespace PacBio