1
0
mirror of https://github.com/CLIUtils/CLI11.git synced 2025-05-01 21:23:52 +00:00

Support for printing subcommands

This commit is contained in:
Henry Fredrick Schreiner 2017-03-23 09:19:28 -04:00
parent 98cdc6eb09
commit 4d61a4530c
4 changed files with 26 additions and 10 deletions

View File

@ -1,8 +1,6 @@
## Version 0.7 (in progress) ## Version 0.7 (in progress)
* Allow comments in ini files (lines starting with `;`) * Allow comments in ini files (lines starting with `;`)
* Ini files support flags * Ini files support flags, vectors, subcommands
* Ini files support vectors
* Ini files support subcommands (only read)
* Added CodeCov code coverage reports * Added CodeCov code coverage reports
* Lots of small bugfixes related to adding tests to increase coverage * Lots of small bugfixes related to adding tests to increase coverage
* Error handling now uses scoped enum in errors * Error handling now uses scoped enum in errors

View File

@ -206,7 +206,8 @@ in_subcommand = Wow
sub.subcommand = true sub.subcommand = true
``` ```
Spaces before and after the name and argument are ignored. Multiple arguments are separated by spaces. One set of quotes will be removed, preserving spaces (the same way the command line works). Boolean options can be `true`, `on`, `1`, `yes`; or `false`, `off`, `0`, `no` (case insensitive). Sections (and `.` separated names) are treated as subcommands (note: this does not mean that subcommand was passed, it just sets the "defaults". Spaces before and after the name and argument are ignored. Multiple arguments are separated by spaces. One set of quotes will be removed, preserving spaces (the same way the command line works). Boolean options can be `true`, `on`, `1`, `yes`; or `false`, `off`, `0`, `no` (case insensitive). Sections (and `.` separated names) are treated as subcommands (note: this does not mean that subcommand was passed, it just sets the "defaults". To print a configuration file from the passed
arguments, use `.config_to_str(default_also=false)`, where `default_also` will also show any defaulted arguments.
## Subclassing ## Subclassing

View File

@ -547,34 +547,37 @@ public:
/// @name Help /// @name Help
///@{ ///@{
/// Produce a string that could be read in as a config of the current values of the App. Set default_also to include default arguments. /// Produce a string that could be read in as a config of the current values of the App. Set default_also to include default arguments. Prefix will add a string to the beginning of each option.
std::string config_to_str(bool default_also=false) const { std::string config_to_str(bool default_also=false, std::string prefix="") const {
std::stringstream out; std::stringstream out;
for(const Option_p &opt : options_) { for(const Option_p &opt : options_) {
// Only process option with a long-name // Only process option with a long-name
if(opt->lnames_.size() > 0) { if(opt->lnames_.size() > 0) {
std::string name = prefix + opt->lnames_[0];
// Non-flags // Non-flags
if(opt->get_expected() != 0) { if(opt->get_expected() != 0) {
// If the option was found on command line // If the option was found on command line
if(opt->count() > 0) if(opt->count() > 0)
out << opt->lnames_[0] << "=" << detail::join(opt->results()) << std::endl; out << name << "=" << detail::join(opt->results()) << std::endl;
// If the option has a default and is requested by optional argument // If the option has a default and is requested by optional argument
else if(default_also && opt->defaultval_ != "") else if(default_also && opt->defaultval_ != "")
out << opt->lnames_[0] << "=" << opt->defaultval_ << std::endl; out << name << "=" << opt->defaultval_ << std::endl;
// Flag, one passed // Flag, one passed
} else if(opt->count() == 1) { } else if(opt->count() == 1) {
out << opt->lnames_[0] << "=true" << std::endl; out << name << "=true" << std::endl;
// Flag, multiple passed // Flag, multiple passed
} else if(opt->count() > 1) { } else if(opt->count() > 1) {
out << opt->lnames_[0] << "=" << opt->count() << std::endl; out << name << "=" << opt->count() << std::endl;
} }
} }
} }
for(const App_p &subcom : subcommands_)
out << subcom->config_to_str(default_also, prefix + subcom->name_ + ".");
return out.str(); return out.str();
} }

View File

@ -549,3 +549,17 @@ TEST_F(TApp, IniOutputDefault) {
str = app.config_to_str(true); str = app.config_to_str(true);
EXPECT_THAT(str, HasSubstr("simple=7")); EXPECT_THAT(str, HasSubstr("simple=7"));
} }
TEST_F(TApp, IniOutputSubcom) {
app.add_flag("--simple");
auto subcom = app.add_subcommand("other");
subcom->add_flag("--newer");
args = {"--simple", "other", "--newer"};
run();
std::string str = app.config_to_str();
EXPECT_THAT(str, HasSubstr("simple=true"));
EXPECT_THAT(str, HasSubstr("other.newer=true"));
}