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);
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  long int parse_int(const char *argn);
81  double parse_float(const char *argn);
82 
83  long int parse_item_int(unsigned int index);
84  double parse_item_float(unsigned int index);
85 
86  const std::vector< const char * > & items() const;
87  std::vector< const char * >::size_type num_items() const;
88 
89 
90  int argc() const;
91  const char ** argv() const;
92 
93  /** Get option string.
94  * @return option string used to create instance */
95  std::string get_optstring() const
96  { return __opt_string; }
97 
98  /** Get long option configuration.
99  * @return vector of long options used to create instance */
100  std::vector<option> get_long_opts() const
101  { return __long_opts; }
102 
103  private:
104  std::map<std::string, const char *> __opts;
105  std::map<std::string, const char *> __opts_cit;
106  std::vector< const char * > __items;
107 
108  char * __program_name;
109  char ** __argv;
110  int __argc;
111 
112  std::string __opt_string;
113  std::vector<option> __long_opts;
114 };
115 
116 } // end namespace fawkes
117 
118 #endif
const char * arg(const char *argn)
Get argument value.
Definition: argparser.cpp:182
Thrown if required argument was missing.
Definition: argparser.h:53
bool parse_hostport(const char *argn, char **host, unsigned short int *port)
Parse host:port string.
Definition: argparser.cpp:231
double parse_float(const char *argn)
Parse argument as double.
Definition: argparser.cpp:330
Fawkes library namespace.
double parse_item_float(unsigned int index)
Parse item as double.
Definition: argparser.cpp:378
Parse command line arguments.
Definition: argparser.h:66
const std::vector< const char * > & items() const
Get non-option items.
Definition: argparser.cpp:398
std::string get_optstring() const
Get option string.
Definition: argparser.h:95
int argc() const
Get number of arguments.
Definition: argparser.cpp:418
long int parse_int(const char *argn)
Parse argument as integer.
Definition: argparser.cpp:306
ArgumentParser(int argc, char **argv, const char *opt_string, option *long_options=NULL)
Constructor.
Definition: argparser.cpp:89
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::vector< option > get_long_opts() const
Get long option configuration.
Definition: argparser.h:100
~ArgumentParser()
Destructor.
Definition: argparser.cpp:157
const char ** argv() const
Program argument array as supplied to constructor.
Definition: argparser.cpp:428
const char * program_name() const
Get name of program.
Definition: argparser.cpp:438
bool has_arg(const char *argn)
Check if argument has been supplied.
Definition: argparser.cpp:169
std::vector< const char * >::size_type num_items() const
Get number of non-option items.
Definition: argparser.cpp:408
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
long int parse_item_int(unsigned int index)
Parse item as integer.
Definition: argparser.cpp:354