| 1 |
#include <pbcopper/cli2/internal/InterfaceHelpPrinter.h> |
| 2 |
|
| 3 |
#include <pbcopper/utility/StringUtils.h> |
| 4 |
#include "PbBoilerplateDisclaimer.h" |
| 5 |
|
| 6 |
#include <algorithm> |
| 7 |
#include <ostream> |
| 8 |
#include <sstream> |
| 9 |
|
| 10 |
using HelpMetrics = PacBio::CLI_v2::internal::HelpMetrics; |
| 11 |
using Option = PacBio::CLI_v2::Option; |
| 12 |
using OptionData = PacBio::CLI_v2::internal::OptionData; |
| 13 |
using OptionValue = PacBio::CLI_v2::OptionValue; |
| 14 |
using OptionValueType = PacBio::CLI_v2::OptionValueType; |
| 15 |
using PositionalArgument = PacBio::CLI_v2::PositionalArgument; |
| 16 |
using PositionalArgumentData = PacBio::CLI_v2::internal::PositionalArgumentData; |
| 17 |
|
| 18 |
namespace PacBio { |
| 19 |
namespace CLI_v2 { |
| 20 |
namespace internal { |
| 21 |
|
| 22 |
InterfaceHelpPrinter::InterfaceHelpPrinter(Interface interface, HiddenOptionMode hiddenOptionMode) |
| 23 |
: metrics_{interface, hiddenOptionMode} |
| 24 |
, interface_{std::move(interface)} |
| 25 |
, showHiddenOptions_{hiddenOptionMode == HiddenOptionMode::SHOW} |
| 26 |
{ |
| 27 |
MakeHelpText(); |
| 28 |
} |
| 29 |
|
| 30 |
InterfaceHelpPrinter::InterfaceHelpPrinter(Interface interface, const size_t maxColumn, |
| 31 |
HiddenOptionMode hiddenOptionMode) |
| 32 |
: metrics_{interface, maxColumn, hiddenOptionMode} |
| 33 |
, interface_{std::move(interface)} |
| 34 |
, showHiddenOptions_{hiddenOptionMode == HiddenOptionMode::SHOW} |
| 35 |
{ |
| 36 |
MakeHelpText(); |
| 37 |
} |
| 38 |
|
| 39 |
std::string InterfaceHelpPrinter::Choices(const OptionData& option) |
| 40 |
{ |
| 41 |
// nothing to show |
| 42 |
if (option.Choices.empty()) { |
| 43 |
return {}; |
| 44 |
} |
| 45 |
|
| 46 |
// check for hidden status |
| 47 |
if ((option.IsHidden || option.IsChoicesHidden) && !showHiddenOptions_) { |
| 48 |
return {}; |
| 49 |
} |
| 50 |
|
| 51 |
std::ostringstream out; |
| 52 |
for (const auto& choice : option.Choices) { |
| 53 |
switch (option.Type) { |
| 54 |
case OptionValueType::INT: |
| 55 |
out << OptionValueToInt(choice); |
| 56 |
break; |
| 57 |
case OptionValueType::UINT: |
| 58 |
out << OptionValueToUInt(choice); |
| 59 |
break; |
| 60 |
case OptionValueType::FLOAT: |
| 61 |
out << OptionValueToDouble(choice); |
| 62 |
break; |
| 63 |
case OptionValueType::STRING: |
| 64 |
case OptionValueType::FILE: |
| 65 |
case OptionValueType::DIR: |
| 66 |
out << OptionValueToString(choice); |
| 67 |
break; |
| 68 |
case OptionValueType::BOOL: |
| 69 |
out << (OptionValueToBool(choice) ? "true" : "false"); |
| 70 |
break; |
| 71 |
} |
| 72 |
out << ", "; |
| 73 |
} |
| 74 |
|
| 75 |
std::string result = out.str(); |
| 76 |
result.erase(result.size() - 2, 2); // trim trailing ", " |
| 77 |
return result; |
| 78 |
} |
| 79 |
|
| 80 |
std::string InterfaceHelpPrinter::DefaultValue(const OptionData& option) |
| 81 |
{ |
| 82 |
if (!ShouldShowDefaultValue(option)) { |
| 83 |
return {}; |
| 84 |
} |
| 85 |
|
| 86 |
std::ostringstream out; |
| 87 |
switch (option.Type) { |
| 88 |
case OptionValueType::INT: |
| 89 |
out << OptionValueToInt(*option.DefaultValue); |
| 90 |
break; |
| 91 |
case OptionValueType::UINT: |
| 92 |
out << OptionValueToUInt(*option.DefaultValue); |
| 93 |
break; |
| 94 |
case OptionValueType::FLOAT: |
| 95 |
out << OptionValueToDouble(*option.DefaultValue); |
| 96 |
break; |
| 97 |
case OptionValueType::STRING: |
| 98 |
case OptionValueType::FILE: |
| 99 |
case OptionValueType::DIR: |
| 100 |
out << OptionValueToString(*option.DefaultValue); |
| 101 |
break; |
| 102 |
case OptionValueType::BOOL: { |
| 103 |
const bool on = OptionValueToBool(*option.DefaultValue); |
| 104 |
out << (on ? "true" : "false"); |
| 105 |
break; |
| 106 |
} |
| 107 |
default: |
| 108 |
assert(false); |
| 109 |
} |
| 110 |
return out.str(); |
| 111 |
} |
| 112 |
|
| 113 |
std::string InterfaceHelpPrinter::Description() |
| 114 |
{ |
| 115 |
const auto& description = interface_.ApplicationDescription(); |
| 116 |
if (description.empty()) { |
| 117 |
return {}; |
| 118 |
} |
| 119 |
|
| 120 |
// maybe word-wrap description |
| 121 |
std::ostringstream out; |
| 122 |
const auto indent = interface_.ApplicationName().size() + 3; // " - " spacer |
| 123 |
const auto max = metrics_.MaxColumn - indent; |
| 124 |
const auto wrappedLines = Utility::WordWrappedLines(description, max); |
| 125 |
if (!wrappedLines.empty()) { |
| 126 |
out << wrappedLines.at(0); |
| 127 |
for (size_t i = 1; i < wrappedLines.size(); ++i) { |
| 128 |
out << '\n' << std::string(indent, ' ') << wrappedLines.at(i); |
| 129 |
} |
| 130 |
} |
| 131 |
return out.str(); |
| 132 |
} |
| 133 |
|
| 134 |
void InterfaceHelpPrinter::MakeHelpText() |
| 135 |
{ |
| 136 |
std::ostringstream result; |
| 137 |
result << interface_.ApplicationName(); |
| 138 |
|
| 139 |
const std::string description = Description(); |
| 140 |
if (!description.empty()) { |
| 141 |
result << " - " << Description(); |
| 142 |
} |
| 143 |
|
| 144 |
result << '\n' << '\n' << Usage() << '\n'; |
| 145 |
|
| 146 |
const std::string posArgs = PositionalArguments(); |
| 147 |
if (!posArgs.empty()) { |
| 148 |
result << '\n' << posArgs; |
| 149 |
} |
| 150 |
|
| 151 |
const std::string options = Options(); |
| 152 |
if (!options.empty()) { |
| 153 |
result << '\n' << options << '\n'; |
| 154 |
} |
| 155 |
|
| 156 |
const auto& footer = interface_.HelpFooter(); |
| 157 |
if (!footer.empty()) { |
| 158 |
result << footer << '\n'; |
| 159 |
|
| 160 |
// Some application footers contain trailing newlines, some do not. |
| 161 |
// Ensure we have at least one empty line between footer and disclaimer. |
| 162 |
if (footer.back() != '\n') { |
| 163 |
result << '\n'; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
result << PB_BOILERPLATE_DISCLAIMER; |
| 168 |
|
| 169 |
text_ = result.str(); |
| 170 |
} |
| 171 |
|
| 172 |
const HelpMetrics& InterfaceHelpPrinter::Metrics() { return metrics_; } |
| 173 |
|
| 174 |
std::string InterfaceHelpPrinter::Option(const OptionData& option) |
| 175 |
{ |
| 176 |
const auto& formattedOption = metrics_.FormattedOptionNames.at(option); |
| 177 |
return metrics_.HelpEntry(formattedOption.nameString, formattedOption.typeString, |
| 178 |
OptionDescription(option)); |
| 179 |
} |
| 180 |
|
| 181 |
std::string InterfaceHelpPrinter::OptionDescription(const OptionData& option) |
| 182 |
{ |
| 183 |
// layout full description, with optional components |
| 184 |
std::ostringstream out; |
| 185 |
out << option.Description; |
| 186 |
|
| 187 |
const std::string choices = Choices(option); |
| 188 |
if (!choices.empty()) { |
| 189 |
out << " Valid choices: (" << choices << ")."; |
| 190 |
} |
| 191 |
|
| 192 |
const auto defaultValue = DefaultValue(option); |
| 193 |
if (!defaultValue.empty() && !option.IsDefaultValueHidden) { |
| 194 |
out << " [" << defaultValue << ']'; |
| 195 |
} |
| 196 |
|
| 197 |
return out.str(); |
| 198 |
} |
| 199 |
|
| 200 |
std::string InterfaceHelpPrinter::OptionGroup(const OptionGroupData& group) |
| 201 |
{ |
| 202 |
if (group.Options.empty()) { |
| 203 |
return {}; |
| 204 |
} |
| 205 |
|
| 206 |
std::ostringstream out; |
| 207 |
if (!group.Name.empty()) { |
| 208 |
out << group.Name << ":\n"; |
| 209 |
} |
| 210 |
|
| 211 |
for (const OptionData& option : group.Options) { |
| 212 |
if (!option.IsHidden || showHiddenOptions_) { |
| 213 |
out << Option(option) << '\n'; |
| 214 |
} |
| 215 |
} |
| 216 |
return out.str(); |
| 217 |
} |
| 218 |
|
| 219 |
std::string InterfaceHelpPrinter::Options() |
| 220 |
{ |
| 221 |
// print client option groups |
| 222 |
std::ostringstream out; |
| 223 |
const auto& optionGroups = interface_.OptionGroups(); |
| 224 |
for (const auto& optionGroup : optionGroups) { |
| 225 |
out << OptionGroup(optionGroup) << '\n'; |
| 226 |
} |
| 227 |
|
| 228 |
// print builtin group |
| 229 |
OptionGroupData group; |
| 230 |
if (optionGroups.empty()) { |
| 231 |
group.Name = "Options"; |
| 232 |
} |
| 233 |
group.Options.push_back(interface_.HelpOption()); |
| 234 |
group.Options.push_back(interface_.VersionOption()); |
| 235 |
group.Options.push_back(interface_.AlarmsOption()); |
| 236 |
group.Options.push_back(interface_.ExceptionsPassthroughOption()); |
| 237 |
group.Options.push_back(interface_.ShowAllHelpOption()); |
| 238 |
|
| 239 |
if (interface_.NumThreadsOption()) { |
| 240 |
group.Options.push_back(*interface_.NumThreadsOption()); |
| 241 |
} |
| 242 |
if (interface_.LogLevelOption()) { |
| 243 |
group.Options.push_back(*interface_.LogLevelOption()); |
| 244 |
} |
| 245 |
if (interface_.LogFileOption()) { |
| 246 |
group.Options.push_back(*interface_.LogFileOption()); |
| 247 |
} |
| 248 |
if (interface_.VerboseOption()) { |
| 249 |
group.Options.push_back(*interface_.VerboseOption()); |
| 250 |
} |
| 251 |
out << OptionGroup(group); |
| 252 |
return out.str(); |
| 253 |
} |
| 254 |
|
| 255 |
std::string InterfaceHelpPrinter::PositionalArgument(const PositionalArgumentData& posArg) |
| 256 |
{ |
| 257 |
const auto& formattedPosArg = metrics_.FormattedPosArgNames.at(posArg); |
| 258 |
return metrics_.HelpEntry(formattedPosArg.nameString, formattedPosArg.typeString, |
| 259 |
posArg.Description); |
| 260 |
} |
| 261 |
|
| 262 |
std::string InterfaceHelpPrinter::PositionalArguments() |
| 263 |
{ |
| 264 |
const auto& posArgs = interface_.PositionalArguments(); |
| 265 |
if (posArgs.empty()) { |
| 266 |
return {}; |
| 267 |
} |
| 268 |
|
| 269 |
std::ostringstream out; |
| 270 |
for (const auto& posArg : posArgs) { |
| 271 |
out << PositionalArgument(posArg) << '\n'; |
| 272 |
} |
| 273 |
|
| 274 |
return out.str(); |
| 275 |
} |
| 276 |
|
| 277 |
void InterfaceHelpPrinter::Print(std::ostream& out) const { out << text_; } |
| 278 |
|
| 279 |
bool InterfaceHelpPrinter::ShouldShowDefaultValue(const OptionData& option) |
| 280 |
{ |
| 281 |
// omit if switch OR null |
| 282 |
if (option.Type == OptionValueType::BOOL) { |
| 283 |
return false; |
| 284 |
} |
| 285 |
|
| 286 |
// omit if string-type option has an empty default |
| 287 |
if (option.Type == OptionValueType::STRING) { |
| 288 |
const auto& defaultValue = OptionValueToString(*option.DefaultValue); |
| 289 |
return !defaultValue.empty(); |
| 290 |
} |
| 291 |
|
| 292 |
// otherwise (maybe) use default value |
| 293 |
return bool{option.DefaultValue}; |
| 294 |
} |
| 295 |
|
| 296 |
std::string InterfaceHelpPrinter::Usage() |
| 297 |
{ |
| 298 |
std::ostringstream usage; |
| 299 |
|
| 300 |
usage << "Usage:\n" |
| 301 |
<< " " << interface_.ApplicationName() << " [options]"; |
| 302 |
|
| 303 |
// print all required pos args & then optional ones after, regardless of order added |
| 304 |
for (const auto& posArg : interface_.PositionalArguments()) { |
| 305 |
if (posArg.Required) { |
| 306 |
usage << " <" << posArg.Name << '>'; |
| 307 |
} |
| 308 |
} |
| 309 |
for (const auto& posArg : interface_.PositionalArguments()) { |
| 310 |
if (!posArg.Required) { |
| 311 |
usage << " [" << posArg.Name << ']'; |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
return usage.str(); |
| 316 |
} |
| 317 |
|
| 318 |
std::ostream& operator<<(std::ostream& out, const InterfaceHelpPrinter& printer) |
| 319 |
{ |
| 320 |
printer.Print(out); |
| 321 |
return out; |
| 322 |
} |
| 323 |
|
| 324 |
} // namespace internal |
| 325 |
} // namespace CLI_v2 |
| 326 |
} // namespace PacBio |