From 73e1d1a71a6d4083a4e7a9e6ce138482145ab46d Mon Sep 17 00:00:00 2001 From: Carl Pearson Date: Thu, 7 May 2020 07:34:37 -0500 Subject: [PATCH] add help to example1 --- README.md | 9 ++++++++- examples/example1.cpp | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) 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";