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

#include <Formatter.h>

Inheritance diagram for CLI::HelpFormatter:
Collaboration diagram for CLI::HelpFormatter:

Public Member Functions

 ~HelpFormatter () override
 

Private Member Functions

std::string make_group (std::string group, bool is_positional, std::vector< const Option * > opts) const override
 
std::string make_description (const App *app) const override
 
std::string make_usage (const App *app, std::string name) const override
 
std::string make_subcommands (const App *app, AppFormatMode mode) const override
 
std::string make_subcommand (const App *sub) const override
 
std::string make_expanded (const App *sub) const override
 
std::string make_option_opts (const Option *opt) const override
 

Detailed Description

Definition at line 56 of file Formatter.h.

Constructor & Destructor Documentation

◆ ~HelpFormatter()

CLI::HelpFormatter::~HelpFormatter ( )
override

Definition at line 149 of file Formatter.cpp.

149 {
150 }

Member Function Documentation

◆ make_description()

CLI11_INLINE std::string CLI::HelpFormatter::make_description ( const App * app) const
overrideprivate

Definition at line 164 of file Formatter.cpp.

164 {
165 std::string desc = app->get_description();
166 auto min_options = app->get_require_option_min();
167 auto max_options = app->get_require_option_max();
168 // ############## Next line changed (if statement removed)
169 if ((max_options == min_options) && (min_options > 0)) {
170 if (min_options == 1) {
171 desc += " \n[Exactly 1 of the following options is required]";
172 } else {
173 desc += " \n[Exactly " + std::to_string(min_options) + " options from the following list are required]";
174 }
175 } else if (max_options > 0) {
176 if (min_options > 0) {
177 desc += " \n[Between " + std::to_string(min_options) + " and " + std::to_string(max_options) +
178 " of the follow options are required]";
179 } else {
180 desc += " \n[At most " + std::to_string(max_options) + " of the following options are allowed]";
181 }
182 } else if (min_options > 0) {
183 desc += " \n[At least " + std::to_string(min_options) + " of the following options are required]";
184 }
185 return (!desc.empty()) ? desc + "\n" : std::string{};
186 }

◆ make_expanded()

CLI11_INLINE std::string CLI::HelpFormatter::make_expanded ( const App * sub) const
overrideprivate

Definition at line 318 of file Formatter.cpp.

318 {
319 std::stringstream out;
320 // ########## Next lines changed
321 const Option* disabledOpt = sub->get_option_no_throw("--disabled");
322 out << sub->get_display_name(true) + " [OPTIONS]" + (!sub->get_subcommands({}).empty() ? " [SECTIONS]" : "") +
323 ((disabledOpt != nullptr ? disabledOpt->as<bool>() : false) ? ""
324 : (sub->get_required() ? " " + get_label("REQUIRED") : ""))
325 << "\n";
326
327 out << make_description(sub);
328 if (sub->get_name().empty() && !sub->get_aliases().empty()) {
329 detail::format_aliases(out, sub->get_aliases(), column_width_ + 2);
330 }
331 out << make_positionals(sub);
332 out << make_groups(sub, AppFormatMode::Sub);
333 out << make_subcommands(sub, AppFormatMode::Sub);
334
335 // Drop blank spaces
336 std::string tmp = out.str();
337 tmp.pop_back();
338
339 // Indent all but the first line (the name)
340 return detail::find_and_replace(tmp, "\n", "\n ") + "\n";
341 }
std::string make_description(const App *app) const override
std::string make_subcommands(const App *app, AppFormatMode mode) const override

◆ make_group()

CLI11_INLINE std::string CLI::HelpFormatter::make_group ( std::string group,
bool is_positional,
std::vector< const Option * > opts ) const
overrideprivate

Definition at line 152 of file Formatter.cpp.

152 {
153 std::stringstream out;
154
155 // ############## Next line changed
156 out << group << ":\n";
157 for (const Option* opt : opts) {
158 out << make_option(opt, is_positional);
159 }
160
161 return out.str();
162 }

◆ make_option_opts()

CLI11_INLINE std::string CLI::HelpFormatter::make_option_opts ( const Option * opt) const
overrideprivate

Definition at line 343 of file Formatter.cpp.

343 {
344 std::stringstream out;
345
346 if (!opt->get_option_text().empty()) {
347 out << " " << opt->get_option_text();
348 } else {
349 if (opt->get_type_size() != 0) {
350 if (!opt->get_type_name().empty()) {
351 // ########## Next line changed
352 out << ((opt->get_items_expected_max() == 0) ? "=" : " ") << get_label(opt->get_type_name());
353 }
354 if (!opt->get_default_str().empty()) {
355 out << " [" << opt->get_default_str() << "]";
356 }
357 if (opt->get_expected_max() == detail::expected_max_vector_size) {
358 out << " ... ";
359 } else if (opt->get_expected_min() > 1) {
360 out << " x " << opt->get_expected();
361 }
362 if (opt->get_required()) {
363 out << " " << get_label("REQUIRED");
364 }
365 if (opt->get_configurable()) {
366 out << " " << get_label("PERSISTENT");
367 }
368 }
369 if (!opt->get_envname().empty()) {
370 out << " (" << get_label("Env") << ":" << opt->get_envname() << ") ";
371 }
372 if (!opt->get_needs().empty()) {
373 out << " " << get_label("Needs") << ":";
374 for (const Option* op : opt->get_needs()) {
375 out << " " << op->get_name();
376 }
377 out << " ";
378 }
379 if (!opt->get_excludes().empty()) {
380 out << " " << get_label("Excludes") << ":";
381 for (const Option* op : opt->get_excludes()) {
382 out << " " << op->get_name();
383 }
384 out << " ";
385 }
386 }
387 return out.str();
388 }

◆ make_subcommand()

CLI11_INLINE std::string CLI::HelpFormatter::make_subcommand ( const App * sub) const
overrideprivate

Definition at line 305 of file Formatter.cpp.

305 {
306 std::stringstream out;
307 // ########## Next lines changed
308 const Option* disabledOpt = sub->get_option_no_throw("--disabled");
309 detail::format_help(out,
310 sub->get_display_name(true) + ((disabledOpt != nullptr ? disabledOpt->as<bool>() : false)
311 ? ""
312 : (sub->get_required() ? " " + get_label("REQUIRED") : "")),
313 sub->get_description(),
314 column_width_);
315 return out.str();
316 }

◆ make_subcommands()

CLI11_INLINE std::string CLI::HelpFormatter::make_subcommands ( const App * app,
AppFormatMode mode ) const
overrideprivate

Definition at line 250 of file Formatter.cpp.

250 {
251 std::stringstream out;
252
253 const std::vector<const App*> subcommands = app->get_subcommands([](const App* subc) {
254 return !subc->get_disabled() && !subc->get_name().empty();
255 });
256
257 // Make a list in alphabetic order of the groups seen
258 std::set<std::string> subcmd_groups_seen;
259 for (const App* com : subcommands) {
260 if (com->get_name().empty()) {
261 if (!com->get_group().empty()) {
262 out << make_expanded(com);
263 }
264 continue;
265 }
266 std::string group_key = com->get_group();
267 if (!group_key.empty() &&
268 std::find_if(subcmd_groups_seen.begin(), subcmd_groups_seen.end(), [&group_key](const std::string& a) {
269 return detail::to_lower(a) == detail::to_lower(group_key);
270 }) == subcmd_groups_seen.end()) {
271 subcmd_groups_seen.insert(group_key);
272 }
273 }
274
275 // For each group, filter out and print subcommands
276 for (const std::string& group : subcmd_groups_seen) {
277 out << "\n" << group << ":\n";
278 const std::vector<const App*> subcommands_group = app->get_subcommands([&group](const App* sub_app) {
279 return detail::to_lower(sub_app->get_group()) == detail::to_lower(group) && !sub_app->get_disabled() &&
280 !sub_app->get_name().empty();
281 });
282 for (const App* new_com : subcommands_group) {
283 if (new_com->get_name().empty()) {
284 continue;
285 }
286 if (mode != AppFormatMode::All) {
287 out << make_subcommand(new_com);
288 } else {
289 out << new_com->help(new_com->get_name(), AppFormatMode::Sub);
290 out << "\n";
291 }
292 }
293 }
294
295 // ########## Next line(s) changed
296
297 std::string tmp = out.str();
298 if (mode == AppFormatMode::All && !tmp.empty()) {
299 tmp.pop_back();
300 }
301
302 return tmp;
303 }
std::string make_subcommand(const App *sub) const override
std::string make_expanded(const App *sub) const override

◆ make_usage()

CLI11_INLINE std::string CLI::HelpFormatter::make_usage ( const App * app,
std::string name ) const
overrideprivate

Definition at line 188 of file Formatter.cpp.

188 {
189 const std::string usage = app->get_usage();
190 if (!usage.empty()) {
191 return usage + "\n";
192 }
193
194 std::stringstream out;
195
196 out << get_label("Usage") << ":" << (name.empty() ? "" : " ") << name;
197
198 // Print an Options badge if any options exist
199 const std::vector<const Option*> non_pos_options = app->get_options([](const Option* opt) {
200 return opt->nonpositional();
201 });
202 if (!non_pos_options.empty()) {
203 out << " [" << get_label("OPTIONS") << "]";
204 }
205
206 // Positionals need to be listed here
207 std::vector<const Option*> positionals = app->get_options([](const Option* opt) {
208 return opt->get_positional();
209 });
210
211 // Print out positionals if any are left
212 if (!positionals.empty()) {
213 // Convert to help names
214 std::vector<std::string> positional_names(positionals.size());
215 std::transform(positionals.begin(), positionals.end(), positional_names.begin(), [this](const Option* opt) {
216 return make_option_usage(opt);
217 });
218
219 out << " " << detail::join(positional_names, " ");
220 }
221
222 // Add a marker if subcommands are expected or optional
223 if (!app->get_subcommands([](const App* subc) {
224 return !subc->get_disabled() && !subc->get_name().empty();
225 })
226 .empty()) {
227 // ############## Next line changed
228 out << " ["
229 << get_label(app->get_subcommands([](const CLI::App* subc) {
230 return ((!subc->get_disabled()) && (!subc->get_name().empty()) /*&& subc->get_required()*/);
231 }).size() <= 1
232 ? "SUBCOMMAND"
233 : "SUBCOMMANDS")
234 << " [--help]"
235 << "]";
236 }
237
238 const Option* disabledOpt = app->get_option_no_throw("--disabled");
239 if (disabledOpt != nullptr ? disabledOpt->as<bool>() : false) {
240 out << " " << get_label("DISABLED");
241 } else if (app->get_required()) {
242 out << " " << get_label("REQUIRED");
243 }
244
245 out << std::endl << std::endl;
246
247 return out.str();
248 }

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