-
Notifications
You must be signed in to change notification settings - Fork 176
/
CmdLineParser.h
91 lines (73 loc) · 2.98 KB
/
CmdLineParser.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// CmdLineParser.h: interface for the CCmdLineParser class.
//
// Copyright (c) Pavel Antonov, 2002
//
// This code is provided "as is", with absolutely no warranty expressed
// or implied. Any use is at your own risk.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_CMDLINEPARSER_H__BE51B7B0_4BC9_44F1_9B88_DF33BE4280DF__INCLUDED_)
#define AFX_CMDLINEPARSER_H__BE51B7B0_4BC9_44F1_9B88_DF33BE4280DF__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
typedef CString CCmdLineParser_String ;
#include <map>
// Example of strings
// /Key1 /Key2 -Key3:Val3 -Key4:"Val 4-with/spaces/and-delimeters" /Key5:Val5
// /Key:"" is equal to /Key: and is equal to /Key
// /Key is equal to -Key
// If getCaseSensitive is false (by default), all keys are made lowercase.
// Examples of use:
// CCmdLineParser parser(_T("/Key1 /Key2: -Key3:Val3 -Key4:\"Val 4-with/spaces/and-delimeters\" /Key5:Val5"));
// ASSERT(parser.HasKey("Key1") == true);
// ASSERT(parser.HasKey("Key10") == false);
// ASSERT(parser.HasVal("Key2") == false);
// parser.GetVal("Key1") -> []; (empty string)
// parser.GetVal("Key2") -> []; (empty string)
// parser.GetVal("Key3") -> [Val3];
// parser.GetVal("Key4") -> [Val 4-with/spaces/and-delimeters];
// CCmdLineParser::POSITION pos = parser.getFirst();
// CString sKey, sVal;
// while(!parser.isLast(pos)) {
// parser.getNext(pos, sKey, sVal);
// printf("Key: [%s], Val: [%s]");
// }
class CCmdLineParser {
public:
class CValsMap : public std::map<CCmdLineParser_String, CCmdLineParser_String> {};
typedef CValsMap::const_iterator POSITION;
public:
CCmdLineParser(LPCTSTR sCmdLine = NULL, bool bCaseSensitive = false);
virtual ~CCmdLineParser();
bool Parse(LPCTSTR sCmdLine);
LPCTSTR getCmdLine() const { return m_sCmdLine; }
void setCaseSensitive(bool bSensitive) { m_bCaseSensitive = bSensitive; }
bool getCaseSensitive() const { return m_bCaseSensitive; }
const CValsMap& getVals() const { return m_ValsMap; }
// Start iterating through keys and values
POSITION getFirst() const;
// Get next key-value pair, returns empty sKey if end reached
POSITION getNext(POSITION& pos, CCmdLineParser_String& sKey, CCmdLineParser_String& sValue) const;
// just helper ;)
bool isLast(POSITION& pos) const;
// TRUE if "Key" present in command line
bool HasKey(LPCTSTR sKey) const;
// Is "key" present in command line and have some value
bool HasVal(LPCTSTR sKey) const;
// Returns value if value was found or NULL otherwise
LPCTSTR GetVal(LPCTSTR sKey) const;
// Returns true if value was found
bool GetVal(LPCTSTR sKey, CCmdLineParser_String& sValue) const;
void SetVal(LPCTSTR sKey, LPCTSTR val);
private:
CValsMap::const_iterator findKey(LPCTSTR sKey) const;
private:
CCmdLineParser_String m_sCmdLine;
CValsMap m_ValsMap;
bool m_bCaseSensitive;
static const TCHAR m_sDelimeters[];
static const TCHAR m_sValueSep[];
static const TCHAR m_sQuotes[];
};
#endif // !defined(AFX_CMDLINEPARSER_H__BE51B7B0_4BC9_44F1_9B88_DF33BE4280DF__INCLUDED_)