E-MailRelay

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