jarro2783 / cxxopts

Lightweight C++ command line option parser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Doesn't wrap `help_string` if it's too long

alex65536 opened this issue · comments

I have

cxxopts::Options optionParser("my_program", "long string...");

where "long string..." is quite long (more than 80 characters). I expect it to be wrapped across multiple lines, but cxxopts just prints it as-is, without wrapping.

Can you tell me a bit more about your usage? I haven't been able to recreate the issue. 110+ character string wraps fine for me.

Can you tell me a bit more about your usage? I haven't been able to recreate the issue. 110+ character string wraps fine for me.

I am trying to do the following:

#include <cxxopts.hpp>
#include <iostream>

int main(int argc, char **argv) {
    cxxopts::Options opts("my_cli", "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
    opts.add_options()
        ("h,help", "Show help");
    auto result = opts.parse(argc, argv);
    if (result.count("help")) {
        std::cout << opts.help() << std::endl;
        return 0;
    }
    std::cout << "Normal execution." << std::endl;
    return 0;
}

What I get:

$ ./my_cli --help
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Usage:
  my_cli [OPTION...]

  -h, --help  Show help

The first line isn't wrapped and is more than 80 characters long.

Maybe I am misusing this argument of cxxopts::Options with quite long strings, but it would mean that there's not way to pass long descriptions to help message.

You are correct. I forgot my output was set to automatically wrap. I do believe the purpose of help_string is to be short and brief. However, @jarro2783 would you be opposed to automatic wrapping?

I believe only the option descriptions are wrapped, not the help_string itself. I am happy to implement a similar wrapping method for help_string.

Example:

#include "cxxopts.hpp"
#include <iostream>

std::string longStr = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

int main(int argc, char **argv) {
    cxxopts::Options opts("my_cli", longStr);
    opts.set_width(80);  // controls width of option description
    opts.add_options()
        ("h,help", longStr);
    auto result = opts.parse(argc, argv);
    if (result.count("help")) {
        std::cout << opts.help() << std::endl;
        return 0;
    }
    std::cout << "Normal execution." << std::endl;
    return 0;
}

Output:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Usage:
  my_cli [OPTION...]

  -h, --help  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do 
              eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut 
              enim ad minim veniam, quis nostrud exercitation ullamco laboris 
              nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in 
              reprehenderit in voluptate velit esse cillum dolore eu fugiat 
              nulla pariatur. Excepteur sint occaecat cupidatat non proident, 
              sunt in culpa qui officia deserunt mollit anim id est laborum.

If so, I can't figure out how! As far as I can tell from Options::help() is that the description (m_help_string) is appended to the result without modification:

cxxopts/include/cxxopts.hpp

Lines 2660 to 2661 in ad2d1a6

String result = m_help_string + "\nUsage:\n " +
toLocalString(m_program) + " " + toLocalString(m_custom_help);

I just looked into this, and I see that there is no wrapping. I would have to write a bit of code to do that.