E-MailRelay
ggetopt.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2021 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/// \file 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 "gstr.h"
28#include "gassert.h"
29#include "glog.h"
30#include <fstream>
31#include <algorithm>
32
33G::GetOpt::GetOpt( const Arg & args_in , const std::string & spec , std::size_t ignore_non_options ) :
34 m_spec(spec) ,
35 m_args(args_in)
36{
37 parseArgs( ignore_non_options ) ;
38}
39
40G::GetOpt::GetOpt( const Arg & args_in , const Options & spec , std::size_t ignore_non_options ) :
41 m_spec(spec) ,
42 m_args(args_in)
43{
44 parseArgs( ignore_non_options ) ;
45}
46
47G::GetOpt::GetOpt( const G::StringArray & args_in , const std::string & spec , std::size_t ignore_non_options ) :
48 m_spec(spec) ,
49 m_args(args_in)
50{
51 parseArgs( ignore_non_options ) ;
52}
53
54G::GetOpt::GetOpt( const G::StringArray & args_in , const Options & spec , std::size_t ignore_non_options ) :
55 m_spec(spec) ,
56 m_args(args_in)
57{
58 parseArgs( ignore_non_options ) ;
59}
60
61void G::GetOpt::reload( const G::StringArray & args_in , std::size_t ignore_non_options )
62{
63 m_map.clear() ;
64 m_errors.clear() ;
65 m_args = Arg( args_in ) ;
66 parseArgs( ignore_non_options ) ;
67}
68
69void G::GetOpt::parseArgs( std::size_t ignore_non_options )
70{
71 StringArray new_args = OptionParser::parse( m_args.array() , m_spec , m_map , &m_errors , 1U , ignore_non_options ) ;
72 new_args.insert( new_args.begin() , m_args.v(0U) ) ;
73 m_args = Arg( new_args ) ;
74}
75
76void G::GetOpt::addOptionsFromFile( std::size_t n , const std::string & varkey , const std::string & varvalue )
77{
78 if( n < m_args.c() )
79 {
80 std::string filename = m_args.v( n ) ;
81 m_args.removeAt( n ) ;
82
83 if( !filename.empty() )
84 {
85 if( !varkey.empty() && !varvalue.empty() && filename.find(varkey) == 0 )
86 G::Str::replace( filename , varkey , varvalue ) ;
87 addOptionsFromFile( filename ) ;
88 }
89 }
90}
91
92G::StringArray G::GetOpt::optionsFromFile( const Options & spec , const Path & filename )
93{
94 StringArray result ;
95 StringMap map = MapFile(filename,"config").map() ;
96 for( const auto & map_item : map )
97 {
98 const std::string & key = map_item.first ;
99 const std::string & value = map_item.second ;
100 if( spec.valued(key) )
101 result.push_back( std::string("--").append(key).append(1U,'=').append(value) ) ;
102 else
103 result.push_back( std::string("--").append(key) ) ;
104 }
105 return result ;
106}
107
109{
110 OptionParser::parse( optionsFromFile(m_spec,filename) , m_spec , m_map , &m_errors , 0U ) ;
111}
112
114{
115 return m_spec ;
116}
117
119{
120 return m_map ;
121}
122
124{
125 return m_errors ;
126}
127
128bool G::GetOpt::contains( char c ) const
129{
130 return m_map.contains( m_spec.lookup(c) ) ;
131}
132
133bool G::GetOpt::contains( const std::string & name ) const
134{
135 return m_map.contains( name ) ;
136}
137
138std::size_t G::GetOpt::count( const std::string & name ) const
139{
140 return m_map.count( name ) ;
141}
142
143std::string G::GetOpt::value( char c , const std::string & default_ ) const
144{
145 G_ASSERT( contains(c) ) ;
146 return value( m_spec.lookup(c) , default_ ) ;
147}
148
149std::string G::GetOpt::value( const std::string & name , const std::string & default_ ) const
150{
151 return m_map.value( name , default_ ) ;
152}
153
155{
156 return m_args ;
157}
158
160{
161 return !m_errors.empty() ;
162}
163
164void G::GetOpt::showErrors( std::ostream & stream ) const
165{
166 showErrors( stream , m_args.prefix() + ": error" ) ;
167}
168
169void G::GetOpt::showErrors( std::ostream & stream , const std::string & prefix_1 , const std::string & prefix_2 ) const
170{
171 for( const auto & error : m_errors )
172 {
173 stream << prefix_1 << prefix_2 << error << std::endl ;
174 }
175}
176
A class which holds a represention of the argc/argv command line array, and supports simple command-l...
Definition: garg.h:44
const OptionMap & map() const
Returns a reference to the OptionMap sub-object.
Definition: ggetopt.cpp:118
void reload(const StringArray &arg, std::size_t ignore_non_options=0U)
Reinitialises the object with the given command-line arguments.
Definition: ggetopt.cpp:61
bool contains(char option_letter) const
Returns true if the command-line contains the option identified by its short-form letter.
Definition: ggetopt.cpp:128
const Options & options() const
Returns a reference to the option specification sub-object.
Definition: ggetopt.cpp:113
GetOpt(const Arg &arg, const std::string &spec, std::size_t ignore_non_options=0U)
Constructor taking a Arg object and a G::Options specification string.
Definition: ggetopt.cpp:33
void addOptionsFromFile(size_type n=1U, const std::string &varkey=std::string(), const std::string &varvalue=std::string())
Adds options from the config file named by the n'th non-option command-line argument (zero-based and ...
bool hasErrors() const
Returns true if there are errors.
Definition: ggetopt.cpp:159
std::size_t count(const std::string &option_name) const
Returns the option's repeat count.
Definition: ggetopt.cpp:138
Arg args() const
Returns the G::Arg command-line, excluding options.
Definition: ggetopt.cpp:154
void showErrors(std::ostream &stream, const std::string &prefix_1, const std::string &prefix_2=std::string(": ")) const
A convenience function which streams out each errorList() item to the given stream,...
Definition: ggetopt.cpp:169
StringArray errorList() const
Returns the list of errors.
Definition: ggetopt.cpp:123
std::string value(const std::string &option_name, const std::string &default_=std::string()) const
Returns the value for the option identified by its long-form name.
Definition: ggetopt.cpp:149
A multimap-like container for command-line options and their values.
Definition: goptionmap.h:42
StringArray parse(const StringArray &args_in, std::size_t start_position=1U, std::size_t ignore_non_options=0U)
Parses the given command-line arguments into the value map and/or error list defined by the construct...
A class to represent allowed command-line options and to provide command-line usage text.
Definition: goptions.h:66
A Path object represents a file system path.
Definition: gpath.h:72
static bool replace(std::string &s, const std::string &from, const std::string &to, std::size_t *pos_p=nullptr)
Replaces 'from' with 'to', starting at offset '*pos_p'.
Definition: gstr.cpp:264
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstrings.h:31
std::map< std::string, std::string > StringMap
A std::map of std::strings.
Definition: gstrings.h:32