-
Notifications
You must be signed in to change notification settings - Fork 0
Examples
simfeo edited this page Jan 10, 2023
·
15 revisions
#include <iostream>
#include "ArgParse/argparse.h"
int main(int argc, char** argv)
{
auto parser = argparse::ArgumentParser("Program name").SetDescription("Description of program");
parser.AddArgument(argparse::CreateNamedArgument()
.SetLongName("numbers")
.SetAnyNumberOfArgumentsButAtleastOne()
.SetType(argparse::ArgTypeCast::e_int));
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));
auto obj = parser.ParseArgs(argc, argv);
if (obj.IsArgValid())
{
auto arg = obj.GetArg("numbers");
if (arg.GetArgumentExists())
{
for (auto& el : arg.GetAsVecInt())
{
std::cout << el << std::endl;
}
}
}
else
{
std::string help = parser.GetHelp(80);
std::cout << help << std::endl;
}
return 0;
}This exmaple will create program with keys -n, --number, -s, --some_boring_long_name, -h and --help. Help will be autogenerated. The types of numbers and some_boring_long_name are integers with count from 1 to infinite.
Note actually terminal cannot get infinite numbers of arguments. In most cases maximum length of all input that terminal can pass is limited with 8 kb.