SNode.C
Loading...
Searching...
No Matches
CLI::ConfigFormatter Class Reference

#include <Formatter.h>

Inheritance diagram for CLI::ConfigFormatter:
Collaboration diagram for CLI::ConfigFormatter:

Public Member Functions

 ~ConfigFormatter () override
 

Private Member Functions

std::string to_config (const App *app, bool default_also, bool write_description, std::string prefix) const override
 

Detailed Description

Definition at line 70 of file Formatter.h.

Constructor & Destructor Documentation

◆ ~ConfigFormatter()

CLI::ConfigFormatter::~ConfigFormatter ( )
override

Definition at line 56 of file Formatter.cpp.

56 {
57 }

Member Function Documentation

◆ to_config()

CLI11_INLINE std::string CLI::ConfigFormatter::to_config ( const App *  app,
bool  default_also,
bool  write_description,
std::string  prefix 
) const
overrideprivate

Definition at line 60 of file Formatter.cpp.

60 {
61 std::stringstream out;
62 std::string commentLead;
63 commentLead.push_back(commentChar);
64 commentLead.push_back(' ');
65
66 std::vector<std::string> groups = app->get_groups();
67 bool defaultUsed = false;
68 groups.insert(groups.begin(), std::string("Options"));
69 for (const auto& group : groups) {
70 if (group == "Options" || group.empty()) {
71 if (defaultUsed) {
72 continue;
73 }
74 defaultUsed = true;
75 }
76 if (write_description && group != "Options" && !group.empty() &&
77 !app->get_options([group](const Option* opt) -> bool {
78 return opt->get_group() == group && opt->get_configurable();
79 })
80 .empty()) {
81 out << '\n' << commentChar << commentLead << group << "\n";
82 }
83 for (const Option* opt : app->get_options({})) {
84 // Only process options that are configurable
85 if (opt->get_configurable()) {
86 if (opt->get_group() != group) {
87 if (!(group == "Options" && opt->get_group().empty())) {
88 continue;
89 }
90 }
91 const std::string name = prefix + opt->get_single_name();
92 std::string value =
93 detail::ini_join(opt->reduced_results(), arraySeparator, arrayStart, arrayEnd, stringQuote, characterQuote);
94
95 std::string defaultValue{};
96 if (default_also) {
97 static_assert(std::string::npos + static_cast<std::string::size_type>(1) == 0,
98 "std::string::npos + static_cast<std::string::size_type>(1) != 0");
99 if (!value.empty() && detail::convert_arg_for_ini(opt->get_default_str(), stringQuote, characterQuote) == value) {
100 value.clear();
101 }
102 if (!opt->get_default_str().empty()) {
103 defaultValue = detail::convert_arg_for_ini(opt->get_default_str(), stringQuote, characterQuote);
104 if (defaultValue == "\"default\"") {
105 defaultValue = "default";
106 }
107 } else if (opt->get_expected_min() == 0) {
108 defaultValue = "false";
109 } else if (opt->get_run_callback_for_default()) {
110 defaultValue = "\"\""; // empty string default value
111 } else if (opt->get_required()) {
112 defaultValue = "\"<REQUIRED>\"";
113 } else {
114 defaultValue = "\"\"";
115 }
116 }
117 if (write_description && opt->has_description() && (default_also || !value.empty())) {
118 out << commentLead << detail::fix_newlines(commentLead, opt->get_description()) << '\n';
119 }
120 if (default_also) {
121 out << commentChar << name << valueDelimiter << defaultValue << "\n";
122 }
123 if (!value.empty()) {
124 if (!opt->get_fnames().empty()) {
125 value = opt->get_flag_value(name, value);
126 }
127 if (value == "\"default\"") {
128 value = "default";
129 }
130 out << name << valueDelimiter << value << "\n\n";
131 } else {
132 out << '\n';
133 }
134 }
135 }
136 }
137 auto subcommands = app->get_subcommands({});
138 for (const App* subcom : subcommands) {
139 if (subcom->get_name().empty()) {
140 if (write_description && !subcom->get_group().empty()) {
141 out << '\n' << commentLead << subcom->get_group() << " Options\n";
142 }
143 out << to_config(subcom, default_also, write_description, prefix);
144 }
145 }
146
147 for (const App* subcom : subcommands) {
148 if (!subcom->get_name().empty()) {
149 if (subcom->get_configurable() && app->got_subcommand(subcom)) {
150 if (!prefix.empty() || app->get_parent() == nullptr) {
151 out << '[' << prefix << subcom->get_name() << "]\n";
152 } else {
153 std::string subname = app->get_name() + parentSeparatorChar + subcom->get_name();
154 const auto* p = app->get_parent();
155 while (p->get_parent() != nullptr) {
156 subname = p->get_name() + parentSeparatorChar + subname;
157 p = p->get_parent();
158 }
159 out << '[' << subname << "]\n";
160 }
161 out << to_config(subcom, default_also, write_description, "");
162 } else {
163 out << to_config(subcom, default_also, write_description, prefix + subcom->get_name() + parentSeparatorChar);
164 }
165 }
166 }
167
168 std::string outString;
169 if (write_description && !out.str().empty()) {
170 outString =
171 commentChar + std::string("#") + commentLead + detail::fix_newlines(commentChar + commentLead, app->get_description());
172 }
173
174 return outString + out.str();
175 }
std::string to_config(const App *app, bool default_also, bool write_description, std::string prefix) const override
Definition Formatter.cpp:60

References to_config().

Referenced by to_config().

Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following files: