StochHMM  v0.34
Flexible Hidden Markov Model C++ Library and Application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | Private Attributes
StochHMM::options Class Reference

Parse, Store, and access commandline options data. More...

#include <options.h>

List of all members.

Public Member Functions

 options ()
void set_parameters (opt_parameters *, int, const char *)
void parse_commandline (int, const char **)
bool getopt (const char *, int &)
bool getopt (const char *, double &)
bool getopt (const char *, std::string &)
bool isFlagSet (const char *, const char *)
bool isSet (const char *)
std::string & sopt (const char *)
int & iopt (const char *)
double & dopt (const char *)

Private Attributes

std::map< std::string, opt_data * > opts
std::map< std::string,
std::string > 
alternatives
const char * usage

Detailed Description

Parse, Store, and access commandline options data.

Definition at line 104 of file options.h.


Constructor & Destructor Documentation

StochHMM::options::options ( )

Definition at line 473 of file options.cpp.

{
return;
}

Member Function Documentation

double & StochHMM::options::dopt ( const char *  param)

Get double value for option *If option is not an OPT_DOUBLE, this will produce an error

Parameters:
paramparameter name
Returns:
double

Definition at line 457 of file options.cpp.

References StochHMM::OPT_DOUBLE, and opts.

Referenced by print_limited_posterior().

{
if (opts.count(param)){
if (opts[param]->set && opts[param]->type==OPT_DOUBLE){
return opts[param]->data.d;
}
else{
std::cerr << "Option: " << param << " not set or not of type OPT_DOUBLE\n";
exit(2);
}
}
else{
std::cerr << "Option: " << param <<" type doesn't exist\n";
exit(2);
}
}
bool StochHMM::options::getopt ( const char *  param,
int &  value 
)

Get the integer value for option

Parameters:
[in]paramthe option name
[out]valueinteger is to be returned to
Returns:
true if option existed and is returned
false if option does not exist

Definition at line 303 of file options.cpp.

References StochHMM::OPT_INT, and opts.

Referenced by main().

{
if (opts.count(param)){
if (opts[param]->set && opts[param]->type==OPT_INT){
value=opts[param]->data.i;
return true;
}
else{
std::cerr << "Option: " << param << " not set or not of type OPT_INT\n";
return false;
}
}
else{
std::cerr << "Option: " << param <<" type doesn't exist\n";
return false;
}
}
bool StochHMM::options::getopt ( const char *  param,
double &  value 
)

Get double type option values

Parameters:
[in]paramthe option name
[out]valuedouble is to be returned to
Returns:
true if option existed and is returned
false if option does not exist

Definition at line 326 of file options.cpp.

References StochHMM::OPT_DOUBLE, and opts.

{
if (opts.count(param)){
if (opts[param]->set && opts[param]->type==OPT_DOUBLE){
value=opts[param]->data.d;
return true;
}
else{
std::cerr << "Option: " << param << " not set or not of type OPT_DOUBLE\n";
return false;
}
}
else{
std::cerr << "Option: " << param <<" type doesn't exist\n";
return false;
}
}
bool StochHMM::options::getopt ( const char *  param,
std::string &  value 
)

Get string type options values

Parameters:
[in]paramthe option name
[out]valuestd::string is to be returned to
Returns:
true if option existed and is returned
false if option does not exist

Definition at line 349 of file options.cpp.

References StochHMM::OPT_STRING, and opts.

{
if (opts.count(param)){
if (opts[param]->set && opts[param]->type==OPT_STRING){
value=opts[param]->str;
return true;
}
else{
std::cerr << "Option: " << param << " not set or not of type OPT_STRING\n";
return false;
}
}
else{
std::cerr << "Option: " << param <<" type doesn't exist\n";
return false;
}
}
int & StochHMM::options::iopt ( const char *  param)

Get integer value for option *If option is not an OPT_INT, this will produce an error

Parameters:
paramparameter name
Returns:
int

Definition at line 437 of file options.cpp.

References StochHMM::OPT_INT, and opts.

Referenced by perform_nbest_decoding(), and perform_stochastic_decoding().

{
if (opts.count(param)){
if (opts[param]->set && opts[param]->type==OPT_INT){
return opts[param]->data.i;
}
else{
std::cerr << "Option: " << param << " not set or not of type OPT_INT\n";
exit(2);
}
}
else{
std::cerr << "Option: " << param <<" type doesn't exist\n";
exit(2);
}
}
bool StochHMM::options::isFlagSet ( const char *  param,
const char *  sec 
)

Check primary and secondary options and returns value of bool flag

Parameters:
paramprimary option name
secsecondary option name
Returns:
true if secondary option is set
false if secondary option is not set

Definition at line 372 of file options.cpp.

References StochHMM::OPT_FLAG, and opts.

Referenced by import_model(), main(), and perform_stochastic_decoding().

{
std::string secondary=sec;
if (opts.count(param)){
if (opts[param]->set && opts[param]->type==OPT_FLAG){
if (opts[param]->flags.count(secondary)){
return opts[param]->flags[secondary];
}
else {
std::cerr << "Secondary command: " << secondary << " doesn't exist.\n";
return false;
}
}
else{
//cerr << "Option: " << param << " not set or not of type OPT_FLAG\n";
return false;
}
}
else{
std::cerr << "Option: " << param <<" type doesn't exist\n";
exit(2);
}
}
bool StochHMM::options::isSet ( const char *  param)

Returns whether an option is set *Used also for OPT_NONE option types

Parameters:
paramparameter name
Returns:
true if option is set or exists in commandline argument
false if option is not set or didn't exist in commandline argument

Definition at line 402 of file options.cpp.

References opts.

Referenced by import_model(), import_sequence(), main(), perform_posterior(), perform_stochastic_decoding(), and print_output().

{
if (opts.count(param)){
return opts[param]->set;
}
else{
std::cerr << "Option: " << param <<" type doesn't exist\n";
return false;
}
}
void StochHMM::options::parse_commandline ( int  ,
const char **   
)

Parse the commandline arguments and save them in the options clas

Parameters:
argcNumber of commandline arguments;
argvCommandline argurments

Definition at line 161 of file options.cpp.

References alternatives, StochHMM::OPT_DOUBLE, StochHMM::OPT_FLAG, StochHMM::OPT_INT, StochHMM::OPT_NONE, StochHMM::OPT_STRING, opts, and usage.

Referenced by main().

{
for(int i=1;i<argc;i++){
std::string tag=argv[i];
if (tag[0]!='-'){
std::cout << "Unpaired/Unknown commandline argument:\t" << tag <<std::endl;
std::cout << usage << std::endl; //Print Usage statement
exit(1);
}
else if (opts.count(tag)){
std::string secondary;
opts[tag]->set=true;
switch (opts[tag]->type) {
case OPT_NONE:
break;
case OPT_INT:
if (i+1==argc){
if (!opts[tag]->default_value){
std::cerr << "Command:\t" << tag <<" is missing a secondary command line argument\n";
std::cout << usage << std::endl; //Print Usage statement
exit(2);
}
}
else if (argv[i+1][0]=='-'){
if (!opts[tag]->default_value){
std::cerr << "Command:\t" << tag <<" is missing a secondary command line argument\n";
}
}
else{
//Probably need to check to make sure that the argument is a number;
opts[tag]->data.i=atoi(argv[++i]);
}
break;
case OPT_DOUBLE:
if (i+1==argc){
if (!opts[tag]->default_value){
std::cerr << "Command:\t" << tag <<" is missing a argument\n";
std::cout << usage << std::endl; //Print Usage statement
exit(2);
}
}
else if (argv[i+1][0]=='-'){
if (!opts[tag]->default_value){
std::cerr << "Command:\t" << tag <<" is missing a secondary command line argument\n";
}
}
else{
opts[tag]->data.d=atof(argv[++i]);
}
break;
case OPT_STRING:
//Check that secondary doesn't start with a dash;
if (i+1==argc){
secondary="";
}
else{
secondary=argv[i+1];
}
if (secondary[0]=='-'){
if (!opts[tag]->default_value){
opts[tag]->str="";
}
}
else{
i++;
if (opts[tag]->restricted){
if (opts[tag]->flags.count(secondary)){
opts[tag]->str=secondary;
}
else{
std::cerr << "Secondary option for:\t" << tag << " ->" << secondary << " is not allowed.\n";
std::cout << usage << std::endl; //Print Usage statement
exit(2);
}
}
else{
if (secondary.compare("")==0){
if (!opts[tag]->default_value){
opts[tag]->str=secondary;
}
}
else{
opts[tag]->str=secondary;
}
}
}
break;
case OPT_FLAG:
if (i+1==argc){
std::cerr << "Command:\t" << tag <<" is missing a secondary command line argument\n";
std::cout << usage << std::endl; //Print Usage statement
exit(2);
}
secondary=argv[++i];
if (opts[tag]->flags.count(secondary)){
opts[tag]->flags[secondary]=true;
}
else{
std::cerr << "Secondary option for:\t" << tag << " ->" << secondary << " is not allowed.\n";
std::cout << usage << std::endl; //Print Usage statement
exit(2);
}
break;
default:
break;
}
}
}
//check that all required are now set;
bool set(true);
std::map<std::string,bool> RequiredNotSet;
std::map<std::string,opt_data*>::iterator it;
for(it=opts.begin();it!=opts.end();it++){
if ((*it).second->required && !(*it).second->set){
set=false;
RequiredNotSet[alternatives[(*it).first]]=false;
}
}
if (!set){
std::map<std::string,bool>::iterator it;
for(it=RequiredNotSet.begin();it!=RequiredNotSet.end();it++){
std::cout << "Required option:\t" << (*it).first <<" not set on command-line\n";
}
std::cout << usage <<std::endl;
exit(1);
}
return;
}
void StochHMM::options::set_parameters ( opt_parameters param,
int  size,
const char *  usageStatement 
)

Setup options to handle the parameters and options defined by the user

Parameters:
parampointer to opt_parameters(user defined options and defaults)
size# of parameters that have been defined
usageStatementUser defined usage statement for the program

Definition at line 34 of file options.cpp.

References StochHMM::opt_parameters::allowable, alternatives, StochHMM::opt_parameters::commandline_tag, StochHMM::opt_data::d, StochHMM::opt_data::data, StochHMM::opt_data::default_value, StochHMM::opt_data::flags, StochHMM::opt_data::i, MAX_ALLOWABLE, StochHMM::OPT_DOUBLE, StochHMM::OPT_FLAG, StochHMM::OPT_INT, StochHMM::OPT_STRING, opts, StochHMM::opt_parameters::preset, StochHMM::opt_parameters::required, StochHMM::opt_data::required, StochHMM::opt_data::restricted, StochHMM::opt_data::set, StochHMM::opt_data::str, StochHMM::opt_parameters::type, StochHMM::opt_data::type, and usage.

Referenced by main().

{
usage=usageStatement;
//Process paramaters
for(int i=0;i<size;i++){
//Get possible commandline tags and split possible tags;
std::string opt_tag=param[i].commandline_tag;
std::vector<std::string> tags;
size_t found=opt_tag.find_first_of(":");
if (found==std::string::npos){
tags.push_back(opt_tag);
}
else{
while (found!=std::string::npos){
tags.push_back(opt_tag.substr(0,found));
opt_tag.erase(0,found+1);
found=opt_tag.find_first_of(":");
if (found==std::string::npos){
tags.push_back(opt_tag);
}
}
}
// Check allowable;
int allowable_defined=0;
for (int j=0;j<MAX_ALLOWABLE;j++){
if (param[i].allowable[j]!=""){
allowable_defined++;
}
else{
break;
}
}
//Make assignments to opt_data
opt_data *tp= new(std::nothrow) opt_data;
if (tp==NULL){
std::cerr << "OUT OF MEMORY\nFile" << __FILE__ << "Line:\t"<< __LINE__ << std::endl;
exit(1);
}
tp->type=param[i].type;
tp->required=param[i].required;
//Process restricted/allowable secondary options parameters
if (allowable_defined>0){
tp->restricted =true;
for(int k=0;k<allowable_defined;k++){
tp->flags[param[i].allowable[k]]=false;
}
}
else {
tp->restricted =false;
}
//Determine presets and assign
if (param[i].preset.compare("")!=0){
if (tp->type==OPT_INT){
double tempValue;
std::istringstream input(param[i].preset);
if(!(input>>tempValue)){
std::cerr << "Couldn't convert preset to numerical integer: " << param[i].preset << std::endl;
tp->set=false;
}
else{
tp->data.i=tempValue;
tp->default_value=true;
tp->set=false;
}
}
else if (tp->type==OPT_DOUBLE){
double tempValue;
std::istringstream input(param[i].preset);
if(!(input >> tempValue)){
std::cerr << "Couldn't convert preset to numerical double: " << param[i].preset << std::endl;
tp->set=false;
}
else{
tp->data.d=tempValue;
tp->default_value=true;
tp->set=false;
}
}
else if (tp->type==OPT_STRING){
tp->str=param[i].preset;
tp->default_value = true;
tp->set=false;
}
else if (tp->type==OPT_FLAG){
tp->set=false;
for(size_t k=0;k<param[i].preset.size();k++){
if (k>=MAX_ALLOWABLE){
std::cerr << "More presets than allowed OPT_FLAG" <<std::endl;
exit(0);
}
char c=param[i].preset[k];
if (c=='1'){
tp->flags[param[i].allowable[k]]=true;
}
else{
tp->flags[param[i].allowable[k]]=false;
}
}
}
}
//Assign the possible tags to map. Linked to pointer to opt_data
for(size_t j=0; j<tags.size();j++){
//cout << tags[j] << endl;
opts[tags[j]]=tp;
alternatives[tags[j]]=tags[0];
}
}
return;
}
std::string & StochHMM::options::sopt ( const char *  param)

Get string value for option *If option is not an OPT_STRING, this will produce an error

Parameters:
paramparameter name
Returns:
std::string

Definition at line 416 of file options.cpp.

References StochHMM::OPT_STRING, and opts.

Referenced by import_model(), and import_sequence().

{
if (opts.count(param)){
if (opts[param]->set && opts[param]->type==OPT_STRING){
return opts[param]->str;
}
else{
std::cerr << "Option: " << param << " not set or not of type OPT_STRING\n";
exit(2);
}
}
else{
std::cerr << "Option: " << param <<" type doesn't exist\n";
exit(2);
}
}

Member Data Documentation

std::map<std::string,std::string> StochHMM::options::alternatives
private

Definition at line 126 of file options.h.

Referenced by parse_commandline(), and set_parameters().

std::map<std::string,opt_data*> StochHMM::options::opts
private

Definition at line 125 of file options.h.

Referenced by dopt(), getopt(), iopt(), isFlagSet(), isSet(), parse_commandline(), set_parameters(), and sopt().

const char* StochHMM::options::usage
private

Definition at line 127 of file options.h.

Referenced by parse_commandline(), and set_parameters().


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