CLI11
CLI11 copied to clipboard
config_to_str(default_also = true) with subcommands has inconsistency in emitting sections
Given the code:
#include "CLI11.hpp"
int main(int argc, char* argv[])
{
bool printConfig = false;
std::string sub_arg;
CLI::App cli_global {"Demo app"};
cli_global.set_config("--config", "config.cfg", "Load configuration from a file", true);
cli_global.set_help_flag("--help-group", "Print help of single group");
cli_global.add_flag("--print-config", printConfig, "Print current config to stdout and exit\n")->configurable(false);
cli_global.add_option("base_arg", sub_arg, "Argument for root");
auto& cli_sub = *cli_global.add_subcommand("sub", "Some subcommand");
cli_sub.add_option("sub_arg", sub_arg, "Argument for subcommand");
cli_sub.add_option("sub_arg2", sub_arg, "Argument for subcommand2");
cli_sub.configurable();
auto& cli_sub2 = *cli_global.add_subcommand("sub2", "Some subcommand");
cli_sub2.add_option("sub_arg", sub_arg, "Argument for subcommand");
cli_sub2.add_option("sub_arg2", sub_arg, "Argument for subcommand2");
cli_sub2.configurable();
CLI11_PARSE(cli_global, argc, argv);
if(printConfig)
{
std::cout << cli_global.config_to_str(true, true) << '\n';
}
return 0;
}
Output for main.exe --print-config
# Demo app
# Argument for root
base_arg=""
# Some subcommand
# Argument for subcommand
sub.sub_arg=""
# Argument for subcommand2
sub.sub_arg2=""
# Some subcommand
# Argument for subcommand
sub2.sub_arg=""
# Argument for subcommand2
sub2.sub_arg2=""
Expected, can be achieved by listing all sections in command line: main.exe sub sub2 --print-config
# Demo app
# Argument for root
base_arg=""
[sub]
# Some subcommand
# Argument for subcommand
sub_arg=""
# Argument for subcommand2
sub_arg2=""
[sub2]
# Some subcommand
# Argument for subcommand
sub_arg=""
# Argument for subcommand2
sub_arg2=""
Simple fix, I am not sure if it covers all cases:
diff --git a/include/CLI/impl/Config_inl.hpp b/include/CLI/impl/Config_inl.hpp
index 92537c0..cb83635 100644
--- a/include/CLI/impl/Config_inl.hpp
+++ b/include/CLI/impl/Config_inl.hpp
@@ -581,7 +581,7 @@ ConfigBase::to_config(const App *app, bool default_also, bool write_description,
std::string subname = subcom->get_name();
clean_name_string(subname, keyChars);
- if(subcom->get_configurable() && app->got_subcommand(subcom)) {
+ if(subcom->get_configurable() && (default_also || app->got_subcommand(subcom))) {
if(!prefix.empty() || app->get_parent() == nullptr) {
out << '[' << prefix << subname << "]\n";