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

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

Closed
wants to merge 2 commits into from

Conversation

rouault
Copy link
Contributor

@rouault rouault commented Mar 13, 2024

  • Display mutually exclusive arguments as [[-a]|[-b]] in usage

  • Add ... trailer to repeatable arguments in usage: `[-x]...```

  • Implement the following enhancements:

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]

- Display mutually exclusive arguments as ``[[-a]|[-b]]`` in usage
- Add ... trailer to repeatable arguments in usage: `[-x]...```

- Implement the following enhancements:

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

```cpp
    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:
```console
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().

```cpp
    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:
```console
Usage: program [--help] [--version] [-a]

Advanced options:
               [-b]
```
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Clang-Tidy found issue(s) with the introduced code (1/1)

include/argparse/argparse.hpp Outdated Show resolved Hide resolved
const std::string arg_inline_usage = argument.get_inline_usage();
const MutuallyExclusiveGroup *arg_mutex =
get_belonging_mutex(&argument);
if (cur_mutex && !arg_mutex) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ readability-implicit-bool-conversion ⚠️
implicit conversion const argparse::ArgumentParser::MutuallyExclusiveGroup * -> bool

Suggested change
if (cur_mutex && !arg_mutex) {
if (cur_mutex && (arg_mutex == nullptr)) {

stream << curline << std::endl;
curline = std::string(indent_size, ' ');
}
} else if (!cur_mutex && arg_mutex) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ readability-implicit-bool-conversion ⚠️
implicit conversion const argparse::ArgumentParser::MutuallyExclusiveGroup * -> bool

Suggested change
} else if (!cur_mutex && arg_mutex) {
} else if ((cur_mutex == nullptr) && arg_mutex) {

stream << curline << std::endl;
curline = std::string(indent_size, ' ');
}
} else if (!cur_mutex && arg_mutex) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ readability-implicit-bool-conversion ⚠️
implicit conversion const argparse::ArgumentParser::MutuallyExclusiveGroup * -> bool

Suggested change
} else if (!cur_mutex && arg_mutex) {
} else if (!cur_mutex && (arg_mutex != nullptr)) {

curline = std::string(indent_size, ' ');
}
curline += " [";
} else if (cur_mutex && arg_mutex) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ readability-implicit-bool-conversion ⚠️
implicit conversion const argparse::ArgumentParser::MutuallyExclusiveGroup * -> bool

Suggested change
} else if (cur_mutex && arg_mutex) {
} else if ((cur_mutex != nullptr) && arg_mutex) {

curline = std::string(indent_size, ' ');
}
curline += " [";
} else if (cur_mutex && arg_mutex) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ readability-implicit-bool-conversion ⚠️
implicit conversion const argparse::ArgumentParser::MutuallyExclusiveGroup * -> bool

Suggested change
} else if (cur_mutex && arg_mutex) {
} else if (cur_mutex && (arg_mutex != nullptr)) {

stream << curline << std::endl;
curline = std::string(indent_size, ' ');
curline += " ";
} else if (!cur_mutex) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ readability-implicit-bool-conversion ⚠️
implicit conversion const argparse::ArgumentParser::MutuallyExclusiveGroup * -> bool

Suggested change
} else if (!cur_mutex) {
} else if (cur_mutex == nullptr) {

}
curline += arg_inline_usage;
}
if (cur_mutex) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ readability-implicit-bool-conversion ⚠️
implicit conversion const argparse::ArgumentParser::MutuallyExclusiveGroup * -> bool

Suggested change
if (cur_mutex) {
if (cur_mutex != nullptr) {

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Clang-Tidy found issue(s) with the introduced code (1/1)

const std::string arg_inline_usage = argument.get_inline_usage();
const MutuallyExclusiveGroup *arg_mutex =
get_belonging_mutex(&argument);
if ((cur_mutex != nullptr) && !arg_mutex) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ readability-implicit-bool-conversion ⚠️
implicit conversion const argparse::ArgumentParser::MutuallyExclusiveGroup * -> bool

Suggested change
if ((cur_mutex != nullptr) && !arg_mutex) {
if ((cur_mutex != nullptr) && (arg_mutex == nullptr)) {

@rouault rouault closed this Mar 13, 2024
@rouault
Copy link
Contributor Author

rouault commented Mar 13, 2024

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

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

Successfully merging this pull request may close these issues.

1 participant