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