Skip to content

Commit

Permalink
Just use Formatter
Browse files Browse the repository at this point in the history
Removing OptionFormatter

Rename files

Rename to just Formatter

Remove OptionFormatMode (just needs a bool)

Renaming option functions on formatter
  • Loading branch information
henryiii committed Apr 30, 2018
1 parent 89975e5 commit af2ed66
Show file tree
Hide file tree
Showing 12 changed files with 346 additions and 452 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
### Version 1.6: Formatters
### Version 1.6: Formatting

Added a new formatting system. You can now set the formatter on Apps and Options.
Added a new formatting system. You can now set the formatter on Apps.

* Added `AppFormatter` and `OptionFormatter`, and `formatter` slot
* Added `CLI::Formatter` and `formatter` slot for apps, inherited.
* Added `help_all` support (not added by default)
* Added filter argument to `get_subcommands`, `get_options`; use empty filter `{}` to avoid filtering
* Added `get_groups()` to get groups
Expand Down
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ The add commands return a pointer to an internally stored `Option`. If you set t
* `->check(CLI::Range(min,max))`: Requires that the option be between min and max (make sure to use floating point if needed). Min defaults to 0.
* `->transform(std::string(std::string))`: Converts the input string into the output string, in-place in the parsed options.
* `->configurable(false)`: Disable this option from being in an ini configuration file.
* `->formatter(fmt)`: Set the formatter, with signature `std::string(const Option*, OptionFormatMode)`. See Formatting for more details.

These options return the `Option` pointer, so you can chain them together, and even skip storing the pointer entirely. Check takes any function that has the signature `void(const std::string&)`; it should throw a `ValidationError` when validation fails. The help message will have the name of the parent option prepended. Since `check` and `transform` use the same underlying mechanism, you can chain as many as you want, and they will be executed in order. If you just want to see the unconverted values, use `.results()` to get the `std::vector<std::string>` of results.

Expand Down Expand Up @@ -306,15 +305,13 @@ The default settings for options are inherited to subcommands, as well.

## Formatting

The job of formatting help printouts is delegated to a formatter callable object on Apps and Options. You are free to replace either formatter by calling `formatter(fmt)` on an `App` or `Option`.
CLI11 comes with a default App formatter functional, `AppFormatter`, and a default `OptionFormatter` as well. They are both customizable; you can set `label(key, value)` to replace the default labels like `REQUIRED`, and `column_width(n)` to set the width of the columns before you add the functional to the app or option. You can also override almost any stage of the formatting process in a subclass of either formatter. If you want to make a new formatter from scratch, you can do
that too; you just need to implement the correct signature. The first argument is a const pointer to the App or Option in question. The App formatter will get a `std::string` usage name as the second option, and both end with with a mode. Both formatters return a `std::string`.
The job of formatting help printouts is delegated to a formatter callable object on Apps and Options. You are free to replace either formatter by calling `formatter(fmt)` on an `App`, where fmt is any copyable callable with the correct signature.
CLI11 comes with a default App formatter functional, `Formatter`. It is customizable; you can set `label(key, value)` to replace the default labels like `REQUIRED`, and `column_width(n)` to set the width of the columns before you add the functional to the app or option. You can also override almost any stage of the formatting process in a subclass of either formatter. If you want to make a new formatter from scratch, you can do
that too; you just need to implement the correct signature. The first argument is a const pointer to the in question. The formatter will get a `std::string` usage name as the second option, and a `AppFormatMode` mode for the final option. It should return a `std::string`.

The `AppFormatMode` can be `Normal`, `All`, or `Sub`, and it indicates the situation the help was called in. `Sub` is optional, but the default formatter uses it to make sure expanded subcommands are called with
their own formatter since you can't access anything but the call operator once a formatter has been set.

The `OptionFormatMode` also has three values, depending on the calling situation: `Positional`, `Optional`, and `Usage`. The default formatter uses these to print out the option in each of these situations.

## Subclassing

The App class was designed allow toolkits to subclass it, to provide preset default options (see above) and setup/teardown code. Subcommands remain an unsubclassed `App`, since those are not expected to need setup and teardown. The default `App` only adds a help flag, `-h,--help`, than can removed/replaced using `.set_help_flag(name, help_string)`. You can also set a help-all flag with `.set_help_all_flag(name, help_string)`; this will expand the subcommands (one level only). You can remove options if you have pointers to them using `.remove_option(opt)`. You can add a `pre_callback` override to customize the after parse
Expand Down
8 changes: 3 additions & 5 deletions examples/formatter.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
#include <CLI/CLI.hpp>

class MyFormatter : public CLI::OptionFormatter {
class MyFormatter : public CLI::Formatter {
public:
std::string make_opts(const CLI::Option *) const override { return " OPTION"; }
std::string make_option_opts(const CLI::Option *) const override { return " OPTION"; }
};

int main(int argc, char **argv) {
CLI::App app;
app.set_help_all_flag("--help-all", "Show all help");

app.option_defaults()->formatter(MyFormatter());

CLI::AppFormatter fmt;
MyFormatter fmt;
fmt.column_width(15);
app.formatter(fmt);

Expand Down
4 changes: 2 additions & 2 deletions include/CLI/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "CLI/Split.hpp"
#include "CLI/StringTools.hpp"
#include "CLI/TypeTools.hpp"
#include "CLI/Formatter.hpp"
#include "CLI/FormatterFwd.hpp"

namespace CLI {

Expand Down Expand Up @@ -107,7 +107,7 @@ class App {
Option *help_all_ptr_{nullptr};

/// This is the formatter for help printing. Default provided. INHERITABLE
std::function<std::string(const App *, std::string, AppFormatMode)> formatter_{AppFormatter()};
std::function<std::string(const App *, std::string, AppFormatMode)> formatter_{Formatter()};

/// The error message printing function INHERITABLE
std::function<std::string(const App *, const Error &e)> failure_message_ = FailureMessage::simple;
Expand Down
185 changes: 0 additions & 185 deletions include/CLI/AppFormatter.hpp

This file was deleted.

6 changes: 2 additions & 4 deletions include/CLI/CLI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@

#include "CLI/Validators.hpp"

#include "CLI/Formatter.hpp"
#include "CLI/FormatterFwd.hpp"

#include "CLI/Option.hpp"

#include "CLI/OptionFormatter.hpp"

#include "CLI/App.hpp"

#include "CLI/AppFormatter.hpp"
#include "CLI/Formatter.hpp"
Loading

0 comments on commit af2ed66

Please sign in to comment.