1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-29 20:23:55 +00:00

Adding set_name and support for names to app

This commit is contained in:
Henry Fredrick Schreiner 2018-04-11 13:00:14 +02:00 committed by Henry Schreiner
parent 8e650c3873
commit c63288a91c
3 changed files with 36 additions and 12 deletions

View File

@ -2,6 +2,7 @@
* Make unlimited positionals vs. unlimited options more intuitive [#102] * Make unlimited positionals vs. unlimited options more intuitive [#102]
* Add missing getters `get_options` and `get_description` to App [#105] * Add missing getters `get_options` and `get_description` to App [#105]
* The app name now can be set, and will override the auto name if present [#105]
[#102]: https://github.com/CLIUtils/CLI11/issues/102 [#102]: https://github.com/CLIUtils/CLI11/issues/102
[#105]: https://github.com/CLIUtils/CLI11/issues/105 [#105]: https://github.com/CLIUtils/CLI11/issues/105

View File

@ -64,8 +64,8 @@ class App {
/// @name Basics /// @name Basics
///@{ ///@{
/// Subcommand name or program name (from parser) /// Subcommand name or program name (from parser if name is empty)
std::string name_{"program"}; std::string name_;
/// Description of the current program/subcommand /// Description of the current program/subcommand
std::string description_; std::string description_;
@ -193,7 +193,8 @@ class App {
///@{ ///@{
/// Create a new program. Pass in the same arguments as main(), along with a help string. /// Create a new program. Pass in the same arguments as main(), along with a help string.
App(std::string description_ = "") : App(description_, nullptr) { App(std::string description_ = "", std::string name = "") : App(description_, nullptr) {
name_ = name;
set_help_flag("-h,--help", "Print this help message and exit"); set_help_flag("-h,--help", "Print this help message and exit");
} }
@ -211,6 +212,12 @@ class App {
return this; return this;
} }
/// Set a name for the app (empty will use parser to set the name)
App *set_name(std::string name = "") {
name_ = name;
return this;
}
/// Remove the error when extras are left over on the command line. /// Remove the error when extras are left over on the command line.
App *allow_extras(bool allow = true) { App *allow_extras(bool allow = true) {
allow_extras_ = allow; allow_extras_ = allow;
@ -723,7 +730,10 @@ class App {
/// Parses the command line - throws errors /// Parses the command line - throws errors
/// This must be called after the options are in but before the rest of the program. /// This must be called after the options are in but before the rest of the program.
void parse(int argc, char **argv) { void parse(int argc, char **argv) {
// If the name is not set, read from command line
if(name_.empty())
name_ = argv[0]; name_ = argv[0];
std::vector<std::string> args; std::vector<std::string> args;
for(int i = argc - 1; i > 0; i--) for(int i = argc - 1; i > 0; i--)
args.emplace_back(argv[i]); args.emplace_back(argv[i]);
@ -890,7 +900,7 @@ class App {
std::stringstream out; std::stringstream out;
out << description_ << std::endl; out << description_ << std::endl;
out << "Usage: " << prev; out << "Usage:" << (prev.empty() ? "" : " ") << prev;
// Check for options_ // Check for options_
bool npos = false; bool npos = false;
@ -1030,6 +1040,7 @@ class App {
/// Get a pointer to the config option. (const) /// Get a pointer to the config option. (const)
const Option *get_config_ptr() const { return config_ptr_; } const Option *get_config_ptr() const { return config_ptr_; }
/// Get the name of the current app /// Get the name of the current app
std::string get_name() const { return name_; } std::string get_name() const { return name_; }

View File

@ -36,7 +36,7 @@ TEST(THelp, Footer) {
} }
TEST(THelp, OptionalPositional) { TEST(THelp, OptionalPositional) {
CLI::App app{"My prog"}; CLI::App app{"My prog", "program"};
std::string x; std::string x;
app.add_option("something", x, "My option here"); app.add_option("something", x, "My option here");
@ -71,7 +71,7 @@ TEST(THelp, Hidden) {
} }
TEST(THelp, OptionalPositionalAndOptions) { TEST(THelp, OptionalPositionalAndOptions) {
CLI::App app{"My prog"}; CLI::App app{"My prog", "AnotherProgram"};
app.add_flag("-q,--quick"); app.add_flag("-q,--quick");
std::string x; std::string x;
@ -82,7 +82,7 @@ TEST(THelp, OptionalPositionalAndOptions) {
EXPECT_THAT(help, HasSubstr("My prog")); EXPECT_THAT(help, HasSubstr("My prog"));
EXPECT_THAT(help, HasSubstr("-h,--help")); EXPECT_THAT(help, HasSubstr("-h,--help"));
EXPECT_THAT(help, HasSubstr("Options:")); EXPECT_THAT(help, HasSubstr("Options:"));
EXPECT_THAT(help, HasSubstr("Usage: program [OPTIONS] [something]")); EXPECT_THAT(help, HasSubstr("Usage: AnotherProgram [OPTIONS] [something]"));
} }
TEST(THelp, RequiredPositionalAndOptions) { TEST(THelp, RequiredPositionalAndOptions) {
@ -98,7 +98,7 @@ TEST(THelp, RequiredPositionalAndOptions) {
EXPECT_THAT(help, HasSubstr("-h,--help")); EXPECT_THAT(help, HasSubstr("-h,--help"));
EXPECT_THAT(help, HasSubstr("Options:")); EXPECT_THAT(help, HasSubstr("Options:"));
EXPECT_THAT(help, HasSubstr("Positionals:")); EXPECT_THAT(help, HasSubstr("Positionals:"));
EXPECT_THAT(help, HasSubstr("Usage: program [OPTIONS] something")); EXPECT_THAT(help, HasSubstr("Usage: [OPTIONS] something"));
} }
TEST(THelp, MultiOpts) { TEST(THelp, MultiOpts) {
@ -111,7 +111,7 @@ TEST(THelp, MultiOpts) {
EXPECT_THAT(help, HasSubstr("My prog")); EXPECT_THAT(help, HasSubstr("My prog"));
EXPECT_THAT(help, Not(HasSubstr("Positionals:"))); EXPECT_THAT(help, Not(HasSubstr("Positionals:")));
EXPECT_THAT(help, HasSubstr("Usage: program [OPTIONS]")); EXPECT_THAT(help, HasSubstr("Usage: [OPTIONS]"));
EXPECT_THAT(help, HasSubstr("INT x 2")); EXPECT_THAT(help, HasSubstr("INT x 2"));
EXPECT_THAT(help, HasSubstr("INT ...")); EXPECT_THAT(help, HasSubstr("INT ..."));
} }
@ -128,6 +128,7 @@ TEST(THelp, VectorOpts) {
TEST(THelp, MultiPosOpts) { TEST(THelp, MultiPosOpts) {
CLI::App app{"My prog"}; CLI::App app{"My prog"};
app.set_name("program");
std::vector<int> x, y; std::vector<int> x, y;
app.add_option("quick", x, "Disc")->expected(2); app.add_option("quick", x, "Disc")->expected(2);
app.add_option("vals", y, "Other"); app.add_option("vals", y, "Other");
@ -243,12 +244,12 @@ TEST(THelp, Subcom) {
app.add_subcommand("sub2"); app.add_subcommand("sub2");
std::string help = app.help(); std::string help = app.help();
EXPECT_THAT(help, HasSubstr("Usage: program [OPTIONS] [SUBCOMMAND]")); EXPECT_THAT(help, HasSubstr("Usage: [OPTIONS] [SUBCOMMAND]"));
app.require_subcommand(); app.require_subcommand();
help = app.help(); help = app.help();
EXPECT_THAT(help, HasSubstr("Usage: program [OPTIONS] SUBCOMMAND")); EXPECT_THAT(help, HasSubstr("Usage: [OPTIONS] SUBCOMMAND"));
help = sub1->help(); help = sub1->help();
EXPECT_THAT(help, HasSubstr("Usage: sub1")); EXPECT_THAT(help, HasSubstr("Usage: sub1"));
@ -263,6 +264,17 @@ TEST(THelp, Subcom) {
EXPECT_THAT(help, HasSubstr("Usage: ./myprogram sub2")); EXPECT_THAT(help, HasSubstr("Usage: ./myprogram sub2"));
} }
TEST(THelp, MasterName) {
CLI::App app{"My prog", "MyRealName"};
char x[] = "./myprogram";
std::vector<char *> args = {x};
app.parse((int)args.size(), args.data());
EXPECT_THAT(app.help(), HasSubstr("Usage: MyRealName"));
}
TEST(THelp, IntDefaults) { TEST(THelp, IntDefaults) {
CLI::App app{"My prog"}; CLI::App app{"My prog"};