clang-tags
C/C++ source code indexing tool based on libclang
 All Classes Functions Variables Typedefs Groups Pages
Public Types | Public Member Functions | List of all members
Getopt Class Reference

Command-line arguments handler. More...

#include <getopt.hxx>

Public Types

typedef std::vector< std::string > OptionValues
typedef std::map< std::string,
OptionValues > 
OptionsMap

Public Member Functions

 Getopt (int argc, char *const *argv, const char *documentation="Usage: %c [options]")
 Constructor.
void add (const char *longOpt, char shortOpt, int argument, const char *description="", const char *argDescription="")
 Define an option.
void get ()
 Process options from the command-line.
std::string usage ()
 Get usage string.
std::string operator[] (std::string opt) const
 Get the value of a command-line option If the switch was given multiple times, only return the last value.
int getCount (std::string opt) const
 Get the number of times a command-line switch was given.
const OptionValues & getAll (std::string opt) const
 Get all provided values for a command-line option.
std::string shift ()
 Shift remaining positional arguments.
int argc () const
 Get number of remaining positional arguments.
char const *const * argv () const
 Get remaining positional arguments.
std::string argv (int i) const
 Get remaining positional arguments.

Detailed Description

Command-line arguments handler.

Getopt opt (argc, argv);
opt.add ("help", 'h', 0, "Show this help");
if (opt["help"] != "") {
std::cout << opt.usage();
}

Constructor & Destructor Documentation

Getopt::Getopt ( int  argc,
char *const *  argv,
const char *  documentation = "Usage: %c [options]" 
)

Constructor.

Parameters
argcnumber of command-line arguments
argvcommand-line arguments
argumentsdescription of non-switch command-line arguments

Member Function Documentation

void Getopt::add ( const char *  longOpt,
char  shortOpt,
int  argument,
const char *  description = "",
const char *  argDescription = "" 
)

Define an option.

Parameters
longOptlong option name
shortOptshort option switch
argument0 (none), 1 (required), 2 (optional)
descriptionlong description of the option
argDescriptionargument description if applicable
void Getopt::get ( )

Process options from the command-line.

This method must be called after all calls to 'add'.

const Getopt::OptionValues & Getopt::getAll ( std::string  opt) const
inline

Get all provided values for a command-line option.

Returns
a vector of values
int Getopt::getCount ( std::string  opt) const
inline

Get the number of times a command-line switch was given.

Returns
count
std::string Getopt::operator[] ( std::string  opt) const
inline

Get the value of a command-line option If the switch was given multiple times, only return the last value.

Returns
"" if the switch is unset
std::string Getopt::shift ( )
inline

Shift remaining positional arguments.

Returns
the first remaining positional argument, "" if there are no more positional args

The documentation for this class was generated from the following files: