CLI11 icon indicating copy to clipboard operation
CLI11 copied to clipboard

disable array bound not take effect

Open liujin001 opened this issue 3 years ago • 2 comments

I want to input 2D array data as: [1,2,3][4,5,6][7,8,9], i also want to get [1,2,3][4,5,6][7,8,9] input as one string, but arrayBounds('\0', '\0') not take effect.

// file: test.cpp
// MSVC: cl test.cpp /EHsc /utf-8 /Fetest
#include <iostream>
#include <vector>
#include <string>
#include "CLI11.hpp"

int main(int argc, char **argv) {
    CLI::App app("Test 2D Array Input");
    app.set_help_all_flag("--help-all", "show all help");
    app.failure_message(CLI::FailureMessage::help);
    app.get_config_formatter_base()->arrayBounds('\0', '\0');

    std::string array2D;
    CLI::Option *opt =
        app.add_option("--array", array2D, "Input 2D array test: --array <2d data>")->expected(1);

    std::vector<std::string> array2D_with_name;
    app.add_option("--array-name", array2D_with_name, 
        "Input 2D array test: --array-name <name> <2d data>");   // ->expected(2);

    CLI11_PARSE(app, argc, argv);

    if(app.count("--array")) {
        std::cout << array2D << "\n";
    }
    if(!array2D_with_name.empty()) {
        for(const auto& item : array2D_with_name)
            std::cout << item << ", ";
        std::cout << "\n";
    }
}

run this test, the result is:

D:\Dev\CPP\CLI11>test --array [1,2,3][4,5,6][7,8,9]
[1,2,3][4,5,6][7,8,9]       # OK!

D:\Dev\CPP\CLI11>test --array-name MyArray [1,2,3][4,5,6][7,8,9]
MyArray, 1, 2, 3][4, 5, 6][7, 8, 9,   # i want to get as [1,2,3][4,5,6][7,8,9]

liujin001 avatar May 28 '22 10:05 liujin001

A couple things here

app.get_config_formatter_base()->arrayBounds('\0', '\0');

has nothing to do with this situation it has no impact.

given what you describe what you probably want is

std::pair<std::string,std::string> array2D_with_name;
    app.add_option("--array-name", array2D_with_name, 
        "Input 2D array test: --array-name <name> <2d data>");   // ->expected(2);

this will generate the results you desire.

What was happening is that since your requested a vector output, it was allowed to detect vector notation [X1,X2,X3] and parsing the results to a bunch of strings. In your cases array2D_with_name contained 8 values.

Now it also looks like there might be bug in the handing of brackets in this situation but in your case using a pair would simplify things and bypass all that completely.

phlptp avatar May 29 '22 14:05 phlptp

Looking at this a little further, the resolution would require generate support for multidimensional vectors in toml like [ [1,2,3],[4,5,6],[7,8,9]] this is not something currently supported. I might try to tackle it at some point but not at the moment. So the solution wasn't trivial like I had hoped.

phlptp avatar Jun 07 '22 00:06 phlptp