Commit 25cca2dcb9cc01948f519db556dd9296de02950a
Committed by
GitHub
1 parent
a8ef5b8d
docs: add custom parse example (#603)
Showing
3 changed files
with
46 additions
and
1 deletions
README.md
| ... | ... | @@ -799,7 +799,7 @@ Every `add_` option you have seen so far depends on one method that takes a lamb |
| 799 | 799 | |
| 800 | 800 | Other values can be added as long as they support `operator>>` (and defaults can be printed if they support `operator<<`). To add a new type, for example, provide a custom `operator>>` with an `istream` (inside the CLI namespace is fine if you don't want to interfere with an existing `operator>>`). |
| 801 | 801 | |
| 802 | -If you wanted to extend this to support a completely new type, use a lambda or add a specialization of the `lexical_cast` function template in the namespace `CLI::detail` with the type you need to convert to. Some examples of some new parsers for `complex<double>` that support all of the features of a standard `add_options` call are in [one of the tests](./tests/NewParseTest.cpp). A simpler example is shown below: | |
| 802 | +If you wanted to extend this to support a completely new type, use a lambda or add a specialization of the `lexical_cast` function template in the namespace of the type you need to convert to. Some examples of some new parsers for `complex<double>` that support all of the features of a standard `add_options` call are in [one of the tests](./tests/NewParseTest.cpp). A simpler example is shown below: | |
| 803 | 803 | |
| 804 | 804 | #### Example |
| 805 | 805 | |
| ... | ... | @@ -871,6 +871,7 @@ The API is [documented here][api-docs]. Also see the [CLI11 tutorial GitBook][gi |
| 871 | 871 | Several short examples of different features are included in the repository. A brief description of each is included here |
| 872 | 872 | |
| 873 | 873 | - [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. |
| 874 | + - [custom_parse](https://github.com/CLIUtils/CLI11/blob/master/examples/custom_parse.cpp): Based on [Issue #566](https://github.com/CLIUtils/CLI11/issues/566), example of custom parser | |
| 874 | 875 | - [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 |
| 875 | 876 | - [enum](https://github.com/CLIUtils/CLI11/blob/master/examples/enum.cpp): Using enumerations in an option, and the use of [CheckedTransformer](#transforming-validators) |
| 876 | 877 | - [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. | ... | ... |
examples/CMakeLists.txt
| ... | ... | @@ -242,3 +242,8 @@ set_property(TEST retired_retired_test2 PROPERTY PASS_REGULAR_EXPRESSION "WARNIN |
| 242 | 242 | set_property(TEST retired_retired_test3 PROPERTY PASS_REGULAR_EXPRESSION "WARNING.*retired") |
| 243 | 243 | |
| 244 | 244 | set_property(TEST retired_deprecated PROPERTY PASS_REGULAR_EXPRESSION "deprecated.*not_deprecated") |
| 245 | + | |
| 246 | +#-------------------------------------------- | |
| 247 | +add_cli_exe(custom_parse custom_parse.cpp) | |
| 248 | +add_test(NAME cp_test COMMAND custom_parse --dv 1.7) | |
| 249 | +set_property(TEST cp_test PROPERTY PASS_REGULAR_EXPRESSION "called correct") | ... | ... |
examples/custom_parse.cpp
0 → 100644
| 1 | +// Copyright (c) 2017-2020, University of Cincinnati, developed by Henry Schreiner | |
| 2 | +// under NSF AWARD 1414736 and by the respective contributors. | |
| 3 | +// All rights reserved. | |
| 4 | +// | |
| 5 | +// SPDX-License-Identifier: BSD-3-Clause | |
| 6 | + | |
| 7 | +// from Issue #566 on github https://github.com/CLIUtils/CLI11/issues/566 | |
| 8 | + | |
| 9 | +#include <CLI/CLI.hpp> | |
| 10 | +#include <iostream> | |
| 11 | +#include <sstream> | |
| 12 | + | |
| 13 | +// example file to demonstrate a custom lexical cast function | |
| 14 | + | |
| 15 | +template <class T = int> struct Values { | |
| 16 | + T a; | |
| 17 | + T b; | |
| 18 | + T c; | |
| 19 | +}; | |
| 20 | + | |
| 21 | +// in C++20 this is constructible from a double due to the new aggregate initialization in C++20. | |
| 22 | +using DoubleValues = Values<double>; | |
| 23 | + | |
| 24 | +// the lexical cast operator should be in the same namespace as the type for ADL to work properly | |
| 25 | +bool lexical_cast(const std::string &input, Values<double> &v) { | |
| 26 | + std::cout << "called correct lexical_cast function ! val: " << input << std::endl; | |
| 27 | + return true; | |
| 28 | +} | |
| 29 | + | |
| 30 | +DoubleValues doubles; | |
| 31 | +void argparse(CLI::Option_group *group) { group->add_option("--dv", doubles)->default_str("0"); } | |
| 32 | + | |
| 33 | +int main(int argc, char **argv) { | |
| 34 | + CLI::App app; | |
| 35 | + | |
| 36 | + argparse(app.add_option_group("param")); | |
| 37 | + CLI11_PARSE(app, argc, argv); | |
| 38 | + return 0; | |
| 39 | +} | ... | ... |