Commit 11519d8c75be95861f8b1f88bfa1a2ff06e29514
Committed by
Henry Schreiner
1 parent
ba07592e
Minor fixes in enum examples (#377)
* Initialize variable, use std::map, update comment * Analog changes as in enum.cpp, clarify ostream operator * Add enum_ostream, fix typo
Showing
3 changed files
with
17 additions
and
16 deletions
README.md
| ... | ... | @@ -825,6 +825,7 @@ Several short examples of different features are included in the repository. A b |
| 825 | 825 | - [callback_passthrough](https://github.com/CLIUtils/CLI11/blob/master/examples/callback_passthrough.cpp): Example of directly passing remaining arguments through to a callback function which generates a CLI11 application based on existing arguments. |
| 826 | 826 | - [digit_args](https://github.com/CLIUtils/CLI11/blob/master/examples/digit_args.cpp): Based on [Issue #123](https://github.com/CLIUtils/CLI11/issues/123), uses digit flags to pass a value |
| 827 | 827 | - [enum](https://github.com/CLIUtils/CLI11/blob/master/examples/enum.cpp): Using enumerations in an option, and the use of [CheckedTransformer](#transforming-validators) |
| 828 | + - [enum_ostream](https://github.com/CLIUtils/CLI11/blob/master/examples/enum_ostream.cpp): In addition to the contents of example enum.cpp, this example shows how a custom ostream operator overrides CLI11's enum streaming. | |
| 828 | 829 | - [formatter](https://github.com/CLIUtils/CLI11/blob/master/examples/formatter.cpp): Illustrating usage of a custom formatter |
| 829 | 830 | - [groups](https://github.com/CLIUtils/CLI11/blob/master/examples/groups.cpp): Example using groups of options for help grouping and a the timer helper class |
| 830 | 831 | - [inter_argument_order](https://github.com/CLIUtils/CLI11/blob/master/examples/inter_argument_order.cpp): An app to practice mixing unlimited arguments, but still recover the original order. |
| ... | ... | @@ -846,7 +847,7 @@ Several short examples of different features are included in the repository. A b |
| 846 | 847 | |
| 847 | 848 | ## Contribute |
| 848 | 849 | |
| 849 | -To contribute, open an [issue][github issues] or [pull request][github pull requests] on GitHub, or ask a question on [gitter][]. The is also a short note to contributors [here](./.github/CONTRIBUTING.md). | |
| 850 | +To contribute, open an [issue][github issues] or [pull request][github pull requests] on GitHub, or ask a question on [gitter][]. There is also a short note to contributors [here](./.github/CONTRIBUTING.md). | |
| 850 | 851 | This readme roughly follows the [Standard Readme Style][] and includes a mention of almost every feature of the library. More complex features are documented in more detail in the [CLI11 tutorial GitBook][gitbook]. |
| 851 | 852 | |
| 852 | 853 | This project was created by [Henry Schreiner](https://github.com/henryiii). | ... | ... |
examples/enum.cpp
| 1 | 1 | #include <CLI/CLI.hpp> |
| 2 | +#include <map> | |
| 2 | 3 | |
| 3 | 4 | enum class Level : int { High, Medium, Low }; |
| 4 | 5 | |
| 5 | 6 | int main(int argc, char **argv) { |
| 6 | 7 | CLI::App app; |
| 7 | 8 | |
| 8 | - Level level; | |
| 9 | + Level level{Level::Low}; | |
| 9 | 10 | // specify string->value mappings |
| 10 | - std::vector<std::pair<std::string, Level>> map{ | |
| 11 | - {"high", Level::High}, {"medium", Level::Medium}, {"low", Level::Low}}; | |
| 12 | - // checked Transform does the translation and checks the results are either in one of the strings or one of the | |
| 11 | + std::map<std::string, Level> map{{"high", Level::High}, {"medium", Level::Medium}, {"low", Level::Low}}; | |
| 12 | + // CheckedTransformer translates and checks whether the results are either in one of the strings or in one of the | |
| 13 | 13 | // translations already |
| 14 | 14 | app.add_option("-l,--level", level, "Level settings") |
| 15 | 15 | ->required() | ... | ... |
examples/enum_ostream.cpp
| ... | ... | @@ -2,30 +2,30 @@ |
| 2 | 2 | |
| 3 | 3 | enum class Level : int { High, Medium, Low }; |
| 4 | 4 | |
| 5 | -// this breaks the code | |
| 6 | -inline std::ostream &operator<<(std::ostream &o, const Level &l) { | |
| 7 | - switch(l) { | |
| 5 | +// Defining operator<<() for your enum class (in this case for 'Level') overrides CLI11's enum streaming | |
| 6 | +inline std::ostream &operator<<(std::ostream &os, const Level &level) { | |
| 7 | + switch(level) { | |
| 8 | 8 | case Level::High: |
| 9 | - o << "High"; | |
| 9 | + os << "High"; | |
| 10 | 10 | break; |
| 11 | 11 | case Level::Medium: |
| 12 | - o << "Medium"; | |
| 12 | + os << "Medium"; | |
| 13 | 13 | break; |
| 14 | 14 | case Level::Low: |
| 15 | - o << "Low"; | |
| 15 | + os << "Low"; | |
| 16 | 16 | break; |
| 17 | 17 | } |
| 18 | - return o; | |
| 18 | + os << " (ft rom custom ostream)"; | |
| 19 | + return os; | |
| 19 | 20 | } |
| 20 | 21 | |
| 21 | 22 | int main(int argc, char **argv) { |
| 22 | 23 | CLI::App app; |
| 23 | 24 | |
| 24 | - Level level; | |
| 25 | + Level level{Level::Low}; | |
| 25 | 26 | // specify string->value mappings |
| 26 | - std::vector<std::pair<std::string, Level>> map{ | |
| 27 | - {"high", Level::High}, {"medium", Level::Medium}, {"low", Level::Low}}; | |
| 28 | - // checked Transform does the translation and checks the results are either in one of the strings or one of the | |
| 27 | + std::map<std::string, Level> map{{"high", Level::High}, {"medium", Level::Medium}, {"low", Level::Low}}; | |
| 28 | + // CheckedTransformer translates and checks whether the results are either in one of the strings or in one of the | |
| 29 | 29 | // translations already |
| 30 | 30 | app.add_option("-l,--level", level, "Level settings") |
| 31 | 31 | ->required() | ... | ... |