Commit 46ce0d1b authored by ESTERIE Pierre's avatar ESTERIE Pierre
Browse files

Update readmes

parent 75d5820d
# cpp_tools : small cpp tools to reuse in you projects
# cpp_tools : small cpp tools to reuse in your projects
`cpp_tools` is a collection of headers organised as modules.
The fisrt effort here, is to use this repository as a submodule in your lib/app.
......@@ -77,5 +77,6 @@ And that's it !
## Documentation
Docs of the modules can be found [here](https://compose.gitlabpages.inria.fr/oldstack/cpp_tools/).
[cl_parser](cl_parser/README.md)
:construction: Docs of the modules can be found [here](https://compose.gitlabpages.inria.fr/oldstack/cpp_tools/) (under construction)
# Command Line Parser
The command line parser allows you to easyly managed command line arguments
of your programs.
The following features are available :
- multiple names for a flag (i.e `--a-very-long-name`, `-shortname`, `--unotherone`, etc)
- required flags that force the user to provide a value.
- vector of values as input of a flag
- automatic `help` flag generation.
## Example
First let's look at the anatomy of a flag.
It's a simple structure with the following required fields :
- `cpp_tools::cl_parser::str_vec flags` : a vector of strings with the different versions of the flag
- `const char* description` : the description of the flag that will appear in the help
- `using type = float` : the type of the flag
- `type def = 1.0` : the default value of the flag (optional)
To use the library, two files are required `cpp_tools/cl_parser/tcli.hpp` and `cpp_tools/cl_parser/help_descriptor.hpp`
Now, let's have a look a a first example :
```cpp
#include <cpp_tools/cl_parser/tcli.hpp>
#include <cpp_tools/cl_parser/help_descriptor.hpp>
struct n_count
{
// here, you specify several flags by providing an initializer list of strings
// to the flags variable.
cpp_tools::cl_parser::str_vec flags = {"--n-count", "-n"};
// then, you specify the description of the flag/s
const char* description = "An integer to catch.";
// register the type of the flag/s
using type = int;
// put the def variable to the default value of the flag (optional)
type def = 1;
};
// another example with some flaoting point type
struct delta
{
cpp_tools::cl_parser::str_vec flags = {"--delta", "-d"};
const char* description = "Delta to apply.";
using type = double;
type def = 0.1;
};
```
Now let's add a required flag that will throw an exception if not provided :
```cpp
\\ you need to inherit from required_tag to register your flag as required.
struct input_file : cpp_tools::cl_parser::required_tag
{
cpp_tools::cl_parser::str_vec flags = {"--input-file", "-fin"};
const char* description = "Input filename.";
using type = std::string;
};
```
After defining all of our flags, we can create our parser :
```cpp
auto main(int argc, char* argv[]) -> int
{
// Parameter handling, register all your flags and add the help flag for automated help.
auto parser = cpp_tools::cl_parser::make_parser( cpp_tools::cl_parser::help{}
, n_count{}
, delta{}
, input_file{});
// then, we ask the parser to parse argc and argv
parser.parse(argc, argv);
...
}
```
Note that we added the `cpp_tools::cl_parser::help{}` flag to generate automaticaly
the help command line argument.
And finally we can access the flag value as follow :
```cpp
const auto n_count = parser.get<n_count>();
const auto file = parser.get<input_file>();
```
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment