mirror of
https://github.com/CLIUtils/CLI11.git
synced 2025-04-30 04:33:53 +00:00
_This is the new PR I've mentioned to work on in PR #858_ ## A better Help Formatter _See below for images of the new help page_ Finally, after a lot of planning, understanding CLI11's codebase, testing and coding, the new default Help Formatter is done. There are a lot of changes to make the help page more readable and closer to UNIX standards, see Changelog below for details. One of the highlights is automatic paragraph formatting with correct line wrapping for App and options/flag descriptions as well as the footer. A goal was to provide more flexibility and better readability for the help page while providing full compatibility with Apps using CLI11 (no breaking changes and no changes to Apps required). Also better support for different terminal sizes. Users can now specify three new optional attributes: `right_column_width_`, `description_paragraph_width_` and `footer_paragraph_width_`. See code documentation for more details. The different columns for options/flags now scale with the set `column_width_` value: Single dash flags occupy 33% of the set `column_width_`, double dash flags and options (like REQUIRED) 66%. These new attributes allow for indirectly respecting terminal geometry, footer paragraph formatting has also been added (#355). This PR also implements the issues #353 and #856. The new help page formatting can also be used as an input for man page generation, since it's oriented on the man page style (#413). [help2man](https://www.gnu.org/software/help2man/) can be used to generate man pages from help output (see comment down below for example). I thoroughly tested this code with all possible combinations of flags, options, positionals, subcommands, validators, ... So far everything works great. I hope this PR looks good and meets all requirements. I'm looking forward to the implementation of this PR into CLI11. If you have any questions or suggestions feel free to comment. ### Fixed/implemented issues by this PR - #353 Better options formatting - #856 Space between options - #355 Footer formatting - #413 Man page generation can be achieved using help2man with the new help formatting - https://github.com/CLIUtils/CLI11/issues/384#issuecomment-570066436 Better help formatting can be marked as complete ### What about the failing tests? Of course the tests expect the old help text format. This is why 6 of the tests are failing. Since it is a bit of work to migrate the tests to the new help format, I first wanted to push out this PR and get confirmation before I'll update all the tests. So please let me know if this PR gets implemented, what changes should be made and then I'll migrate the tests to the new help format, either in this PR or I'll make a new one. ## Changelog: #### There are _no breaking changes_. Every App using CLI11 will work with this new formatter with no changes required. - Added empty lines at beginning and end of help text - Removed double new-line between option groups for consistency. Now all sections have the same number of new-lines - Switched usage and description order - Only show "Usage"-string if no App name is present. This provides better readability - Made categories (Options, Positionals, ...) capital - Changed `ConfigBase::to_config` to correctly process capital "OPTIONS"-group (only affects descriptions of the config file, not a breaking change) - Added a paragraph formatter function `streamOutAsParagraph` to StringTools.hpp - Made "description" a paragraph block with correct, word respecting line wrapping and indentation (using the new paragraph formatter function) - Made the footer a paragraph block with correct, word respecting line wrapping and indentation - Updated documentation for `column_width_` to make it more clear - Added new member: `right_column_width_`, added getter and setter for `right_column_width_` - Added new member: `description_paragraph_width_`, added getter and setter for `description_paragraph_width_` - Added new member: `footer_paragraph_width_`, added getter and setter for `footer_paragraph_width_ ` - Positionals description are now formatted as paragraph with correct, word respecting line wrapping - Options description are now formatted as paragraph with correct, word respecting line wrapping - Short and long options/flags/names are now correctly formatted to always be at the right position (also for subcommand options/flags) - Short and long options/flags/names column widths scale linearly with the `column_width_` attribute to better adapt to different `column_width_` sizes - Merged PR #860 ## What's planned for the future? - I'm thinking of better formatting the options of flags (like REQUIRED, TEXT, INT, ...) and make them also in a seperate column. This way they would also always be at the same position. However I decided against it for this PR, since I wanted them to be as close as possible to the actual flag. With my implementation it is quite easy to add this change in the future. - Subcommands: I'm planning on better formatting the Subcommands. With this PR only the short and long flags/options of subcommands are better formatted (like it is with the main flags, see images down below). - Maybe implement a different way to display expected data type options (TEXT, INT, ...). For example: `--file-name=<TEXT>` for long flags only and if `disable_flag_override_` is false. - Maybe add something like this: https://github.com/CLIUtils/CLI11/issues/554 --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Philip Top <phlptp@gmail.com>
214 lines
7.1 KiB
C++
214 lines
7.1 KiB
C++
// Copyright (c) 2017-2024, University of Cincinnati, developed by Henry Schreiner
|
|
// under NSF AWARD 1414736 and by the respective contributors.
|
|
// All rights reserved.
|
|
//
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
#pragma once
|
|
|
|
// IWYU pragma: private, include "CLI/CLI.hpp"
|
|
|
|
// [CLI11:public_includes:set]
|
|
#include <functional>
|
|
#include <map>
|
|
#include <string>
|
|
#include <utility>
|
|
#include <vector>
|
|
// [CLI11:public_includes:end]
|
|
|
|
#include "StringTools.hpp"
|
|
|
|
namespace CLI {
|
|
// [CLI11:formatter_fwd_hpp:verbatim]
|
|
|
|
class Option;
|
|
class App;
|
|
|
|
/// This enum signifies the type of help requested
|
|
///
|
|
/// This is passed in by App; all user classes must accept this as
|
|
/// the second argument.
|
|
|
|
enum class AppFormatMode {
|
|
Normal, ///< The normal, detailed help
|
|
All, ///< A fully expanded help
|
|
Sub, ///< Used when printed as part of expanded subcommand
|
|
};
|
|
|
|
/// This is the minimum requirements to run a formatter.
|
|
///
|
|
/// A user can subclass this is if they do not care at all
|
|
/// about the structure in CLI::Formatter.
|
|
class FormatterBase {
|
|
protected:
|
|
/// @name Options
|
|
///@{
|
|
|
|
/// The width of the left column (options/flags/subcommands)
|
|
std::size_t column_width_{30};
|
|
|
|
/// The width of the right column (description of options/flags/subcommands)
|
|
std::size_t right_column_width_{65};
|
|
|
|
/// The width of the description paragraph at the top of help
|
|
std::size_t description_paragraph_width_{80};
|
|
|
|
/// The width of the footer paragraph
|
|
std::size_t footer_paragraph_width_{80};
|
|
|
|
/// @brief The required help printout labels (user changeable)
|
|
/// Values are Needs, Excludes, etc.
|
|
std::map<std::string, std::string> labels_{};
|
|
|
|
///@}
|
|
/// @name Basic
|
|
///@{
|
|
|
|
public:
|
|
FormatterBase() = default;
|
|
FormatterBase(const FormatterBase &) = default;
|
|
FormatterBase(FormatterBase &&) = default;
|
|
FormatterBase &operator=(const FormatterBase &) = default;
|
|
FormatterBase &operator=(FormatterBase &&) = default;
|
|
|
|
/// Adding a destructor in this form to work around bug in GCC 4.7
|
|
virtual ~FormatterBase() noexcept {} // NOLINT(modernize-use-equals-default)
|
|
|
|
/// This is the key method that puts together help
|
|
virtual std::string make_help(const App *, std::string, AppFormatMode) const = 0;
|
|
|
|
///@}
|
|
/// @name Setters
|
|
///@{
|
|
|
|
/// Set the "REQUIRED" label
|
|
void label(std::string key, std::string val) { labels_[key] = val; }
|
|
|
|
/// Set the left column width (options/flags/subcommands)
|
|
void column_width(std::size_t val) { column_width_ = val; }
|
|
|
|
/// Set the right column width (description of options/flags/subcommands)
|
|
void right_column_width(std::size_t val) { right_column_width_ = val; }
|
|
|
|
/// Set the description paragraph width at the top of help
|
|
void description_paragraph_width(std::size_t val) { description_paragraph_width_ = val; }
|
|
|
|
/// Set the footer paragraph width
|
|
void footer_paragraph_width(std::size_t val) { footer_paragraph_width_ = val; }
|
|
|
|
///@}
|
|
/// @name Getters
|
|
///@{
|
|
|
|
/// Get the current value of a name (REQUIRED, etc.)
|
|
CLI11_NODISCARD std::string get_label(std::string key) const {
|
|
if(labels_.find(key) == labels_.end())
|
|
return key;
|
|
return labels_.at(key);
|
|
}
|
|
|
|
/// Get the current left column width (options/flags/subcommands)
|
|
CLI11_NODISCARD std::size_t get_column_width() const { return column_width_; }
|
|
|
|
/// Get the current right column width (description of options/flags/subcommands)
|
|
CLI11_NODISCARD std::size_t get_right_column_width() const { return right_column_width_; }
|
|
|
|
/// Get the current description paragraph width at the top of help
|
|
CLI11_NODISCARD std::size_t get_description_paragraph_width() const { return description_paragraph_width_; }
|
|
|
|
/// Get the current footer paragraph width
|
|
CLI11_NODISCARD std::size_t get_footer_paragraph_width() const { return footer_paragraph_width_; }
|
|
|
|
///@}
|
|
};
|
|
|
|
/// This is a specialty override for lambda functions
|
|
class FormatterLambda final : public FormatterBase {
|
|
using funct_t = std::function<std::string(const App *, std::string, AppFormatMode)>;
|
|
|
|
/// The lambda to hold and run
|
|
funct_t lambda_;
|
|
|
|
public:
|
|
/// Create a FormatterLambda with a lambda function
|
|
explicit FormatterLambda(funct_t funct) : lambda_(std::move(funct)) {}
|
|
|
|
/// Adding a destructor (mostly to make GCC 4.7 happy)
|
|
~FormatterLambda() noexcept override {} // NOLINT(modernize-use-equals-default)
|
|
|
|
/// This will simply call the lambda function
|
|
std::string make_help(const App *app, std::string name, AppFormatMode mode) const override {
|
|
return lambda_(app, name, mode);
|
|
}
|
|
};
|
|
|
|
/// This is the default Formatter for CLI11. It pretty prints help output, and is broken into quite a few
|
|
/// overridable methods, to be highly customizable with minimal effort.
|
|
class Formatter : public FormatterBase {
|
|
public:
|
|
Formatter() = default;
|
|
Formatter(const Formatter &) = default;
|
|
Formatter(Formatter &&) = default;
|
|
Formatter &operator=(const Formatter &) = default;
|
|
Formatter &operator=(Formatter &&) = default;
|
|
|
|
/// @name Overridables
|
|
///@{
|
|
|
|
/// This prints out a group of options with title
|
|
///
|
|
CLI11_NODISCARD virtual std::string
|
|
make_group(std::string group, bool is_positional, std::vector<const Option *> opts) const;
|
|
|
|
/// This prints out just the positionals "group"
|
|
virtual std::string make_positionals(const App *app) const;
|
|
|
|
/// This prints out all the groups of options
|
|
std::string make_groups(const App *app, AppFormatMode mode) const;
|
|
|
|
/// This prints out all the subcommands
|
|
virtual std::string make_subcommands(const App *app, AppFormatMode mode) const;
|
|
|
|
/// This prints out a subcommand
|
|
virtual std::string make_subcommand(const App *sub) const;
|
|
|
|
/// This prints out a subcommand in help-all
|
|
virtual std::string make_expanded(const App *sub, AppFormatMode mode) const;
|
|
|
|
/// This prints out all the groups of options
|
|
virtual std::string make_footer(const App *app) const;
|
|
|
|
/// This displays the description line
|
|
virtual std::string make_description(const App *app) const;
|
|
|
|
/// This displays the usage line
|
|
virtual std::string make_usage(const App *app, std::string name) const;
|
|
|
|
/// This puts everything together
|
|
std::string make_help(const App *app, std::string, AppFormatMode mode) const override;
|
|
|
|
///@}
|
|
/// @name Options
|
|
///@{
|
|
|
|
/// This prints out an option help line, either positional or optional form
|
|
virtual std::string make_option(const Option *, bool) const;
|
|
|
|
/// @brief This is the name part of an option, Default: left column
|
|
virtual std::string make_option_name(const Option *, bool) const;
|
|
|
|
/// @brief This is the options part of the name, Default: combined into left column
|
|
virtual std::string make_option_opts(const Option *) const;
|
|
|
|
/// @brief This is the description. Default: Right column, on new line if left column too large
|
|
virtual std::string make_option_desc(const Option *) const;
|
|
|
|
/// @brief This is used to print the name on the USAGE line
|
|
virtual std::string make_option_usage(const Option *opt) const;
|
|
|
|
///@}
|
|
};
|
|
|
|
// [CLI11:formatter_fwd_hpp:end]
|
|
} // namespace CLI
|