ArgumentParser is easy and lightweight C++ argument parser to keep parsing command-line
arguments simple. It is composed just from few header files, so you add it to the project
just by including it.

Jan Brejcha f865fb37c9 Fixed error: when the user supports no arguments, the parse() method 5 years ago
argumentparser f865fb37c9 Fixed error: when the user supports no arguments, the parse() method 5 years ago
.gitignore 826296b335 Updated Makefile to compile with c++11, to get rid of the warnings. 5 years ago
LICENCE 7d4e43f23f Added ArgumentParser C++ header library. 5 years ago
Makefile 826296b335 Updated Makefile to compile with c++11, to get rid of the warnings. 5 years ago
README.md edbff27b59 Fixed another indentation problem. Also added Improve and fork notice. 5 years ago
main.cpp 7d4e43f23f Added ArgumentParser C++ header library. 5 years ago

README.md

ArgumentParser

ArgumentParser is easy and lightweight C++ argument parser to keep parsing command-line arguments simple. It is composed just from few header files, so you add it to the project just by including it.

Format

Each argument can be defined as short, long or both:

-arg --argument

Each argument can have multiple (or zero) position-defined parameters

-arg <param 1> <param 2> ... <param n>

Each argument can be also optional:

[-arg <param 1> <param 2> ... <param n>]

Usage

At first, include the argumentparser.h file:

#include "argumentparser.h"

Then, bootstrap the ArgumentParser object:

ArgumentParser ap("<your binary name>",
                  "<your binary description>");

Create parameters and arguments:

vector<Parameter> param1;
param1.push_back(Parameter("<param name>", "<param description>"));

Finally, add the arguments with desired parameters

ap.addArgument( "<argument shortname without dashes(e.g. a for -a)", 
                "<argument full name without dashes (e.g. argument for --argument>", 
                param1,
                "<argument description>",
                <boolean optional flag (true for optional)>
               );

At last, run the parsing stage and obtain the results:

bool parsed = ap.parse(argc, (const char **)argv);

if (parsed)
{
	cout << "Parsed successfully" << endl;
	vector<string> arg = ap.resultByShortname("a");
	cout << "param 1: " << arg[0] << " << endl;
	vector<string> segps_res = ap.resultByShortname("segps");
	cout << "south east lat: " << segps_res[0] << ", lon: " << segps_res[1] << endl;
}

You can also check if the argument has been found in the argv during parsing:

Argument *arg = ap.argumentByShortname("a");
if (arg->exists())
	//exists

Example code

You can compile the example code that shows particular usage of this header library. just run make on your system and the run ./example.

LICENCE

This source code is under BUT OPEN SOURCE LICENCE Version 1. Copyright (c) 2010, Brno University of Technology, Antonínská 548/1, 601 90, Czech Republic. For more info, see LICENCE.

Fork and Improve!

If you want to improve this solution, feel free to fork this repository and add your hacks. In case you make some contribution, I would be glad to merge your changes into this original version. Please, let me know!