filmstro_optionsparser
 All Classes Files Functions Variables Enumerations Enumerator Pages
Classes | Public Types | Public Member Functions | Public Attributes | List of all members
OptionsParser Class Reference

This class provides a parser for command line arguments in unix style. More...

#include <filmstro_optionsParser.h>

Classes

class  Option
 

Public Types

enum  OptionType {
  OptString = 0, OptFile, OptInteger, OptDouble,
  OptBoolean
}
 

Public Member Functions

OptionsParser::OptionaddOption (juce::String optId, juce::String optArg, const OptionType, const bool req=false)
 Create an option to be used in the parser. More...
 
juce::String getHelpText () const
 Returns a help text for all options. More...
 
bool parseArguments (const juce::StringArray &arguments, const bool failOnUnknownOption=true)
 Read arguments and set them into the options. More...
 
juce::String getErrorMessage () const
 if parseArguments failed, this will contain a helpful text about bad arguments More...
 
bool isOptionSet (juce::StringRef optId) const
 After parseArguments check if a certain option was set by the user. More...
 
juce::String getOptString (juce::StringRef optId) const
 Return a text value set by argument. More...
 
juce::File getOptFile (juce::StringRef optId) const
 Return a file set via option. More...
 
int getOptInt (juce::StringRef optId) const
 Return an integer value set by argument. More...
 
double getOptDouble (juce::StringRef optId) const
 Return a float value set by argument. More...
 
bool getOptBoolean (juce::StringRef optId) const
 Return a boolean value set by a flag. More...
 
OptiongetOption (juce::StringRef optId)
 Returns a pointer to a option instance. More...
 
const OptiongetOption (juce::StringRef optId) const
 

Public Attributes

juce::String header
 This will be printed before the help text. More...
 
juce::String footer
 This will be printed after the help text. More...
 

Detailed Description

This class provides a parser for command line arguments in unix style.

You can add options with long arguments prefixed by "--" or short arguments using "-". It will create a help text and error messages automatically. An example would look like that:

OptionsParser options;
options.header = "My console application";
options.footer = "Build date: " + String(__DATE__) + " at " + String(__TIME__);
option->longArg = "help";
option->helpText = "Display this help text and exit";
option = options.addOption ("logfile", "l", OptionsParser::OptFile, false);
option->longArg = "logfile";
option->helpText = "Set a logfile to enable logging";
if (! options.parseArguments (StringArray::fromTokens (commandLine, true))) {
std::cout << options.getErrorMessage () << std::endl;
exit (-1);
}
if (options.getErrorMessage().isNotEmpty()) // there can also be warnings...
std::cout << options.getErrorMessage () << std::endl;
if (options.getOptBoolean("help")) {
std::cout << options.getHelpText() << std::endl;
exit (0);
}
if (options.isOptionSet ("logfile")) {
File logfile = options.getOptFile("logfile");
Logger::setCurrentLogger (new FileLogger (logfile, "Started My Application"));
}

Member Enumeration Documentation

Enumerator
OptString 
OptFile 
OptInteger 
OptDouble 
OptBoolean 

Member Function Documentation

OptionsParser::Option * OptionsParser::addOption ( juce::String  optId,
juce::String  optArg,
const OptionType  type,
const bool  req = false 
)

Create an option to be used in the parser.

juce::String OptionsParser::getErrorMessage ( ) const

if parseArguments failed, this will contain a helpful text about bad arguments

juce::String OptionsParser::getHelpText ( ) const

Returns a help text for all options.

bool OptionsParser::getOptBoolean ( juce::StringRef  optId) const

Return a boolean value set by a flag.

double OptionsParser::getOptDouble ( juce::StringRef  optId) const

Return a float value set by argument.

juce::File OptionsParser::getOptFile ( juce::StringRef  optId) const

Return a file set via option.

It will also create paths relative to the executable

int OptionsParser::getOptInt ( juce::StringRef  optId) const

Return an integer value set by argument.

OptionsParser::Option * OptionsParser::getOption ( juce::StringRef  optId)

Returns a pointer to a option instance.

const OptionsParser::Option * OptionsParser::getOption ( juce::StringRef  optId) const
juce::String OptionsParser::getOptString ( juce::StringRef  optId) const

Return a text value set by argument.

bool OptionsParser::isOptionSet ( juce::StringRef  optId) const

After parseArguments check if a certain option was set by the user.

bool OptionsParser::parseArguments ( const juce::StringArray &  arguments,
const bool  failOnUnknownOption = true 
)

Read arguments and set them into the options.

Returns true, if all requirements are met.

Member Data Documentation

juce::String OptionsParser::footer

This will be printed after the help text.

juce::String OptionsParser::header

This will be printed before the help text.


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