1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-04-30 04:33:53 +00:00

Added required subcommand

This commit is contained in:
Henry Fredrick Schreiner 2017-02-13 22:30:16 -05:00
parent 54a2f720d6
commit b359039332
2 changed files with 20 additions and 4 deletions

View File

@ -1,3 +1,7 @@
## Version 0.5 (in progress)
* Added `require_subcommand` to `App`, to simplify forcing subcommands. Do not "chain" with `add_subcommand`, since that is the subcommand, not the master `App`. Untested.
## Version 0.4 ## Version 0.4
* Updates to help print * Updates to help print

View File

@ -47,6 +47,7 @@ protected:
std::vector<App_p> subcommands; std::vector<App_p> subcommands;
bool parsed {false}; bool parsed {false};
App* subcommand {nullptr}; App* subcommand {nullptr};
bool required_subcommand = false;
std::string progname {"program"}; std::string progname {"program"};
Option* help_flag {nullptr}; Option* help_flag {nullptr};
@ -117,7 +118,6 @@ public:
return subcommands.back().get(); return subcommands.back().get();
} }
/// Add an option, will automatically understand the type for common types. /// Add an option, will automatically understand the type for common types.
/** To use, create a variable with the expected type, and pass it in after the name. /** To use, create a variable with the expected type, and pass it in after the name.
* After start is called, you can use count to see if the value was passed, and * After start is called, you can use count to see if the value was passed, and
@ -332,6 +332,7 @@ public:
} }
/// This allows subclasses to inject code before callbacks but after parse /// This allows subclasses to inject code before callbacks but after parse
/// This does not run if any errors or help is thrown.
virtual void pre_callback() {} virtual void pre_callback() {}
/// Parses the command line - throws errors /// Parses the command line - throws errors
@ -411,8 +412,12 @@ public:
pos=true; pos=true;
} }
if(subcommands.size() > 0) if(subcommands.size() > 0) {
out << " [SUBCOMMANDS]"; if(required_subcommand)
out << " SUBCOMMAND";
else
out << " [SUBCOMMAND]";
}
out << std::endl << std::endl; out << std::endl << std::endl;
@ -459,6 +464,10 @@ public:
return name; return name;
} }
/// Require a subcommand to be given (does not affect help call)
void require_subcommand(bool value = true) {
required_subcommand = value;
}
protected: protected:
@ -579,6 +588,9 @@ protected:
throw ExcludesError(opt->get_name(), opt_ex->get_name()); throw ExcludesError(opt->get_name(), opt_ex->get_name());
} }
if(required_subcommand && subcommand == nullptr)
throw RequiredError("Subcommand required");
if(positionals.size()>0) if(positionals.size()>0)
throw PositionalError("[" + detail::join(positionals) + "]"); throw PositionalError("[" + detail::join(positionals) + "]");