Repository navigation
Tutorial
To use Varse, first include the Varse.hpp file:
#include <Varse/Varse.hpp>This will give you access to the Parser class.
Everything in this library is done by the Parser class, so let's go ahead and create an instance of it.
va::Parser parser;To parse something from an argument array, vector or string, we need to add tokens to the parser. There are 3 types of tokens, each operates on a different kind of data. The first uses strings directly, the seconds used integers, and the thirds uses floating-point numbers. Let's try out the first type of token:
parser.setToken("$name", std::function<void(std::string)>[&](std::string next){
name = next;
});In this case, name is a string object that will contain the new name. It would be wise to set the value of name to some special case, such as "ERROR_NAME". This will help determine if the variable was changed or not.
the '$' preceding the "name" is part of the token, it can be anything or nothing at all. Up to you.
To do something with this token, let's feed the parser some arguments:
parser.parse(argc, argv);We're passing the program's arguments to the parser, and if one of these arguments matches a registered token, it's function will be called with the next argument as a parameter. The parse function will return true if everything was correctly parsed and false if we run out of arguments after a token has been found and has not been called yet -or if there are no arguments, see ignore mode-.
On a side note, the string received might contain a number or maybe an incorrect encoding, wide strings might be supported someday. The token types that actually do some checking are the numeric types, if for instance, a token of type int is found but next argument is a string with non-numeric text (e.g. "eb42"), the token function will not be called, because the next argument will not be converted. Same goes for float tokens.
After the call to parse returns, the rest of the program can flow normally, the Parser object can be destroyed, should you want to.
The parse function can take a great variety of arguments:
bool parse(int argc, char* argv[]);
bool parse(std::string, char delimiter);
bool parse(std::vector<std::string>);
bool parse(std::vector<char*>);Here's a full example:
#include <iostream>
#include <functional>
#include <Varse/Varse.hpp>
int main(int argc, char* argv[])
{
std::string name = "John Smith";
int age = 32;
float height = 1.80f; //Metric system rules
va::Parser parser;
parser.setToken("/name", std::function<void(std::string)>([&](std::string next)
{name = next; }));
parser.setToken("/age", std::function<void(int)>([&](int next)
{age = next; }));
parser.setToken("/height", std::function<void(float)>([&](float next)
{height = next; }));
parser.parse(argc, argv);
std::cout << "Name: " << name << std::endl;
std::cout << "Age: " << age << std::endl;
std::cout << "Height: " << height << std::endl;
getchar();
return 0;
}Run with
test /name "Carl Johnson" /age 30 /height 1.78
To obtain:
Name: Carl Johnson
Age: 30
Height: 1.78
👍