E-MailRelay
ggetopt.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2023 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 "goptions.h"
24#include "goptionvalue.h"
25#include "goptionparser.h"
26#include "goptionreader.h"
27#include "gstr.h"
28#include "gstringmap.h"
29#include "gstringtoken.h"
30#include "gfile.h"
31#include "gassert.h"
32#include "glog.h"
33#include <fstream>
34#include <algorithm>
35
36#ifndef G_LIB_SMALL
37G::GetOpt::GetOpt( const Arg & args_in , const std::string & spec , std::size_t ignore_non_options ) :
38 m_spec(spec) ,
39 m_args(args_in)
40{
41 parseArgs( ignore_non_options ) ;
42}
43#endif
44
45G::GetOpt::GetOpt( const Arg & args_in , const Options & spec , std::size_t ignore_non_options ) :
46 m_spec(spec) ,
47 m_args(args_in)
48{
49 parseArgs( ignore_non_options ) ;
50}
51
52#ifndef G_LIB_SMALL
53G::GetOpt::GetOpt( const StringArray & args_in , const std::string & spec , std::size_t ignore_non_options ) :
54 m_spec(spec) ,
55 m_args(args_in)
56{
57 parseArgs( ignore_non_options ) ;
58}
59#endif
60
61#ifndef G_LIB_SMALL
62G::GetOpt::GetOpt( const StringArray & args_in , const Options & spec , std::size_t ignore_non_options ) :
63 m_spec(spec) ,
64 m_args(args_in)
65{
66 parseArgs( ignore_non_options ) ;
67}
68#endif
69
70#ifndef G_LIB_SMALL
71void G::GetOpt::reload( const StringArray & args_in , std::size_t ignore_non_options )
72{
73 m_map.clear() ;
74 m_errors.clear() ;
75 m_args = Arg( args_in ) ;
76 parseArgs( ignore_non_options ) ;
77}
78#endif
79
80void G::GetOpt::parseArgs( std::size_t ignore_non_options )
81{
82 StringArray new_args = OptionParser::parse( m_args.array() , m_spec , m_map , &m_errors , 1U , ignore_non_options ) ;
83 new_args.insert( new_args.begin() , m_args.v(0U) ) ;
84 m_args = Arg( new_args ) ;
85}
86
87#ifndef G_LIB_SMALL
88bool G::GetOpt::addOptionsFromFile( std::size_t n , const StringArray & blocklist )
89{
90 if( n < m_args.c() )
91 {
92 G::Path path = m_args.v( n ) ;
93 if( std::find( blocklist.begin() , blocklist.end() , path.extension() ) != blocklist.end() )
94 return false ;
95 m_args.removeAt( n ) ;
96 addOptionsFromFile( path ) ;
97 }
98 return true ;
99}
100#endif
101
102#ifndef G_LIB_SMALL
103void G::GetOpt::addOptionsFromFile( std::size_t n , const std::string & varkey , const std::string & varvalue )
104{
105 if( n < m_args.c() )
106 {
107 std::string filename = m_args.v( n ) ;
108 m_args.removeAt( n ) ;
109
110 if( !filename.empty() )
111 {
112 if( !varkey.empty() && !varvalue.empty() && filename.find(varkey) == 0 )
113 G::Str::replace( filename , varkey , varvalue ) ;
114 addOptionsFromFile( filename ) ;
115 }
116 }
117}
118#endif
119
121{
122 return OptionReader::read( filename ) ;
123}
124
125void G::GetOpt::addOptionsFromFile( const Path & filename )
126{
127 OptionParser::parse( readOptionsFromFile(filename) , m_spec , m_map , &m_errors , 0U ) ;
128}
129
130const std::vector<G::Option> & G::GetOpt::options() const
131{
132 return m_spec.list() ;
133}
134
135#ifndef G_LIB_SMALL
137{
138 return m_map ;
139}
140#endif
141
142#ifndef G_LIB_SMALL
144{
145 return m_errors ;
146}
147#endif
148
149#ifndef G_LIB_SMALL
150bool G::GetOpt::contains( char c ) const
151{
152 return m_map.contains( m_spec.lookup(c) ) ;
153}
154#endif
155
157{
158 return m_map.contains( name ) ;
159}
160
161#ifndef G_LIB_SMALL
162std::size_t G::GetOpt::count( string_view name ) const
163{
164 return m_map.count( name ) ;
165}
166#endif
167
168#ifndef G_LIB_SMALL
169std::string G::GetOpt::value( char c , string_view default_ ) const
170{
171 G_ASSERT( contains(c) ) ;
172 return value( m_spec.lookup(c) , default_ ) ;
173}
174#endif
175
176std::string G::GetOpt::value( string_view name , string_view default_ ) const
177{
178 return m_map.value( name , default_ ) ;
179}
180
182{
183 return G::optional<std::string>( m_map.contains(name) , value(name) ) ;
184}
185
187{
188 return m_args ;
189}
190
192{
193 return !m_errors.empty() ;
194}
195
196void G::GetOpt::showErrors( std::ostream & stream ) const
197{
198 showErrors( stream , m_args.prefix() + ": error" ) ;
199}
200
201void G::GetOpt::showErrors( std::ostream & stream , const std::string & prefix_1 , const std::string & prefix_2 ) const
202{
203 for( const auto & error : m_errors )
204 {
205 stream << prefix_1 << prefix_2 << error << std::endl ;
206 }
207}
208
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 the map of option-values.
Definition: ggetopt.cpp:136
std::size_t count(string_view option_name) const
Returns the option's repeat count.
Definition: ggetopt.cpp:162
void reload(const StringArray &arg, std::size_t ignore_non_options=0U)
Reinitialises the object with the given command-line arguments.
Definition: ggetopt.cpp:71
bool contains(char option_letter) const
Returns true if the command-line contains the option identified by its short-form letter.
Definition: ggetopt.cpp:150
std::string value(string_view option_name, string_view default_={}) const
Returns the value for the option identified by its long-form name.
Definition: ggetopt.cpp:176
const std::vector< Option > & options() const
Returns the list of option specification objects.
Definition: ggetopt.cpp:130
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:37
G::optional< std::string > optional(string_view option_name) const
Returns an optional value identified by its long-form name.
Definition: ggetopt.cpp:181
static StringArray readOptionsFromFile(const Path &)
Reads options from file as a list of strings like "--foo=bar".
Definition: ggetopt.cpp:120
bool hasErrors() const
Returns true if there are errors.
Definition: ggetopt.cpp:191
void addOptionsFromFile(std::size_t n=1U, const std::string &varkey={}, const std::string &varvalue={})
Adds options from the config file named by the n'th non-option command-line argument (zero-based and ...
Definition: ggetopt.cpp:103
Arg args() const
Returns the G::Arg command-line, excluding options.
Definition: ggetopt.cpp:186
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:201
StringArray errorList() const
Returns the list of errors.
Definition: ggetopt.cpp:143
A multimap-like container for command-line options and their values.
Definition: goptionmap.h:43
StringArray parse(const StringArray &args_in, std::size_t start_position=1U, std::size_t ignore_non_options=0U, std::function< std::string(const std::string &, bool)> callback_fn={})
Parses the given command-line arguments into the value map and/or error list defined by the construct...
static StringArray read(const G::Path &, std::size_t limit=1000U)
Reads options from file as a list of strings like "--foo=bar".
A class to assemble a list of command-line options and provide access by name.
Definition: goptions.h:40
A Path object represents a file system path.
Definition: gpath.h:73
std::string extension() const
Returns the path's basename extension, ie.
Definition: gpath.cpp:428
static bool replace(std::string &s, string_view from, string_view to, std::size_t *pos_p=nullptr)
A string_view overload.
Definition: gstr.cpp:226
A class like c++17's std::string_view.
Definition: gstringview.h:51
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstringarray.h:30