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

Detailed Description

Definition at line 78 of file Formatter.h.

Constructor & Destructor Documentation

◆ ~HelpFormatter()

CLI::HelpFormatter::~HelpFormatter ( )
override

Definition at line 178 of file Formatter.cpp.

178 {
179 }

Member Function Documentation

◆ make_description()

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

Definition at line 193 of file Formatter.cpp.

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

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

375 {
376 std::stringstream out;
377 // ########## Next lines changed
378 const Option* disabledOpt = sub->get_option_no_throw("--disabled");
379 out << sub->get_display_name(true) + " [OPTIONS]" + (!sub->get_subcommands({}).empty() ? " [SECTIONS]" : "") +
380 ((disabledOpt != nullptr ? disabledOpt->as<bool>() : false) ? " " + get_label("DISABLED")
381 : (sub->get_required() ? " " + get_label("REQUIRED") : ""))
382 << "\n";
383
384 detail::streamOutAsParagraph(out, make_description(sub), description_paragraph_width_, ""); // Format description as paragraph
385
386 if (sub->get_name().empty() && !sub->get_aliases().empty()) {
387 detail::format_aliases(out, sub->get_aliases(), column_width_ + 2);
388 }
389 out << make_positionals(sub);
390 out << make_groups(sub, mode);
391 out << make_subcommands(sub, mode);
392
393 // Drop blank spaces
394 std::string tmp = out.str();
395 tmp.pop_back();
396
397 // Indent all but the first line (the name)
398 return detail::find_and_replace(tmp, "\n", "\n ") + "\n";
399 }
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_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 181 of file Formatter.cpp.

181 {
182 std::stringstream out;
183
184 // ############## Next line changed
185 out << "\n" << group << ":\n";
186 for (const Option* opt : opts) {
187 out << make_option(opt, is_positional);
188 }
189
190 return out.str();
191 }

◆ make_option_opts()

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

Definition at line 401 of file Formatter.cpp.

401 {
402 std::stringstream out;
403
404 if (!opt->get_option_text().empty()) {
405 out << " " << opt->get_option_text();
406 } else {
407 if (opt->get_type_size() != 0) {
408 if (!opt->get_type_name().empty()) {
409 // ########## Next line changed
410 out << ((opt->get_items_expected_max() == 0) ? "=" : " ") << get_label(opt->get_type_name());
411 }
412 if (!opt->get_default_str().empty()) {
413 out << " [" << opt->get_default_str() << "]";
414 }
415 if (opt->count() > 0 && opt->get_default_str() != opt->as<std::string>()) {
416 out << " {" << opt->as<std::string>() << "}";
417 }
418 if (opt->get_expected_max() == detail::expected_max_vector_size) {
419 out << " ... ";
420 } else if (opt->get_expected_min() > 1) {
421 out << " x " << opt->get_expected();
422 }
423 if (opt->get_required() && !get_label("REQUIRED").empty()) {
424 out << " " << get_label("REQUIRED");
425 }
426 if (opt->get_configurable() && !get_label("PERSISTENT").empty()) {
427 out << " " << get_label("PERSISTENT");
428 }
429 }
430 if (!opt->get_envname().empty()) {
431 out << " (" << get_label("Env") << ":" << opt->get_envname() << ") ";
432 }
433 if (!opt->get_needs().empty()) {
434 out << " " << get_label("Needs") << ":";
435 for (const Option* op : opt->get_needs()) {
436 out << " " << op->get_name();
437 }
438 }
439 if (!opt->get_excludes().empty()) {
440 out << " " << get_label("Excludes") << ":";
441 for (const Option* op : opt->get_excludes()) {
442 out << " " << op->get_name();
443 }
444 }
445 }
446 return out.str();
447 }

◆ make_subcommand()

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

Definition at line 355 of file Formatter.cpp.

355 {
356 std::stringstream out;
357 std::string name = " " + sub->get_display_name(true) + (sub->get_required() ? " " + get_label("REQUIRED") : "");
358
359 out << std::setw(static_cast<int>(column_width_)) << std::left << name;
360 std::string desc = sub->get_description();
361 if (!desc.empty()) {
362 bool skipFirstLinePrefix = true;
363 if (out.str().length() >= column_width_) {
364 out << '\n';
365 skipFirstLinePrefix = false;
366 }
367 detail::streamOutAsParagraph(out, desc, right_column_width_, std::string(column_width_, ' '), skipFirstLinePrefix);
368 }
369
370 out << '\n';
371
372 return out.str();
373 }

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

279 {
280 std::stringstream out;
281
282 const std::vector<const App*> subcommands = app->get_subcommands([](const App* subc) {
283 if (subc->get_group() == "Instances") {
284 if (subc->get_option("--disabled")->as<bool>()) {
285 const_cast<CLI::App*>(subc)->group(subc->get_group() + " (disabled)");
286 }
287 }
288 return !subc->get_disabled() && !subc->get_name().empty();
289 });
290
291 // Make a list in alphabetic order of the groups seen
292 std::set<std::string> subcmd_groups_seen;
293 for (const App* com : subcommands) {
294 if (com->get_name().empty()) {
295 if (!com->get_group().empty()) {
296 out << make_expanded(com, mode);
297 }
298 continue;
299 }
300 std::string group_key = com->get_group();
301 if (!group_key.empty() &&
302 std::find_if(subcmd_groups_seen.begin(), subcmd_groups_seen.end(), [&group_key](const std::string& a) {
303 return detail::to_lower(a) == detail::to_lower(group_key);
304 }) == subcmd_groups_seen.end()) {
305 subcmd_groups_seen.insert(group_key);
306 }
307 }
308
309 // For each group, filter out and print subcommands
310 const Option* disabledOpt = app->get_option_no_throw("--disabled");
311 bool disabled = false;
312 if (disabledOpt != nullptr) {
313 disabled = disabledOpt->as<bool>();
314 }
315
316 if (!disabled) {
317 for (const std::string& group : subcmd_groups_seen) {
318 out << "\n" << group << ":\n";
319 const std::vector<const App*> subcommands_group = app->get_subcommands([&group](const App* sub_app) {
320 return detail::to_lower(sub_app->get_group()) == detail::to_lower(group) && !sub_app->get_disabled() &&
321 !sub_app->get_name().empty();
322 });
323 for (const App* new_com : subcommands_group) {
324 if (new_com->get_name().empty()) {
325 continue;
326 }
327 if (mode != AppFormatMode::All) {
328 out << make_subcommand(new_com);
329 } else {
330 out << new_com->help(disabledOpt != nullptr && (app->get_help_ptr()->as<std::string>() == "exact" ||
331 app->get_help_ptr()->as<std::string>() == "expanded")
332 ? new_com
333 : nullptr,
334 new_com->get_name(),
335 AppFormatMode::Sub);
336 out << "\n";
337 }
338 }
339 }
340 }
341
342 // ########## Next line(s) changed
343
344 std::string tmp = out.str();
345 if (mode == AppFormatMode::All && !tmp.empty()) {
346 tmp.pop_back();
347 }
348
349 out.str(tmp);
350 out.clear();
351
352 return out.str();
353 }
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 217 of file Formatter.cpp.

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

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