Commit 98cdc6eb094e777e0a126cf72307d0ea0681c95c

Authored by Henry Fredrick Schreiner
1 parent 68f31927

Adding output and tests for ini vectors and flags

CHANGELOG.md
1 1 ## Version 0.7 (in progress)
2 2 * Allow comments in ini files (lines starting with `;`)
3   -* Ini files support flags (only read)
  3 +* Ini files support flags
  4 +* Ini files support vectors
4 5 * Ini files support subcommands (only read)
5   -* Ini files support vectors (only read)
6 6 * Added CodeCov code coverage reports
7 7 * Lots of small bugfixes related to adding tests to increase coverage
8 8 * Error handling now uses scoped enum in errors
9 9 * Reparsing rules changed a little to accommodate Ini files. Callbacks are now called when parsing INI, and reset any time results are added.
10   -* Adding extra utilities in seperate files version only, `Timer` (not needed for parsing, but useful for general CLI applications).
  10 +* Adding extra utilities in full version only, `Timer` (not needed for parsing, but useful for general CLI applications).
11 11  
12 12 ## Version 0.6
13 13  
... ...
include/CLI/App.hpp
... ... @@ -547,12 +547,33 @@ public:
547 547 /// @name Help
548 548 ///@{
549 549  
550   - /// Produce a string that could be read in as a config of the current values of the App
551   - std::string config_to_str() const {
  550 + /// Produce a string that could be read in as a config of the current values of the App. Set default_also to include default arguments.
  551 + std::string config_to_str(bool default_also=false) const {
552 552 std::stringstream out;
553 553 for(const Option_p &opt : options_) {
554   - if(opt->lnames_.size() > 0 && opt->count() > 0 && opt->get_expected() > 0)
555   - out << opt->lnames_[0] << "=" << detail::join(opt->results()) << std::endl;
  554 +
  555 + // Only process option with a long-name
  556 + if(opt->lnames_.size() > 0) {
  557 +
  558 + // Non-flags
  559 + if(opt->get_expected() != 0) {
  560 +
  561 + // If the option was found on command line
  562 + if(opt->count() > 0)
  563 + out << opt->lnames_[0] << "=" << detail::join(opt->results()) << std::endl;
  564 +
  565 + // If the option has a default and is requested by optional argument
  566 + else if(default_also && opt->defaultval_ != "")
  567 + out << opt->lnames_[0] << "=" << opt->defaultval_ << std::endl;
  568 + // Flag, one passed
  569 + } else if(opt->count() == 1) {
  570 + out << opt->lnames_[0] << "=true" << std::endl;
  571 +
  572 + // Flag, multiple passed
  573 + } else if(opt->count() > 1) {
  574 + out << opt->lnames_[0] << "=" << opt->count() << std::endl;
  575 + }
  576 + }
556 577 }
557 578 return out.str();
558 579 }
... ...
tests/IniTest.cpp
... ... @@ -488,22 +488,64 @@ TEST_F(TApp, IniOutputSimple) {
488 488  
489 489 std::string str = app.config_to_str();
490 490 EXPECT_EQ("simple=3\n", str);
  491 +}
  492 +
  493 +TEST_F(TApp, IniOutputVector) {
  494 +
  495 + std::vector<int> v;
  496 + app.add_option("--vector", v);
  497 +
  498 + args = {"--vector", "1", "2", "3"};
491 499  
  500 + run();
  501 +
  502 + std::string str = app.config_to_str();
  503 + EXPECT_EQ("vector=1,2,3\n", str);
492 504 }
493 505  
494   -/// Flags should not show up in config file
495 506 TEST_F(TApp, IniOutputFlag) {
496 507  
497   - int v;
  508 + int v, q;
498 509 app.add_option("--simple", v);
499 510 app.add_flag("--nothing");
  511 + app.add_flag("--onething");
  512 + app.add_flag("--something", q);
500 513  
501   - args = {"--simple=3", "--nothing"};
  514 + args = {"--simple=3", "--onething", "--something", "--something"};
502 515  
503 516 run();
504 517  
505 518 std::string str = app.config_to_str();
506 519 EXPECT_THAT(str, HasSubstr("simple=3"));
507   - EXPECT_THAT(str, Not(HasSubstr("nothing=")));
  520 + EXPECT_THAT(str, Not(HasSubstr("nothing")));
  521 + EXPECT_THAT(str, HasSubstr("onething=true"));
  522 + EXPECT_THAT(str, HasSubstr("something=2"));
  523 +}
  524 +
  525 +TEST_F(TApp, IniOutputSet) {
  526 +
  527 + int v;
  528 + app.add_set("--simple", v, {1,2,3});
  529 +
  530 + args = {"--simple=2"};
  531 +
  532 + run();
  533 +
  534 + std::string str = app.config_to_str();
  535 + EXPECT_THAT(str, HasSubstr("simple=2"));
  536 +}
  537 +
  538 +
  539 +TEST_F(TApp, IniOutputDefault) {
  540 +
  541 + int v=7;
  542 + app.add_option("--simple", v, "", true);
  543 +
  544 + run();
  545 +
  546 + std::string str = app.config_to_str();
  547 + EXPECT_THAT(str, Not(HasSubstr("simple=7")));
508 548  
  549 + str = app.config_to_str(true);
  550 + EXPECT_THAT(str, HasSubstr("simple=7"));
509 551 }
... ...