Repository navigation
Expand file tree
/
Copy pathCommandLineArg.cpp
More file actions
86 lines (76 loc) · 2.15 KB
/
Copy pathCommandLineArg.cpp
File metadata and controls
86 lines (76 loc) · 2.15 KB
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
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
// Developed by Minigraph
//
// Author: Jack Elliott
//
#include "pch.h"
#include <unordered_map>
#include <string>
#include <sstream>
namespace CommandLineArgs
{
std::unordered_map<std::wstring, std::wstring> m_argumentMap;
template<typename GetStringFunc>
void GatherArgs(size_t numArgs, const GetStringFunc& pfnGet)
{
// the first arg is always the exe name and we are looing for key/value pairs
if (numArgs > 1)
{
size_t i = 0;
while (i < numArgs)
{
const wchar_t* key = pfnGet(i);
i++;
if (i < numArgs && key[0] == '-')
{
const wchar_t* strippedKey = key + 1;
m_argumentMap[strippedKey] = pfnGet(i);
i++;
}
}
}
}
void Initialize(int argc, wchar_t ** argv)
{
GatherArgs(size_t(argc), [argv](size_t i) -> const wchar_t* { return argv[i]; });
}
template<typename FunctionType>
bool Lookup(const wchar_t* key, const FunctionType& pfnFunction)
{
const auto found = m_argumentMap.find(key);
if (found != m_argumentMap.end())
{
pfnFunction(found->second);
return true;
}
return false;
}
bool GetInteger(const wchar_t* key, uint32_t& value)
{
return Lookup(key, [&value](std::wstring& val)
{
value = std::stoi(val);
});
}
bool GetFloat(const wchar_t* key, float& value)
{
return Lookup(key, [&value](std::wstring& val)
{
value = (float)_wtof(val.c_str());
});
}
bool GetString(const wchar_t* key, std::wstring& value)
{
return Lookup(key, [&value](std::wstring& val)
{
value = val;
});
}
}