Remember "subfolder" as correctly spelled
[TortoiseGit.git] / src / Utils / CmdLineParser.h
blob1da52d1380a5dd9634396841ea6dc0d3008e9194
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2016, 2021, 2023 - 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>
24 using std::map;
26 /**
27 * \ingroup Utils
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
33 * \code
34 * "/key1 /key2:myvalue -key3:anothervalue -key4:"this could be a path with spaces"
35 * \endcode
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.
40 * Example:\n
41 * \code
42 * CCmdLineParser parser(::GetCommandLine());
43 * if (parser.HasKey("r"))
44 * {
45 * // the key -r or /r is there (could mean e.g. 'recurse')
46 * }
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
52 * \endcode
54 class CCmdLineParser
56 public:
57 using CValsMap = map<std::wstring, std::wstring>;
58 using ITERPOS = CValsMap::const_iterator;
59 public:
60 /**
61 * Creates a CCmdLineParser object and parses the parameters in.
62 * \param sCmdLine the command line
64 CCmdLineParser(LPCWSTR sCmdLine = nullptr);
66 CCmdLineParser(const CCmdLineParser&) = default;
67 CCmdLineParser& operator=(const CCmdLineParser&) = default;
68 CCmdLineParser& operator=(CCmdLineParser&& other) noexcept;
70 /**
71 * returns the command line string this object was created on.
72 * \return the command line
74 LPCWSTR getCmdLine() const { return m_sCmdLine.c_str(); }
76 /**
77 * Starts an iteration over all command line parameters.
78 * \return the first position
80 ITERPOS begin() const;
82 /**
83 * Get the next key/value pair. If no more keys are available then
84 * an empty key is returned.
85 * \param pos the position from where to get. To get the first pair use the
86 * begin() method. \a pos is incremented by 1 on return.
87 * \param sKey returns the key
88 * \param sValue returns the value
89 * \return the next position
91 ITERPOS getNext(ITERPOS& pos, std::wstring& sKey, std::wstring& sValue) const;
93 /**
94 * Checks if the position is the last or if there are more key/value pairs in the command line.
95 * \param pos the position to check
96 * \return TRUE if no more key/value pairs are available
98 BOOL isLast(const ITERPOS& pos) const;
101 * Checks if the given key is in the command line.
102 * \param sKey the key to check for
103 * \return TRUE if the key exists, FALSE if the key is not in command line
105 BOOL HasKey(LPCWSTR sKey) const;
108 * Checks if a key also has a value or not.
109 * \param sKey the key to check for a value
110 * \return TRUE if the key has a value, FALSE if no value (or no key) was found
112 BOOL HasVal(LPCWSTR sKey) const;
115 * Reads the value for a key. If the key has no value then nullptr is returned.
116 * \param sKey the key to get the value from
117 * \return the value string of the key
119 LPCWSTR GetVal(LPCWSTR sKey) const;
122 * Reads the value for a key as a long. If the value is a string which can't be
123 * converted to a number then 0 is returned.
124 * \param sKey the key to get the value from
125 * \return the value converted to a long
127 LONG GetLongVal(LPCWSTR sKey) const;
129 __int64 GetLongLongVal(LPCWSTR sKey) const;
131 private:
132 BOOL Parse(LPCWSTR sCmdLine);
133 CValsMap::const_iterator findKey(LPCWSTR sKey) const;
134 const CValsMap& getVals() const { return m_valueMap; }
135 private:
136 std::wstring m_sCmdLine;
137 CValsMap m_valueMap;
139 static const wchar_t m_sDelims[];
140 static const wchar_t m_sValueSep[];
141 static const wchar_t m_sQuotes[];