VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
garg.h
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 /// \file garg.h
19 ///
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 
29 namespace G
30 {
31  class Arg ;
32 }
33 
34 /// \class G::Arg
35 /// A class which holds a represention of the argc/argv command line array,
36 /// and supports simple command-line parsing.
37 ///
38 /// A copy of argv[0] is squirrelled away and made accessible via a static
39 /// method.
40 ///
41 /// \see G::GetOpt
42 ///
43 class G::Arg
44 {
45 public:
46  typedef size_t size_type ;
47 
48  Arg( int argc , char * argv [] ) ;
49  ///< Constructor taking argc/argv. Should not be used in a shared
50  ///< object or dll.
51 
52  explicit Arg( const G::StringArray & ) ;
53  ///< Constructor taking an array of command-line arguments. The
54  ///< program name in the first position is expected but may be
55  ///< ignored.
56 
57  Arg() ;
58  ///< Default constructor. Initialise with parse().
59 
60  void parse( HINSTANCE hinstance , const std::string & command_line_tail ) ;
61  ///< Parses the given command-line tail, splitting it up into
62  ///< an array of tokens. The v(0) value comes from the supplied
63  ///< instance handle.
64 
65  void parse( const std::string & command_line ) ;
66  ///< Parses the given command line, splitting it up into an array
67  ///< of tokens. The command-line should start with the v(0) value.
68 
69  void reparse( const std::string & command_line_tail ) ;
70  ///< Reinitialises the object with the given command-line tail. The
71  ///< command-line should not contain the program name: the v(0)
72  ///< value and prefix() are unchanged.
73 
74  ~Arg() ;
75  ///< Destructor.
76 
77  static std::string v0() ;
78  ///< Returns a copy of argv[0] from the first call to the constructor
79  ///< that takes argc/argv. Returns the empty string if that constructor
80  ///< overload has never been used. See also exe().
81 
82  static std::string exe( bool do_throw = true ) ;
83  ///< Returns Process::exe() or an absolute path constructed from v0().
84  ///< Throws on error by default, or optionally returns the empty string.
85  ///< See also v0().
86 
87  size_type c() const ;
88  ///< Returns the number of tokens in the command line, including the
89  ///< program name.
90 
91  std::string v( size_type i ) const ;
92  ///< Returns the i'th argument.
93  ///< Precondition: i < c()
94 
95  std::string prefix() const ;
96  ///< Returns the basename of v(0) without any extension. Typically used
97  ///< as a prefix in error messages.
98 
99  static const char * prefix( char * argv[] ) ;
100  ///< An exception-free version of prefix() which can be used in
101  ///< main() outside of the outermost try block.
102 
103  bool contains( const std::string & option , size_type option_args = 0U , bool case_sensitive = true ) const ;
104  ///< Returns true if the command line contains the given option
105  ///< with enough command line arguments left to satisfy the required
106  ///< number of option arguments. (By convention an option starts
107  ///< with a dash, but that is not required here; it's just a string
108  ///< that is matched against command-line arguments.)
109 
110  size_type index( const std::string & option , size_type option_args = 0U ) const ;
111  ///< Returns the index of the given option. Returns zero if not present.
112 
113  bool remove( const std::string & option , size_type option_args = 0U ) ;
114  ///< Removes the given option and its arguments. Returns false if
115  ///< the option does not exist.
116 
117  void removeAt( size_type option_index , size_type option_args = 0U ) ;
118  ///< Removes the given argument and the following 'option_args' ones.
119 
120  StringArray array( unsigned int shift = 0U ) const ;
121  ///< Returns the arguments a string array, including the program name
122  ///< in the first position.
123 
124  Arg & operator=( const Arg & ) ;
125  ///< Assignment operator.
126 
127  Arg( const Arg & ) ;
128  ///< Copy constructor.
129 
130 private:
131  bool find( bool , const std::string & , size_type , size_type * ) const ;
132  void setPrefix() ;
133  void setExe() ;
134  static bool match( bool , const std::string & , const std::string & ) ;
135  void parseCore( const std::string & ) ;
136  static void protect( std::string & ) ;
137  static void unprotect( StringArray & ) ;
138  static void dequote( StringArray & ) ;
139 
140 private:
141  StringArray m_array ;
142  static bool m_first ;
143  static std::string m_v0 ;
144  static std::string m_cwd ;
145 } ;
146 
147 #endif
std::string prefix() const
Returns the basename of v(0) without any extension.
Definition: garg.cpp:172
~Arg()
Destructor.
Definition: garg.cpp:59
std::vector< std::string > StringArray
A std::vector of std::strings.
Definition: gstrings.h:33
Arg()
Default constructor. Initialise with parse().
Definition: garg.cpp:63
static std::string exe(bool do_throw=true)
Returns Process::exe() or an absolute path constructed from v0().
Definition: garg.cpp:248
Arg & operator=(const Arg &)
Assignment operator.
Definition: garg.cpp:93
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
std::string v(size_type i) const
Returns the i'th argument.
Definition: garg.cpp:166
void reparse(const std::string &command_line_tail)
Reinitialises the object with the given command-line tail.
Definition: garg.cpp:82
size_type c() const
Returns the number of tokens in the command line, including the program name.
Definition: garg.cpp:161
void parse(HINSTANCE hinstance, const std::string &command_line_tail)
Parses the given command-line tail, splitting it up into an array of tokens.
Definition: garg.cpp:68
size_type index(const std::string &option, size_type option_args=0U) const
Returns the index of the given option. Returns zero if not present.
Definition: garg.cpp:154
A class which holds a represention of the argc/argv command line array, and supports simple command-l...
Definition: garg.h:43
void removeAt(size_type option_index, size_type option_args=0U)
Removes the given argument and the following 'option_args' ones.
Definition: garg.cpp:141
bool contains(const std::string &option, size_type option_args=0U, bool case_sensitive=true) const
Returns true if the command line contains the given option with enough command line arguments left to...
Definition: garg.cpp:108
static std::string v0()
Returns a copy of argv[0] from the first call to the constructor that takes argc/argv.
Definition: garg.cpp:88