Simple command-line options handler (C++11).
- Handy, one line per option.
- Tiny, cross-platform, self-contained, header-only.
- ZLIB/libPNG licensed.
#> cat sample.cc
#include <iostream>
#include <string>
#include "getopt.hpp"
int main() {
// - No initialization required: (argc, argv) pair automatically retrieved.
// - First argument is default option value, then all option indentifiers follow.
bool help = getarg( false, "-h", "--help", "-?" );
int version = getarg( 0, "-v", "--version", "--show-version" );
int depth = getarg( 1, "-d", "--depth", "--max-depth");
std::string file = getarg( "", "-f", "--file" );
// [...]
std::cout << help << ',' << file << ',' << version << std::endl;
}
#> g++ sample.cc && ./a.out --file=/a/b/c --depth=123
0, /a/b/c, 123Two APIs provided:
- Simple functional api
getarg(...).- No initialization required: (argc, argv) pair automatically retrieved.
- First argument is default option value, then all option indentifiers follow.
int main() {
bool help = getarg( false, "-h", "--help", "-?" );
int version = getarg( 0, "-v", "--version", "--show-version" );
int depth = getarg( 1, "-d", "--depth", "--max-depth");
std::string file = getarg( "", "-f", "--file" );
[...]
}-
Simple OOP map-based api
getopt class. Initialization (argc, argv) pair required.This getopt class is a
std::mapreplacement where key/value arestd::stringtypes. Given invokation'./app.out --user=me --pass=123 -h'this class delivers not only:map[0] = "./app.out",map[1] = "--user=me",map[2]="--pass=123",map[3]='-h'but also,map["--user"]="me",map["--pass"]="123"and also,map["-h"]=trueAdditional API:
cmdline()for a print app invocation stringstr()for pretty map printingsize()number of arguments (equivalent to argc), rather thanstd::map.size()
int main( int argc, const char **argv ) {
getopt args( argc, argv );
if( args.has("-h")
|| args.has("--help")
|| args.has("-?")
|| args.size() == 1 ) {
std::cout << args["0"]
<< " [-?|-h|--help] [-v|--version] [--depth=number]"
<< std::endl;
return 0;
}
if( args.has("-v")
|| args.has("--version") ) {
std::cout << args["0"]
<< " sample v1.0.0. Compiled on "
<< __DATE__ << std::endl;
}
if( args.has("--depth") ) {
int depth = atoi( args["--depth"].c_str() );
std::cout << "depth set to "
<< depth
<< std::endl;
}
[...]
}- v1.0.0 (2016/04/18): Initial version
- v1.1.0 (2021/??/??): ???????????????