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 // ===
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 namespace G
31 {
32 	class Arg ;
33 }
34 
35 /// \class G::Arg
36 /// A class which holds a represention of the argc/argv
37 /// command line array, and supports simple command-line parsing.
38 ///
39 /// In some environments the argv(0) path is fixed up so that it
40 /// refers to the running executable, regardless of what the parent
41 /// process specified in the exec() call. Refer to the various
42 /// implementations of G::Arg::setExe().
43 ///
44 /// \see G::GetOpt
45 ///
46 class G::Arg
47 {
48 public:
49 	typedef size_t size_type ;
50 
51 	Arg( int argc , char *argv[] ) ;
52 		///< Constructor taking argc/argv. Should not be used in
53 		///< a shared object or dll.
54 
55 	Arg() ;
56 		///< Default constructor for Windows. Initialise (once)
57 		///< with parse().
58 
59 	void parse( HINSTANCE hinstance , const std::string & command_line ) ;
60 		///< Windows only.
61 		///<
62 		///< Parses the given command line, splitting it up into
63 		///< an array of tokens.
64 
65 	void reparse( const std::string & command_line ) ;
66 		///< Reinitialises the object with the given command-line.
67 		///< The command-line should not contain the program
68 		///< name: the v(0) value and prefix() are unchanged.
69 
70 	~Arg() ;
71 		///< Destructor.
72 
73 	size_type c() const ;
74 		///< Returns the number of tokens in the command line,
75 		///< including the program name.
76 
77 	std::string v( size_type i ) const ;
78 		///< Returns the i'th argument.
79 		///< Precondition: i < c()
80 
81 	std::string prefix() const ;
82 		///< Returns the basename of v(0) without any extension.
83 		///< Typically used as a prefix in error messages.
84 
85 	static const char * prefix( char * argv[] ) ; // throw()
86 		///< An exception-free version of prefix() which can
87 		///< be used in main() outside of the outermost try
88 		///< block.
89 
90 	bool contains( const std::string & sw , size_type sw_args = 0U , bool case_sensitive = true ) const ;
91 		///< Returns true if the command line contains the
92 		///< given switch with enough command line arguments
93 		///< left to satisfy the given number of switch
94 		///< arguments.
95 
96 	size_type index( const std::string & sw , size_type sw_args = 0U ) const ;
97 		///< Returns the index of the given switch.
98 		///< Returns zero if not present.
99 
100 	bool remove( const std::string & sw , size_type sw_args = 0U ) ;
101 		///< Removes the given switch and its arguments.
102 		///< Returns false if the switch does not exist.
103 
104 	void removeAt( size_type sw_index , size_type sw_args = 0U ) ;
105 		///< Removes the given argument and the following
106 		///< 'sw_args' ones.
107 
108 	Arg & operator=( const Arg & ) ;
109 		///< Assignment operator.
110 
111 	Arg( const Arg & ) ;
112 		///< Copy constructor.
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
131