E-MailRelay

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

ggetopt.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2018 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 // ===
17 //
18 // ggetopt.cpp
19 //
20
21 #include "gdef.h"
22 #include "ggetopt.h"
23 #include "gmapfile.h"
24 #include "goptions.h"
25 #include "goptionvalue.h"
26 #include "goptionparser.h"
27 #include "gstrings.h"
28 #include "gstr.h"
29 #include "gassert.h"
30 #include "gdebug.h"
31 #include <fstream>
32
33 G::GetOpt::GetOpt( const Arg & args_in , const std::string & spec ) :
34 m_spec(spec) ,
35 m_args(args_in) ,
36 m_parser(m_spec,m_map,m_errors)
37 {
38 parseArgs( m_args ) ;
39 }
40
41 G::GetOpt::GetOpt( const G::StringArray & args_in , const std::string & spec ) :
42 m_spec(spec) ,
43 m_args(args_in) ,
44 m_parser(m_spec,m_map,m_errors)
45 {
46 parseArgs( m_args ) ;
47 }
48
49 void G::GetOpt::reload( const G::StringArray & args_in )
50 {
51 m_map.clear() ;
52 m_errors.clear() ;
53 m_args = Arg( args_in ) ;
54
55 parseArgs( m_args ) ;
56 }
57
58 void G::GetOpt::parseArgs( Arg & args )
59 {
60 size_t pos = m_parser.parse( args.array() ) ;
61 if( pos > 1U )
62 m_args.removeAt( 1U , pos-2U ) ;
63 }
64
65 void G::GetOpt::addOptionsFromFile( size_t n )
66 {
67 if( n < m_args.c() )
68 {
69 std::string filename = m_args.v( n ) ;
70 m_args.removeAt( n ) ;
71
72 if( !filename.empty() )
73 addOptionsFromFile( filename ) ;
74 }
75 }
76
77 G::StringArray G::GetOpt::optionsFromFile( const G::Path & filename ) const
78 {
79 StringArray result ;
80 StringMap map = MapFile(filename).map() ;
81 for( StringMap::const_iterator p = map.begin() ; p != map.end() ; ++p )
82 {
83 std::string key = (*p).first ;
84 std::string value = (*p).second ;
85 if( m_spec.valued(key) )
86 {
87 result.push_back( "--" + key ) ;
88 result.push_back( value ) ;
89 }
90 else
91 {
92 result.push_back( "--" + key + "=" + value ) ;
93 }
94 }
95 return result ;
96 }
97
98 void G::GetOpt::addOptionsFromFile( const G::Path & filename )
99 {
100 m_parser.parse( optionsFromFile(filename) , 0U ) ;
101 }
102
103 const G::Options & G::GetOpt::options() const
104 {
105 return m_spec ;
106 }
107
108 const G::OptionMap & G::GetOpt::map() const
109 {
110 return m_map ;
111 }
112
113 G::StringArray G::GetOpt::errorList() const
114 {
115 return m_errors ;
116 }
117
118 bool G::GetOpt::contains( char c ) const
119 {
120 return m_map.contains( m_spec.lookup(c) ) ;
121 }
122
123 bool G::GetOpt::contains( const std::string & name ) const
124 {
125 return m_map.contains( name ) ;
126 }
127
128 size_t G::GetOpt::count( const std::string & name ) const
129 {
130 return m_map.count(name) ;
131 }
132
133 std::string G::GetOpt::value( char c , const std::string & default_ ) const
134 {
135 G_ASSERT( contains(c) ) ;
136 return value( m_spec.lookup(c) , default_ ) ;
137 }
138
139 std::string G::GetOpt::value( const std::string & name , const std::string & default_ ) const
140 {
141 return m_map.contains(name) ? m_map.value(name) : default_ ;
142 }
143
144 G::Arg G::GetOpt::args() const
145 {
146 return m_args ;
147 }
148
149 bool G::GetOpt::hasErrors() const
150 {
151 return m_errors.size() != 0U ;
152 }
153
154 void G::GetOpt::showErrors( std::ostream & stream ) const
155 {
156 showErrors( stream , m_args.prefix() + ": error" ) ;
157 }
158
159 void G::GetOpt::showErrors( std::ostream & stream , std::string prefix_1 , std::string prefix_2 ) const
160 {
161 for( StringArray::const_iterator p = m_errors.begin() ; p != m_errors.end() ; ++p )
162 {
163 stream << prefix_1 << prefix_2 << *p << std::endl ;
164 }
165 }
166
167 /// \file ggetopt.cpp
G::GetOpt::showErrors
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:159
G::GetOpt::count
size_t count(const std::string &option_name) const
Returns the number of times the option was supplied.
Definition: ggetopt.cpp:128
goptions.h
gstrings.h
G::GetOpt::errorList
StringArray errorList() const
Returns the list of errors.
Definition: ggetopt.cpp:113
G::GetOpt::hasErrors
bool hasErrors() const
Returns true if there are errors.
Definition: ggetopt.cpp:149
G::StringArray
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstrings.h:33
G::GetOpt::map
const OptionMap & map() const
Returns a reference to the OptionMap sub-object.
Definition: ggetopt.cpp:108
goptionvalue.h
ggetopt.h
G::OptionMap
A multimap-like container for command-line options and their values.
Definition: goptionmap.h:39
G::GetOpt::addOptionsFromFile
void addOptionsFromFile(size_type n=1U)
Adds options from the config file named by the n'th non-option command-line argument (zero-based but ...
gmapfile.h
G::Arg::array
StringArray array(unsigned int shift=0U) const
Returns the arguments a string array, including the program name in the first position.
Definition: garg.cpp:100
G::GetOpt::args
Arg args() const
Returns all the non-option command-line arguments.
Definition: ggetopt.cpp:144
gstr.h
gassert.h
G::GetOpt::options
const Options & options() const
Returns a reference to the option specification sub-object.
Definition: ggetopt.cpp:103
G::GetOpt::GetOpt
GetOpt(const Arg &arg, const std::string &spec)
Constructor taking a Arg reference and a G::Options specification string.
Definition: ggetopt.cpp:33
G::GetOpt::value
std::string value(const std::string &option_name, const std::string &default_=std::string()) const
Returns the value related to the option identified by its long-form name.
Definition: ggetopt.cpp:139
G::StringMap
std::map< std::string, std::string > StringMap
A std::map of std::strings.
Definition: gstrings.h:34
goptionparser.h
G::Arg
A class which holds a represention of the argc/argv command line array, and supports simple command-l...
Definition: garg.h:43
G::GetOpt::contains
bool contains(char option_letter) const
Returns true if the command line contains the option identified by its short-form letter...
Definition: ggetopt.cpp:118
G::GetOpt::reload
void reload(const StringArray &arg)
Reinitialises the object with the given command-line arguments.
Definition: ggetopt.cpp:49
G::Options
A class to represent allowed command-line options and to provide command-line usage text...
Definition: goptions.h:65
G::Path
A Path object represents a file system path.
Definition: gpath.h:72
gdebug.h