MIRA
Public Types | Public Member Functions | Static Public Member Functions | List of all members
ProgramOptions Class Reference

A central singleton class for accessing command line parameters Underlying class is boost::program::options See http://www.boost.org/doc/libs/1_44_0/doc/html/program_options.html Usage: More...

#include <utils/ProgramOptions.h>

Inheritance diagram for ProgramOptions:
Inheritance graph
[legend]

Public Types

typedef boost::program_options::options_description Description
 
typedef boost::program_options::positional_options_description PositionalDescription
 
typedef boost::program_options::variables_map VariableMap
 
typedef Base::Type Type
 

Public Member Functions

 ProgramOptions (int argc, char **argv, const std::string &helpCategory="")
 Constructor that takes command line arguments. More...
 
 ProgramOptions (const std::vector< std::string > &args, const std::string &helpCategory="")
 Constructor that takes command line arguments as vector. More...
 
DescriptiongetDescriptions ()
 Return the option description map. More...
 
PositionalDescriptiongetPositionalDescriptions ()
 Return the option description map. More...
 
void getOptions (VariableMap &oMap) const
 Return the option variable map. More...
 
VariableMap getOptions () const
 Return the option variable map. More...
 
const std::vector< std::string > & getArgs () const
 Return the command line arguments as passed in the constructor. More...
 

Static Public Member Functions

static Description createOptionsDescription (const std::string &name="")
 Create empty options description. More...
 
static Typeinstance ()
 Returns a reference to the singleton instance. More...
 
static bool isDestroyed ()
 Returns true, if the singleton was already destroyed. More...
 

Detailed Description

A central singleton class for accessing command line parameters Underlying class is boost::program::options See http://www.boost.org/doc/libs/1_44_0/doc/html/program_options.html Usage:

// First add some options
MIRA_CMDLINE.getDescriptions().add_options()
("version,v", "Displays version");
("file,f", boost::program_options::value<std::string>()->default_value("File.xml"), "My file");
auto category = ProgramOptions::createOptionsDescription("A category");
category.add_options()
("count,c", boost::program_options::value<int>(), "Count of something");
MIRA_CMDLINE.getDescriptions().add(category);
// Then ask if the parameters exist
if ( vmap.count("version") )
{
std::cout << "Version 1.0.0" << std::endl;
::exit(-1);
}
// and get the value
if ( vmap.count("count") )
int c = vmap["count"].as<int>();

Member Typedef Documentation

◆ Description

typedef boost::program_options::options_description Description

◆ PositionalDescription

typedef boost::program_options::positional_options_description PositionalDescription

◆ VariableMap

typedef boost::program_options::variables_map VariableMap

◆ Type

typedef Base::Type Type
inherited

Constructor & Destructor Documentation

◆ ProgramOptions() [1/2]

ProgramOptions ( int  argc,
char **  argv,
const std::string &  helpCategory = "" 
)

Constructor that takes command line arguments.

Parameters
helpCategoryname of a category to be created where the '–help' option is added (if empty, it is added to options directly without subcategory)

◆ ProgramOptions() [2/2]

ProgramOptions ( const std::vector< std::string > &  args,
const std::string &  helpCategory = "" 
)

Constructor that takes command line arguments as vector.

Parameters
helpCategorysee above

Member Function Documentation

◆ createOptionsDescription()

static Description createOptionsDescription ( const std::string &  name = "")
inlinestatic

Create empty options description.

Use this e.g. to create an options category with automatically determined display width, to add to the main options description after populating it (see example code above). Categories are just for structured display of options, they do not affect querying the values.

◆ getDescriptions()

Description& getDescriptions ( )
inline

Return the option description map.

Provided to add more options.

◆ getPositionalDescriptions()

PositionalDescription& getPositionalDescriptions ( )
inline

Return the option description map.

Provided to add more options. Calling this method also enables positional arguments. Positional arguments are identified by their position rather than their name. For positional options only the relative order of the arguments matters.

◆ getOptions() [1/2]

void getOptions ( VariableMap oMap) const

Return the option variable map.

Command line arguments are parsed according to the given description map and stored in this variable map.

◆ getOptions() [2/2]

VariableMap getOptions ( ) const
inline

Return the option variable map.

Command line arguments are parsed according to the given description map and stored in this variable map.

◆ getArgs()

const std::vector<std::string>& getArgs ( ) const
inline

Return the command line arguments as passed in the constructor.

Returns
Command line options as a pair of number of arguments and pointer to arguments.

◆ instance()

static Type& instance ( )
inlinestaticinherited

Returns a reference to the singleton instance.

Exceptions
XSingletonIf the singleton was already destroyed (dead reference) or not yet created (when using ExplicitInstantiation).

◆ isDestroyed()

static bool isDestroyed ( )
inlinestaticinherited

Returns true, if the singleton was already destroyed.

Trying to access it will result in an exception.


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