-/* $Id$
-
-This file is part of libmspcore
-Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
#ifndef MSP_CORE_GETOPT_H_
#define MSP_CORE_GETOPT_H_
-#include <sstream>
+#include <list>
+#include <stdexcept>
#include <string>
#include <vector>
-#include "except.h"
+#include <msp/strings/lexicalcast.h>
namespace Msp {
+class usage_error: public std::runtime_error
+{
+private:
+ std::string help_;
+
+public:
+ usage_error(const std::string &w, const std::string &h = std::string()): std::runtime_error(w), help_(h) { }
+ ~usage_error() throw() { }
+
+ const char *help() const throw() { return help_.c_str(); }
+};
+
+
class GetOpt
{
public:
class OptBase
{
- public:
- OptBase &set_help(const std::string &);
- OptBase &set_help(const std::string &, const std::string &);
- char get_short() const { return shrt; }
- const std::string &get_long() const { return lng; }
- ArgType get_arg_type() const { return arg_type; }
- const std::string &get_help() const { return help; }
- const std::string &get_metavar() const { return metavar; }
- unsigned get_seen_count() const { return seen_count; }
- virtual void process()=0;
- virtual void process(const std::string &)=0;
- virtual ~OptBase() { }
protected:
- char shrt;
+ char shrt;
std::string lng;
- ArgType arg_type;
- unsigned seen_count;
+ ArgType arg_type;
+ unsigned seen_count;
std::string help;
std::string metavar;
OptBase(char, const std::string &, ArgType);
+ public:
+ virtual ~OptBase() { }
+
+ OptBase &set_help(const std::string &);
+ OptBase &set_help(const std::string &, const std::string &);
+ char get_short() const { return shrt; }
+ const std::string &get_long() const { return lng; }
+ ArgType get_arg_type() const { return arg_type; }
+ const std::string &get_help() const { return help; }
+ const std::string &get_metavar() const { return metavar; }
+ unsigned get_seen_count() const { return seen_count; }
+ void process();
+ void process(const std::string &);
+ protected:
+ virtual void store() = 0;
+ virtual void store(const std::string &) = 0;
};
private:
template<typename T>
class Option: public OptBase
{
+ private:
+ T &data;
+
public:
Option(char s, const std::string &l, T &d, ArgType a): OptBase(s, l, a), data(d) { }
- virtual void process()
- {
- if(arg_type==REQUIRED_ARG)
- throw UsageError("--"+lng+" requires an argument");
- process_();
- ++seen_count;
- }
+ virtual void store() { }
- virtual void process(const std::string &a)
+ virtual void store(const std::string &a)
{
- if(arg_type==NO_ARG)
- throw UsageError("--"+lng+" takes no argument");
-
- T tmp;
- std::istringstream ss(a);
- ss>>tmp;
- if(ss.fail())
- throw UsageError("Invalid argument for --"+lng);
-
- data=tmp;
- ++seen_count;
+ try
+ {
+ data = lexical_cast<T>(a);
+ }
+ catch(const lexical_error &e)
+ {
+ throw usage_error("Invalid argument for --"+lng+" ("+e.what()+")");
+ }
}
- private:
- T &data;
-
- void process_() { }
};
template<typename T>
class ListOption: public OptBase
{
+ private:
+ T &data;
+
public:
ListOption(char s, const std::string &l, T &d, ArgType a): OptBase(s, l, a), data(d)
- { if(arg_type!=REQUIRED_ARG) throw Exception("ListOption with arg_type!=REQUIRED makes no sense"); }
+ { if(arg_type!=REQUIRED_ARG) throw std::invalid_argument("ListOption arg_type!=REQUIRED"); }
- virtual void process()
- {
- throw UsageError("--"+lng+" requires an argument");
- }
+ virtual void store() { }
- virtual void process(const std::string &a)
+ virtual void store(const std::string &a)
{
- typename T::value_type tmp;
- std::istringstream ss(a);
- ss>>tmp;
- if(ss.fail())
- throw UsageError("Invalid argument for --"+lng);
-
- data.push_back(tmp);
- ++seen_count;
+ try
+ {
+ data.push_back(lexical_cast<typename T::value_type>(a));
+ }
+ catch(const lexical_error &e)
+ {
+ throw usage_error("Invalid argument for --"+lng+" ("+e.what()+")");
+ }
}
- private:
- T &data;
};
+ bool help;
std::list<OptBase *> opts;
std::vector<std::string> args;
public:
+ GetOpt();
~GetOpt();
const std::vector<std::string> &get_args() const { return args; }
template<typename T>
- OptBase &add_option(char s, const std::string &l, T &d, ArgType a=NO_ARG)
+ OptBase &add_option(char s, const std::string &l, T &d, ArgType a = NO_ARG)
{ opts.push_back(new Option<T>(s, l, d, a)); return *opts.back(); }
template<typename T>
- OptBase &add_option(char s, const std::string &l, std::list<T> &d, ArgType a=REQUIRED_ARG)
+ OptBase &add_option(char s, const std::string &l, std::list<T> &d, ArgType a = REQUIRED_ARG)
{ opts.push_back(new ListOption<std::list<T> >(s, l, d, a)); return *opts.back(); }
template<typename T>
OptBase &add_option(const std::string &l, T &d, ArgType a)
{ return add_option(0, l, d, a); }
- std::string generate_usage(const std::string &) const;
- std::string generate_help() const;
- void operator()(unsigned, const char *const *);
private:
-
OptBase &get_option(char);
OptBase &get_option(const std::string &);
+
+public:
+ /** Processes argc/argv style command line arguments. The contents of argv
+ will be unchanged; use get_args to access non-option arguments. */
+ void operator()(unsigned, const char *const *);
+
+private:
+ /** Processes a long option. Returns the number of arguments eaten. */
unsigned process_long(const char *const *);
+
+ /** Processes short options. Returns the number of arguments eaten. */
unsigned process_short(const char *const *);
+
+public:
+ /** Generates a single line that describes known options. */
+ std::string generate_usage(const std::string &) const;
+
+ /** Generates help for known options in tabular format, one option per
+ line. The returned string will have a linefeed at the end. */
+ std::string generate_help() const;
};
-template<> inline void GetOpt::Option<bool>::process_() { data=true; }
-template<> inline void GetOpt::Option<unsigned>::process_() { ++data; }
+template<> inline void GetOpt::Option<bool>::store() { data = true; }
+template<> inline void GetOpt::Option<unsigned>::store() { ++data; }
+
+template<> inline void GetOpt::Option<std::string>::store(const std::string &a)
+{ data = a; }
+
+template<> inline void GetOpt::ListOption<std::list<std::string> >::store(const std::string &a)
+{ data.push_back(a); }
} // namespace Msp