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.
29 * A helper class for parsing command lines.
30 * It provides methods to extract 'key' and 'value'
31 * pairs of the form -keyname:value or /keyname:value.
32 * Parameter examples:\n
34 * "/key1 /key2:myvalue -key3:anothervalue -key4:"this could be a path with spaces"
36 * /key is the same as -key\n
37 * all keys and values are case-insensitive.
38 * Please note that both keys and values are strings although the class
39 * provides a method to get a long as a value.
42 * CCmdLineParser parser(::GetCommandLine());
43 * if (parser.HasKey("r"))
45 * // the key -r or /r is there (could mean e.g. 'recurse')
47 * //now assume the command line is /open:"c:\test.txt" /wait:30
48 * CString file = parser.GetVal("open");
49 * //file contains now c:\test.txt
50 * long number = parser.GetLongVal("seconds");
51 * //number has now the value 30
57 typedef map
<tstring
, tstring
> CValsMap
;
58 typedef CValsMap::const_iterator ITERPOS
;
61 * Creates a CCmdLineParser object and parses the parameters in.
62 * \param sCmdLine the command line
64 CCmdLineParser(LPCTSTR sCmdLine
= nullptr);
65 virtual ~CCmdLineParser();
68 * returns the command line string this object was created on.
69 * \return the command line
71 LPCTSTR
getCmdLine() const { return m_sCmdLine
.c_str(); }
74 * Starts an iteration over all command line parameters.
75 * \return the first position
77 ITERPOS
begin() const;
80 * Get the next key/value pair. If no more keys are available then
81 * an empty key is returned.
82 * \param pos the position from where to get. To get the first pair use the
83 * begin() method. \a pos is incremented by 1 on return.
84 * \param sKey returns the key
85 * \param sValue returns the value
86 * \return the next position
88 ITERPOS
getNext(ITERPOS
& pos
, tstring
& sKey
, tstring
& sValue
) const;
91 * Checks if the position is the last or if there are more key/value pairs in the command line.
92 * \param pos the position to check
93 * \return TRUE if no more key/value pairs are available
95 BOOL
isLast(const ITERPOS
& pos
) const;
98 * Checks if the given key is in the command line.
99 * \param sKey the key to check for
100 * \return TRUE if the key exists, FALSE if the key is not in command line
102 BOOL
HasKey(LPCTSTR sKey
) const;
105 * Checks if a key also has a value or not.
106 * \param sKey the key to check for a value
107 * \return TRUE if the key has a value, FALSE if no value (or no key) was found
109 BOOL
HasVal(LPCTSTR sKey
) const;
112 * Reads the value for a key. If the key has no value then nullptr is returned.
113 * \param sKey the key to get the value from
114 * \return the value string of the key
116 LPCTSTR
GetVal(LPCTSTR sKey
) const;
119 * Reads the value for a key as a long. If the value is a string which can't be
120 * converted to a number then 0 is returned.
121 * \param sKey the key to get the value from
122 * \return the value converted to a long
124 LONG
GetLongVal(LPCTSTR sKey
) const;
126 __int64
GetLongLongVal(LPCTSTR sKey
) const;
129 BOOL
Parse(LPCTSTR sCmdLine
);
130 CValsMap::const_iterator
findKey(LPCTSTR sKey
) const;
131 const CValsMap
& getVals() const { return m_valueMap
; }
136 static const TCHAR m_sDelims
[];
137 static const TCHAR m_sValueSep
[];
138 static const TCHAR m_sQuotes
[];