p-ranav / argparse

Argument Parser for Modern C++
MIT License
2.72k stars 250 forks source link

Several bug fixes in usage, and improvement in usage and help #333

Closed rouault closed 8 months ago

rouault commented 8 months ago

By default usage is reported on a single line.

The ArgumentParser::set_usage_max_line_width(width) method can be used to display the usage() on multiple line, by defining the maximum line width.

It can be accompanied by a call to ArgumentParser::set_usage_break_on_mutex() to ask grouped mutually exclusive arguments to be displayed on a separate line.

The following snippet

    argparse::ArgumentParser program("program");
    program.set_usage_max_line_width(80);
    program.set_usage_break_on_mutex();
    program.add_argument("--quite-long-option-name").flag();
    auto &group = program.add_mutually_exclusive_group();
    group.add_argument("-a").flag();
    group.add_argument("-b").flag();
    program.add_argument("-c").flag();
    program.add_argument("--another-one").flag();
    program.add_argument("-d").flag();
    program.add_argument("--yet-another-long-one").flag();
    program.add_argument("--will-go-on-new-line").flag();
    std::cout << program.usage() << std::endl;

will display:

Usage: program [--help] [--version] [--quite-long-option-name]
               [[-a]|[-b]]
               [-c] [--another-one] [-d] [--yet-another-long-one]
               [--will-go-on-new-line]

Furthermore arguments can be separated into several groups by calling ArgumentParser::add_group(group_name). Only optional arguments should be specified after the first call to add_group().

    argparse::ArgumentParser program("program");
    program.set_usage_max_line_width(80);
    program.add_argument("-a").flag().help("help_a");
    program.add_group("Advanced options");
    program.add_argument("-b").flag().help("help_b");

will display:

Usage: program [--help] [--version] [-a]

Advanced options:
               [-b]
rouault commented 8 months ago

oops sorry for the erroneous closing. reopened as #334 with ctidy fixes