Skip to content

Specifying container of containers in configuration file #1067

Closed
@pauldmccarthy

Description

@pauldmccarthy

Howdy,

I am trying to build a CLI application with an option that both expects multiple values, and can be given multiple times, e.g.:

myapp --option 1 2 3 --option 4 5 6

I want the values to be aggregated into a vector of vectors, so am using the following
code:

std::vector<std::vector<int>> option;
app.add_option(
  "--option",
  option,
  "Vector of vectors");

This works just fine when the --option values are specified at the command-line - I end up with a vector containing two vectors, which contain 1 2 3 and 4 5 6 respectively.

However there appears to be an issue when I try to specify the options in a configuration file, enabled via app.set_config("--config"). If I create a config.ini file with the following:

option = 1 2 3
option = 4 5 6

and run myapp --config config.ini, I end up with a vector which contains just one vector, containing 1 2 3 4 5 6.

Here is a minimal example which reproduces the issue:

#include <iostream>
#include <vector>

#include "CLI/CLI.hpp"

int main(int argc, char * argv[]) {

  CLI::App app{"MyApp"};

  std::vector<std::vector<int>> option;
  app.add_option(
    "--option",
    option,
    "Container of containers");

  app.set_config("--config");

  CLI11_PARSE(app, argc, argv);

  std::cout << "CLI11 version "
            << CLI11_VERSION_MAJOR << "."
            << CLI11_VERSION_MINOR << "."
            << CLI11_VERSION_PATCH << std::endl;

  std::cout << "Value for --option" << std::endl;
  for (auto &vec : option) {
    for (auto &val : vec) {
      std::cout << val << " ";
    }
    std::cout << std::endl;
  }
}

When specifying the values at the command-line:

./myapp --option 1 2 3 --option 4 5 6

I get the expected result:

CLI11 version 2.4.2
Value for --option
1 2 3
4 5 6

However, when I use a config file:

echo "option = 1 2 3"  > config.ini
echo "option = 4 5 6" >> config.ini
./myapp --config config.ini

I get:

CLI11 version 2.4.2
Value for --option
1 2 3 4 5 6

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions