improve example

This commit is contained in:
Carl Pearson
2020-03-24 13:00:06 -05:00
parent ee63e05ac9
commit 260cb18ee0
2 changed files with 15 additions and 5 deletions

View File

@@ -28,11 +28,16 @@ int main(int argc, char **argv) {
std::string maybePrint; std::string maybePrint;
int repeats = 1; int repeats = 1;
// Inform the parser of the program data. // Inform the parser of the program data. It will do type-specific conversion
// It will do type-specific conversion // from string.
// An option invoked with `--repeat N`
p.add_option(repeats, "--repeat"); p.add_option(repeats, "--repeat");
// A flag invoked with `--verbose` or `-v`
p.add_flag(verbose, "--verbose", "-v"); p.add_flag(verbose, "--verbose", "-v");
// a required positional argument (position 1)
p.add_positional(toPrint)->required(); p.add_positional(toPrint)->required();
// an optional positional argument (position 2)
auto psnl = p.add_positional(maybePrint); auto psnl = p.add_positional(maybePrint);
// If there was an error during parsing, report it. // If there was an error during parsing, report it.
@@ -45,7 +50,7 @@ int main(int argc, char **argv) {
if (verbose) { if (verbose) {
std::cerr << "about to print '" << toPrint << "' " << repeats << " times"; std::cerr << "about to print '" << toPrint << "' " << repeats << " times";
if (psnl->found()) { if (psnl->found()) {
std::cerr << ", then '" << maybePrint << "'"; std::cerr << ", then '" << maybePrint << "'";
} }
std::cerr << std::endl; std::cerr << std::endl;
} }

View File

@@ -11,11 +11,16 @@ int main(int argc, char **argv) {
std::string maybePrint; std::string maybePrint;
int repeats = 1; int repeats = 1;
// Inform the parser of the program data. // Inform the parser of the program data. It will do type-specific conversion
// It will do type-specific conversion // from string.
// An option invoked with `--repeat N`
p.add_option(repeats, "--repeat"); p.add_option(repeats, "--repeat");
// A flag invoked with `--verbose` or `-v`
p.add_flag(verbose, "--verbose", "-v"); p.add_flag(verbose, "--verbose", "-v");
// a required positional argument (position 1)
p.add_positional(toPrint)->required(); p.add_positional(toPrint)->required();
// an optional positional argument (position 2)
auto psnl = p.add_positional(maybePrint); auto psnl = p.add_positional(maybePrint);
// If there was an error during parsing, report it. // If there was an error during parsing, report it.