-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathCommandLine.cs
More file actions
161 lines (142 loc) · 4.78 KB
/
CommandLine.cs
File metadata and controls
161 lines (142 loc) · 4.78 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace Oxide.Core
{
/// <summary>
/// Represents a command line string in managed form
/// </summary>
public sealed class CommandLine
{
// The flags and variables of this command line
private readonly Dictionary<string, string> variables = new Dictionary<string, string>();
/// <summary>
/// Initializes a new instance of the CommandLine class
/// </summary>
/// <param name="commandline"></param>
public CommandLine(string[] commandline)
{
string cmdline = string.Empty;
string key = string.Empty;
foreach (string str in commandline)
{
cmdline += "\"" + str.Trim('/', '\\') + "\"";
}
foreach (string str in Split(cmdline))
{
if (str.Length <= 0)
{
continue;
}
string val = str;
if (str[0] == '-' || str[0] == '+')
{
if (key != string.Empty && !variables.ContainsKey(key))
{
variables.Add(key, string.Empty);
}
key = val.Substring(1);
}
else if (key != string.Empty)
{
if (!variables.ContainsKey(key))
{
if (key.Contains("dir"))
{
val = val.Replace('/', '\\');
}
variables.Add(key, val);
}
key = string.Empty;
}
}
if (key != string.Empty && !variables.ContainsKey(key))
{
variables.Add(key, string.Empty);
}
}
/// <summary>
/// Split the commandline arguments
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string[] Split(string input)
{
input = input.Replace("\\\"", """);
MatchCollection matches = new Regex("\"([^\"]+)\"|'([^']+)'|\\S+").Matches(input);
string[] strArray = new string[matches.Count];
for (int i = 0; i < matches.Count; i++)
{
char[] trimChars = { ' ', '"' };
strArray[i] = matches[i].Groups[0].Value.Trim(trimChars);
strArray[i] = strArray[i].Replace(""", "\"");
}
return strArray;
}
/// <summary>
/// Returns if this command line has the specified variable (prefixed with +)
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public bool HasVariable(string name) => variables.Any(v => v.Key == name);
/// <summary>
/// Gets the value for the specified variable
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public string GetVariable(string name)
{
try
{
return variables.Single(v => v.Key == name).Value;
}
catch (Exception)
{
return null;
}
}
/// <summary>
/// Gets variable data for the specified argument
/// </summary>
/// <param name="var"></param>
/// <param name="varname"></param>
/// <param name="format"></param>
public void GetArgument(string var, out string varname, out string format)
{
// Format is "folder/{variable}/otherfolder"
string cmd = GetVariable(var);
StringBuilder varnamesb = new StringBuilder(), formatsb = new StringBuilder();
int invar = 0;
foreach (char c in cmd)
{
switch (c)
{
case '{':
invar++;
break;
case '}':
invar--;
if (invar == 0)
{
formatsb.Append("{0}");
}
break;
default:
if (invar == 0)
{
formatsb.Append(c);
}
else
{
varnamesb.Append(c);
}
break;
}
}
varname = varnamesb.ToString();
format = formatsb.ToString();
}
}
}