garg.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2013 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 // ===
20 
21 #ifndef G_ARG_H
22 #define G_ARG_H
23 
24 #include "gdef.h"
25 #include "gstrings.h"
26 #include <vector>
27 #include <string>
28 
30 namespace G
31 {
32  class Arg ;
33 }
34 
46 class G::Arg
47 {
48 public:
49  typedef size_t size_type ;
50 
51  Arg( int argc , char *argv[] ) ;
54 
55  Arg() ;
58 
59  void parse( HINSTANCE hinstance , const std::string & command_line ) ;
64 
65  void reparse( const std::string & command_line ) ;
69 
70  ~Arg() ;
72 
73  size_type c() const ;
76 
77  std::string v( size_type i ) const ;
80 
81  std::string prefix() const ;
84 
85  static const char * prefix( char * argv[] ) ; // throw()
89 
90  bool contains( const std::string & sw , size_type sw_args = 0U , bool case_sensitive = true ) const ;
95 
96  size_type index( const std::string & sw , size_type sw_args = 0U ) const ;
99 
100  bool remove( const std::string & sw , size_type sw_args = 0U ) ;
103 
104  void removeAt( size_type sw_index , size_type sw_args = 0U ) ;
107 
108  Arg & operator=( const Arg & ) ;
110 
111  Arg( const Arg & ) ;
113 
114 private:
115  static std::string moduleName( HINSTANCE h ) ;
116  bool find( bool , const std::string & , size_type , size_type * ) const ;
117  void setPrefix() ;
118  void setExe() ;
119  static bool match( bool , const std::string & , const std::string & ) ;
120  void parseCore( const std::string & ) ;
121  static void protect( std::string & ) ;
122  static void unprotect( StringArray & ) ;
123  static void dequote( StringArray & ) ;
124 
125 private:
126  StringArray m_array ;
127  std::string m_prefix ;
128 } ;
129 
130 #endif
std::string prefix() const
Returns the basename of v(0) without any extension.
Definition: garg.cpp:142
~Arg()
Destructor.
Definition: garg.cpp:40
bool contains(const std::string &sw, size_type sw_args=0U, bool case_sensitive=true) const
< An exception-free version of prefix() which can be used in main() outside of the outermost try bloc...
Definition: garg.cpp:75
size_t size_type
Definition: garg.h:49
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstrings.h:44
Arg()
Default constructor for Windows.
Definition: garg.cpp:44
void parse(HINSTANCE hinstance, const std::string &command_line)
Windows only.
size_type index(const std::string &sw, size_type sw_args=0U) const
Returns the index of the given switch.
Definition: garg.cpp:124
Arg & operator=(const Arg &)
Assignment operator.
Definition: garg.cpp:57
std::string v(size_type i) const
Returns the i'th argument.
Definition: garg.cpp:136
Low-level classes.
void reparse(const std::string &command_line)
Reinitialises the object with the given command-line.
size_type c() const
Returns the number of tokens in the command line, including the program name.
Definition: garg.cpp:131
void removeAt(size_type sw_index, size_type sw_args=0U)
Removes the given argument and the following 'sw_args' ones.
Definition: garg.cpp:111
A class which holds a represention of the argc/argv command line array, and supports simple command-l...
Definition: garg.h:46