1 // TortoiseSVN - a Windows shell extension for easy version control
\r
3 // Copyright (C) 2003-2007 - Stefan Kueng
\r
5 // This program is free software; you can redistribute it and/or
\r
6 // modify it under the terms of the GNU General Public License
\r
7 // as published by the Free Software Foundation; either version 2
\r
8 // of the License, or (at your option) any later version.
\r
10 // This program is distributed in the hope that it will be useful,
\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 // GNU General Public License for more details.
\r
15 // You should have received a copy of the GNU General Public License
\r
16 // along with this program; if not, write to the Free Software Foundation,
\r
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
\r
25 #pragma warning (push,1)
\r
26 typedef std::wstring wide_string;
\r
29 # define stdstring wide_string
\r
31 # define stdstring std::string
\r
34 #pragma warning (pop)
\r
39 * A helper class for parsing command lines.
\r
40 * It provides methods to extract 'key' and 'value'
\r
41 * pairs of the form -keyname:value or /keyname:value.
\r
42 * Parameter examples:\n
\r
44 * "/key1 /key2:myvalue -key3:anothervalue -key4:"this could be a path with spaces"
\r
46 * /key is the same as -key\n
\r
47 * all keys and values are case-insensitive.
\r
48 * Please note that both keys and values are strings although the class
\r
49 * provides a method to get a long as a value.
\r
52 * CCmdLineParser parser(::GetCommandLine());
\r
53 * if (parser.HasKey("r"))
\r
55 * // the key -r or /r is there (could mean e.g. 'recurse')
\r
57 * //now assume the command line is /open:"c:\test.txt" /wait:30
\r
58 * CString file = parser.GetVal("open");
\r
59 * //file contains now c:\test.txt
\r
60 * long number = parser.GetLongVal("seconds");
\r
61 * //number has now the value 30
\r
64 class CCmdLineParser
\r
67 typedef map<stdstring, stdstring> CValsMap;
\r
68 typedef CValsMap::const_iterator ITERPOS;
\r
71 * Creates a CCmdLineParser object and parses the parameters in.
\r
72 * \param sCmdLine the command line
\r
74 CCmdLineParser(LPCTSTR sCmdLine = NULL);
\r
75 virtual ~CCmdLineParser();
\r
78 * returns the command line string this object was created on.
\r
79 * \return the command line
\r
81 LPCTSTR getCmdLine() const { return m_sCmdLine.c_str(); }
\r
84 * Starts an iteration over all command line parameters.
\r
85 * \return the first position
\r
87 ITERPOS begin() const;
\r
90 * Get the next key/value pair. If no more keys are available then
\r
91 * an empty key is returned.
\r
92 * \param the position from where to get. To get the first pair use the
\r
93 * begin() method. \a pos is incremented by 1 on return.
\r
94 * \param sKey returns the key
\r
95 * \param sValue returns the value
\r
96 * \return the next position
\r
98 ITERPOS getNext(ITERPOS& pos, stdstring& sKey, stdstring& sValue) const;
\r
101 * Checks if the position is the last or if there are more key/value pairs in the command line.
\r
102 * \param pos the position to check
\r
103 * \return TRUE if no more key/value pairs are available
\r
105 BOOL isLast(const ITERPOS& pos) const;
\r
108 * Checks if the given key is in the command line.
\r
109 * \param sKey the key to check for
\r
110 * \return TRUE if the key exists, FALSE if the key is not in command line
\r
112 BOOL HasKey(LPCTSTR sKey) const;
\r
115 * Checks if a key also has a value or not.
\r
116 * \param sKey the key to check for a value
\r
117 * \return TRUE if the key has a value, FALSE if no value (or no key) was found
\r
119 BOOL HasVal(LPCTSTR sKey) const;
\r
122 * Reads the value for a key. If the key has no value then NULL is returned.
\r
123 * \param sKey the key to get the value from
\r
124 * \return the value string of the key
\r
126 LPCTSTR GetVal(LPCTSTR sKey) const;
\r
129 * Reads the value for a key as a long. If the value is a string which can't be
\r
130 * converted to a number then 0 is returned.
\r
131 * \param the key to get the value from
\r
132 * \return the value converted to a long
\r
134 LONG GetLongVal(LPCTSTR sKey) const;
\r
137 BOOL Parse(LPCTSTR sCmdLine);
\r
138 CValsMap::const_iterator findKey(LPCTSTR sKey) const;
\r
139 const CValsMap& getVals() const { return m_valueMap; }
\r
141 stdstring m_sCmdLine;
\r
142 CValsMap m_valueMap;
\r
144 static const TCHAR m_sDelims[];
\r
145 static const TCHAR m_sValueSep[];
\r
146 static const TCHAR m_sQuotes[];
\r