mirror of
https://github.com/CLIUtils/CLI11.git
synced 2025-04-29 12:13:52 +00:00
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
This commit is contained in:
parent
ba07592ed8
commit
11519d8c75
@ -825,6 +825,7 @@ Several short examples of different features are included in the repository. A b
|
||||
- [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.
|
||||
- [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
|
||||
- [enum](https://github.com/CLIUtils/CLI11/blob/master/examples/enum.cpp): Using enumerations in an option, and the use of [CheckedTransformer](#transforming-validators)
|
||||
- [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.
|
||||
- [formatter](https://github.com/CLIUtils/CLI11/blob/master/examples/formatter.cpp): Illustrating usage of a custom formatter
|
||||
- [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
|
||||
- [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
|
||||
|
||||
## Contribute
|
||||
|
||||
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).
|
||||
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).
|
||||
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].
|
||||
|
||||
This project was created by [Henry Schreiner](https://github.com/henryiii).
|
||||
|
@ -1,15 +1,15 @@
|
||||
#include <CLI/CLI.hpp>
|
||||
#include <map>
|
||||
|
||||
enum class Level : int { High, Medium, Low };
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
CLI::App app;
|
||||
|
||||
Level level;
|
||||
Level level{Level::Low};
|
||||
// specify string->value mappings
|
||||
std::vector<std::pair<std::string, Level>> map{
|
||||
{"high", Level::High}, {"medium", Level::Medium}, {"low", Level::Low}};
|
||||
// checked Transform does the translation and checks the results are either in one of the strings or one of the
|
||||
std::map<std::string, Level> map{{"high", Level::High}, {"medium", Level::Medium}, {"low", Level::Low}};
|
||||
// CheckedTransformer translates and checks whether the results are either in one of the strings or in one of the
|
||||
// translations already
|
||||
app.add_option("-l,--level", level, "Level settings")
|
||||
->required()
|
||||
|
@ -2,30 +2,30 @@
|
||||
|
||||
enum class Level : int { High, Medium, Low };
|
||||
|
||||
// this breaks the code
|
||||
inline std::ostream &operator<<(std::ostream &o, const Level &l) {
|
||||
switch(l) {
|
||||
// Defining operator<<() for your enum class (in this case for 'Level') overrides CLI11's enum streaming
|
||||
inline std::ostream &operator<<(std::ostream &os, const Level &level) {
|
||||
switch(level) {
|
||||
case Level::High:
|
||||
o << "High";
|
||||
os << "High";
|
||||
break;
|
||||
case Level::Medium:
|
||||
o << "Medium";
|
||||
os << "Medium";
|
||||
break;
|
||||
case Level::Low:
|
||||
o << "Low";
|
||||
os << "Low";
|
||||
break;
|
||||
}
|
||||
return o;
|
||||
os << " (ft rom custom ostream)";
|
||||
return os;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
CLI::App app;
|
||||
|
||||
Level level;
|
||||
Level level{Level::Low};
|
||||
// specify string->value mappings
|
||||
std::vector<std::pair<std::string, Level>> map{
|
||||
{"high", Level::High}, {"medium", Level::Medium}, {"low", Level::Low}};
|
||||
// checked Transform does the translation and checks the results are either in one of the strings or one of the
|
||||
std::map<std::string, Level> map{{"high", Level::High}, {"medium", Level::Medium}, {"low", Level::Low}};
|
||||
// CheckedTransformer translates and checks whether the results are either in one of the strings or in one of the
|
||||
// translations already
|
||||
app.add_option("-l,--level", level, "Level settings")
|
||||
->required()
|
||||
|
Loading…
x
Reference in New Issue
Block a user