-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCmdLine.h
More file actions
120 lines (96 loc) · 3.01 KB
/
Copy pathCmdLine.h
File metadata and controls
120 lines (96 loc) · 3.01 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
///----------------------------------------------------------------------------------------------------
/// Copyright (c) Raidcore.GG - All rights reserved.
///
/// Name : CmdLine.h
/// Description : Contains functions for the commandline.
/// Authors : K. Bieniek
///----------------------------------------------------------------------------------------------------
#pragma once
#include <shellapi.h>
#include <string>
#include <vector>
#include <windows.h>
#include "Strings.h"
///----------------------------------------------------------------------------------------------------
/// CmdLine Namespace
///----------------------------------------------------------------------------------------------------
namespace CmdLine
{
///----------------------------------------------------------------------------------------------------
/// Parse:
/// Returns all the arguments.
///----------------------------------------------------------------------------------------------------
inline std::vector<std::string> Parse()
{
static bool s_Parsed = false;
static std::vector<std::string> s_Arguments{};
if (s_Parsed)
{
return s_Arguments;
}
std::vector<std::string> args; // extra vector to return value arguments merged
int argc;
LPWSTR* argv = CommandLineToArgvW(GetCommandLineW(), &argc);
for (int i = 0; i < argc; i++)
{
std::wstring paramW = argv[i];
std::string token = String::ToString(paramW);
s_Arguments.push_back(token);
if (String::StartsWith(token, "-"))
{
args.push_back(token);
}
else
{
if (args.size() >= 1)
{
args[args.size() - 1].append(" " + token);
}
else
{
args.push_back(token);
}
}
}
s_Parsed = true;
return args;
}
///----------------------------------------------------------------------------------------------------
/// HasArgument:
/// Returns true if the commandline contains the given argument.
///----------------------------------------------------------------------------------------------------
inline bool HasArgument(const std::string& aArgument)
{
std::vector<std::string> args = Parse();
std::string arg = String::ToLower(aArgument);
for (const std::string& str : args)
{
std::string cmp = String::ToLower(str);
if (arg == cmp) { return true; }
}
return false;
}
///----------------------------------------------------------------------------------------------------
/// GetArgumentValue:
/// Returns the value following the given argument.
///----------------------------------------------------------------------------------------------------
inline std::string GetArgumentValue(const std::string& aArgument)
{
std::vector<std::string> args = Parse();
std::string arg = String::ToLower(aArgument);
for (size_t i = 0; i < args.size(); i++)
{
std::string cmp = String::ToLower(args[i]);
if (arg == cmp)
{
if (i < args.size() - 1)
{
const std::string& argVal = args[i + 1];
return argVal;
}
break;
}
}
return "";
}
}