multovl  1.3
Multiple overlaps of genomic regions
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
Public Member Functions | Protected Member Functions | List of all members
multovl::Polite Class Referenceabstract

Program Options LITE base class. The Polite class provides a wrapper over boost::program_options to make life a bit easier. Functionality includes parsing the default –help and –version options, and access to positional parameters. Apps using this are supposed to subclass Polite and add their own extra options. More...

#include "polite.hh"

Inheritance diagram for multovl::Polite:
multovl::MultovlOptbase multovl::ClassicOpts multovl::prob::ProbOpts multovl::prob::ParProbOpts

Public Member Functions

void process_commandline (int argc, char *argv[])
 
bool parse_check (int argc, char *argv[])
 
std::vector< std::string > pos_opts () const
 
bool error_status () const
 
std::string error_messages () const
 
bool version_needed () const
 
virtual std::ostream & print_version (std::ostream &out) const
 
bool help_needed () const
 
virtual std::ostream & print_help (std::ostream &out) const
 

Protected Member Functions

 Polite (const std::string &descr="Options")
 
template<typename Opttype >
void add_option (const std::string &name, Opttype *optvarp, const Opttype &defval, const std::string &descr, const char nm= '\0')
 
void add_bool_switch (const std::string &name, bool *varp, const std::string &descr, const char nm= '\0')
 
template<typename Opttype >
void add_mandatory_option (const std::string &name, const std::string &descr, const char nm= '\0')
 
bool perform_parsing (int argc, char *argv[])
 
virtual bool check_variables ()=0
 
bool option_seen (const std::string &optnm) const
 
template<typename Opttype >
Opttype fetch_value (const std::string &optnm) const
 
void add_error (const std::string &msg)
 
void error_reset ()
 Reset the error status to "no error".
 
virtual std::ostream & version_info (std::ostream &out) const
 

Detailed Description

Program Options LITE base class. The Polite class provides a wrapper over boost::program_options to make life a bit easier. Functionality includes parsing the default –help and –version options, and access to positional parameters. Apps using this are supposed to subclass Polite and add their own extra options.

Constructor & Destructor Documentation

multovl::Polite::Polite ( const std::string &  descr = "Options")
explicitprotected

Inits with a single option "--help" or "-h" and a single positional option. For simplicity's sake, the positionals are std::strings only, the name is fixed to "posopts", and they can be anywhere after the "real" options.

Parameters
descrthe overall description of the visible options

Member Function Documentation

void multovl::Polite::add_bool_switch ( const std::string &  name,
bool *  varp,
const std::string &  descr,
const char  nm = '\0' 
)
protected

Add a non-mandatory Boolean switch option.

Parameters
namethe 'long' option name like "--opt"
varppointer to the data member storing the option value
descrthe description of the option
nmthe 'short' option name like "-o", can be omitted
void multovl::Polite::add_error ( const std::string &  msg)
protected

Adds an error message, sets error status. This method may be called more than once during parsing, the error messages will be "remembered".

Parameters
msga descriptive error message.
template<typename Opttype >
void multovl::Polite::add_mandatory_option ( const std::string &  name,
const std::string &  descr,
const char  nm = '\0' 
)
inlineprotected

Add a mandatory option. Derived classes should have members for each option variable, and then invoke this method in their constructors to set them up.

Parameters
namethe 'long' option name like "--opt"
descrthe description of the option
nmthe 'short' option name like "-o", can be omitted
template<typename Opttype >
void multovl::Polite::add_option ( const std::string &  name,
Opttype *  optvarp,
const Opttype &  defval,
const std::string &  descr,
const char  nm = '\0' 
)
inlineprotected

Add a non-mandatory option. Derived classes should have members for each option variable, and then invoke this method in their constructors to set them up.

Parameters
namethe 'long' option name like "--opt"
optvarppointer to the data member storing the option value
defvaldefault value for the option
descrthe description of the option
nmthe 'short' option name like "-o", can be omitted
virtual bool multovl::Polite::check_variables ( )
protectedpure virtual

Derived classes must override this method so that first an appropriate base class version is invoked to process the base class' commandline variables and then additional processing must be done on the derived class' variables if /true/ was returned, like this:-

bool DerivedOpts::check_variables()
{
        bool ok = BaseOpts::check_variables();  // don't invoke Polite::check_variables() !
        if (ok) {
                // e.g. check some variables here
                if (option_seen("opt") > 0)
                {
                        int opt = fetch_value<int>("opt");
                        if (opt < 1) opt = 1;
                        // ...
                        ok = true;
                }
                else
                {
                    add_error("opt must be present");
                    ok = false;
  }
        }
        return ok;
}

Implemented in multovl::prob::ProbOpts, multovl::ClassicOpts, multovl::MultovlOptbase, and multovl::prob::ParProbOpts.

std::string multovl::Polite::error_messages ( ) const
inline

Error messages

Returns
all error messages as a multiline std::string each line contains one message added with the (protected) add_error(msg) method.
bool multovl::Polite::error_status ( ) const
inline

Error status

Returns
/true/ if at least one error has been seen during parsing.
template<typename Opttype >
Opttype multovl::Polite::fetch_value ( const std::string &  optnm) const
inlineprotected

Fetches the value of an option.

Parameters
optnmthe name of the option.
Returns
the value of the option.
bool multovl::Polite::help_needed ( ) const
inline

Indicates whether the help should be printed.

Returns
/true/ if the "--help" or "-h" option has seen, or if error(s) occurred.
bool multovl::Polite::option_seen ( const std::string &  optnm) const
inlineprotected

Checks whether an option has been seen.

Parameters
optnmthe name of the option.
Returns
true if the option has been seen on the command line.
bool multovl::Polite::parse_check ( int  argc,
char *  argv[] 
)

Parses the command line and checks the variables. the number of commandline arguments as provided by main() the commandline arguments in a C std::string array as provided by main()

Returns
/true/ on success, /false/ on error. Upon success, you may access the variables' values.
bool multovl::Polite::perform_parsing ( int  argc,
char *  argv[] 
)
protected

Actually parses the command line. NOTE: You may invoke this method only once, because otherwise strange things happen (seem to be a "feature" of boost::program_options).

Parameters
argcthe length of the argument array, must be >=1
argvthe arguments, argv[0] is the program name
Returns
/true/ on success, /false/ on error, including repeated parsing.
std::vector<std::string> multovl::Polite::pos_opts ( ) const

Returns the positional options as a std::string vector.

Returns
a std::string vector or an empty vector if there were no arguments.
virtual std::ostream& multovl::Polite::print_help ( std::ostream &  out) const
virtual

Prints help, using the facilities provided by boost::program_options

Parameters
outthis is the stream the help text is printed to.
Returns
/out/ is returned here

Reimplemented in multovl::prob::ProbOpts, multovl::ClassicOpts, and multovl::prob::ParProbOpts.

virtual std::ostream& multovl::Polite::print_version ( std::ostream &  out) const
inlinevirtual

Prints the version information

Parameters
outthis is the stream the version text is printed to.
Returns
/out/ is returned here

Reimplemented in multovl::MultovlOptbase.

void multovl::Polite::process_commandline ( int  argc,
char *  argv[] 
)

Convenience method to process all command line arguments at once. This method invokes parse_check(argc, argv) and then does the following:- If version information was requested, invokes print_version() and then invokes exit(EXIT_SUCCESS). If help was requested, invokes print_help(std::cout) and then exit(EXIT_SUCCESS). If parsing fails, then the error messages are printed to std::cerr prefixed with "ERROR: ", then print_help(std::cerr) is invoked, then exit(EXIT_FAILURE) is invoked. If parsing was successful, then the method returns normally and the program can continue. the number of commandline arguments as provided by main() the commandline arguments in a C std::string array as provided by main()

virtual std::ostream& multovl::Polite::version_info ( std::ostream &  out) const
protectedvirtual

This method prints version information relevant to the current class. It is invoked by the print_version() method by default. Do not invoke the base class version(s) here, just specify what is relevant to your current class, and override print_version() so that the base class infos are combined correctly (important when doing diamond-style inheritance).

Parameters
outthe stream the version information is printed to
Returns
out

Reimplemented in multovl::MultovlOptbase.

bool multovl::Polite::version_needed ( ) const
inline

Indicates whether version information should be printed.

Returns
/true/ if the "--version" option has seen.

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