Fawkes API  Fawkes Development Version
argparser.h
1 
2 /***************************************************************************
3  * argparser.h - Header for argument parser
4  *
5  * Generated: Mon May 30 13:07:41 2005 (from FireVision)
6  * Copyright 2005 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #ifndef __UTILS_SYSTEM_ARGPARSER_H_
25 #define __UTILS_SYSTEM_ARGPARSER_H_
26 
27 #include <core/exception.h>
28 
29 #include <utils/misc/string_compare.h>
30 #include <vector>
31 #include <map>
32 #include <string>
33 
34 #include <getopt.h>
35 
36 
37 namespace fawkes {
38 
39 /** Thrown if unknown argument was supplied */
41 {
42  public:
43  /** Constructor.
44  * @param c Unknown option character
45  */
47  {
48  append("Unknown option '%c'", c);
49  }
50 };
51 
52 /** Thrown if required argument was missing. */
54 {
55  public:
56  /** Constructor.
57  * @param c option with missing argument
58  */
60  {
61  append("Option '%c' requires an argument", c);
62  }
63 };
64 
65 
67 {
68  public:
69  ArgumentParser(int argc, char **argv,
70  const char *opt_string, option *long_options = NULL);
71  ~ArgumentParser();
72 
73  bool has_arg(const char *argn);
74  const char * arg(const char *argn);
75  bool arg(const char *argn, char **value);
76  const char * program_name() const;
77 
78  bool parse_hostport(const char *argn, char **host, unsigned short int *port);
79  bool parse_hostport(const char *argn, std::string &host, unsigned short int &port);
80 
81  static void parse_hostport_s(const char *s, char **host, unsigned short int *port);
82  static void parse_hostport_s(const char *s, std::string &host, unsigned short int &port);
83 
84  long int parse_int(const char *argn);
85  double parse_float(const char *argn);
86 
87  long int parse_item_int(unsigned int index);
88  double parse_item_float(unsigned int index);
89 
90  const std::vector< const char * > & items() const;
91  std::vector< const char * >::size_type num_items() const;
92 
93 
94  int argc() const;
95  const char ** argv() const;
96 
97  /** Get option string.
98  * @return option string used to create instance */
99  std::string get_optstring() const
100  { return __opt_string; }
101 
102  /** Get long option configuration.
103  * @return vector of long options used to create instance */
104  std::vector<option> get_long_opts() const
105  { return __long_opts; }
106 
107  private:
108  std::map<std::string, const char *> __opts;
109  std::map<std::string, const char *> __opts_cit;
110  std::vector< const char * > __items;
111 
112  char * __program_name;
113  char ** __argv;
114  int __argc;
115 
116  std::string __opt_string;
117  std::vector<option> __long_opts;
118 };
119 
120 } // end namespace fawkes
121 
122 #endif
Thrown if required argument was missing.
Definition: argparser.h:53
Fawkes library namespace.
Parse command line arguments.
Definition: argparser.h:66
UnknownArgumentException(char c)
Constructor.
Definition: argparser.h:46
MissingArgumentException(char c)
Constructor.
Definition: argparser.h:59
Base class for exceptions in Fawkes.
Definition: exception.h:36
std::string get_optstring() const
Get option string.
Definition: argparser.h:99
std::vector< option > get_long_opts() const
Get long option configuration.
Definition: argparser.h:104
Thrown if unknown argument was supplied.
Definition: argparser.h:40
void append(const char *format,...)
Append messages to the message list.
Definition: exception.cpp:341