| 1 |
#include <pbcopper/cli2/internal/CommandLineParser.h> |
| 2 |
|
| 3 |
#include <pbcopper/utility/StringUtils.h> |
| 4 |
|
| 5 |
#include <boost/algorithm/string/predicate.hpp> |
| 6 |
|
| 7 |
#include <deque> |
| 8 |
#include <sstream> |
| 9 |
#include <stdexcept> |
| 10 |
#include <string> |
| 11 |
#include <string_view> |
| 12 |
#include <vector> |
| 13 |
|
| 14 |
#include <cassert> |
| 15 |
|
| 16 |
using OptionData = PacBio::CLI_v2::internal::OptionData; |
| 17 |
using OptionValue = PacBio::CLI_v2::OptionValue; |
| 18 |
using OptionValueType = PacBio::CLI_v2::OptionValueType; |
| 19 |
using Results = PacBio::CLI_v2::Results; |
| 20 |
|
| 21 |
namespace { |
| 22 |
|
| 23 |
constexpr std::string_view TOKEN_DOUBLE_DASH{"--"}; |
| 24 |
constexpr char TOKEN_DASH = '-'; |
| 25 |
constexpr char TOKEN_EQUAL = '='; |
| 26 |
|
| 27 |
struct CommandLineParserException : public std::runtime_error |
| 28 |
{ |
| 29 |
CommandLineParserException(const std::string& reason) : std::runtime_error("") |
| 30 |
{ |
| 31 |
std::ostringstream msg; |
| 32 |
msg << "[pbcopper] command line parser ERROR: " << reason << '\n'; |
| 33 |
msg_ = msg.str(); |
| 34 |
} |
| 35 |
|
| 36 |
const char* what() const noexcept override { return msg_.c_str(); } |
| 37 |
|
| 38 |
std::string msg_; |
| 39 |
}; |
| 40 |
|
| 41 |
bool IsTrueBoolean(const std::string& s) |
| 42 |
{ |
| 43 |
return boost::iequals(s, "true") || boost::iequals(s, "on"); |
| 44 |
} |
| 45 |
|
| 46 |
bool IsFalseBoolean(const std::string& s) |
| 47 |
{ |
| 48 |
return boost::iequals(s, "false") || boost::iequals(s, "off"); |
| 49 |
} |
| 50 |
|
| 51 |
OptionValue ValueFromString(const OptionData& option, const std::string valueString) |
| 52 |
{ |
| 53 |
const auto IsScientificNotation = [](const std::string& s) { |
| 54 |
return s.find_first_of("eE") != std::string::npos; |
| 55 |
}; |
| 56 |
|
| 57 |
switch (option.Type) { |
| 58 |
case OptionValueType::INT: { |
| 59 |
if (IsScientificNotation(valueString)) { |
| 60 |
return OptionValue{PacBio::Utility::ScientificNotationToInt(valueString)}; |
| 61 |
} else { |
| 62 |
return OptionValue{PacBio::Utility::SIStringToInt(valueString)}; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
case OptionValueType::UINT: { |
| 67 |
if (IsScientificNotation(valueString)) { |
| 68 |
return OptionValue{PacBio::Utility::ScientificNotationToUInt(valueString)}; |
| 69 |
} else { |
| 70 |
return OptionValue{PacBio::Utility::SIStringToUInt(valueString)}; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
case OptionValueType::FLOAT: { |
| 75 |
if (IsScientificNotation(valueString)) { |
| 76 |
return OptionValue{PacBio::Utility::ScientificNotationToDouble(valueString)}; |
| 77 |
} else { |
| 78 |
return OptionValue{PacBio::Utility::SIStringToDouble(valueString)}; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
case OptionValueType::STRING: |
| 83 |
case OptionValueType::FILE: |
| 84 |
case OptionValueType::DIR: |
| 85 |
return OptionValue{valueString}; |
| 86 |
|
| 87 |
case OptionValueType::BOOL: { |
| 88 |
if (IsTrueBoolean(valueString)) { |
| 89 |
return OptionValue{true}; |
| 90 |
} |
| 91 |
if (IsFalseBoolean(valueString)) { |
| 92 |
return OptionValue{false}; |
| 93 |
} |
| 94 |
throw CommandLineParserException{"value '" + valueString + |
| 95 |
"' is unexpected after a switch"}; |
| 96 |
} |
| 97 |
default: |
| 98 |
throw CommandLineParserException{"unknown option type"}; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
void EnsureOptionValue(const std::string& valueString, const std::string& optionName, |
| 103 |
const OptionValueType optionType) |
| 104 |
{ |
| 105 |
// value string does not begin with a dash, treat as option value |
| 106 |
if (valueString.find(TOKEN_DASH) != 0) { |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
// value string begins with dash - may be either a new option, bare '-', or a |
| 111 |
// valid negative value |
| 112 |
else { |
| 113 |
|
| 114 |
// bare '-' value may be OK for string-like options, but not numerical ones |
| 115 |
if (valueString.size() == 1) { |
| 116 |
if (IsStringLike(optionType)) { |
| 117 |
return; |
| 118 |
} else { |
| 119 |
throw CommandLineParserException{ |
| 120 |
"single dash value '-' is not allowed for option '" + optionName + "'"}; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
// next character is not a number, e.g. "-X", we're definitely missing our expected value |
| 125 |
else if (!std::isdigit(valueString.at(1))) { |
| 126 |
throw CommandLineParserException{"value is missing for option '" + optionName + "'"}; |
| 127 |
} |
| 128 |
|
| 129 |
// next token looks like a negative number, but is not valid |
| 130 |
// for this option type |
| 131 |
else if (!CanBeNegative(optionType)) { |
| 132 |
throw CommandLineParserException{"negative value " + valueString + |
| 133 |
" is not allowed for option '" + optionName + "'"}; |
| 134 |
} |
| 135 |
|
| 136 |
// else looks like valid negative number |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
} // namespace |
| 141 |
|
| 142 |
namespace PacBio { |
| 143 |
namespace CLI_v2 { |
| 144 |
namespace internal { |
| 145 |
|
| 146 |
CommandLineParser::CommandLineParser(Interface interface) : interface_{std::move(interface)} |
| 147 |
{ |
| 148 |
options_ = interface_.Options(); |
| 149 |
for (const auto& option : options_) { |
| 150 |
for (const auto& name : option.Names) { |
| 151 |
optionsByName_.insert({name, option}); |
| 152 |
} |
| 153 |
for (const auto& hiddenName : option.HiddenNames) { |
| 154 |
optionsByName_.insert({hiddenName, option}); |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
const OptionData& CommandLineParser::OptionFor(const std::string& name) const |
| 160 |
{ |
| 161 |
const auto found = optionsByName_.find(name); |
| 162 |
if (found != optionsByName_.cend()) { |
| 163 |
return found->second; |
| 164 |
} |
| 165 |
throw CommandLineParserException{"unknown option '" + name + "'"}; |
| 166 |
} |
| 167 |
|
| 168 |
Results CommandLineParser::Parse(const std::vector<std::string>& arguments) const |
| 169 |
{ |
| 170 |
if (arguments.empty()) { |
| 171 |
throw CommandLineParserException{ |
| 172 |
"empty argument list (should have at least the program name)"}; |
| 173 |
} |
| 174 |
|
| 175 |
Results results = interface_.MakeDefaultResults(); |
| 176 |
results.InputCommandLine(PacBio::Utility::Join(arguments, " ")); |
| 177 |
|
| 178 |
// (+1 to skip application name) |
| 179 |
std::deque<std::string> args{arguments.cbegin() + 1, arguments.cend()}; |
| 180 |
while (!args.empty()) { |
| 181 |
const auto arg = args.front(); |
| 182 |
args.pop_front(); |
| 183 |
|
| 184 |
// bare '--' signifies the end of options & start of posArgs |
| 185 |
if (arg == "--") { |
| 186 |
break; |
| 187 |
} |
| 188 |
|
| 189 |
const auto isLongOption = (arg.find(TOKEN_DOUBLE_DASH) == 0); |
| 190 |
const auto isShortOption = (arg.find(TOKEN_DASH) == 0); |
| 191 |
|
| 192 |
// long option (--reference) |
| 193 |
if (isLongOption) { |
| 194 |
ParseLongOption(arg, args, results); |
| 195 |
} |
| 196 |
|
| 197 |
// short option (-r) |
| 198 |
else if (isShortOption) { |
| 199 |
ParseShortOption(arg, args, results); |
| 200 |
} |
| 201 |
|
| 202 |
// positional argument |
| 203 |
else { |
| 204 |
results.AddPositionalArgument(arg); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
// add any remaining positional args after '--' break |
| 209 |
while (!args.empty()) { |
| 210 |
results.AddPositionalArgument(args.front()); |
| 211 |
args.pop_front(); |
| 212 |
} |
| 213 |
|
| 214 |
return results; |
| 215 |
} |
| 216 |
|
| 217 |
void CommandLineParser::ParseLongOption(const std::string& arg, std::deque<std::string>& args, |
| 218 |
Results& results) const |
| 219 |
{ |
| 220 |
assert(arg.length() > 2); |
| 221 |
|
| 222 |
// strip leading '--' |
| 223 |
const auto optionToken = arg.substr(2); |
| 224 |
|
| 225 |
std::string optionName; |
| 226 |
std::string valueString; |
| 227 |
|
| 228 |
// if arg is "opt=value", do the simple split & store |
| 229 |
const auto equalOffset = optionToken.find(TOKEN_EQUAL); |
| 230 |
if (equalOffset != std::string::npos) { |
| 231 |
optionName = optionToken.substr(0, equalOffset); |
| 232 |
valueString = optionToken.substr(equalOffset + 1); |
| 233 |
|
| 234 |
const auto& option = OptionFor(optionName); |
| 235 |
auto value = ValueFromString(option, valueString); |
| 236 |
results.AddObservedValue(optionName, value, SetByMode::USER); |
| 237 |
return; |
| 238 |
} |
| 239 |
|
| 240 |
// arg is either "opt" or "opt value", ignore parsing out the "option token" |
| 241 |
optionName = optionToken; |
| 242 |
|
| 243 |
// boolean options do not require a value |
| 244 |
const auto& option = OptionFor(optionName); |
| 245 |
if (option.Type == OptionValueType::BOOL) { |
| 246 |
results.AddObservedFlag(optionName, SetByMode::USER); |
| 247 |
} |
| 248 |
|
| 249 |
// non-boolean options do requre a value |
| 250 |
else { |
| 251 |
const bool argsRemaining = !args.empty(); |
| 252 |
if (argsRemaining) { |
| 253 |
valueString = args.front(); |
| 254 |
|
| 255 |
// parse value for option |
| 256 |
EnsureOptionValue(valueString, optionName, option.Type); |
| 257 |
auto value = ValueFromString(option, valueString); |
| 258 |
results.AddObservedValue(optionName, value, SetByMode::USER); |
| 259 |
args.pop_front(); |
| 260 |
|
| 261 |
} else { |
| 262 |
throw CommandLineParserException{"value is missing for option '" + optionName + "'"}; |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
void CommandLineParser::ParseShortOption(const std::string& arg, std::deque<std::string>& args, |
| 268 |
Results& results) const |
| 269 |
{ |
| 270 |
// single dash only (usually indicating 'stdin') |
| 271 |
if (arg.length() == 1) { |
| 272 |
results.AddPositionalArgument(arg); |
| 273 |
return; |
| 274 |
} |
| 275 |
|
| 276 |
std::string optionName; |
| 277 |
bool valueExpected = true; |
| 278 |
|
| 279 |
// iterate over arg chars, may be a combo of short args (e.g. -xvf) |
| 280 |
for (size_t i = 1; i < arg.size(); ++i) { |
| 281 |
optionName = arg.substr(i, 1); |
| 282 |
const auto& option = OptionFor(optionName); |
| 283 |
|
| 284 |
// boolean options do not require a value |
| 285 |
if (option.Type == OptionValueType::BOOL) { |
| 286 |
results.AddObservedFlag(optionName, SetByMode::USER); |
| 287 |
valueExpected = false; |
| 288 |
} |
| 289 |
|
| 290 |
// otherwise expects value, check for "attached" value (e.g. -n5 or -f=2) |
| 291 |
// if found, no remaining value expected |
| 292 |
else { |
| 293 |
if (i + 1 < arg.size()) { |
| 294 |
if (arg.at(i + 1) == TOKEN_EQUAL) { |
| 295 |
++i; |
| 296 |
} |
| 297 |
auto valueString = arg.substr(i + 1); |
| 298 |
auto value = ValueFromString(option, valueString); |
| 299 |
results.AddObservedValue(optionName, value, SetByMode::USER); |
| 300 |
return; |
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
// value still expected, use the next token |
| 306 |
if (valueExpected) { |
| 307 |
assert(!optionName.empty()); |
| 308 |
const auto& option = OptionFor(optionName); |
| 309 |
|
| 310 |
auto valueString = args.front(); |
| 311 |
args.pop_front(); |
| 312 |
|
| 313 |
// parse value for option |
| 314 |
EnsureOptionValue(valueString, optionName, option.Type); |
| 315 |
auto value = ValueFromString(option, valueString); |
| 316 |
results.AddObservedValue(optionName, value, SetByMode::USER); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
} // namespace internal |
| 321 |
} // namespace CLI_v2 |
| 322 |
} // namespace PacBio |