All Classes Namespaces Functions Pages
halCLParser.h
/*
* Copyright (C) 2012 by Glenn Hickey (hickey@soe.ucsc.edu)
*
* Released under the MIT license, see LICENSE.txt
*/
#ifndef _HALCLPARSER_H
#define _HALCLPARSER_H
#include <string>
#include <map>
#include <vector>
#include <sstream>
#include <iostream>
#include "halDefs.h"
namespace hal {
class CLParser
{
public:
CLParser();
virtual ~CLParser();
virtual void setOptionPrefix(const std::string& prefix);
template <typename T>
void addOption(const std::string& name,
const std::string& descritpion,
const T& defaultValue);
template <typename T>
T getOption(const std::string& name) const;
bool hasOption(const std::string& name) const;
void addArgument(const std::string& name,
const std::string& description);
template <typename T>
T getArgument(const std::string& name) const;
bool hasArgument(const std::string& name) const;
void addOptionFlag(const std::string& name,
const std::string& description,
bool defaultValue);
bool getFlag(const std::string& name) const;
bool hasFlag(const std::string& name) const;
template <typename T>
T get(const std::string& name) const;
void setDescription(const std::string& description);
void setExample(const std::string& example);
virtual void parseOptions(int argc, char** argv);
virtual void printUsage(std::ostream& os) const;
protected:
template<typename T> T convertFromString(const std::string& token) const;
static size_t lineWidth;
static std::string multiLine(const std::string& line, size_t indent);
struct Option {
std::string _description;
std::string _defaultValue;
std::string _value;
bool _flag;
};
struct Argument {
std::string _name;
std::string _description;
std::string _value;
};
std::string _prefix;
std::string _exeName;
std::string _description;
std::string _example;
std::vector<Argument> _args;
std::map<std::string, Option> _options;
size_t _maxArgLen;
size_t _maxOptLen;
};
template <typename T>
inline T CLParser::convertFromString(const std::string& token) const
{
std::stringstream ss(token);
T value;
try
{
ss << token;
ss >> value;
}
catch (...)
{
throw hal_exception(std::string("type conversion error parsing"
" token ") + token);
}
return value;
}
// above function won't work on strings with spaces so we specialize!
template <>
inline std::string
CLParser::convertFromString<std::string>(const std::string& token) const
{
return token;
}
template <typename T>
inline void CLParser::addOption(const std::string& name,
const std::string& descritpion,
const T& defaultValue)
{
if (hasOption(name) || hasFlag(name) || hasArgument(name))
{
throw hal_exception(std::string("name ") + name + "already present");
}
std::stringstream ss;
Option opt;
try
{
ss << defaultValue;
opt._description = descritpion;
opt._defaultValue = ss.str();
opt._value = opt._defaultValue;
opt._flag = false;
}
catch (...)
{
throw hal_exception(std::string("type conversion adding option ") + name);
}
_options.insert(std::pair<std::string, Option>(name, opt));
_maxOptLen = std::max(_maxOptLen, name.length());
}
template <typename T>
inline T CLParser::getOption(const std::string& name) const
{
std::map<std::string, Option>::const_iterator i = _options.find(name);
if (i == _options.end() || i->second._flag == true)
{
throw hal_exception(std::string("Option ") + name + " not recognized");
}
return convertFromString<T>(i->second._value);
}
template <typename T>
inline T CLParser::getArgument(const std::string& name) const
{
for (size_t i = 0; i < _args.size(); ++i)
{
if (_args[i]._name == name)
{
return convertFromString<T>(_args[i]._value);
}
}
throw hal_exception(std::string("Argument ") + name + " not recognized");
return T();
}
template <typename T>
inline T CLParser::get(const std::string& name) const
{
if (hasArgument(name))
{
return getArgument<T>(name);
}
else if (hasOption(name))
{
return getOption<T>(name);
}
else if (hasFlag(name))
{
return (T)getFlag(name);
}
else
{
throw hal_exception(std::string("Name ") + name + " not recognized");
}
}
}
#endif
hal::CLParser::addArgument
void addArgument(const std::string &name, const std::string &description)
hal::CLParser
Definition: halCLParser.h:30
hal::CLParser::addOptionFlag
void addOptionFlag(const std::string &name, const std::string &description, bool defaultValue)
hal::CLParser::get
T get(const std::string &name) const
Definition: halCLParser.h:221
hal::CLParser::setExample
void setExample(const std::string &example)
hal::CLParser::Option
Definition: halCLParser.h:118
hal::CLParser::setOptionPrefix
virtual void setOptionPrefix(const std::string &prefix)
hal::CLParser::printUsage
virtual void printUsage(std::ostream &os) const
hal::CLParser::Argument
Definition: halCLParser.h:125
hal::CLParser::addOption
void addOption(const std::string &name, const std::string &descritpion, const T &defaultValue)
Definition: halCLParser.h:168
hal::CLParser::hasArgument
bool hasArgument(const std::string &name) const
hal::CLParser::hasOption
bool hasOption(const std::string &name) const
hal::CLParser::parseOptions
virtual void parseOptions(int argc, char **argv)
hal::CLParser::getOption
T getOption(const std::string &name) const
Definition: halCLParser.h:196
hal::CLParser::getArgument
T getArgument(const std::string &name) const
Definition: halCLParser.h:207
hal::CLParser::getFlag
bool getFlag(const std::string &name) const
hal::CLParser::setDescription
void setDescription(const std::string &description)
hal::CLParser::hasFlag
bool hasFlag(const std::string &name) const