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_footer (const App *app) 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, AppFormatMode mode) const override
std::string make_option_opts (const Option *opt) const override

Detailed Description

Definition at line 86 of file Formatter.h.

Constructor & Destructor Documentation

◆ ~HelpFormatter()

CLI::HelpFormatter::~HelpFormatter ( )
override

Definition at line 186 of file Formatter.cpp.

186 {
187 }

Member Function Documentation

◆ make_description()

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

Definition at line 201 of file Formatter.cpp.

201 {
202 std::string desc = app->get_description();
203 auto min_options = app->get_require_option_min();
204 auto max_options = app->get_require_option_max();
205 // ############## Next line changed (if statement removed)
206 if ((max_options == min_options) && (min_options > 0)) {
207 if (min_options == 1) {
208 desc += " \n[Exactly 1 of the following options is required]";
209 } else {
210 desc += " \n[Exactly " + std::to_string(min_options) + " options from the following list are required]";
211 }
212 } else if (max_options > 0) {
213 if (min_options > 0) {
214 desc += " \n[Between " + std::to_string(min_options) + " and " + std::to_string(max_options) +
215 " of the follow options are required]";
216 } else {
217 desc += " \n[At most " + std::to_string(max_options) + " of the following options are allowed]";
218 }
219 } else if (min_options > 0) {
220 desc += " \n[At least " + std::to_string(min_options) + " of the following options are required]";
221 }
222 return (!desc.empty()) ? desc + "\n" : std::string{};
223 }

Referenced by make_expanded().

Here is the caller graph for this function:

◆ make_expanded()

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

Definition at line 391 of file Formatter.cpp.

391 {
392 std::stringstream out;
393 // ########## Next lines changed
394 const Option* disabledOpt = sub->get_option_no_throw("--disabled");
395 out << sub->get_display_name(true) + " [OPTIONS]" + (!sub->get_subcommands({}).empty() ? " [SECTIONS]" : "") +
396 ((disabledOpt != nullptr ? disabledOpt->as<bool>() : false) ? " " + get_label("DISABLED")
397 : (sub->get_required() ? " " + get_label("REQUIRED") : ""))
398 << "\n";
399
400 detail::streamOutAsParagraph(out, make_description(sub), description_paragraph_width_, ""); // Format description as paragraph
401
402 if (sub->get_name().empty() && !sub->get_aliases().empty()) {
403 detail::format_aliases(out, sub->get_aliases(), column_width_ + 2);
404 }
405 out << make_positionals(sub);
406 out << make_groups(sub, mode);
407 out << make_subcommands(sub, mode);
408
409 // Drop blank spaces
410 std::string tmp = out.str();
411 tmp.pop_back();
412
413 // Indent all but the first line (the name)
414 return detail::find_and_replace(tmp, "\n", "\n ") + "\n";
415 }
std::string make_description(const App *app) const override
std::string make_subcommands(const App *app, AppFormatMode mode) const override

References make_description(), and make_subcommands().

Referenced by make_subcommands().

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

◆ make_footer()

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

Definition at line 287 of file Formatter.cpp.

287 {
288 const std::string footer = app->get_footer();
289 if (footer.empty()) {
290 return std::string{"\n"};
291 }
292 return '\n' + footer + "\n\n";
293 }

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

189 {
190 std::stringstream out;
191
192 // ############## Next line changed
193 out << "\n" << group << ":\n";
194 for (const Option* opt : opts) {
195 out << make_option(opt, is_positional);
196 }
197
198 return out.str();
199 }

◆ make_option_opts()

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

Definition at line 417 of file Formatter.cpp.

417 {
418 std::stringstream out;
419
420 if (!opt->get_option_text().empty()) {
421 out << " " << opt->get_option_text();
422 } else {
423 if (opt->get_type_size() != 0) {
424 if (!opt->get_type_name().empty()) {
425 // ########## Next line changed
426 out << ((opt->get_items_expected_max() == 0) ? "=" : " ") << get_label(opt->get_type_name());
427 }
428 if (!opt->get_default_str().empty()) {
429 out << " [" << opt->get_default_str() << "]";
430 }
431 try {
432 if (opt->count() > 0 && opt->get_default_str() != opt->as<std::string>()) {
433 out << " {";
434 std::string completeResult;
435 for (const auto& result : opt->reduced_results()) {
436 completeResult += (!result.empty() ? result : "\"\"") + " ";
437 }
438 completeResult.pop_back();
439 out << completeResult << "}";
440 }
441 } catch (CLI::ParseError& e) {
442 out << " <[" << Color::Code::FG_RED << e.get_name() << Color::Code::FG_DEFAULT << "] " << e.what() << ">";
443 }
444 if (opt->get_expected_max() == detail::expected_max_vector_size) {
445 out << " ... ";
446 } else if (opt->get_expected_min() > 1) {
447 out << " x " << opt->get_expected();
448 }
449 if (opt->get_required() && !get_label("REQUIRED").empty()) {
450 out << " " << get_label("REQUIRED");
451 }
452 if (opt->get_configurable() && !get_label("PERSISTENT").empty()) {
453 out << " " << get_label("PERSISTENT");
454 }
455 }
456 if (!opt->get_envname().empty()) {
457 out << " (" << get_label("Env") << ":" << opt->get_envname() << ") ";
458 }
459 if (!opt->get_needs().empty()) {
460 out << " " << get_label("Needs") << ":";
461 for (const Option* op : opt->get_needs()) {
462 out << " " << op->get_name();
463 }
464 }
465 if (!opt->get_excludes().empty()) {
466 out << " " << get_label("Excludes") << ":";
467 for (const Option* op : opt->get_excludes()) {
468 out << " " << op->get_name();
469 }
470 }
471 }
472 return out.str();
473 }
@ FG_DEFAULT
Definition Logger.h:69

References Color::FG_DEFAULT, and Color::FG_RED.

◆ make_subcommand()

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

Definition at line 371 of file Formatter.cpp.

371 {
372 std::stringstream out;
373 const std::string name = " " + sub->get_display_name(true) + (sub->get_required() ? " " + get_label("REQUIRED") : "");
374
375 out << std::setw(static_cast<int>(column_width_)) << std::left << name;
376 const std::string desc = sub->get_description();
377 if (!desc.empty()) {
378 bool skipFirstLinePrefix = true;
379 if (out.str().length() >= column_width_) {
380 out << '\n';
381 skipFirstLinePrefix = false;
382 }
383 detail::streamOutAsParagraph(out, desc, right_column_width_, std::string(column_width_, ' '), skipFirstLinePrefix);
384 }
385
386 out << '\n';
387
388 return out.str();
389 }

Referenced by make_subcommands().

Here is the caller graph for this function:

◆ make_subcommands()

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

Definition at line 295 of file Formatter.cpp.

295 {
296 std::stringstream out;
297
298 const std::vector<const App*> subcommands = app->get_subcommands([](const App* subc) {
299 if (subc->get_group() == "Instances") {
300 if (subc->get_option("--disabled")->as<bool>()) {
301 const_cast<CLI::App*>(subc)->group(subc->get_group() + " (disabled)");
302 }
303 }
304 return !subc->get_disabled() && !subc->get_name().empty();
305 });
306
307 // Make a list in alphabetic order of the groups seen
308 std::set<std::string> subcmd_groups_seen;
309 for (const App* com : subcommands) {
310 if (com->get_name().empty()) {
311 if (!com->get_group().empty()) {
312 out << make_expanded(com, mode);
313 }
314 continue;
315 }
316 std::string group_key = com->get_group();
317 if (!group_key.empty() &&
318 std::find_if(subcmd_groups_seen.begin(), subcmd_groups_seen.end(), [&group_key](const std::string& a) {
319 return detail::to_lower(a) == detail::to_lower(group_key);
320 }) == subcmd_groups_seen.end()) {
321 subcmd_groups_seen.insert(group_key);
322 }
323 }
324
325 // For each group, filter out and print subcommands
326 const Option* disabledOpt = app->get_option_no_throw("--disabled");
327 bool disabled = false;
328 if (disabledOpt != nullptr) {
329 disabled = disabledOpt->as<bool>();
330 }
331
332 if (!disabled) {
333 for (const std::string& group : subcmd_groups_seen) {
334 out << "\n" << group << ":\n";
335 const std::vector<const App*> subcommands_group = app->get_subcommands([&group](const App* sub_app) {
336 return detail::to_lower(sub_app->get_group()) == detail::to_lower(group) && !sub_app->get_disabled() &&
337 !sub_app->get_name().empty();
338 });
339 for (const App* new_com : subcommands_group) {
340 if (new_com->get_name().empty()) {
341 continue;
342 }
343 if (mode != AppFormatMode::All) {
344 out << make_subcommand(new_com);
345 } else {
346 out << new_com->help(disabledOpt != nullptr && (app->get_help_ptr()->as<std::string>() == "exact" ||
347 app->get_help_ptr()->as<std::string>() == "expanded")
348 ? new_com
349 : nullptr,
350 new_com->get_name(),
351 AppFormatMode::Sub);
352 out << "\n";
353 }
354 }
355 }
356 }
357
358 // ########## Next line(s) changed
359
360 std::string tmp = out.str();
361 if (mode == AppFormatMode::All && !tmp.empty()) {
362 tmp.pop_back();
363 }
364
365 out.str(tmp);
366 out.clear();
367
368 return out.str();
369 }
std::string make_subcommand(const App *sub) const override
std::string make_expanded(const App *sub, AppFormatMode mode) const override

References make_expanded(), and make_subcommand().

Referenced by make_expanded().

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

◆ make_usage()

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

Definition at line 225 of file Formatter.cpp.

225 {
226 const std::string usage = app->get_usage();
227 if (!usage.empty()) {
228 return usage + "\n";
229 }
230
231 std::stringstream out;
232
233 out << get_label("Usage") << ":" << (name.empty() ? "" : " ") << name;
234
235 // Print an Options badge if any options exist
236 const std::vector<const Option*> non_pos_options = app->get_options([](const Option* opt) {
237 return opt->nonpositional();
238 });
239 if (!non_pos_options.empty()) {
240 out << " [" << get_label("OPTIONS") << "]";
241 }
242
243 // Positionals need to be listed here
244 std::vector<const Option*> positionals = app->get_options([](const Option* opt) {
245 return opt->get_positional();
246 });
247
248 // Print out positionals if any are left
249 if (!positionals.empty()) {
250 // Convert to help names
251 std::vector<std::string> positional_names(positionals.size());
252 std::transform(positionals.begin(), positionals.end(), positional_names.begin(), [this](const Option* opt) {
253 return make_option_usage(opt);
254 });
255
256 out << " " << detail::join(positional_names, " ");
257 }
258
259 // Add a marker if subcommands are expected or optional
260 if (!app->get_subcommands([](const App* subc) {
261 return !subc->get_disabled() && !subc->get_name().empty();
262 })
263 .empty()) {
264 // ############## Next line changed
265 out << " ["
266 << get_label(app->get_subcommands([](const CLI::App* subc) {
267 return ((!subc->get_disabled()) && (!subc->get_name().empty()) /*&& subc->get_required()*/);
268 }).size() <= 1
269 ? "SUBCOMMAND"
270 : "SUBCOMMANDS")
271 << " [--help]"
272 << "]";
273 }
274
275 const Option* disabledOpt = app->get_option_no_throw("--disabled");
276 if (disabledOpt != nullptr ? disabledOpt->as<bool>() : false) {
277 out << " " << get_label("DISABLED");
278 } else if (app->get_required()) {
279 out << " " << get_label("REQUIRED");
280 }
281
282 out << std::endl << std::endl;
283
284 return out.str();
285 }

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