cli++ is a minimalist command line parser for c++.
#include <cli++/cli.h>
#include <iostream>
using namespace std;
using namespace clipp;
int main(int argc, char** argv)
{
CLIParser parser;
//add command line options
CLIOpt cli_help = parser.addOpt("help"); //--help
CLIOpt cli_test = parser.addOpt('t'); //-t
CLIOpt cli_output = parser.addOpt('o', "output", 1);
//-o, --output <param>
CLIOpt cli_size = parser.addOpt('s', "size", 2);
//-s, --size <param1> <param2>
try
{
//parse command line arguments
for(CLIArg next : parser.parse(argc, argv))
if(next.opt == cli_help)
cout << "print helpfile here" << endl;
else if(next.opt == cli_test)
cout << "short option worked" << endl;
else if(next.opt == cli_output)
cout << "output: " << next.params[0] << endl;
else if(next.opt == cli_size)
cout << "size: " << next.params[0] << " x " << next.params[1] << endl;
else //if(next.opt == CLINonOpt)
cout << "argument: " << next.str << endl;
}
catch(CLIException& ex)
{
//in case of unknown option or missing argument
cerr << argv[0] << ": " << ex.what() << endl;
return 1;
}
return 0;
}
cli++ has the following dependencies:
- make
- a C++11 compliant compiler
The project can be built with
$ make
and installed with into /usr/local
with
$ make install
To specify a custom installation prefix, use
$ make install PREFIX=path/to/prefix