argparser.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
00040 class UnknownArgumentException : public Exception
00041 {
00042 public:
00043
00044
00045
00046 UnknownArgumentException(char c) : Exception()
00047 {
00048 append("Unknown option '%c'", c);
00049 }
00050 };
00051
00052
00053 class MissingArgumentException : public Exception
00054 {
00055 public:
00056
00057
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 }
00103
00104 #endif