diff --git a/README.md b/README.md index be262fd..52b27fa 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,8 @@ int main(int argc, char **argv) { // from string. // An option invoked with `--repeat N` - p.add_option(repeats, "--repeat")->help("how many times to repeat first argument"); + p.add_option(repeats, "--repeat") + ->help("how many times to repeat first argument"); // A flag invoked with `--verbose` or `-v` p.add_flag(verbose, "--verbose", "-v"); // a required positional argument (position 1) @@ -42,6 +43,12 @@ int main(int argc, char **argv) { exit(EXIT_FAILURE); } + // If help was requested, print it + if (p.need_help()) { + std::cerr << p.help(); + return 0; + } + // Execute the program logic if (verbose) { std::cerr << "about to print '" << toPrint << "' " << repeats << " times"; diff --git a/examples/example1.cpp b/examples/example1.cpp index fb7b0eb..06da701 100644 --- a/examples/example1.cpp +++ b/examples/example1.cpp @@ -15,7 +15,8 @@ int main(int argc, char **argv) { // from string. // An option invoked with `--repeat N` - p.add_option(repeats, "--repeat")->help("how many times to repeat first argument"); + p.add_option(repeats, "--repeat") + ->help("how many times to repeat first argument"); // A flag invoked with `--verbose` or `-v` p.add_flag(verbose, "--verbose", "-v"); // a required positional argument (position 1) @@ -29,6 +30,12 @@ int main(int argc, char **argv) { exit(EXIT_FAILURE); } + // If help was requested, print it + if (p.need_help()) { + std::cerr << p.help(); + return 0; + } + // Execute the program logic if (verbose) { std::cerr << "about to print '" << toPrint << "' " << repeats << " times";