| 1 |
#include <pbcopper/cli2/internal/CommandLineParser.h> |
| 2 |
|
| 3 |
#include <string> |
| 4 |
#include <vector> |
| 5 |
|
| 6 |
#include <gtest/gtest.h> |
| 7 |
|
| 8 |
#include <pbcopper/cli2/internal/OptionTranslator.h> |
| 9 |
|
| 10 |
using CommandLineParser = PacBio::CLI_v2::internal::CommandLineParser; |
| 11 |
using Interface = PacBio::CLI_v2::Interface; |
| 12 |
using Option = PacBio::CLI_v2::Option; |
| 13 |
using OptionTranslator = PacBio::CLI_v2::internal::OptionTranslator; |
| 14 |
using PositionalArgument = PacBio::CLI_v2::PositionalArgument; |
| 15 |
|
| 16 |
// clang-format off |
| 17 |
|
| 18 |
namespace CLI_v2_CommandLineParserTests { |
| 19 |
namespace Options { |
| 20 |
|
| 21 |
const Option TargetDirectory |
| 22 |
{ |
| 23 |
R"({ |
| 24 |
"names" : ["t", "target-dir"], |
| 25 |
"description" : "Copy all source files into <DIR>.", |
| 26 |
"type" : "string", |
| 27 |
"default" : "my/default/dir" |
| 28 |
})" |
| 29 |
}; |
| 30 |
|
| 31 |
const Option Force |
| 32 |
{ |
| 33 |
R"({ |
| 34 |
"names" : ["f", "force"], |
| 35 |
"description" : "Overwrite things.", |
| 36 |
"type" : "boolean" |
| 37 |
})" |
| 38 |
}; |
| 39 |
|
| 40 |
const Option Progress |
| 41 |
{ |
| 42 |
R"({ |
| 43 |
"names" : ["p"], |
| 44 |
"description" : "Show progress during copy.", |
| 45 |
"type" : "boolean" |
| 46 |
})" |
| 47 |
}; |
| 48 |
|
| 49 |
const Option DryRun |
| 50 |
{ |
| 51 |
R"({ |
| 52 |
"names" : ["n", "no-op"], |
| 53 |
"description" : "Dry run. Report actions that would be taken but do not perform them.", |
| 54 |
"type" : "boolean" |
| 55 |
})" |
| 56 |
}; |
| 57 |
|
| 58 |
const Option Timeout |
| 59 |
{ |
| 60 |
R"({ |
| 61 |
"names" : ["timeout"], |
| 62 |
"description" : "Abort execution after <INT> milliseconds.", |
| 63 |
"type" : "integer", |
| 64 |
"default" : 5000 |
| 65 |
})" |
| 66 |
}; |
| 67 |
|
| 68 |
const Option Delta |
| 69 |
{ |
| 70 |
R"({ |
| 71 |
"names" : ["delta"], |
| 72 |
"description" : "Some delta for things.", |
| 73 |
"type" : "float", |
| 74 |
"default" : 0.01 |
| 75 |
})" |
| 76 |
}; |
| 77 |
|
| 78 |
const Option Ploidy |
| 79 |
{ |
| 80 |
R"({ |
| 81 |
"names" : ["ploidy"], |
| 82 |
"description" : "Genome ploidy.", |
| 83 |
"type" : "string", |
| 84 |
"default" : "haploid", |
| 85 |
"choices" : ["haploid", "diploid"] |
| 86 |
})" |
| 87 |
}; |
| 88 |
|
| 89 |
const Option Element |
| 90 |
{ |
| 91 |
R"({ |
| 92 |
"names" : ["e", "element"], |
| 93 |
"description" : "Choice of element indicates mood. Science.", |
| 94 |
"type" : "string", |
| 95 |
"default" : "fire", |
| 96 |
"choices" : ["earth", "wind", "fire", "water"] |
| 97 |
})" |
| 98 |
}; |
| 99 |
|
| 100 |
const Option ToolContractOnly |
| 101 |
{ |
| 102 |
R"({ |
| 103 |
"names" : ["tc-only"], |
| 104 |
"description" : "Hidden from cmdline help, but still available from TC.", |
| 105 |
"hidden" : true, |
| 106 |
"type" : "boolean" |
| 107 |
})" |
| 108 |
}; |
| 109 |
|
| 110 |
const PositionalArgument Source |
| 111 |
{ |
| 112 |
R"({ |
| 113 |
"name" : "source", |
| 114 |
"description" : "Source file to copy.", |
| 115 |
"syntax" : "FILE" |
| 116 |
})" |
| 117 |
}; |
| 118 |
|
| 119 |
const PositionalArgument Dest |
| 120 |
{ |
| 121 |
R"({ |
| 122 |
"name" : "dest", |
| 123 |
"description" : "Destination directory.", |
| 124 |
"syntax" : "DIR" |
| 125 |
})" |
| 126 |
}; |
| 127 |
|
| 128 |
} // namespace Options |
| 129 |
|
| 130 |
Interface makeInterface() |
| 131 |
{ |
| 132 |
Interface i { |
| 133 |
"frobber", |
| 134 |
"Frobb your files in a most delightful, nobbly way", |
| 135 |
"3.14" |
| 136 |
}; |
| 137 |
|
| 138 |
i.AddOptions({ |
| 139 |
Options::TargetDirectory, |
| 140 |
Options::Force, |
| 141 |
Options::Progress, |
| 142 |
Options::DryRun, |
| 143 |
Options::Timeout, |
| 144 |
Options::Delta, |
| 145 |
Options::Ploidy, |
| 146 |
Options::Element, |
| 147 |
Options::ToolContractOnly |
| 148 |
}); |
| 149 |
|
| 150 |
i.AddPositionalArguments({ |
| 151 |
Options::Source, |
| 152 |
Options::Dest |
| 153 |
}); |
| 154 |
|
| 155 |
return i; |
| 156 |
} |
| 157 |
|
| 158 |
} // namespace CLI_v2_CommandLineParserTests |
| 159 |
|
| 160 |
using namespace CLI_v2_CommandLineParserTests; |
| 161 |
|
| 162 |
TEST(CLI2_CommandLineParser, can_parse_boolean_arg_after_long_option) |
| 163 |
{ |
| 164 |
auto argIsTrue = [](const std::string& arg) { |
| 165 |
const auto i = makeInterface(); |
| 166 |
const std::vector<std::string> args{i.ApplicationName(), "--force=" + arg, "requiredIn", |
| 167 |
"requiredOut"}; |
| 168 |
CommandLineParser parser{i}; |
| 169 |
const auto results = parser.Parse(args); |
| 170 |
const bool force = results[Options::Force]; |
| 171 |
return force; |
| 172 |
}; |
| 173 |
|
| 174 |
EXPECT_TRUE(argIsTrue("true")); |
| 175 |
EXPECT_TRUE(argIsTrue("TRUE")); |
| 176 |
EXPECT_TRUE(argIsTrue("True")); |
| 177 |
EXPECT_TRUE(argIsTrue("on")); |
| 178 |
EXPECT_TRUE(argIsTrue("ON")); |
| 179 |
EXPECT_TRUE(argIsTrue("On")); |
| 180 |
|
| 181 |
EXPECT_FALSE(argIsTrue("false")); |
| 182 |
EXPECT_FALSE(argIsTrue("FALSE")); |
| 183 |
EXPECT_FALSE(argIsTrue("False")); |
| 184 |
EXPECT_FALSE(argIsTrue("off")); |
| 185 |
EXPECT_FALSE(argIsTrue("OFF")); |
| 186 |
EXPECT_FALSE(argIsTrue("Off")); |
| 187 |
|
| 188 |
// invalid switch labels |
| 189 |
EXPECT_THROW(argIsTrue(""), std::runtime_error); |
| 190 |
EXPECT_THROW(argIsTrue("junk"), std::runtime_error); |
| 191 |
} |
| 192 |
|
| 193 |
TEST(CLI2_CommandLineParser, can_parse_standard_command_line) |
| 194 |
{ |
| 195 |
const auto i = makeInterface(); |
| 196 |
const std::vector<std::string> args{i.ApplicationName(), |
| 197 |
"-f", |
| 198 |
"--timeout=42", |
| 199 |
"--delta=0.2", |
| 200 |
"--ploidy=diploid", |
| 201 |
"-e", |
| 202 |
"water", |
| 203 |
"--target-dir", |
| 204 |
"/path/to/dir/", |
| 205 |
"--logLevel=DEBUG", |
| 206 |
"requiredIn", |
| 207 |
"requiredOut"}; |
| 208 |
|
| 209 |
CommandLineParser parser{i}; |
| 210 |
const auto results = parser.Parse(args); |
| 211 |
|
| 212 |
const std::string targetDir = results[Options::TargetDirectory]; |
| 213 |
const bool force = results[Options::Force]; |
| 214 |
const bool progress = results[Options::Progress]; |
| 215 |
const bool dryRun = results[Options::DryRun]; |
| 216 |
const int timeout = results[Options::Timeout]; |
| 217 |
const float delta = results[Options::Delta]; |
| 218 |
const std::string ploidy = results[Options::Ploidy]; |
| 219 |
const std::string element = results[Options::Element]; |
| 220 |
|
| 221 |
EXPECT_EQ("/path/to/dir/", targetDir); |
| 222 |
EXPECT_TRUE(force); |
| 223 |
EXPECT_FALSE(progress); |
| 224 |
EXPECT_FALSE(dryRun); |
| 225 |
EXPECT_EQ(42, timeout); |
| 226 |
EXPECT_FLOAT_EQ(0.2, delta); |
| 227 |
EXPECT_EQ("diploid", ploidy); |
| 228 |
EXPECT_EQ("water", element); |
| 229 |
} |
| 230 |
|
| 231 |
TEST(CLI2_CommandLineParser, throws_on_empty_args) |
| 232 |
{ |
| 233 |
const auto i = makeInterface(); |
| 234 |
const std::vector<std::string> args{}; |
| 235 |
|
| 236 |
CommandLineParser parser{i}; |
| 237 |
EXPECT_THROW({ const auto results = parser.Parse(args); }, std::runtime_error); |
| 238 |
} |
| 239 |
|
| 240 |
TEST(CLI2_CommandLineParser, can_parse_positional_args_after_double_dash) |
| 241 |
{ |
| 242 |
const auto i = makeInterface(); |
| 243 |
const std::vector<std::string> args{i.ApplicationName(), |
| 244 |
"-f", |
| 245 |
"--timeout=42", |
| 246 |
"--delta=0.2", |
| 247 |
"--ploidy=diploid", |
| 248 |
"-e", |
| 249 |
"water", |
| 250 |
"--target-dir", |
| 251 |
"/path/to/dir/", |
| 252 |
"--logLevel=DEBUG", |
| 253 |
"--", |
| 254 |
"requiredIn", |
| 255 |
"requiredOut"}; |
| 256 |
|
| 257 |
CommandLineParser parser{i}; |
| 258 |
const auto results = parser.Parse(args); |
| 259 |
|
| 260 |
const std::string targetDir = results[Options::TargetDirectory]; |
| 261 |
const bool force = results[Options::Force]; |
| 262 |
const bool progress = results[Options::Progress]; |
| 263 |
const bool dryRun = results[Options::DryRun]; |
| 264 |
const int timeout = results[Options::Timeout]; |
| 265 |
const float delta = results[Options::Delta]; |
| 266 |
const std::string ploidy = results[Options::Ploidy]; |
| 267 |
const std::string element = results[Options::Element]; |
| 268 |
|
| 269 |
EXPECT_EQ("/path/to/dir/", targetDir); |
| 270 |
EXPECT_TRUE(force); |
| 271 |
EXPECT_FALSE(progress); |
| 272 |
EXPECT_FALSE(dryRun); |
| 273 |
EXPECT_EQ(42, timeout); |
| 274 |
EXPECT_FLOAT_EQ(0.2, delta); |
| 275 |
EXPECT_EQ("diploid", ploidy); |
| 276 |
EXPECT_EQ("water", element); |
| 277 |
} |
| 278 |
|
| 279 |
TEST(CLI2_CommandLineParser, long_option_expecting_value_throws_on_missing_value) |
| 280 |
{ |
| 281 |
const auto i = makeInterface(); |
| 282 |
const std::vector<std::string> args{i.ApplicationName(), |
| 283 |
"-f", |
| 284 |
"--timeout=42", |
| 285 |
"--delta=0.2", |
| 286 |
"--ploidy", |
| 287 |
"-e", |
| 288 |
"water", |
| 289 |
"--target-dir", |
| 290 |
"/path/to/dir/", |
| 291 |
"--logLevel=DEBUG", |
| 292 |
"requiredIn", |
| 293 |
"requiredOut"}; |
| 294 |
|
| 295 |
CommandLineParser parser{i}; |
| 296 |
EXPECT_THROW({ const auto results = parser.Parse(args); }, std::runtime_error); |
| 297 |
} |
| 298 |
|
| 299 |
TEST(CLI2_CommandLineParser, can_parse_switch_with_true_value) |
| 300 |
{ |
| 301 |
const auto i = makeInterface(); |
| 302 |
const std::vector<std::string> args{i.ApplicationName(), |
| 303 |
"--force=ON", |
| 304 |
"--timeout=42", |
| 305 |
"--delta=0.2", |
| 306 |
"--ploidy=diploid", |
| 307 |
"-e", |
| 308 |
"water", |
| 309 |
"--target-dir", |
| 310 |
"/path/to/dir/", |
| 311 |
"--logLevel=DEBUG", |
| 312 |
"requiredIn", |
| 313 |
"requiredOut"}; |
| 314 |
|
| 315 |
CommandLineParser parser{i}; |
| 316 |
const auto results = parser.Parse(args); |
| 317 |
|
| 318 |
const std::string targetDir = results[Options::TargetDirectory]; |
| 319 |
const bool force = results[Options::Force]; |
| 320 |
const bool progress = results[Options::Progress]; |
| 321 |
const bool dryRun = results[Options::DryRun]; |
| 322 |
const int timeout = results[Options::Timeout]; |
| 323 |
const float delta = results[Options::Delta]; |
| 324 |
const std::string ploidy = results[Options::Ploidy]; |
| 325 |
const std::string element = results[Options::Element]; |
| 326 |
|
| 327 |
EXPECT_EQ("/path/to/dir/", targetDir); |
| 328 |
EXPECT_TRUE(force); |
| 329 |
EXPECT_FALSE(progress); |
| 330 |
EXPECT_FALSE(dryRun); |
| 331 |
EXPECT_EQ(42, timeout); |
| 332 |
EXPECT_FLOAT_EQ(0.2, delta); |
| 333 |
EXPECT_EQ("diploid", ploidy); |
| 334 |
EXPECT_EQ("water", element); |
| 335 |
} |
| 336 |
|
| 337 |
TEST(CLI2_CommandLineParser, can_parse_switch_with_false_value) |
| 338 |
{ |
| 339 |
const auto i = makeInterface(); |
| 340 |
const std::vector<std::string> args{i.ApplicationName(), |
| 341 |
"--force=OFF", |
| 342 |
"--timeout=42", |
| 343 |
"--delta=0.2", |
| 344 |
"--ploidy=diploid", |
| 345 |
"-e", |
| 346 |
"water", |
| 347 |
"--target-dir", |
| 348 |
"/path/to/dir/", |
| 349 |
"--logLevel=DEBUG", |
| 350 |
"requiredIn", |
| 351 |
"requiredOut"}; |
| 352 |
|
| 353 |
CommandLineParser parser{i}; |
| 354 |
const auto results = parser.Parse(args); |
| 355 |
|
| 356 |
const std::string targetDir = results[Options::TargetDirectory]; |
| 357 |
const bool force = results[Options::Force]; |
| 358 |
const bool progress = results[Options::Progress]; |
| 359 |
const bool dryRun = results[Options::DryRun]; |
| 360 |
const int timeout = results[Options::Timeout]; |
| 361 |
const float delta = results[Options::Delta]; |
| 362 |
const std::string ploidy = results[Options::Ploidy]; |
| 363 |
const std::string element = results[Options::Element]; |
| 364 |
|
| 365 |
EXPECT_EQ("/path/to/dir/", targetDir); |
| 366 |
EXPECT_FALSE(force); |
| 367 |
EXPECT_FALSE(progress); |
| 368 |
EXPECT_FALSE(dryRun); |
| 369 |
EXPECT_EQ(42, timeout); |
| 370 |
EXPECT_FLOAT_EQ(0.2, delta); |
| 371 |
EXPECT_EQ("diploid", ploidy); |
| 372 |
EXPECT_EQ("water", element); |
| 373 |
} |
| 374 |
|
| 375 |
TEST(CLI2_CommandLineParser, throws_on_switch_with_invalid_value) |
| 376 |
{ |
| 377 |
const auto i = makeInterface(); |
| 378 |
const std::vector<std::string> args{i.ApplicationName(), |
| 379 |
"--force=foo", |
| 380 |
"--timeout=42", |
| 381 |
"--delta=0.2", |
| 382 |
"--ploidy=diploid", |
| 383 |
"-e", |
| 384 |
"water", |
| 385 |
"--target-dir", |
| 386 |
"/path/to/dir/", |
| 387 |
"--logLevel=DEBUG", |
| 388 |
"requiredIn", |
| 389 |
"requiredOut"}; |
| 390 |
|
| 391 |
CommandLineParser parser{i}; |
| 392 |
EXPECT_THROW({ const auto results = parser.Parse(args); }, std::runtime_error); |
| 393 |
} |
| 394 |
|
| 395 |
TEST(CLI2_CommandLineParser, can_handle_short_name_switch_before_positional_args) |
| 396 |
{ |
| 397 |
const auto i = makeInterface(); |
| 398 |
const std::vector<std::string> args{i.ApplicationName(), |
| 399 |
"--timeout=42", |
| 400 |
"--delta=0.2", |
| 401 |
"--ploidy=diploid", |
| 402 |
"-e", |
| 403 |
"water", |
| 404 |
"--target-dir", |
| 405 |
"/path/to/dir/", |
| 406 |
"--logLevel=DEBUG", |
| 407 |
"-f", |
| 408 |
"requiredIn", |
| 409 |
"requiredOut"}; |
| 410 |
|
| 411 |
CommandLineParser parser{i}; |
| 412 |
const auto results = parser.Parse(args); |
| 413 |
|
| 414 |
const std::string targetDir = results[Options::TargetDirectory]; |
| 415 |
const bool force = results[Options::Force]; |
| 416 |
const bool progress = results[Options::Progress]; |
| 417 |
const bool dryRun = results[Options::DryRun]; |
| 418 |
const int timeout = results[Options::Timeout]; |
| 419 |
const float delta = results[Options::Delta]; |
| 420 |
const std::string ploidy = results[Options::Ploidy]; |
| 421 |
const std::string element = results[Options::Element]; |
| 422 |
|
| 423 |
EXPECT_EQ("/path/to/dir/", targetDir); |
| 424 |
EXPECT_TRUE(force); |
| 425 |
EXPECT_FALSE(progress); |
| 426 |
EXPECT_FALSE(dryRun); |
| 427 |
EXPECT_EQ(42, timeout); |
| 428 |
EXPECT_FLOAT_EQ(0.2, delta); |
| 429 |
EXPECT_EQ("diploid", ploidy); |
| 430 |
EXPECT_EQ("water", element); |
| 431 |
} |
| 432 |
|
| 433 |
TEST(CLI2_CommandLineParser, can_handle_long_name_switch_before_positional_args) |
| 434 |
{ |
| 435 |
const auto i = makeInterface(); |
| 436 |
const std::vector<std::string> args{i.ApplicationName(), |
| 437 |
"--timeout=42", |
| 438 |
"--delta=0.2", |
| 439 |
"--ploidy=diploid", |
| 440 |
"-e", |
| 441 |
"water", |
| 442 |
"--target-dir", |
| 443 |
"/path/to/dir/", |
| 444 |
"--logLevel=DEBUG", |
| 445 |
"--force", |
| 446 |
"requiredIn", |
| 447 |
"requiredOut"}; |
| 448 |
|
| 449 |
CommandLineParser parser{i}; |
| 450 |
const auto results = parser.Parse(args); |
| 451 |
|
| 452 |
const std::string targetDir = results[Options::TargetDirectory]; |
| 453 |
const bool force = results[Options::Force]; |
| 454 |
const bool progress = results[Options::Progress]; |
| 455 |
const bool dryRun = results[Options::DryRun]; |
| 456 |
const int timeout = results[Options::Timeout]; |
| 457 |
const float delta = results[Options::Delta]; |
| 458 |
const std::string ploidy = results[Options::Ploidy]; |
| 459 |
const std::string element = results[Options::Element]; |
| 460 |
|
| 461 |
EXPECT_EQ("/path/to/dir/", targetDir); |
| 462 |
EXPECT_TRUE(force); |
| 463 |
EXPECT_FALSE(progress); |
| 464 |
EXPECT_FALSE(dryRun); |
| 465 |
EXPECT_EQ(42, timeout); |
| 466 |
EXPECT_FLOAT_EQ(0.2, delta); |
| 467 |
EXPECT_EQ("diploid", ploidy); |
| 468 |
EXPECT_EQ("water", element); |
| 469 |
} |
| 470 |
|
| 471 |
TEST(CLI2_CommandLineParser, displays_useful_message_on_bad_args) |
| 472 |
{ |
| 473 |
// rather than ungraceful 'unordered_map::at: key not found' |
| 474 |
|
| 475 |
CommandLineParser parser{makeInterface()}; |
| 476 |
|
| 477 |
try { |
| 478 |
const std::vector<std::string> args{"frobber", "--badarg"}; |
| 479 |
const auto results = parser.Parse(args); |
| 480 |
} catch (const std::exception& e) { |
| 481 |
const std::string message{e.what()}; |
| 482 |
EXPECT_NE("unordered_map::at: key not found", message); |
| 483 |
EXPECT_TRUE(message.find("unknown option") != std::string::npos); |
| 484 |
EXPECT_TRUE(message.find("badarg") != std::string::npos); |
| 485 |
} |
| 486 |
|
| 487 |
try { |
| 488 |
const std::vector<std::string> args{"frobber", "-X"}; |
| 489 |
const auto results = parser.Parse(args); |
| 490 |
} catch (const std::exception& e) { |
| 491 |
const std::string message{e.what()}; |
| 492 |
EXPECT_NE("unordered_map::at: key not found", message); |
| 493 |
EXPECT_TRUE(message.find("unknown option") != std::string::npos); |
| 494 |
EXPECT_TRUE(message.find("X") != std::string::npos); |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
TEST(CLI2_CommandLineParser, can_interpret_consecutive_short_options) |
| 499 |
{ |
| 500 |
const Option ShortFlag_X{ |
| 501 |
R"({ |
| 502 |
"names" : ["x"], |
| 503 |
"description" : "short flag" |
| 504 |
})"}; |
| 505 |
const Option ShortFlag_Y{ |
| 506 |
R"({ |
| 507 |
"names" : ["y"], |
| 508 |
"description" : "short flag" |
| 509 |
})"}; |
| 510 |
const Option ShortFlag_Z{ |
| 511 |
R"({ |
| 512 |
"names" : ["z"], |
| 513 |
"description" : "short flag" |
| 514 |
})"}; |
| 515 |
|
| 516 |
Interface i { |
| 517 |
"frobber", |
| 518 |
"Frobb your files in a most delightful, nobbly way", |
| 519 |
"3.14" |
| 520 |
}; |
| 521 |
i.AddOptions( |
| 522 |
{ |
| 523 |
ShortFlag_X, |
| 524 |
ShortFlag_Y, |
| 525 |
ShortFlag_Z |
| 526 |
}); |
| 527 |
|
| 528 |
const std::vector<std::string> args{ |
| 529 |
"frobber", |
| 530 |
"-xz" |
| 531 |
}; |
| 532 |
|
| 533 |
CommandLineParser parser{i}; |
| 534 |
const auto results = parser.Parse(args); |
| 535 |
|
| 536 |
EXPECT_TRUE(results[ShortFlag_X]); |
| 537 |
EXPECT_FALSE(results[ShortFlag_Y]); |
| 538 |
EXPECT_TRUE(results[ShortFlag_Z]); |
| 539 |
} |
| 540 |
|
| 541 |
TEST(CLI2_CommandLineParser, can_interpret_negative_values_for_options) |
| 542 |
{ |
| 543 |
const Option ShortOptionAttached{ |
| 544 |
R"({ |
| 545 |
"names" : ["x"], |
| 546 |
"description" : "short attached", |
| 547 |
"type" : "int", |
| 548 |
"default" : 0 |
| 549 |
})"}; |
| 550 |
|
| 551 |
const Option ShortOptionDetached{ |
| 552 |
R"({ |
| 553 |
"names" : ["y"], |
| 554 |
"description" : "short detached", |
| 555 |
"type" : "int", |
| 556 |
"default" : 0 |
| 557 |
})"}; |
| 558 |
|
| 559 |
const Option ShortFlag{ |
| 560 |
R"({ |
| 561 |
"names" : ["z"], |
| 562 |
"description" : "short flag" |
| 563 |
})"}; |
| 564 |
|
| 565 |
const Option LongOptionAttached{ |
| 566 |
R"({ |
| 567 |
"names" : ["attached"], |
| 568 |
"description" : "attached", |
| 569 |
"type" : "int", |
| 570 |
"default" : 0 |
| 571 |
})"}; |
| 572 |
|
| 573 |
const Option LongOptionDetached{ |
| 574 |
R"({ |
| 575 |
"names" : ["detached"], |
| 576 |
"description" : "detached", |
| 577 |
"type" : "int", |
| 578 |
"default" : 0 |
| 579 |
})"}; |
| 580 |
|
| 581 |
Interface i { |
| 582 |
"frobber", |
| 583 |
"Frobb your files in a most delightful, nobbly way", |
| 584 |
"3.14" |
| 585 |
}; |
| 586 |
i.AddOptions( |
| 587 |
{ |
| 588 |
ShortOptionAttached, |
| 589 |
ShortOptionDetached, |
| 590 |
ShortFlag, |
| 591 |
LongOptionAttached, |
| 592 |
LongOptionDetached |
| 593 |
}); |
| 594 |
|
| 595 |
const std::vector<std::string> args{ |
| 596 |
"frobber", |
| 597 |
"-x=-1", // short option, attached via '=' |
| 598 |
"-y", // short option, detached |
| 599 |
"-6", |
| 600 |
"--attached=-8", // long option, attached via '=' |
| 601 |
"--detached", // long option, detached |
| 602 |
"-42" |
| 603 |
}; |
| 604 |
|
| 605 |
const CommandLineParser parser{i}; |
| 606 |
const auto results = parser.Parse(args); |
| 607 |
|
| 608 |
const int shortAttachedValue = results[ShortOptionAttached]; |
| 609 |
const int shortDetachedValue = results[ShortOptionDetached]; |
| 610 |
const int longAttachedValue = results[LongOptionAttached]; |
| 611 |
const int longDetachedValue = results[LongOptionDetached]; |
| 612 |
EXPECT_EQ(-1, shortAttachedValue); |
| 613 |
EXPECT_EQ(-6, shortDetachedValue); |
| 614 |
EXPECT_EQ(-8, longAttachedValue); |
| 615 |
EXPECT_EQ(-42, longDetachedValue); |
| 616 |
} |
| 617 |
TEST(CLI2_CommandLineParser, throws_if_expected_value_is_next_option_instead) |
| 618 |
{ |
| 619 |
const Option ShortOption{ |
| 620 |
R"({ |
| 621 |
"names" : ["x"], |
| 622 |
"description" : "short option", |
| 623 |
"type" : "int", |
| 624 |
"default" : 0 |
| 625 |
})"}; |
| 626 |
|
| 627 |
const Option LongOption{ |
| 628 |
R"({ |
| 629 |
"names" : ["long-y"], |
| 630 |
"description" : "long option", |
| 631 |
"type" : "int", |
| 632 |
"default" : 0 |
| 633 |
})"}; |
| 634 |
|
| 635 |
Interface i { |
| 636 |
"frobber", |
| 637 |
"Frobb your files in a most delightful, nobbly way", |
| 638 |
"3.14" |
| 639 |
}; |
| 640 |
|
| 641 |
i.AddOptions( |
| 642 |
{ |
| 643 |
ShortOption, |
| 644 |
LongOption |
| 645 |
}); |
| 646 |
|
| 647 |
const CommandLineParser parser{i}; |
| 648 |
|
| 649 |
{ |
| 650 |
const std::vector<std::string> args{ |
| 651 |
"frobber", |
| 652 |
"-x", // value expected for short option, but given another option instead |
| 653 |
"-z" |
| 654 |
}; |
| 655 |
|
| 656 |
try { |
| 657 |
const auto results = parser.Parse(args); |
| 658 |
ASSERT_TRUE(false); // should not execute |
| 659 |
} catch (const std::exception& e) |
| 660 |
{ |
| 661 |
const std::string msg{e.what()}; |
| 662 |
EXPECT_TRUE(msg.find("value is missing for option 'x'") != std::string::npos); |
| 663 |
} |
| 664 |
} |
| 665 |
{ |
| 666 |
const std::vector<std::string> args{ |
| 667 |
"frobber", |
| 668 |
"--long-y", // value expected for long option, but given another option instead |
| 669 |
"-x" |
| 670 |
}; |
| 671 |
|
| 672 |
try { |
| 673 |
const auto results = parser.Parse(args); |
| 674 |
ASSERT_TRUE(false); // should not execute |
| 675 |
} catch (const std::exception& e) |
| 676 |
{ |
| 677 |
const std::string msg{e.what()}; |
| 678 |
EXPECT_TRUE(msg.find("value is missing for option 'long-y'") != std::string::npos); |
| 679 |
} |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
TEST(CLI2_CommandLineParser, does_not_allow_negative_value_for_unsigned_option) |
| 684 |
{ |
| 685 |
const Option UIntOption{ |
| 686 |
R"({ |
| 687 |
"names" : ["x"], |
| 688 |
"description" : "x", |
| 689 |
"type" : "unsigned int", |
| 690 |
"default" : 0 |
| 691 |
})"}; |
| 692 |
Interface i { |
| 693 |
"frobber", |
| 694 |
"Frobb your files in a most delightful, nobbly way", |
| 695 |
"3.14" |
| 696 |
}; |
| 697 |
i.AddOption(UIntOption); |
| 698 |
|
| 699 |
try { |
| 700 |
const CommandLineParser parser{i}; |
| 701 |
parser.Parse({"frobber", "-x", "-42"}); |
| 702 |
ASSERT_TRUE(false); // should not execute |
| 703 |
} catch (const std::exception& e) { |
| 704 |
const std::string msg{e.what()}; |
| 705 |
EXPECT_TRUE(msg.find("negative value -42 is not allowed for option 'x'") != std::string::npos); |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
TEST(CLI2_CommandLineParser, does_not_allow_negative_value_for_string_option) |
| 710 |
{ |
| 711 |
const Option StringOption{ |
| 712 |
R"({ |
| 713 |
"names" : ["x"], |
| 714 |
"description" : "x", |
| 715 |
"type" : "string" |
| 716 |
})"}; |
| 717 |
Interface i { |
| 718 |
"frobber", |
| 719 |
"Frobb your files in a most delightful, nobbly way", |
| 720 |
"3.14" |
| 721 |
}; |
| 722 |
i.AddOption(StringOption); |
| 723 |
|
| 724 |
try { |
| 725 |
const CommandLineParser parser{i}; |
| 726 |
parser.Parse({"frobber", "-x", "-42"}); |
| 727 |
ASSERT_TRUE(false); // should not execute |
| 728 |
} catch (const std::exception& e) { |
| 729 |
const std::string msg{e.what()}; |
| 730 |
EXPECT_TRUE(msg.find("negative value -42 is not allowed for option 'x'") != std::string::npos); |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
TEST(CLI2_CommandLineParser, can_interpret_numerics_with_si_suffix) |
| 735 |
{ |
| 736 |
const Option UIntOption{ |
| 737 |
R"({ |
| 738 |
"names" : ["x"], |
| 739 |
"description" : "x", |
| 740 |
"type" : "unsigned int", |
| 741 |
"default" : 0 |
| 742 |
})"}; |
| 743 |
const Option IntOption{ |
| 744 |
R"({ |
| 745 |
"names" : ["yoink"], |
| 746 |
"description" : "y", |
| 747 |
"type" : "int", |
| 748 |
"default" : 0 |
| 749 |
})"}; |
| 750 |
const Option FloatOption{ |
| 751 |
R"({ |
| 752 |
"names" : ["z"], |
| 753 |
"description" : "z", |
| 754 |
"type" : "float", |
| 755 |
"default" : 0.0 |
| 756 |
})"}; |
| 757 |
|
| 758 |
Interface i { |
| 759 |
"frobber", |
| 760 |
"Frobb your files in a most delightful, nobbly way", |
| 761 |
"3.14" |
| 762 |
}; |
| 763 |
i.AddOption(UIntOption) |
| 764 |
.AddOption(IntOption) |
| 765 |
.AddOption(FloatOption); |
| 766 |
|
| 767 |
const CommandLineParser parser{i}; |
| 768 |
const auto results = parser.Parse({ |
| 769 |
"frobber", |
| 770 |
"-x", "42K", |
| 771 |
"--yoink=300m", |
| 772 |
"-z", "3.14G" |
| 773 |
}); |
| 774 |
|
| 775 |
const uint64_t x = results[UIntOption]; |
| 776 |
const int64_t y = results[IntOption]; |
| 777 |
const double z = results[FloatOption]; |
| 778 |
|
| 779 |
EXPECT_EQ(x, 42'000); |
| 780 |
EXPECT_EQ(y, 300'000'000); |
| 781 |
EXPECT_EQ(z, 3'140'000'000); |
| 782 |
} |
| 783 |
|
| 784 |
TEST(CLI2_CommandLineParser, can_interpret_numerics_with_scientific_notation) |
| 785 |
{ |
| 786 |
const Option UIntOption{ |
| 787 |
R"({ |
| 788 |
"names" : ["x"], |
| 789 |
"description" : "x", |
| 790 |
"type" : "unsigned int", |
| 791 |
"default" : 0 |
| 792 |
})"}; |
| 793 |
const Option IntOption{ |
| 794 |
R"({ |
| 795 |
"names" : ["yoink"], |
| 796 |
"description" : "y", |
| 797 |
"type" : "int", |
| 798 |
"default" : 0 |
| 799 |
})"}; |
| 800 |
const Option FloatOption{ |
| 801 |
R"({ |
| 802 |
"names" : ["z"], |
| 803 |
"description" : "z", |
| 804 |
"type" : "float", |
| 805 |
"default" : 0.0 |
| 806 |
})"}; |
| 807 |
|
| 808 |
Interface i { |
| 809 |
"frobber", |
| 810 |
"Frobb your files in a most delightful, nobbly way", |
| 811 |
"3.14" |
| 812 |
}; |
| 813 |
i.AddOption(UIntOption) |
| 814 |
.AddOption(IntOption) |
| 815 |
.AddOption(FloatOption); |
| 816 |
|
| 817 |
const CommandLineParser parser{i}; |
| 818 |
const auto results = parser.Parse({ |
| 819 |
"frobber", |
| 820 |
"-x", "1.5e6", |
| 821 |
"--yoink=-3E7", |
| 822 |
"-z", "1.2e-4" |
| 823 |
}); |
| 824 |
|
| 825 |
const uint64_t x = results[UIntOption]; |
| 826 |
const int64_t y = results[IntOption]; |
| 827 |
const double z = results[FloatOption]; |
| 828 |
|
| 829 |
EXPECT_EQ(x, 1'500'000); |
| 830 |
EXPECT_EQ(y, -30'000'000); |
| 831 |
EXPECT_EQ(z, 0.00012); |
| 832 |
} |
| 833 |
|
| 834 |
TEST(CLI2_CommandLineParser, can_interpret_dash_option_value) |
| 835 |
{ |
| 836 |
const PacBio::CLI_v2::PositionalArgument Input { |
| 837 |
R"({ |
| 838 |
"name" : "input", |
| 839 |
"description" : "Input file(s)." |
| 840 |
})"}; |
| 841 |
|
| 842 |
const PacBio::CLI_v2::PositionalArgument StatsFile { |
| 843 |
R"({ |
| 844 |
"name" : "stats", |
| 845 |
"description" : "Stats file." |
| 846 |
})"}; |
| 847 |
|
| 848 |
const PacBio::CLI_v2::Option OutputPrefix { |
| 849 |
R"({ |
| 850 |
"names" : ["o", "output"], |
| 851 |
"description" : [ |
| 852 |
"Prefix of output filenames, '-' implies streaming. Streaming not supported ", |
| 853 |
"with compression nor with split_barcodes" |
| 854 |
], |
| 855 |
"type" : "string" |
| 856 |
})"}; |
| 857 |
|
| 858 |
const PacBio::CLI_v2::Option CompressionLevel { |
| 859 |
R"({ |
| 860 |
"names" : ["c"], |
| 861 |
"description" : "Gzip compression level [1-9]", |
| 862 |
"type" : "int", |
| 863 |
"default" : 1 |
| 864 |
})"}; |
| 865 |
|
| 866 |
PacBio::CLI_v2::Interface i{"app", "desc", "0.0"}; |
| 867 |
i.DisableLogLevelOption() |
| 868 |
.DisableLogFileOption() |
| 869 |
.DisableNumThreadsOption(); |
| 870 |
|
| 871 |
i.AddPositionalArguments({Input, StatsFile}); |
| 872 |
i.AddOptionGroup("Options", {OutputPrefix, CompressionLevel}); |
| 873 |
|
| 874 |
const CommandLineParser parser{i}; |
| 875 |
const auto results = parser.Parse({ |
| 876 |
"app", |
| 877 |
"input.bam", |
| 878 |
"-o", "-", |
| 879 |
"-c", "7", |
| 880 |
"stats.txt" |
| 881 |
}); |
| 882 |
|
| 883 |
const std::string inputFile = results[Input]; |
| 884 |
const std::string statsFile = results[StatsFile]; |
| 885 |
const std::string outputPrefix = results[OutputPrefix]; |
| 886 |
const int compressionLevel = results[CompressionLevel]; |
| 887 |
|
| 888 |
EXPECT_EQ("input.bam", inputFile); |
| 889 |
EXPECT_EQ("stats.txt", statsFile); |
| 890 |
EXPECT_EQ("-", outputPrefix); |
| 891 |
EXPECT_EQ(7, compressionLevel); |
| 892 |
} |
| 893 |
|
| 894 |
// clang-format on |