Disable unsupported commands
[TortoiseGit.git] / src / Utils / CmdLineParser.h
blob219a98cb205a9d405fc7989a4f82b13ea1b4f0b6
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2016 - TortoiseGit
4 // Copyright (C) 2003-2007, 2009, 2013 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #pragma once
21 #include <map>
22 #include <string>
23 #include "tstring.h"
25 using std::map;
27 /**
28 * \ingroup Utils
30 * A helper class for parsing command lines.
31 * It provides methods to extract 'key' and 'value'
32 * pairs of the form -keyname:value or /keyname:value.
33 * Parameter examples:\n
34 * \code
35 * "/key1 /key2:myvalue -key3:anothervalue -key4:"this could be a path with spaces"
36 * \endcode
37 * /key is the same as -key\n
38 * all keys and values are case-insensitive.
39 * Please note that both keys and values are strings although the class
40 * provides a method to get a long as a value.
41 * Example:\n
42 * \code
43 * CCmdLineParser parser(::GetCommandLine());
44 * if (parser.HasKey("r"))
45 * {
46 * // the key -r or /r is there (could mean e.g. 'recurse')
47 * }
48 * //now assume the command line is /open:"c:\test.txt" /wait:30
49 * CString file = parser.GetVal("open");
50 * //file contains now c:\test.txt
51 * long number = parser.GetLongVal("seconds");
52 * //number has now the value 30
53 * \endcode
55 class CCmdLineParser
57 public:
58 typedef map<std::wstring, std::wstring> CValsMap;
59 typedef CValsMap::const_iterator ITERPOS;
60 public:
61 /**
62 * Creates a CCmdLineParser object and parses the parameters in.
63 * \param sCmdLine the command line
65 CCmdLineParser(LPCTSTR sCmdLine = nullptr);
67 CCmdLineParser(const CCmdLineParser&) = default;
68 CCmdLineParser& operator=(const CCmdLineParser&) = default;
69 CCmdLineParser& operator=(CCmdLineParser&& other);
71 /**
72 * returns the command line string this object was created on.
73 * \return the command line
75 LPCTSTR getCmdLine() const { return m_sCmdLine.c_str(); }
77 /**
78 * Starts an iteration over all command line parameters.
79 * \return the first position
81 ITERPOS begin() const;
83 /**
84 * Get the next key/value pair. If no more keys are available then
85 * an empty key is returned.
86 * \param pos the position from where to get. To get the first pair use the
87 * begin() method. \a pos is incremented by 1 on return.
88 * \param sKey returns the key
89 * \param sValue returns the value
90 * \return the next position
92 ITERPOS getNext(ITERPOS& pos, std::wstring& sKey, std::wstring& sValue) const;
94 /**
95 * Checks if the position is the last or if there are more key/value pairs in the command line.
96 * \param pos the position to check
97 * \return TRUE if no more key/value pairs are available
99 BOOL isLast(const ITERPOS& pos) const;
102 * Checks if the given key is in the command line.
103 * \param sKey the key to check for
104 * \return TRUE if the key exists, FALSE if the key is not in command line
106 BOOL HasKey(LPCTSTR sKey) const;
109 * Checks if a key also has a value or not.
110 * \param sKey the key to check for a value
111 * \return TRUE if the key has a value, FALSE if no value (or no key) was found
113 BOOL HasVal(LPCTSTR sKey) const;
116 * Reads the value for a key. If the key has no value then nullptr is returned.
117 * \param sKey the key to get the value from
118 * \return the value string of the key
120 LPCTSTR GetVal(LPCTSTR sKey) const;
123 * Reads the value for a key as a long. If the value is a string which can't be
124 * converted to a number then 0 is returned.
125 * \param sKey the key to get the value from
126 * \return the value converted to a long
128 LONG GetLongVal(LPCTSTR sKey) const;
130 __int64 GetLongLongVal(LPCTSTR sKey) const;
132 private:
133 BOOL Parse(LPCTSTR sCmdLine);
134 CValsMap::const_iterator findKey(LPCTSTR sKey) const;
135 const CValsMap& getVals() const { return m_valueMap; }
136 private:
137 std::wstring m_sCmdLine;
138 CValsMap m_valueMap;
140 static const TCHAR m_sDelims[];
141 static const TCHAR m_sValueSep[];
142 static const TCHAR m_sQuotes[];