VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
goptionparser.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
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 // ===
17 ///
18 /// \file goptionparser.h
19 ///
20 
21 #ifndef G_OPTION_PARSER_H
22 #define G_OPTION_PARSER_H
23 
24 #include "gdef.h"
25 #include "gstrings.h"
26 #include "goptions.h"
27 #include "goptionvalue.h"
28 #include "goptionmap.h"
29 #include <string>
30 #include <map>
31 
32 namespace G
33 {
34  class OptionParser ;
35 }
36 
37 /// \class G::OptionParser
38 /// A parser for command-line arguments that operates according to an Options
39 /// specification and returns an OptionValue multimap.
40 /// \see G::Options, G::OptionValue
41 ///
43 {
44 public:
45  OptionParser( const Options & spec , OptionMap & values_out , StringArray & errors_out ) ;
46  ///< Constructor. Output references are kept. The output map is a
47  ///< multimap, but with methods that also allow it to be used as
48  ///< a simple map with multi-valued options concatenated into a
49  ///< comma-separated list.
50 
51  OptionParser( const Options & spec , OptionMap & values_out ) ;
52  ///< Constructor for when errors can be ignored.
53 
54  size_t parse( const StringArray & args , size_t start_position = 1U ) ;
55  ///< Parses the given command-line arguments into the value map and/or
56  ///< error list defined by the constructor.
57  ///<
58  ///< By default the program name is expected to be the first item in
59  ///< the array and it is ignored, although the 'start-position' parameter
60  ///< can be used to change this. See also G::Arg::array().
61  ///<
62  ///< Individual arguments can be in short-form like "-c", or long-form
63  ///< like "--foo" or "--foo=bar". Long-form arguments can be passed in
64  ///< two separate arguments, eg. "--foo" followed by "bar". Short-form
65  ///< options can be grouped (eg. "-abc"). Boolean options can be enabled
66  ///< by (eg.) "--verbose" or "--verbose=yes", and disabled by "--verbose=no".
67  ///< Boolean options cannot use two separate arguments (eg. "--verbose"
68  ///< followed by "yes").
69  ///<
70  ///< Entries in the output map are keyed by the option's long name,
71  ///< even if supplied in short-form.
72  ///<
73  ///< Errors are reported into the error list.
74  ///<
75  ///< Returns the position in the array where the non-option command-line
76  ///< arguments begin.
77 
78 private:
79  OptionParser( const OptionParser & ) ;
80  void operator=( const OptionParser & ) ;
81  bool haveSeen( const std::string & ) const ;
82  static std::string::size_type eqPos( const std::string & ) ;
83  static std::string eqValue( const std::string & , std::string::size_type ) ;
84  void processOptionOn( char c ) ;
85  void processOption( char c , const std::string & value ) ;
86  void processOptionOn( const std::string & s ) ;
87  void processOptionOff( const std::string & s ) ;
88  void processOption( const std::string & s , const std::string & value , bool ) ;
89  void errorNoValue( char ) ;
90  void errorNoValue( const std::string & ) ;
91  void errorUnknownOption( char ) ;
92  void errorUnknownOption( const std::string & ) ;
93  void errorDubiousValue( const std::string & , const std::string & ) ;
94  void errorDuplicate( char ) ;
95  void errorDuplicate( const std::string & ) ;
96  void errorExtraValue( char ) ;
97  void errorExtraValue( const std::string & ) ;
98  void errorConflict( const std::string & ) ;
99  bool haveSeenOn( const std::string & name ) const ;
100  bool haveSeenOff( const std::string & name ) const ;
101  static bool isOldOption( const std::string & ) ;
102  static bool isNewOption( const std::string & ) ;
103  static bool isAnOptionSet( const std::string & ) ;
104 
105 private:
106  bool m_multimap ;
107  const Options & m_spec ;
108  OptionMap & m_map ;
109  StringArray m_errors_ignored ;
110  StringArray & m_errors ;
111 } ;
112 
113 #endif
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstrings.h:33
A parser for command-line arguments that operates according to an Options specification and returns a...
Definition: goptionparser.h:42
size_t parse(const StringArray &args, size_t start_position=1U)
Parses the given command-line arguments into the value map and/or error list defined by the construct...
OptionParser(const Options &spec, OptionMap &values_out, StringArray &errors_out)
Constructor.
A map-like container for command-line options and their values.
Definition: goptionmap.h:38
A class to represent allowed command-line options and to provide command-line usage text...
Definition: goptions.h:65