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 80 of file Formatter.h.

Constructor & Destructor Documentation

◆ ~HelpFormatter()

CLI::HelpFormatter::~HelpFormatter ( )
override

Definition at line 179 of file Formatter.cpp.

179 {
180 }

Member Function Documentation

◆ make_description()

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

Definition at line 194 of file Formatter.cpp.

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

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

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

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

◆ make_option_opts()

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

Definition at line 402 of file Formatter.cpp.

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

◆ make_subcommand()

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

Definition at line 356 of file Formatter.cpp.

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

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

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

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

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