Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config_to_str(default_also = true) with subcommands has inconsistency in emitting sections #1007

Closed
R2RT opened this issue Feb 16, 2024 · 0 comments

Comments

@R2RT
Copy link

R2RT commented Feb 16, 2024

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";
@phlptp phlptp mentioned this issue Jul 27, 2024
phlptp added a commit that referenced this issue Jul 29, 2024
Fix docstring related to #1052 
Fix config_to_string with defaults #1007

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
@phlptp phlptp closed this as completed Aug 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants