CLI11 icon indicating copy to clipboard operation
CLI11 copied to clipboard

TOML multiline support for Array (and Strings ?)

Open dfleury2 opened this issue 5 years ago • 2 comments

Hi, is there a schedule to integrate multiline string/array parsing into cli11? For example :

# Comments are supported, using a #
# The default section is [default], case insensitive

value = 1

str = """\
A very long \
multiline string \
"""

vector = [1,2,3]
vector_str = [
    abc,
    def,
    ghi
]

Other examples can be found on https://toml.io/en/

Regrads,

dfleury2 avatar Oct 18 '20 11:10 dfleury2

Someone with greater knowledge of the internal could make it better. Here my sample for my need (//Multiline Array, and // Multiline basic string) (An update for ConfigBase::from_config(std::istream &input))

        // Find = in string, split and recombine
        auto pos = line.find(valueDelimiter);
        if(pos != std::string::npos) {
            name = detail::trim_copy(line.substr(0, pos));
            std::string item = detail::trim_copy(line.substr(pos + 1));
            if(item.size() > 1 && item.front() == aStart && item.back() == aEnd) { // One line array
                items_buffer = detail::split_up(item.substr(1, item.length() - 2), aSep);
            } else if(item.size() > 0 && item.front() == aStart) { // Multiline array...
                for(std::string multiline; getline(input, multiline);) {
                    detail::trim(multiline);
                    item += multiline;
                    if (multiline.size() > 0 && multiline.back() == aEnd) break;
                }
                items_buffer = detail::split_up(item.substr(1, item.length() - 2), aSep);
            } else if(defaultArray && item.find_first_of(aSep) != std::string::npos) {
                items_buffer = detail::split_up(item, aSep);
            } else if(item.find("\"\"\"") != std::string::npos) { // Multiline basic string
                item.erase(0, 3);
                for(std::string multiline; getline(input, multiline);) {
                    if (auto found = multiline.find("\"\"\""); found != std::string::npos) {
                        multiline.erase(found);
                        item += multiline;
                        break;
                    }
                    item += multiline;
                }
                auto found = item.find('\\');
                while(found != std::string::npos) {
                    if (found < item.size() - 1 && item[found + 1] == ' ') {
                        item.erase(found + 1, 1);
                    } else {
                        item.erase(found, 1);
                    }
                    found = item.find('\\');
                }
                items_buffer = {item};
            } else if(defaultArray && item.find_first_of(' ') != std::string::npos) {
                items_buffer = detail::split_up(item);
            } else {
                items_buffer = {item};
            }
        } else {
            name = detail::trim_copy(line);
            items_buffer = {"true"};
        }

dfleury2 avatar Oct 18 '20 14:10 dfleury2

Just made a pull request for the array only.

dfleury2 avatar Oct 22 '20 15:10 dfleury2