VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ggetopt.cpp
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 // 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 
104 {
105  return m_spec ;
106 }
107 
109 {
110  return m_errors ;
111 }
112 
113 bool G::GetOpt::contains( char c ) const
114 {
115  return m_map.contains( m_spec.lookup(c) ) ;
116 }
117 
118 bool G::GetOpt::contains( const std::string & name ) const
119 {
120  return m_map.contains( name ) ;
121 }
122 
123 unsigned int G::GetOpt::count( const std::string & name ) const
124 {
125  return m_map.count( name ) ;
126 }
127 
128 std::string G::GetOpt::value( char c , const std::string & default_ ) const
129 {
130  G_ASSERT( contains(c) ) ;
131  return value( m_spec.lookup(c) , default_ ) ;
132 }
133 
134 std::string G::GetOpt::value( const std::string & name , const std::string & default_ ) const
135 {
136  return m_map.contains(name) ? m_map.value(name) : default_ ;
137 }
138 
140 {
141  return m_args ;
142 }
143 
145 {
146  return m_errors.size() != 0U ;
147 }
148 
149 void G::GetOpt::showErrors( std::ostream & stream ) const
150 {
151  showErrors( stream , m_args.prefix() + ": error" ) ;
152 }
153 
154 void G::GetOpt::showErrors( std::ostream & stream , std::string prefix_1 , std::string prefix_2 ) const
155 {
156  for( StringArray::const_iterator p = m_errors.begin() ; p != m_errors.end() ; ++p )
157  {
158  stream << prefix_1 << prefix_2 << *p << std::endl ;
159  }
160 }
161 
162 /// \file ggetopt.cpp
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:154
StringArray errorList() const
Returns the list of errors.
Definition: ggetopt.cpp:108
bool hasErrors() const
Returns true if there are errors.
Definition: ggetopt.cpp:144
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstrings.h:33
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 ...
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
Arg args() const
Returns all the non-option command-line arguments.
Definition: ggetopt.cpp:139
unsigned int count(const std::string &option_name) const
Returns the number of times the option was supplied.
Definition: ggetopt.cpp:123
const Options & options() const
Returns a reference to the internal option specification object.
Definition: ggetopt.cpp:103
GetOpt(const Arg &arg, const std::string &spec)
Constructor taking a Arg reference and a G::Options specification string.
Definition: ggetopt.cpp:33
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:134
std::map< std::string, std::string > StringMap
A std::map of std::strings.
Definition: gstrings.h:34
A class which holds a represention of the argc/argv command line array, and supports simple command-l...
Definition: garg.h:43
bool contains(char option_letter) const
Returns true if the command line contains the option identified by its short-form letter...
Definition: ggetopt.cpp:113
void reload(const StringArray &arg)
Reinitialises the object with the given command-line arguments.
Definition: ggetopt.cpp:49
A class to represent allowed command-line options and to provide command-line usage text...
Definition: goptions.h:65
A Path object represents a file system path.
Definition: gpath.h:72