Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't wrap help_string if it's too long #321

Open
alex65536 opened this issue Dec 26, 2021 · 8 comments
Open

Doesn't wrap help_string if it's too long #321

alex65536 opened this issue Dec 26, 2021 · 8 comments

Comments

@alex65536
Copy link

alex65536 commented Dec 26, 2021

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.

@bng919
Copy link

bng919 commented Dec 27, 2021

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.

@alex65536
Copy link
Author

alex65536 commented Dec 27, 2021

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.

@bng919
Copy link

bng919 commented Dec 31, 2021

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?

@jarro2783
Copy link
Owner

jarro2783 commented Dec 31, 2021 via email

@bng919
Copy link

bng919 commented Jan 1, 2022

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.

@jarro2783
Copy link
Owner

jarro2783 commented Jan 1, 2022 via email

@bng919
Copy link

bng919 commented Jan 1, 2022

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);

@jarro2783
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants