-
Notifications
You must be signed in to change notification settings - Fork 0
Tutorial
First of all clone repository to your local disk.
You can get library via git way:
- open terminal
- navigate to desired location with cd command
- then use "git clone https://github.com/simfeo/FancyArgumentParser.git"
Alternatively you can download zip line with source code by this link. Unpack zip file to desired location.
If you putted library under your projects root "argparse.h" file of library will be visible for search. Otherwise don't forget to add library location to "includes search paths" according to you build system rules.
After that you can finally use library.
First of all add library to your cpp source file. It should look something like
#include "ArgParse/argparse.h" or #include <argparse.h> depending to where you put file and where build system searches includes.
If build succeed after that then you can go to next step. If not then you should fix errors related with includes.
First of all you should create instance of argument parser object:
auto parser = argparse::ArgumentParser("Program name").SetDescription("Description of program");After that you can add some arguments though AddArgument and argparse::CreateNamedArgument() or argparse::CreatePositionalArgument. For example:
// required argument of type int with name "numbers" and 1 or more arguments count
parser.AddArgument(argparse::CreateNamedArgument().SetLongName("numbers").SetAnyNumberOfArgumentsButAtleastOne().SetType(argparse::ArgTypeCast::e_int));
// non required argument of type int with name "some_boring_long_name" and 1 or more arguments count and custom help
parser.AddArgument(argparse::CreateNamedArgument()
.SetLongName("some_boring_long_name")
.SetAnyNumberOfArgumentsButAtleastOne()
.SetType(argparse::ArgTypeCast::e_int)
.SetHelp("some_boring_long_name description with some important information for user.")
.SetRequired(false));When you've putted all desired arguments, you can pass command line arguments to parser. As result you'll get instance of argparse::ArgumentsObject. The instance can indicate error's if parsing failed. Or you can access to parsed arguments if parsing was successful.
// pass argc and argv to parser
auto obj = parser.ParseArgs(argc, argv);
// check is prsing was successful
if (obj.IsArgValid())
{
// retrieving argument with name "numbers"
auto arg = obj.GetArg("numbers");
// checking is argument exist is redundant here, cause argument is required
if (arg.GetArgumentExists())
{
// iterating though input
for (auto& el : arg.GetAsVecInt())
{
std::cout << el << std::endl;
}
}
}
else
{
// 80 is length of you terminal
std::string help = parser.GetHelp(80);
std::cout << help << std::endl;
}This example will print following help in console in case if parsing was failed:
Program name -n,--numbers [n ...] [-s,--some_boring_long_name [s ...] ] -h,--help
Description of program
optional arguments:
-n,--numbers Type: INT. Args count: at least one.
-s,--some_boring_long_name
some_boring_long_name description with some important information
for user. Type: INT. Args count: at least one.
-h,--help Show help!
argparse::ArgumentParser::AddArgument function can throw error which indicates wrong formed argument. You don't need to use try/catch block here. Just run the program and edit mistakes using exceptions string tips.
For more details please use Classes overview, examples and Doxygen documentation