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 #include <utils/misc/string_compare.h>
29 
30 #include <getopt.h>
31 #include <map>
32 #include <string>
33 #include <vector>
34 
35 namespace fawkes {
36 
37 /** Thrown if unknown argument was supplied */
39 {
40 public:
41  /** Constructor.
42  * @param c Unknown option character
43  */
45  {
46  append("Unknown option '%c'", c);
47  }
48 };
49 
50 /** Thrown if required argument was missing. */
52 {
53 public:
54  /** Constructor.
55  * @param c option with missing argument
56  */
58  {
59  append("Option '%c' requires an argument", c);
60  }
61 };
62 
64 {
65 public:
66  ArgumentParser(int argc, char **argv, const char *opt_string, option *long_options = NULL);
68 
69  bool has_arg(const char *argn);
70  const char *arg(const char *argn);
71  bool arg(const char *argn, char **value);
72  const char *program_name() const;
73 
74  bool parse_hostport(const char *argn, char **host, unsigned short int *port);
75  bool parse_hostport(const char *argn, std::string &host, unsigned short int &port);
76 
77  static void parse_hostport_s(const char *s, char **host, unsigned short int *port);
78  static void parse_hostport_s(const char *s, std::string &host, unsigned short int &port);
79 
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  int argc() const;
90  const char **argv() const;
91 
92  /** Get option string.
93  * @return option string used to create instance */
94  std::string
95  get_optstring() const
96  {
97  return opt_string_;
98  }
99 
100  /** Get long option configuration.
101  * @return vector of long options used to create instance */
102  std::vector<option>
104  {
105  return long_opts_;
106  }
107 
108 private:
109  std::map<std::string, const char *> opts_;
110  std::map<std::string, const char *> opts_cit_;
111  std::vector<const char *> items_;
112 
113  char * program_name_;
114  char **argv_;
115  int argc_;
116 
117  std::string opt_string_;
118  std::vector<option> long_opts_;
119 };
120 
121 } // end namespace fawkes
122 
123 #endif
fawkes::ArgumentParser::parse_hostport
bool parse_hostport(const char *argn, char **host, unsigned short int *port)
Parse host:port string.
Definition: argparser.cpp:224
fawkes::ArgumentParser::argc
int argc() const
Get number of arguments.
Definition: argparser.cpp:465
fawkes::ArgumentParser::get_optstring
std::string get_optstring() const
Get option string.
Definition: argparser.h:95
fawkes::ArgumentParser::parse_item_float
double parse_item_float(unsigned int index)
Parse item as double.
Definition: argparser.cpp:428
fawkes::MissingArgumentException::MissingArgumentException
MissingArgumentException(char c)
Constructor.
Definition: argparser.h:57
fawkes::ArgumentParser::~ArgumentParser
~ArgumentParser()
Destructor.
Definition: argparser.cpp:154
fawkes::UnknownArgumentException
Thrown if unknown argument was supplied.
Definition: argparser.h:39
fawkes::ArgumentParser::get_long_opts
std::vector< option > get_long_opts() const
Get long option configuration.
Definition: argparser.h:103
fawkes::ArgumentParser::has_arg
bool has_arg(const char *argn)
Check if argument has been supplied.
Definition: argparser.cpp:165
fawkes::ArgumentParser::argv
const char ** argv() const
Program argument array as supplied to constructor.
Definition: argparser.cpp:474
fawkes::Exception::append
void append(const char *format,...)
Append messages to the message list.
Definition: exception.cpp:333
fawkes::ArgumentParser::parse_hostport_s
static void parse_hostport_s(const char *s, char **host, unsigned short int *port)
Parse host:port string.
Definition: argparser.cpp:251
fawkes::ArgumentParser::parse_int
long int parse_int(const char *argn)
Parse argument as integer.
Definition: argparser.cpp:359
fawkes::ArgumentParser::items
const std::vector< const char * > & items() const
Get non-option items.
Definition: argparser.cpp:447
fawkes::ArgumentParser::program_name
const char * program_name() const
Get name of program.
Definition: argparser.cpp:483
fawkes::ArgumentParser::ArgumentParser
ArgumentParser(int argc, char **argv, const char *opt_string, option *long_options=NULL)
Constructor.
Definition: argparser.cpp:89
fawkes
Fawkes library namespace.
fawkes::ArgumentParser
Parse command line arguments.
Definition: argparser.h:64
fawkes::MissingArgumentException
Thrown if required argument was missing.
Definition: argparser.h:52
fawkes::ArgumentParser::arg
const char * arg(const char *argn)
Get argument value.
Definition: argparser.cpp:177
fawkes::ArgumentParser::parse_float
double parse_float(const char *argn)
Parse argument as double.
Definition: argparser.cpp:382
fawkes::ArgumentParser::num_items
std::vector< const char * >::size_type num_items() const
Get number of non-option items.
Definition: argparser.cpp:456
fawkes::ArgumentParser::parse_item_int
long int parse_item_int(unsigned int index)
Parse item as integer.
Definition: argparser.cpp:405
fawkes::UnknownArgumentException::UnknownArgumentException
UnknownArgumentException(char c)
Constructor.
Definition: argparser.h:44
fawkes::Exception
Base class for exceptions in Fawkes.
Definition: exception.h:36