Fawkes API  Fawkes Development Version
argparser.h
00001 
00002 /***************************************************************************
00003  *  argparser.h - Header for argument parser
00004  *
00005  *  Generated: Mon May 30 13:07:41 2005 (from FireVision)
00006  *  Copyright  2005  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #ifndef __UTILS_SYSTEM_ARGPARSER_H_
00025 #define __UTILS_SYSTEM_ARGPARSER_H_
00026 
00027 #include <core/exception.h>
00028 
00029 #include <utils/misc/string_compare.h>
00030 #include <vector>
00031 #include <map>
00032 #include <string>
00033 
00034 #include <getopt.h>
00035 
00036 
00037 namespace fawkes {
00038 
00039 /** Thrown if unknown argument was supplied */
00040 class UnknownArgumentException : public Exception
00041 {
00042  public:
00043   /** Constructor.
00044    * @param c Unknown option character
00045    */
00046   UnknownArgumentException(char c) : Exception()
00047   {
00048     append("Unknown option '%c'", c);
00049   }
00050 };
00051 
00052 /** Thrown if required argument was missing. */
00053 class MissingArgumentException : public Exception
00054 {
00055  public:
00056   /** Constructor.
00057    * @param c option with missing argument
00058    */
00059   MissingArgumentException(char c) : Exception()
00060   {
00061     append("Option '%c' requires an argument", c);
00062   }
00063 };
00064 
00065 
00066 class ArgumentParser
00067 {
00068  public:
00069   ArgumentParser(int argc, char **argv, const char *opt_string, option *long_options = NULL);
00070   ~ArgumentParser();
00071 
00072   bool         has_arg(const char *argn);
00073   const char * arg(const char *argn);
00074   bool         arg(const char *argn, char **value);
00075   const char * program_name() const;
00076 
00077   bool         parse_hostport(const char *argn, char **host, unsigned short int *port);
00078   bool         parse_hostport(const char *argn, std::string &host, unsigned short int &port);
00079   long int     parse_int(const char *argn);
00080   double       parse_float(const char *argn);
00081 
00082   long int     parse_item_int(unsigned int index);
00083   double       parse_item_float(unsigned int index);
00084 
00085   const std::vector< const char * > &    items() const;
00086   std::vector< const char * >::size_type num_items() const;
00087 
00088   int            argc() const;
00089   const char **  argv() const;
00090 
00091  private:
00092   std::map<std::string, const char *> _opts;
00093   std::map<std::string, const char *> _opts_cit;
00094   std::vector< const char * >  _items;
00095 
00096   char *  _program_name;
00097   char ** _argv;
00098   int     _argc;
00099 
00100 };
00101 
00102 } // end namespace fawkes
00103 
00104 #endif