| 1 |
#include <pbcopper/cli2/Interface.h> |
| 2 |
|
| 3 |
#include <pbcopper/cli2/Interface.h> |
| 4 |
#include <pbcopper/cli2/internal/BuiltinOptions.h> |
| 5 |
#include <pbcopper/cli2/internal/OptionTranslator.h> |
| 6 |
#include <pbcopper/cli2/internal/PositionalArgumentTranslator.h> |
| 7 |
|
| 8 |
#include <map> |
| 9 |
#include <set> |
| 10 |
#include <stdexcept> |
| 11 |
#include <type_traits> |
| 12 |
|
| 13 |
#include <cassert> |
| 14 |
|
| 15 |
using OptionData = PacBio::CLI_v2::internal::OptionData; |
| 16 |
using OptionGroupData = PacBio::CLI_v2::internal::OptionGroupData; |
| 17 |
using OptionTranslator = PacBio::CLI_v2::internal::OptionTranslator; |
| 18 |
using PositionalArgumentData = PacBio::CLI_v2::internal::PositionalArgumentData; |
| 19 |
using PositionalArgumentTranslator = PacBio::CLI_v2::internal::PositionalArgumentTranslator; |
| 20 |
|
| 21 |
namespace PacBio { |
| 22 |
namespace CLI_v2 { |
| 23 |
|
| 24 |
Interface::Interface(std::string name, std::string description, std::string version) |
| 25 |
: data_{std::move(name), |
| 26 |
std::move(description), |
| 27 |
std::move(version), |
| 28 |
OptionTranslator::Translate(Builtin::Help), |
| 29 |
OptionTranslator::Translate(Builtin::Version), |
| 30 |
OptionTranslator::Translate(Builtin::NumThreads), |
| 31 |
OptionTranslator::Translate(Builtin::LogFile), |
| 32 |
OptionTranslator::Translate(Builtin::LogLevel), |
| 33 |
OptionTranslator::Translate(Builtin::Alarms), |
| 34 |
OptionTranslator::Translate(Builtin::ExceptionPassthrough), |
| 35 |
OptionTranslator::Translate(Builtin::ShowAllHelp)} |
| 36 |
{ |
| 37 |
if (data_.AppName.empty()) { |
| 38 |
throw std::runtime_error{ |
| 39 |
"[pbcopper] command line interface setup ERROR: application name must not be empty"}; |
| 40 |
} |
| 41 |
|
| 42 |
EnsureUniqueOptionNames(data_.HelpOption); |
| 43 |
EnsureUniqueOptionNames(data_.VersionOption); |
| 44 |
EnsureUniqueOptionNames(data_.NumThreadsOption.value()); |
| 45 |
EnsureUniqueOptionNames(data_.LogFileOption.value()); |
| 46 |
EnsureUniqueOptionNames(data_.LogLevelOption.value()); |
| 47 |
EnsureUniqueOptionNames(data_.AlarmsOption); |
| 48 |
EnsureUniqueOptionNames(data_.ExceptionPassthroughOption); |
| 49 |
EnsureUniqueOptionNames(data_.ShowAllHelpOption); |
| 50 |
} |
| 51 |
|
| 52 |
Interface& Interface::AddOption(const Option& option) |
| 53 |
{ |
| 54 |
if (data_.OptionGroups.empty()) { |
| 55 |
data_.OptionGroups.push_back(OptionGroupData{}); |
| 56 |
} |
| 57 |
internal::OptionData optionData = OptionTranslator::Translate(option); |
| 58 |
EnsureUniqueOptionNames(optionData); |
| 59 |
data_.OptionGroups[0].Options.push_back(std::move(optionData)); |
| 60 |
return *this; |
| 61 |
} |
| 62 |
|
| 63 |
Interface& Interface::AddOptions(const std::vector<Option>& options) |
| 64 |
{ |
| 65 |
for (const auto& opt : options) { |
| 66 |
AddOption(opt); |
| 67 |
} |
| 68 |
return *this; |
| 69 |
} |
| 70 |
|
| 71 |
Interface& Interface::AddOptionGroup(const std::string& title, const std::vector<Option>& options) |
| 72 |
{ |
| 73 |
std::vector<internal::OptionData> groupOptions = OptionTranslator::Translate(options); |
| 74 |
EnsureUniqueOptionNames(groupOptions); |
| 75 |
EnsureUniqueOptionGroups(title); |
| 76 |
data_.OptionGroups.emplace_back(title, std::move(groupOptions)); |
| 77 |
return *this; |
| 78 |
} |
| 79 |
|
| 80 |
Interface& Interface::AddOptionGroup(const OptionGroup& group) |
| 81 |
{ |
| 82 |
data_.OptionGroups.emplace_back(group.name, OptionTranslator::Translate(group.options)); |
| 83 |
return *this; |
| 84 |
} |
| 85 |
|
| 86 |
Interface& Interface::AddPositionalArgument(const PositionalArgument& posArg) |
| 87 |
{ |
| 88 |
data_.PositionalArgs.push_back(PositionalArgumentTranslator::Translate(posArg)); |
| 89 |
return *this; |
| 90 |
} |
| 91 |
|
| 92 |
Interface& Interface::AddPositionalArguments(const std::vector<PositionalArgument>& posArgs) |
| 93 |
{ |
| 94 |
for (const auto& arg : posArgs) { |
| 95 |
AddPositionalArgument(arg); |
| 96 |
} |
| 97 |
return *this; |
| 98 |
} |
| 99 |
|
| 100 |
const internal::OptionData& Interface::AlarmsOption() const { return data_.AlarmsOption; } |
| 101 |
|
| 102 |
const std::string& Interface::ApplicationDescription() const { return data_.AppDescription; } |
| 103 |
|
| 104 |
const std::string& Interface::ApplicationName() const { return data_.AppName; } |
| 105 |
|
| 106 |
const std::string& Interface::ApplicationVersion() const { return data_.AppVersion; } |
| 107 |
|
| 108 |
Logging::LogLevel Interface::DefaultLogLevel() const |
| 109 |
{ |
| 110 |
if (data_.LogLevelOption) { |
| 111 |
const auto& value = *data_.LogLevelOption->DefaultValue; |
| 112 |
return Logging::LogLevel{std::get<std::string>(value)}; |
| 113 |
} else { |
| 114 |
return data_.LogConfig.Level; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
Interface& Interface::DefaultLogLevel(Logging::LogLevel level) |
| 119 |
{ |
| 120 |
// Error if client is setting default log level, when that option has been disabled. |
| 121 |
assert(data_.LogLevelOption); |
| 122 |
data_.LogLevelOption->DefaultValue = level.ToString(); |
| 123 |
data_.LogConfig.Level = level; |
| 124 |
return *this; |
| 125 |
} |
| 126 |
|
| 127 |
Interface& Interface::DisableLogFileOption() |
| 128 |
{ |
| 129 |
data_.LogFileOption.reset(); |
| 130 |
return *this; |
| 131 |
} |
| 132 |
|
| 133 |
Interface& Interface::DisableLogLevelOption() |
| 134 |
{ |
| 135 |
data_.LogLevelOption.reset(); |
| 136 |
return *this; |
| 137 |
} |
| 138 |
|
| 139 |
Interface& Interface::DisableNumThreadsOption() |
| 140 |
{ |
| 141 |
data_.NumThreadsOption.reset(); |
| 142 |
return *this; |
| 143 |
} |
| 144 |
|
| 145 |
Interface& Interface::EnableVerboseOption() |
| 146 |
{ |
| 147 |
data_.VerboseOption = OptionTranslator::Translate(Builtin::Verbose); |
| 148 |
return *this; |
| 149 |
} |
| 150 |
|
| 151 |
void Interface::EnsureUniqueOptionGroups(const std::string& title) const |
| 152 |
{ |
| 153 |
for (const auto& group : data_.OptionGroups) { |
| 154 |
if (group.Name == title) { |
| 155 |
throw std::runtime_error{ |
| 156 |
"[pbcopper] command line interface setup ERROR: option groups must be unique"}; |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
void Interface::EnsureUniqueOptionNames(const internal::OptionData& option) |
| 162 |
{ |
| 163 |
for (const std::string& name : option.Names) { |
| 164 |
const auto insertResult = data_.AllOptionNames.insert(name); |
| 165 |
if (!insertResult.second) { |
| 166 |
throw std::runtime_error{ |
| 167 |
"[pbcopper] command line interface setup ERROR: option names must be unique"}; |
| 168 |
} |
| 169 |
} |
| 170 |
for (const std::string& name : option.HiddenNames) { |
| 171 |
const auto insertResult = data_.AllOptionNames.insert(name); |
| 172 |
if (!insertResult.second) { |
| 173 |
throw std::runtime_error{ |
| 174 |
"[pbcopper] command line interface setup ERROR: option names must be unique"}; |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
void Interface::EnsureUniqueOptionNames(const std::vector<internal::OptionData>& options) |
| 180 |
{ |
| 181 |
for (const auto& option : options) { |
| 182 |
EnsureUniqueOptionNames(option); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
const std::string& Interface::Example() const { return data_.Example; } |
| 187 |
|
| 188 |
Interface& Interface::Example(std::string example) |
| 189 |
{ |
| 190 |
data_.Example = std::move(example); |
| 191 |
return *this; |
| 192 |
} |
| 193 |
|
| 194 |
const internal::OptionData& Interface::ExceptionsPassthroughOption() const |
| 195 |
{ |
| 196 |
return data_.ExceptionPassthroughOption; |
| 197 |
} |
| 198 |
|
| 199 |
bool Interface::HasRequiredOptions() const { return NumRequiredOptions() > 0; } |
| 200 |
|
| 201 |
bool Interface::HasRequiredPosArgs() const { return NumRequiredPosArgs() > 0; } |
| 202 |
|
| 203 |
const std::string& Interface::HelpFooter() const { return data_.Footer; } |
| 204 |
|
| 205 |
Interface& Interface::HelpFooter(std::string footer) |
| 206 |
{ |
| 207 |
data_.Footer = std::move(footer); |
| 208 |
return *this; |
| 209 |
} |
| 210 |
|
| 211 |
const OptionData& Interface::HelpOption() const { return data_.HelpOption; } |
| 212 |
|
| 213 |
const Logging::LogConfig& Interface::LogConfig() const { return data_.LogConfig; } |
| 214 |
|
| 215 |
Interface& Interface::LogConfig(const Logging::LogConfig& config) |
| 216 |
{ |
| 217 |
data_.LogConfig = config; |
| 218 |
return *this; |
| 219 |
} |
| 220 |
|
| 221 |
const std::optional<internal::OptionData>& Interface::LogFileOption() const |
| 222 |
{ |
| 223 |
return data_.LogFileOption; |
| 224 |
} |
| 225 |
|
| 226 |
const std::optional<internal::OptionData>& Interface::LogLevelOption() const |
| 227 |
{ |
| 228 |
return data_.LogLevelOption; |
| 229 |
} |
| 230 |
|
| 231 |
const std::optional<internal::OptionData>& Interface::NumThreadsOption() const |
| 232 |
{ |
| 233 |
return data_.NumThreadsOption; |
| 234 |
} |
| 235 |
|
| 236 |
Results Interface::MakeDefaultResults() const |
| 237 |
{ |
| 238 |
Results results; |
| 239 |
const auto options = Options(); |
| 240 |
for (const auto& opt : options) { |
| 241 |
if (opt.DefaultValue) { |
| 242 |
results.AddDefaultOption(opt); |
| 243 |
} |
| 244 |
} |
| 245 |
results.PositionalArguments(PositionalArguments()); |
| 246 |
return results; |
| 247 |
} |
| 248 |
|
| 249 |
size_t Interface::NumRequiredOptions() const |
| 250 |
{ |
| 251 |
size_t count = 0; |
| 252 |
for (const auto& group : data_.OptionGroups) { |
| 253 |
for (const auto& opt : group.Options) { |
| 254 |
if (opt.IsRequired) { |
| 255 |
++count; |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
return count; |
| 260 |
} |
| 261 |
|
| 262 |
size_t Interface::NumRequiredPosArgs() const |
| 263 |
{ |
| 264 |
size_t count = 0; |
| 265 |
for (const auto& posArg : data_.PositionalArgs) { |
| 266 |
if (posArg.Required) { |
| 267 |
++count; |
| 268 |
} |
| 269 |
} |
| 270 |
return count; |
| 271 |
} |
| 272 |
|
| 273 |
std::vector<OptionData> Interface::Options() const |
| 274 |
{ |
| 275 |
std::vector<OptionData> result; |
| 276 |
|
| 277 |
// add registered objects |
| 278 |
for (const auto& group : data_.OptionGroups) { |
| 279 |
for (const auto& opt : group.Options) { |
| 280 |
result.push_back(opt); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
// add builtins |
| 285 |
result.push_back(data_.HelpOption); |
| 286 |
result.push_back(data_.VersionOption); |
| 287 |
result.push_back(data_.AlarmsOption); |
| 288 |
result.push_back(data_.ExceptionPassthroughOption); |
| 289 |
result.push_back(data_.ShowAllHelpOption); |
| 290 |
if (data_.NumThreadsOption) { |
| 291 |
result.push_back(*data_.NumThreadsOption); |
| 292 |
} |
| 293 |
if (data_.LogLevelOption) { |
| 294 |
result.push_back(*data_.LogLevelOption); |
| 295 |
} |
| 296 |
if (data_.LogFileOption) { |
| 297 |
result.push_back(*data_.LogFileOption); |
| 298 |
} |
| 299 |
if (data_.VerboseOption) { |
| 300 |
result.push_back(*data_.VerboseOption); |
| 301 |
} |
| 302 |
|
| 303 |
return result; |
| 304 |
} |
| 305 |
|
| 306 |
const std::vector<OptionGroupData>& Interface::OptionGroups() const { return data_.OptionGroups; } |
| 307 |
|
| 308 |
const std::vector<PositionalArgumentData>& Interface::PositionalArguments() const |
| 309 |
{ |
| 310 |
return data_.PositionalArgs; |
| 311 |
} |
| 312 |
|
| 313 |
void Interface::PrintVersion() const { data_.VersionPrinter(*this); } |
| 314 |
|
| 315 |
Interface& Interface::RegisterVersionPrinter(VersionPrinterCallback printer) |
| 316 |
{ |
| 317 |
data_.VersionPrinter = printer; |
| 318 |
return *this; |
| 319 |
} |
| 320 |
|
| 321 |
const internal::OptionData& Interface::ShowAllHelpOption() const { return data_.ShowAllHelpOption; } |
| 322 |
|
| 323 |
const std::optional<internal::OptionData>& Interface::VerboseOption() const |
| 324 |
{ |
| 325 |
return data_.VerboseOption; |
| 326 |
} |
| 327 |
|
| 328 |
const internal::OptionData& Interface::VersionOption() const { return data_.VersionOption; } |
| 329 |
|
| 330 |
} // namespace CLI_v2 |
| 331 |
} // namespace PacBio |