ggetopt.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2013 Graeme Walker <graeme_walker@users.sourceforge.net>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
20 
21 #ifndef G_GETOPT_H
22 #define G_GETOPT_H
23 
24 #include "gdef.h"
25 #include "garg.h"
26 #include "gstrings.h"
27 #include "gexception.h"
28 #include <string>
29 #include <list>
30 #include <map>
31 
33 namespace G
34 {
35  class GetOpt ;
36 }
37 
51 class G::GetOpt
52 {
53 public:
55  struct Level
56  { unsigned int level ; explicit Level(unsigned int l) : level(l) {} } ;
57  G_EXCEPTION( InvalidSpecification , "invalid options specification string" ) ;
60  struct SwitchSpec
61  {
62  char c ;
63  std::string name ;
64  std::string description ;
65  std::string description_extra ;
66  bool valued ;
67  bool hidden ;
68  std::string value_description ;
69  unsigned int level ;
70  SwitchSpec( char c_ , const std::string & name_ ,
71  const std::string & description_ ,
72  const std::string & description_extra_ ,
73  bool v_ , const std::string & vd_ , unsigned int level_ ) :
74  c(c_) , name(name_) , description(description_) ,
75  description_extra(description_extra_) ,
76  valued(v_) , hidden(description_.empty()||level_==0U) ,
77  value_description(vd_) , level(level_) {}
78  } ;
79 
80  GetOpt( const Arg & arg , const std::string & spec ,
81  char sep_major = '|' , char sep_minor = '/' , char escape = '\\' ) ;
98 
99  Arg args() const ;
101 
102  Strings errorList() const ;
104 
105  static size_type wrapDefault() ;
107 
108  static size_type tabDefault() ;
110 
111  static Level levelDefault() ;
113 
114  static std::string introducerDefault() ;
116 
117  std::string usageSummary( const std::string & exe , const std::string & args ,
118  const std::string & introducer = introducerDefault() ,
119  Level level = levelDefault() , size_type wrap_width = wrapDefault() ) const ;
122 
123  std::string usageSummarySwitches( Level level = levelDefault() ) const ;
127 
128  std::string usageHelp( Level level = levelDefault() ,
129  size_type tab_stop = tabDefault() , size_type wrap_width = wrapDefault() ,
130  bool level_exact = false , bool extra = true ) const ;
132 
133  void showUsage( std::ostream & stream , const std::string & exe ,
134  const std::string & args , const std::string & introducer = introducerDefault() ,
135  Level level = levelDefault() , size_type tab_stop = tabDefault() ,
136  size_type wrap_width = wrapDefault() , bool extra = true ) const ;
141 
142  void showUsage( std::ostream & stream , const std::string & args , bool verbose ) const ;
146 
147  bool hasErrors() const ;
149 
150  void showErrors( std::ostream & stream , std::string prefix_1 ,
151  std::string prefix_2 = std::string(": ") ) const ;
155 
156  void showErrors( std::ostream & stream ) const ;
158 
159  void show( std::ostream & stream , std::string prefix ) const ;
161 
162  bool contains( char switch_letter ) const ;
165 
166  bool contains( const std::string & switch_name ) const ;
169 
170  std::string value( const std::string & switch_name ) const ;
175 
176  std::string value( char switch_letter ) const ;
181 
182 private:
183  typedef std::map<std::string,SwitchSpec> SwitchSpecMap ;
184  typedef std::pair<bool,std::string> Value ;
185  typedef std::map<char,Value> SwitchMap ;
186 
187  void operator=( const GetOpt & ) ;
188  GetOpt( const GetOpt & ) ;
189  void parseSpec( const std::string & spec , char , char , char ) ;
190  void addSpec( const std::string & , char c , const std::string & ,
191  const std::string & , const std::string & , bool ,
192  const std::string & , unsigned int ) ;
193  size_type parseArgs( const Arg & args_in ) ;
194  bool isOldSwitch( const std::string & arg ) const ;
195  bool isNewSwitch( const std::string & arg ) const ;
196  bool isSwitchSet( const std::string & arg ) const ;
197  void processSwitch( char c ) ;
198  void processSwitch( char c , const std::string & value ) ;
199  void processSwitch( const std::string & s ) ;
200  void processSwitch( const std::string & s , const std::string & value ) ;
201  bool valued( char c ) const ;
202  bool valued( const std::string & ) const ;
203  void errorNoValue( char c ) ;
204  void errorNoValue( const std::string & ) ;
205  void errorUnknownSwitch( char c ) ;
206  void errorUnknownSwitch( const std::string & ) ;
207  char key( const std::string & s ) const ;
208  void remove( size_type n ) ;
209  bool valid( const std::string & ) const ;
210  bool valid( char c ) const ;
211  std::string usageSummaryPartOne( Level ) const ;
212  std::string usageSummaryPartTwo( Level ) const ;
213  std::string usageHelpCore( const std::string & , Level , size_type ,
214  size_type , bool , bool ) const ;
215  static size_type widthLimit( size_type ) ;
216  static bool visible( SwitchSpecMap::const_iterator , Level , bool ) ;
217  static size_type eqPos( const std::string & ) ;
218  static std::string eqValue( const std::string & , size_type ) ;
219 
220 private:
221  SwitchSpecMap m_spec_map ;
222  SwitchMap m_map ;
223  Strings m_errors ;
224  Arg m_args ;
225 } ;
226 
227 #endif
bool contains(char switch_letter) const
Returns true if the command line contains the given switch.
Definition: ggetopt.cpp:463
void showErrors(std::ostream &stream, std::string prefix_1, std::string prefix_2=std::string(": ")) const
A convenience function which streams out each errorList() item to the given stream, prefixed with the given prefix(es).
Definition: ggetopt.cpp:527
Strings errorList() const
Returns the list of errors.
Definition: ggetopt.cpp:458
std::string description_extra
Definition: ggetopt.h:65
std::list< std::string > Strings
A std::list of std::strings.
Definition: gstrings.h:39
bool hasErrors() const
Returns true if there are errors.
Definition: ggetopt.cpp:517
std::string value_description
Definition: ggetopt.h:68
unsigned int level
Definition: ggetopt.h:56
std::string::size_type size_type
A std::size_t type.
Definition: md5.h:43
void show(std::ostream &stream, std::string prefix) const
For debugging.
Definition: ggetopt.cpp:495
std::string value(const std::string &switch_name) const
Returns the value related to the given value-based switch.
Definition: ggetopt.cpp:484
A private implementation structure used by G::GetOpt.
Definition: ggetopt.h:60
GetOpt(const Arg &arg, const std::string &spec, char sep_major= '|', char sep_minor= '/', char escape= '\\')
Constructor taking a Arg reference and a specification string.
Definition: ggetopt.cpp:46
static size_type tabDefault()
Returns a default tab-stop.
Definition: ggetopt.cpp:124
A command line switch parser.
Definition: ggetopt.h:51
Arg args() const
Returns all the non-switch command-line arguments.
Definition: ggetopt.cpp:490
std::string usageHelp(Level level=levelDefault(), size_type tab_stop=tabDefault(), size_type wrap_width=wrapDefault(), bool level_exact=false, bool extra=true) const
Returns a multi-line string giving help on each switch.
Definition: ggetopt.cpp:236
Low-level classes.
Level(unsigned int l)
Definition: ggetopt.h:56
unsigned int level
Definition: ggetopt.h:69
std::string description
Definition: ggetopt.h:64
SwitchSpec(char c_, const std::string &name_, const std::string &description_, const std::string &description_extra_, bool v_, const std::string &vd_, unsigned int level_)
Definition: ggetopt.h:70
static size_type wrapDefault()
Returns a default word-wrapping width.
Definition: ggetopt.cpp:113
#define G_EXCEPTION(class_name, description)
define as a function rather than a type if optimising for size
Definition: gexception.h:93
static Level levelDefault()
Returns the default level.
Definition: ggetopt.cpp:134
A class which holds a represention of the argc/argv command line array, and supports simple command-l...
Definition: garg.h:46
std::string::size_type size_type
Definition: ggetopt.h:57
std::string usageSummary(const std::string &exe, const std::string &args, const std::string &introducer=introducerDefault(), Level level=levelDefault(), size_type wrap_width=wrapDefault()) const
Returns a one-line usage summary, as "usage: ".
Definition: ggetopt.cpp:158
std::string name
Definition: ggetopt.h:63
static std::string introducerDefault()
Returns "usage: ".
Definition: ggetopt.cpp:129
Used by G::GetOpt for extra type safety.
Definition: ggetopt.h:55
std::string usageSummarySwitches(Level level=levelDefault()) const
Returns the one-line summary of switches.
Definition: ggetopt.cpp:172
void showUsage(std::ostream &stream, const std::string &exe, const std::string &args, const std::string &introducer=introducerDefault(), Level level=levelDefault(), size_type tab_stop=tabDefault(), size_type wrap_width=wrapDefault(), bool extra=true) const
Streams out multi-line usage text using usageSummary() and usageHelp().
Definition: ggetopt.cpp:150