Fawkes API
Fawkes Development Version
|
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, 00070 const char *opt_string, option *long_options = NULL); 00071 ~ArgumentParser(); 00072 00073 bool has_arg(const char *argn); 00074 const char * arg(const char *argn); 00075 bool arg(const char *argn, char **value); 00076 const char * program_name() const; 00077 00078 bool parse_hostport(const char *argn, char **host, unsigned short int *port); 00079 bool parse_hostport(const char *argn, std::string &host, unsigned short int &port); 00080 long int parse_int(const char *argn); 00081 double parse_float(const char *argn); 00082 00083 long int parse_item_int(unsigned int index); 00084 double parse_item_float(unsigned int index); 00085 00086 const std::vector< const char * > & items() const; 00087 std::vector< const char * >::size_type num_items() const; 00088 00089 00090 int argc() const; 00091 const char ** argv() const; 00092 00093 /** Get option string. 00094 * @return option string used to create instance */ 00095 std::string get_optstring() const 00096 { return __opt_string; } 00097 00098 /** Get long option configuration. 00099 * @return vector of long options used to create instance */ 00100 std::vector<option> get_long_opts() const 00101 { return __long_opts; } 00102 00103 private: 00104 std::map<std::string, const char *> __opts; 00105 std::map<std::string, const char *> __opts_cit; 00106 std::vector< const char * > __items; 00107 00108 char * __program_name; 00109 char ** __argv; 00110 int __argc; 00111 00112 std::string __opt_string; 00113 std::vector<option> __long_opts; 00114 }; 00115 00116 } // end namespace fawkes 00117 00118 #endif