Gnash  0.8.11dev
arg_parser.h
Go to the documentation of this file.
1 // Arg_parser - A POSIX/GNU command line argument parser.
2 // Copyright (C) 2006, 2007, 2008, 2009, 2010 Antonio Diaz Diaz.
3 // Copyright (C) 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 //
18 //
19 // Arg_parser reads the arguments in `argv' and creates a number of
20 // option codes, option arguments and non-option arguments.
21 //
22 // In case of error, `error' returns a non-empty error message.
23 //
24 // `options' is an array of `struct Option' terminated by an element
25 // containing a code which is zero. A null name means a short-only
26 // option. A code value outside the unsigned char range means a
27 // long-only option.
28 //
29 // Arg_parser normally makes it appear as if all the option arguments
30 // were specified before all the non-option arguments for the purposes
31 // of parsing, even if the user of your program intermixed option and
32 // non-option arguments. If you want the arguments in the exact order
33 // the user typed them, call `Arg_parser' with `in_order' = true.
34 //
35 // The argument `--' terminates all options; any following arguments are
36 // treated as non-option arguments, even if they begin with a hyphen.
37 //
38 // The syntax for optional option arguments is `-<short_option><argument>'
39 // (without whitespace), or `--<long_option>=<argument>'.
40 
41 // This class has been modified with a templated parser.argument<>
42 // method, allowing typesafe handling of different return types, and
43 // saving using strto* on the user side. I've added an exception class
44 // because I'd like to know if we call an argument outside the range
45 // of argument - there's no reasonable situation in which that would
46 // happen. <bwy>
47 
48 #include "dsodefs.h"
49 #include <vector>
50 #include <sstream>
51 #include <utility>
52 
54 {
55 public:
56  enum Has_arg { no, yes, maybe };
57 
58  struct Option
59  {
60  int code; // Short option letter or code ( code != 0 )
61  const char * name; // Long option name (maybe null)
63  };
64 
65  class ArgParserException : public std::exception
66  {
67  public:
68  ArgParserException(std::string s)
69  :
70  _msg(std::move(s))
71  {}
72 
73  virtual ~ArgParserException() throw() {}
74 
75  const char* what() const throw() { return _msg.c_str(); }
76 
77  private:
78 
79  std::string _msg;
80  };
81 
82 private:
83  struct Record
84  {
85  int code;
86  std::string argument;
87  Record( const int c = 0 ) : code( c ) {}
88  };
89 
90  std::string _error;
91  std::vector< Record > data;
92 
93  bool parse_long_option( const char * const opt, const char * const arg,
94  const Option options[], int & argind ) throw();
95  bool parse_short_option( const char * const opt, const char * const arg,
96  const Option options[], int & argind ) throw();
97 
98 public:
99  DSOEXPORT Arg_parser( const int argc, const char * const argv[],
100  const Option options[], const bool in_order = false ) throw();
101 
102  // Restricted constructor. Parses a single token and argument (if any)
103  DSOEXPORT Arg_parser( const char * const opt, const char * const arg,
104  const Option options[] ) throw();
105 
106  const std::string & error() const throw() { return _error; }
107 
108  // The number of arguments parsed (may be different from argc)
109  int arguments() const throw() { return data.size(); }
110 
111  // If code( i ) is 0, argument( i ) is a non-option.
112  // Else argument( i ) is the option's argument (or empty).
113  int code( const int i ) const throw()
114  {
115  if( i >= 0 && i < arguments() ) return data[i].code;
116  else return 0;
117  }
118 
119  std::string argument(const int i) const throw(ArgParserException)
120  {
121  if( i >= 0 && i < arguments() ) return data[i].argument;
122  else return _error;
123  }
124 
125  template<typename T>
126  T argument(const int i) const throw (ArgParserException)
127  {
128  T t = 0;
129  if( i >= 0 && i < arguments() )
130  {
131  std::istringstream in(data[i].argument);
132  in >> t;
133  return t;
134  }
135  else throw ArgParserException("Code out of range");
136  }
137 };
138 
139 
140 // local Variables:
141 // mode: C++
142 // indent-tabs-mode: t
143 // End:
Definition: arg_parser.h:65
std::string argument(const int i) const
Definition: arg_parser.h:119
int arguments() const
Definition: arg_parser.h:109
const char * what() const
Definition: arg_parser.h:75
Definition: arg_parser.h:56
SimpleBuffer data
Definition: LocalConnection_as.cpp:151
Definition: arg_parser.h:56
Definition: GnashKey.h:149
Definition: arg_parser.h:58
DSOEXPORT Arg_parser(const int argc, const char *const argv[], const Option options[], const bool in_order=false)
Definition: arg_parser.cpp:126
Definition: GnashKey.h:166
const std::string & error() const
Definition: arg_parser.h:106
ArgParserException(std::string s)
Definition: arg_parser.h:68
T argument(const int i) const
Definition: arg_parser.h:126
Has_arg has_arg
Definition: arg_parser.h:62
#define DSOEXPORT
Definition: dsodefs.h:55
int code(const int i) const
Definition: arg_parser.h:113
Definition: GnashKey.h:132
Definition: GnashKey.h:155
Definition: arg_parser.h:56
int code
Definition: arg_parser.h:60
const char * name
Definition: arg_parser.h:61
Definition: GnashKey.h:165
virtual ~ArgParserException()
Definition: arg_parser.h:73
Definition: arg_parser.h:53
Has_arg
Definition: arg_parser.h:56