From 5810494286f108517658c58e853d185a9514b75b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?BENEDEK=20L=C3=A1szl=C3=B3?= Date: Fri, 1 Dec 2023 15:22:47 +0100 Subject: [PATCH] readme --- Readme.md | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Readme.md diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..129885d --- /dev/null +++ b/Readme.md @@ -0,0 +1,48 @@ +# Flags for C++ +This project aims to recreate the functionality of the `flag` package from Golang. + +## Usage +### Create a parser +```c++ +#include "flags.h" + +Parser parser; +or +// set the prefix and the help text +Parser parser("--", "Help:"); +``` + +### Declare a flag +```c++ +// name, description, required, default value +std::string* x = parser.add("myStr", "some description", true, "default value"); +``` + +### Parse arguments and show help text +```c++ +if (!parser.parse(argc, argv)) + parser.help(); +``` + +### Declare a custom parser class +```c++ +PARSER(MyTypeFlag, MyType, { + // parser body + // available variables: + // MyType value; <- storage for your value + // std::string arg; <- the next argument after the current flag + set_found(true); + try { + value = my_parser_function(arg); + set_found(true); + } catch(std::exception&) {} +}) + +parser.set_parser(flag_constructor_t(MyTypeFlag::make)); +``` + +## Built-in parsers +- std::string +- int (long, long long, unsigned long, unsigned long long) +- float, double, long double +- bool