From fda42d27464f2fdd1f2689467966c910a28920ea Mon Sep 17 00:00:00 2001 From: tansy Date: Mon, 8 Jan 2024 18:41:34 +0000 Subject: [PATCH 1/4] Full help/usage display and modification example --- examples/help_usage.cpp | 67 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 examples/help_usage.cpp diff --git a/examples/help_usage.cpp b/examples/help_usage.cpp new file mode 100644 index 000000000..b0e268269 --- /dev/null +++ b/examples/help_usage.cpp @@ -0,0 +1,67 @@ +// Copyright (c) 2017-2023, University of Cincinnati, developed by Henry Schreiner +// under NSF AWARD 1414736 and by the respective contributors. +// All rights reserved. +// +// SPDX-License-Identifier: BSD-3-Clause + +#include +#include + +int main(int argc, char** argv) +{ + std::string input_file_name, output_file_name; + int level, subopt; + + // app caption + CLI::App app{ "CLI11 help" }; + + app.require_subcommand(1); + // subcommands options and flags + CLI::App* const encode = app.add_subcommand("e", "encode")->ignore_case(); // ignore case + encode->add_option("input", input_file_name, "input file")->option_text(" ")->required()->check(CLI::ExistingFile); // file must exist + encode->add_option("output", output_file_name, "output file")->option_text(" ")->required(); // required option + encode->add_option("-l, --level", level, "encoding level")->option_text("[1..9]")->check(CLI::Range(1, 9))->default_val(5); // limit parameter range + encode->add_option("-R, --remove", level, "remove input file"); // no parameter option + encode->add_flag("-s, --suboption", subopt, "suboption")->option_text(" "); + + CLI::App* const decode = app.add_subcommand("d", "decode")->ignore_case(); + decode->add_option("input", input_file_name, "input file")->option_text(" ")->required()->check(CLI::ExistingFile); + decode->add_option("output", output_file_name, "output file")->option_text(" ")->required(); + + // Usage message modification + std::string usage_msg = "Usage: " + std::string(argv[0]) + " [options] "; + app.usage(usage_msg); + // flag to display full help at once + app.set_help_flag(""); + app.set_help_all_flag("-h, --help"); + + CLI11_PARSE(app, argc, argv); + + return 0; +} + +/* +$ ./help_usage -h +CLI11 help +Usage: help_usage [options] + +Options: + -h,--help + +Subcommands: +e + encode + Positionals: + input input file + output output file + Options: + -l,--level [1..9] encoding level + -K,--remove INT remove input file + -s,--suboption suboption + +d + decode + Positionals: + input input file + output output file +*/ From da0cab35bf267b54e5f940b8f9da0c344eebc260 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 8 Jan 2024 17:46:36 +0000 Subject: [PATCH 2/4] style: pre-commit.ci fixes --- examples/help_usage.cpp | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/examples/help_usage.cpp b/examples/help_usage.cpp index b0e268269..1f3695c40 100644 --- a/examples/help_usage.cpp +++ b/examples/help_usage.cpp @@ -7,24 +7,29 @@ #include #include -int main(int argc, char** argv) -{ +int main(int argc, char **argv) { std::string input_file_name, output_file_name; int level, subopt; // app caption - CLI::App app{ "CLI11 help" }; + CLI::App app{"CLI11 help"}; app.require_subcommand(1); // subcommands options and flags - CLI::App* const encode = app.add_subcommand("e", "encode")->ignore_case(); // ignore case - encode->add_option("input", input_file_name, "input file")->option_text(" ")->required()->check(CLI::ExistingFile); // file must exist - encode->add_option("output", output_file_name, "output file")->option_text(" ")->required(); // required option - encode->add_option("-l, --level", level, "encoding level")->option_text("[1..9]")->check(CLI::Range(1, 9))->default_val(5); // limit parameter range - encode->add_option("-R, --remove", level, "remove input file"); // no parameter option + CLI::App *const encode = app.add_subcommand("e", "encode")->ignore_case(); // ignore case + encode->add_option("input", input_file_name, "input file") + ->option_text(" ") + ->required() + ->check(CLI::ExistingFile); // file must exist + encode->add_option("output", output_file_name, "output file")->option_text(" ")->required(); // required option + encode->add_option("-l, --level", level, "encoding level") + ->option_text("[1..9]") + ->check(CLI::Range(1, 9)) + ->default_val(5); // limit parameter range + encode->add_option("-R, --remove", level, "remove input file"); // no parameter option encode->add_flag("-s, --suboption", subopt, "suboption")->option_text(" "); - CLI::App* const decode = app.add_subcommand("d", "decode")->ignore_case(); + CLI::App *const decode = app.add_subcommand("d", "decode")->ignore_case(); decode->add_option("input", input_file_name, "input file")->option_text(" ")->required()->check(CLI::ExistingFile); decode->add_option("output", output_file_name, "output file")->option_text(" ")->required(); From 2d3a000014d13c8b2cc421397487388828232efd Mon Sep 17 00:00:00 2001 From: Philip Top Date: Mon, 7 Oct 2024 08:15:06 -0700 Subject: [PATCH 3/4] Update examples/help_usage.cpp --- examples/help_usage.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/help_usage.cpp b/examples/help_usage.cpp index 1f3695c40..b6c48f370 100644 --- a/examples/help_usage.cpp +++ b/examples/help_usage.cpp @@ -26,7 +26,7 @@ int main(int argc, char **argv) { ->option_text("[1..9]") ->check(CLI::Range(1, 9)) ->default_val(5); // limit parameter range - encode->add_option("-R, --remove", level, "remove input file"); // no parameter option + encode->add_flag("-R, --remove", "remove input file"); // no parameter option encode->add_flag("-s, --suboption", subopt, "suboption")->option_text(" "); CLI::App *const decode = app.add_subcommand("d", "decode")->ignore_case(); From 53258e3f05bab7356071bb825fc358f42e60fec0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 7 Oct 2024 15:15:17 +0000 Subject: [PATCH 4/4] style: pre-commit.ci fixes --- examples/help_usage.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/help_usage.cpp b/examples/help_usage.cpp index b6c48f370..6b7d19011 100644 --- a/examples/help_usage.cpp +++ b/examples/help_usage.cpp @@ -25,7 +25,7 @@ int main(int argc, char **argv) { encode->add_option("-l, --level", level, "encoding level") ->option_text("[1..9]") ->check(CLI::Range(1, 9)) - ->default_val(5); // limit parameter range + ->default_val(5); // limit parameter range encode->add_flag("-R, --remove", "remove input file"); // no parameter option encode->add_flag("-s, --suboption", subopt, "suboption")->option_text(" ");