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 ()
 ~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 78 of file Formatter.h.

Constructor & Destructor Documentation

◆ ConfigFormatter()

CLI::ConfigFormatter::ConfigFormatter ( )

Definition at line 59 of file Formatter.cpp.

59 {
60 arrayDelimiter(' ');
61 }

◆ ~ConfigFormatter()

CLI::ConfigFormatter::~ConfigFormatter ( )
override

Definition at line 63 of file Formatter.cpp.

63 {
64 }

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 67 of file Formatter.cpp.

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

References Color::FG_DEFAULT, Color::FG_RED, Color::operator+(), and 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: