CoinUtils 2.11.13
Loading...
Searching...
No Matches
CoinParam.hpp File Reference

Declaration of a class for command line parameters. More...

#include <vector>
#include <string>
#include <cstdio>
Include dependency graph for CoinParam.hpp:

Go to the source code of this file.

Functions

Constructors and Destructors

Be careful how you specify parameters for the constructors!

Some compilers are entirely too willing to convert almost anything to bool.

 CoinParam ()
 Default constructor.
 CoinParam (std::string name, std::string help, double lower, double upper, double dflt=0.0, bool display=true)
 Constructor for a parameter with a double value.
 CoinParam (std::string name, std::string help, int lower, int upper, int dflt=0, bool display=true)
 Constructor for a parameter with an integer value.
 CoinParam (std::string name, std::string help, std::string firstValue, int dflt, bool display=true)
 Constructor for a parameter with keyword values.
 CoinParam (std::string name, std::string help, std::string dflt, bool display=true)
 Constructor for a string parameter.
 CoinParam (std::string name, std::string help, bool display=true)
 Constructor for an action parameter.
 CoinParam (const CoinParam &orig)
 Copy constructor.
virtual CoinParamclone ()
 Clone.
CoinParamoperator= (const CoinParam &rhs)
 Assignment.
virtual ~CoinParam ()
 Destructor.
Methods to query and manipulate the value(s) of a parameter
void appendKwd (std::string kwd)
 Add an additional value-keyword to a keyword parameter.
int kwdIndex (std::string kwd) const
 Return the integer associated with the specified value-keyword.
std::string kwdVal () const
 Return the value-keyword that is the current value of the keyword parameter.
void setKwdVal (int value, bool printIt=false)
 Set the value of the keyword parameter using the integer associated with a value-keyword.
void setKwdVal (const std::string value)
 Set the value of the keyword parameter using a value-keyword string.
void printKwds () const
 Prints the set of value-keywords defined for this keyword parameter.
void setStrVal (std::string value)
 Set the value of a string parameter.
std::string strVal () const
 Get the value of a string parameter.
void setDblVal (double value)
 Set the value of a double parameter.
double dblVal () const
 Get the value of a double parameter.
void setIntVal (int value)
 Set the value of a integer parameter.
int intVal () const
 Get the value of a integer parameter.
void setShortHelp (const std::string help)
 Add a short help string to a parameter.
std::string shortHelp () const
 Retrieve the short help string.
void setLongHelp (const std::string help)
 Add a long help message to a parameter.
std::string longHelp () const
 Retrieve the long help message.
void printLongHelp () const
 Print long help.
Methods to query and manipulate a parameter object
CoinParamType type () const
 Return the type of the parameter.
void setType (CoinParamType type)
 Set the type of the parameter.
std::string name () const
 Return the parameter keyword (name) string.
void setName (std::string name)
 Set the parameter keyword (name) string.
int matches (std::string input) const
 Check if the specified string matches the parameter keyword (name) string.
std::string matchName () const
 Return the parameter keyword (name) string formatted to show the minimum match length.
void setDisplay (bool display)
 Set visibility of parameter.
bool display () const
 Get visibility of parameter.
CoinParamFunc pushFunc ()
 Get push function.
void setPushFunc (CoinParamFunc func)
 Set push function.
CoinParamFunc pullFunc ()
 Get pull function.
void setPullFunc (CoinParamFunc func)
 Set pull function.
Private methods
void processName ()
 Process a name for efficient matching.

Subtypes

enum  CoinParamType {
  coinParamInvalid = 0 , coinParamAct , coinParamInt , coinParamDbl ,
  coinParamStr , coinParamKwd
}
 Enumeration for the types of parameters supported by CoinParam. More...
typedef int(* CoinParamFunc) (CoinParam *param)
 Type declaration for push and pull functions.

Private parameter data

CoinParamType type_
 Parameter type (see CoinParamType).
std::string name_
 Parameter name.
size_t lengthName_
 Length of parameter name.
size_t lengthMatch_
 Minimum length required to declare a match for the parameter name.
double lowerDblValue_
 Lower bound on value for a double parameter.
double upperDblValue_
 Upper bound on value for a double parameter.
double dblValue_
 Double parameter - current value.
int lowerIntValue_
 Lower bound on value for an integer parameter.
int upperIntValue_
 Upper bound on value for an integer parameter.
int intValue_
 Integer parameter - current value.
std::string strValue_
 String parameter - current value.
std::vector< std::string > definedKwds_
 Set of valid value-keywords for a keyword parameter.
int currentKwd_
 Current value for a keyword parameter (index into definedKwds_).
CoinParamFunc pushFunc_
 Push function.
CoinParamFunc pullFunc_
 Pull function.
std::string shortHelp_
 Short help.
std::string longHelp_
 Long help.
bool display_
 Display when processing lists of parameters?
std::ostream & operator<< (std::ostream &s, const CoinParam &param)
 A stream output function for a CoinParam object.
void setInputSrc (FILE *src)
 Utility functions for processing CoinParam parameters.
bool isCommandLine ()
 Returns true if command line parameters are being processed.
bool isInteractive ()
 Returns true if parameters are being obtained from stdin.
std::string getStringField (int argc, const char *argv[], int *valid)
 Attempt to read a string from the input.
int getIntField (int argc, const char *argv[], int *valid)
 Attempt to read an integer from the input.
double getDoubleField (int argc, const char *argv[], int *valid)
 Attempt to read a real (double) from the input.
int matchParam (const CoinParamVec &paramVec, std::string name, int &matchNdx, int &shortCnt)
 Scan a parameter vector for parameters whose keyword (name) string matches name using minimal match rules.
std::string getCommand (int argc, const char *argv[], const std::string prompt, std::string *pfx=0)
 Get the next command keyword (name).
void printIt (const char *msg)
 Look up the command keyword (name) in the parameter vector.
void shortOrHelpMany (CoinParamVec &paramVec, std::string name, int numQuery)
 Utility routine to print help given a short match or explicit request for help.

Detailed Description

Declaration of a class for command line parameters.

Definition in file CoinParam.hpp.

Typedef Documentation

◆ CoinParamFunc

typedef int(* CoinParamFunc) (CoinParam *param)

Type declaration for push and pull functions.

By convention, a return code of 0 indicates execution without error, >0 indicates nonfatal error, and <0 indicates fatal error. This is only convention, however; the base class makes no use of the push and pull functions and has no hardcoded interpretation of the return code.

Definition at line 107 of file CoinParam.hpp.

Enumeration Type Documentation

◆ CoinParamType

Enumeration for the types of parameters supported by CoinParam.

CoinParam provides support for several types of parameters:

  • Action parameters, which require no value.
  • Integer and double numeric parameters, with upper and lower bounds.
  • String parameters that take an arbitrary string value.
  • Keyword parameters that take a defined set of string (value-keyword) values. Value-keywords are associated with integers in the order in which they are added, starting from zero.
Enumerator
coinParamInvalid 
coinParamAct 
coinParamInt 
coinParamDbl 
coinParamStr 
coinParamKwd 

Definition at line 93 of file CoinParam.hpp.

Function Documentation

◆ CoinParam() [1/7]

CoinParam ( )

Default constructor.

◆ CoinParam() [2/7]

CoinParam ( std::string name,
std::string help,
double lower,
double upper,
double dflt = 0.0,
bool display = true )

Constructor for a parameter with a double value.

The default value is 0.0. Be careful to clearly indicate that lower and upper are real (double) values to distinguish this constructor from the constructor for an integer parameter.

◆ CoinParam() [3/7]

CoinParam ( std::string name,
std::string help,
int lower,
int upper,
int dflt = 0,
bool display = true )

Constructor for a parameter with an integer value.

The default value is 0.

◆ CoinParam() [4/7]

CoinParam ( std::string name,
std::string help,
std::string firstValue,
int dflt,
bool display = true )

Constructor for a parameter with keyword values.

The string supplied as firstValue becomes the first value-keyword. Additional value-keywords can be added using appendKwd(). It's necessary to specify both the first value-keyword (firstValue) and the default value-keyword index (dflt) in order to distinguish this constructor from the constructors for string and action parameters.

Value-keywords are associated with an integer, starting with zero and increasing as each keyword is added. The value-keyword given as firstValue will be associated with the integer zero. The integer supplied for dflt can be any value, as long as it will be valid once all value-keywords have been added.

◆ CoinParam() [5/7]

CoinParam ( std::string name,
std::string help,
std::string dflt,
bool display = true )

Constructor for a string parameter.

For some compilers, the default value (dflt) must be specified explicitly with type std::string to distinguish the constructor for a string parameter from the constructor for an action parameter. For example, use std::string("default") instead of simply "default", or use a variable of type std::string.

◆ CoinParam() [6/7]

CoinParam ( std::string name,
std::string help,
bool display = true )

Constructor for an action parameter.

◆ CoinParam() [7/7]

CoinParam ( const CoinParam & orig)

Copy constructor.

◆ clone()

virtual CoinParam * clone ( )
virtual

Clone.

◆ operator=()

CoinParam & operator= ( const CoinParam & rhs)

Assignment.

◆ ~CoinParam()

virtual ~CoinParam ( )
virtual

Destructor.

◆ appendKwd()

void appendKwd ( std::string kwd)

Add an additional value-keyword to a keyword parameter.

◆ kwdIndex()

int kwdIndex ( std::string kwd) const

Return the integer associated with the specified value-keyword.

Returns -1 if no value-keywords match the specified string.

◆ kwdVal()

std::string kwdVal ( ) const

Return the value-keyword that is the current value of the keyword parameter.

◆ setKwdVal() [1/2]

void setKwdVal ( int value,
bool printIt = false )

Set the value of the keyword parameter using the integer associated with a value-keyword.

If printIt is true, the corresponding value-keyword string will be echoed to std::cout.

◆ setKwdVal() [2/2]

void setKwdVal ( const std::string value)

Set the value of the keyword parameter using a value-keyword string.

The given string will be tested against the set of value-keywords for the parameter using the shortest match rules.

◆ printKwds()

void printKwds ( ) const

Prints the set of value-keywords defined for this keyword parameter.

◆ setStrVal()

void setStrVal ( std::string value)

Set the value of a string parameter.

◆ strVal()

std::string strVal ( ) const

Get the value of a string parameter.

◆ setDblVal()

void setDblVal ( double value)

Set the value of a double parameter.

◆ dblVal()

double dblVal ( ) const

Get the value of a double parameter.

◆ setIntVal()

void setIntVal ( int value)

Set the value of a integer parameter.

◆ intVal()

int intVal ( ) const

Get the value of a integer parameter.

◆ setShortHelp()

void setShortHelp ( const std::string help)
inline

Add a short help string to a parameter.

Definition at line 256 of file CoinParam.hpp.

◆ shortHelp()

std::string shortHelp ( ) const
inline

Retrieve the short help string.

Definition at line 260 of file CoinParam.hpp.

◆ setLongHelp()

void setLongHelp ( const std::string help)
inline

Add a long help message to a parameter.

See printLongHelp() for a description of how messages are broken into lines.

Definition at line 267 of file CoinParam.hpp.

◆ longHelp()

std::string longHelp ( ) const
inline

Retrieve the long help message.

Definition at line 271 of file CoinParam.hpp.

◆ printLongHelp()

void printLongHelp ( ) const

Print long help.

Prints the long help string, plus the valid range and/or keywords if appropriate. The routine makes a best effort to break the message into lines appropriate for an 80-character line. Explicit line breaks in the message will be observed. The short help string will be used if long help is not available.

◆ type()

CoinParamType type ( ) const
inline

Return the type of the parameter.

Definition at line 290 of file CoinParam.hpp.

◆ setType()

void setType ( CoinParamType type)
inline

Set the type of the parameter.

Definition at line 294 of file CoinParam.hpp.

◆ name()

std::string name ( ) const
inline

Return the parameter keyword (name) string.

Definition at line 298 of file CoinParam.hpp.

◆ setName()

void setName ( std::string name)
inline

Set the parameter keyword (name) string.

Definition at line 302 of file CoinParam.hpp.

◆ matches()

int matches ( std::string input) const

Check if the specified string matches the parameter keyword (name) string.

Returns 1 if the string matches and meets the minimum match length, 2 if the string matches but doesn't meet the minimum match length, and 0 if the string doesn't match. Matches are not case-sensitive.

◆ matchName()

std::string matchName ( ) const

Return the parameter keyword (name) string formatted to show the minimum match length.

For example, if the parameter name was defined as allow!ableGap, the string returned by matchName would be allow(ableGap).

◆ setDisplay()

void setDisplay ( bool display)
inline

Set visibility of parameter.

Intended to control whether the parameter is shown when a list of parameters is processed. Used by CoinParamUtils::printHelp when printing help messages for a list of parameters.

Definition at line 331 of file CoinParam.hpp.

◆ display()

bool display ( ) const
inline

Get visibility of parameter.

Definition at line 335 of file CoinParam.hpp.

◆ pushFunc()

CoinParamFunc pushFunc ( )
inline

Get push function.

Definition at line 339 of file CoinParam.hpp.

◆ setPushFunc()

void setPushFunc ( CoinParamFunc func)
inline

Set push function.

Definition at line 343 of file CoinParam.hpp.

◆ pullFunc()

CoinParamFunc pullFunc ( )
inline

Get pull function.

Definition at line 347 of file CoinParam.hpp.

◆ setPullFunc()

void setPullFunc ( CoinParamFunc func)
inline

Set pull function.

Definition at line 351 of file CoinParam.hpp.

◆ processName()

void processName ( )
private

Process a name for efficient matching.

◆ operator<<()

std::ostream & operator<< ( std::ostream & s,
const CoinParam & param )
private

A stream output function for a CoinParam object.

◆ setInputSrc()

void setInputSrc ( FILE * src)
private

Utility functions for processing CoinParam parameters.

The functions in CoinParamUtils support command line or interactive parameter processing and a help facility. Consult the `Related Functions' section of the CoinParam class documentation for individual function documentation. */ namespace CoinParamUtils { /*!

Take command input from the file specified by src.

Use stdin for src to specify interactive prompting for commands.

◆ isCommandLine()

bool isCommandLine ( )
private

Returns true if command line parameters are being processed.

◆ isInteractive()

bool isInteractive ( )
private

Returns true if parameters are being obtained from stdin.

◆ getStringField()

std::string getStringField ( int argc,
const char * argv[],
int * valid )
private

Attempt to read a string from the input.

argc and argv are used only if isCommandLine() would return true. If valid is supplied, it will be set to 0 if a string is parsed without error, 2 if no field is present.

◆ getIntField()

int getIntField ( int argc,
const char * argv[],
int * valid )
private

Attempt to read an integer from the input.

argc and argv are used only if isCommandLine() would return true. If valid is supplied, it will be set to 0 if an integer is parsed without error, 1 if there's a parse error, and 2 if no field is present.

◆ getDoubleField()

double getDoubleField ( int argc,
const char * argv[],
int * valid )
private

Attempt to read a real (double) from the input.

argc and argv are used only if isCommandLine() would return true. If valid is supplied, it will be set to 0 if a real number is parsed without error, 1 if there's a parse error, and 2 if no field is present.

◆ matchParam()

int matchParam ( const CoinParamVec & paramVec,
std::string name,
int & matchNdx,
int & shortCnt )
private

Scan a parameter vector for parameters whose keyword (name) string matches name using minimal match rules.

matchNdx is set to the index of the last parameter that meets the minimal match criteria (but note there should be at most one matching parameter if the parameter vector is properly configured). shortCnt is set to the number of short matches (should be zero for a properly configured parameter vector if a minimal match is found). The return value is the number of matches satisfying the minimal match requirement (should be 0 or 1 in a properly configured vector).

◆ getCommand()

std::string getCommand ( int argc,
const char * argv[],
const std::string prompt,
std::string * pfx = 0 )
private

Get the next command keyword (name).

To be precise, return the next field from the current command input source, after a bit of processing. In command line mode (isCommandLine() returns true) the next field will normally be of the form -keyword' or –keyword' (i.e., a parameter keyword), and the string returned would be keyword'. In interactive mode (isInteractive() returns true), the user will be prompted if necessary. It is assumed that the user knows not to use the -' or `–' prefixes unless specifying parameters on the command line.

There are a number of special cases if we're in command line mode. The order of processing of the raw string goes like this:

  • A stand-alone -' is forced to stdin'.
  • A stand-alone '–' is returned as a word; interpretation is up to the client.
  • A prefix of '-' or '–' is stripped from the string.

If the result is the string `stdin', command processing shifts to interactive mode and the user is immediately prompted for a new command.

Whatever results from the above sequence is returned to the user as the return value of the function. An empty string indicates end of input.

prompt will be used only if it's necessary to prompt the user in interactive mode.

◆ printIt()

void printIt ( const char * msg)
private

Look up the command keyword (name) in the parameter vector.

Print help if requested.

In the most straightforward use, name is a string without `?', and the value returned is the index in paramVec of the single parameter that matched name. One or more '?' characters at the end of name is a query for information. The routine prints short (one '?') or long (more than one '?') help messages for a query. Help is also printed in the case where the name is ambiguous (some of the matches did not meet the minimal match length requirement).

Note that multiple matches meeting the minimal match requirement is a configuration error. The mimimal match length for the parameters involved is too short.

If provided as parameters, on return

  • matchCnt will be set to the number of matches meeting the minimal match requirement
  • shortCnt will be set to the number of matches that did not meet the miminal match requirement
  • queryCnt will be set to the number of '?' characters at the end of the name

The return values are:

  • >0: index in paramVec of the single unique match for name
  • -1: a query was detected (one or more '?' characters at the end of name
  • -2: one or more short matches, not a query
  • -3: no matches, not a query
  • -4: multiple matches meeting the minimal match requirement (configuration error)

*/ int lookupParam(std::string name, CoinParamVec &paramVec, int *matchCnt = 0, int *shortCnt = 0, int *queryCnt = 0);

/*!

Utility to print a long message as filled lines of text

The routine makes a best effort to break lines without exceeding the standard 80 character line length. Explicit newlines in msg will be obeyed.

◆ shortOrHelpMany()

void shortOrHelpMany ( CoinParamVec & paramVec,
std::string name,
int numQuery )
private

Utility routine to print help given a short match or explicit request for help.

The two really are related, in that a query (a string that ends with one or more `?' characters) will often result in a short match. The routine expects that name matches a single parameter, and does not look for multiple matches.

If called with matchNdx < 0, the routine will look up name in paramVec and print the full name from the parameter. If called with matchNdx > 0, it just prints the name from the specified parameter. If the name is a query, short (one '?') or long (more than one '?') help is printed.

*/ void shortOrHelpOne(CoinParamVec &paramVec, int matchNdx, std::string name, int numQuery);

/*!

Utility routine to print help given multiple matches.

If the name is not a query, or asks for short help (i.e., contains zero or one '?' characters), the list of matching names is printed. If the name asks for long help (contains two or more '?' characters), short help is printed for each matching name.

Variable Documentation

◆ type_

CoinParamType type_
private

Parameter type (see CoinParamType).

Definition at line 367 of file CoinParam.hpp.

◆ name_

std::string name_
private

Parameter name.

Definition at line 370 of file CoinParam.hpp.

◆ lengthName_

size_t lengthName_
private

Length of parameter name.

Definition at line 373 of file CoinParam.hpp.

◆ lengthMatch_

size_t lengthMatch_
private

Minimum length required to declare a match for the parameter name.

Definition at line 378 of file CoinParam.hpp.

◆ lowerDblValue_

double lowerDblValue_
private

Lower bound on value for a double parameter.

Definition at line 381 of file CoinParam.hpp.

◆ upperDblValue_

double upperDblValue_
private

Upper bound on value for a double parameter.

Definition at line 384 of file CoinParam.hpp.

◆ dblValue_

double dblValue_
private

Double parameter - current value.

Definition at line 387 of file CoinParam.hpp.

◆ lowerIntValue_

int lowerIntValue_
private

Lower bound on value for an integer parameter.

Definition at line 390 of file CoinParam.hpp.

◆ upperIntValue_

int upperIntValue_
private

Upper bound on value for an integer parameter.

Definition at line 393 of file CoinParam.hpp.

◆ intValue_

int intValue_
private

Integer parameter - current value.

Definition at line 396 of file CoinParam.hpp.

◆ strValue_

std::string strValue_
private

String parameter - current value.

Definition at line 399 of file CoinParam.hpp.

◆ definedKwds_

std::vector< std::string > definedKwds_
private

Set of valid value-keywords for a keyword parameter.

Definition at line 402 of file CoinParam.hpp.

◆ currentKwd_

int currentKwd_
private

Current value for a keyword parameter (index into definedKwds_).

Definition at line 406 of file CoinParam.hpp.

◆ pushFunc_

CoinParamFunc pushFunc_
private

Push function.

Definition at line 409 of file CoinParam.hpp.

◆ pullFunc_

CoinParamFunc pullFunc_
private

Pull function.

Definition at line 412 of file CoinParam.hpp.

◆ shortHelp_

std::string shortHelp_
private

Short help.

Definition at line 415 of file CoinParam.hpp.

◆ longHelp_

std::string longHelp_
private

Long help.

Definition at line 418 of file CoinParam.hpp.

◆ display_

bool display_
private

Display when processing lists of parameters?

Definition at line 421 of file CoinParam.hpp.