Fixed issue #1264: TortoiseProc might crash if commands are executed w/o a working...
[TortoiseGit.git] / src / Utils / CmdLineParser.h
blob2c1cde11405929a5da68fdd6ae1071c4a9228824
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2007 - Stefan Kueng
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.
19 #pragma once
20 #include <map>
21 #include <string>
23 using std::map;
25 #pragma warning (push,1)
26 typedef std::wstring wide_string;
27 #ifndef stdstring
28 #ifdef UNICODE
29 # define stdstring wide_string
30 #else
31 # define stdstring std::string
32 #endif
33 #endif
34 #pragma warning (pop)
36 /**
37 * \ingroup Utils
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
43 * \code
44 * "/key1 /key2:myvalue -key3:anothervalue -key4:"this could be a path with spaces"
45 * \endcode
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.
50 * Example:\n
51 * \code
52 * CCmdLineParser parser(::GetCommandLine());
53 * if (parser.HasKey("r"))
54 * {
55 * // the key -r or /r is there (could mean e.g. 'recurse')
56 * }
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
62 * \endcode
64 class CCmdLineParser
66 public:
67 typedef map<stdstring, stdstring> CValsMap;
68 typedef CValsMap::const_iterator ITERPOS;
69 public:
70 /**
71 * Creates a CCmdLineParser object and parses the parameters in.
72 * \param sCmdLine the command line
74 CCmdLineParser(LPCTSTR sCmdLine = NULL);
75 virtual ~CCmdLineParser();
77 /**
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(); }
83 /**
84 * Starts an iteration over all command line parameters.
85 * \return the first position
87 ITERPOS begin() const;
89 /**
90 * Get the next key/value pair. If no more keys are available then
91 * an empty key is returned.
92 * \param 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 the key to get the value from
132 * \return the value converted to a long
134 LONG GetLongVal(LPCTSTR sKey) const;
136 private:
137 BOOL Parse(LPCTSTR sCmdLine);
138 CValsMap::const_iterator findKey(LPCTSTR sKey) const;
139 const CValsMap& getVals() const { return m_valueMap; }
140 private:
141 stdstring m_sCmdLine;
142 CValsMap m_valueMap;
144 static const TCHAR m_sDelims[];
145 static const TCHAR m_sValueSep[];
146 static const TCHAR m_sQuotes[];