forked from JeremyDurnell/locbaml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommandline.cs
More file actions
250 lines (213 loc) · 9.18 KB
/
commandline.cs
File metadata and controls
250 lines (213 loc) · 9.18 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//---------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Description: LocBaml command line parsing tool.
//
//---------------------------------------------------------------------------
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Reflection.Emit;
using System.Collections;
using System.Globalization;
namespace BamlLocalization
{
internal class Option
{
private String m_strName;
private String m_strValue;
public Option(String strName, String strValue)
{
m_strName = strName;
m_strValue = strValue;
}
public String Name { get { return m_strName; } }
public String Value { get { return m_strValue; } }
}
internal class Abbrevs
{
private String[] m_aOptions;
private bool[] m_bRequiresValue;
private bool[] m_bCanHaveValue;
public Abbrevs(String[] aOptions)
{
m_aOptions = new String[aOptions.Length];
m_bRequiresValue = new bool[aOptions.Length];
m_bCanHaveValue = new bool[aOptions.Length];
// Store option list in lower case for canonical comparison.
for (int i = 0; i < aOptions.Length; i++)
{
String strOption = aOptions[i].ToLower(CultureInfo.InvariantCulture);
// A leading '*' implies the option requires a value
// (the '*' itself is not stored in the option name).
if (strOption.StartsWith("*"))
{
m_bRequiresValue[i] = true;
m_bCanHaveValue[i] = true;
strOption = strOption.Substring(1);
}
else if (strOption.StartsWith("+"))
{
m_bRequiresValue[i] = false;
m_bCanHaveValue[i] = true;
strOption = strOption.Substring(1);
}
m_aOptions[i] = strOption;
}
}
public String Lookup(String strOpt, out bool bRequiresValue, out bool bCanHaveValue)
{
String strOptLower = strOpt.ToLower(CultureInfo.InvariantCulture);
int i;
bool bMatched = false;
int iMatch = -1;
// Compare option to stored list.
for (i = 0; i < m_aOptions.Length; i++)
{
// Exact matches always cause immediate termination of
// the search
if (strOptLower.Equals(m_aOptions[i]))
{
bRequiresValue = m_bRequiresValue[i];
bCanHaveValue = m_bCanHaveValue[i];
return m_aOptions[i];
}
// Check for potential match (the input word is a prefix
// of the current stored option).
if (m_aOptions[i].StartsWith(strOptLower))
{
// If we've already seen a prefix match then the
// input word is ambiguous.
if (bMatched)
throw new ArgumentException(StringLoader.Get("Err_AmbigousOption", strOpt));
// Remember this partial match.
bMatched = true;
iMatch = i;
}
}
// If we get here with bMatched set, we saw one and only one
// partial match, so we've got a winner.
if (bMatched)
{
bRequiresValue = m_bRequiresValue[iMatch];
bCanHaveValue = m_bCanHaveValue[iMatch];
return m_aOptions[iMatch];
}
// Else the word doesn't match at all.
throw new ArgumentException(StringLoader.Get("Err_UnknownOption", strOpt));
}
}
internal class CommandLine
{
private String[] m_aArgList;
private Option[] m_aOptList;
private int m_iArgCursor;
private int m_iOptCursor;
private Abbrevs m_sValidOptions;
public CommandLine(String[] aArgs, String[] aValidOpts)
{
int i, iArg, iOpt;
// Keep a list of valid option names.
m_sValidOptions = new Abbrevs(aValidOpts);
// Temporary lists of raw arguments and options and their
// associated values.
String[] aArgList = new String[aArgs.Length];
Option[] aOptList = new Option[aArgs.Length];
// Reset counters of raw arguments and option/value pairs found
// so far.
iArg = 0;
iOpt = 0;
// Iterate through words of command line.
for (i = 0; i < aArgs.Length; i++)
{
// Check for option or raw argument.
if (aArgs[i].StartsWith("/") ||
aArgs[i].StartsWith("-"))
{
String strOpt;
String strVal = null;
bool bRequiresValue;
bool bCanHaveValue;
// It's an option. Strip leading '/' or '-' and
// anything after a value separator (':' or
// '=').
int iColon = aArgs[i].IndexOfAny(new char[] {':', '='});
if (iColon == -1)
strOpt = aArgs[i].Substring(1);
else
strOpt = aArgs[i].Substring(1, iColon - 1);
// Look it up in the table of valid options (to
// check it exists, get the full option name and
// to see if an associated value is expected).
strOpt = m_sValidOptions.Lookup(strOpt, out bRequiresValue, out bCanHaveValue);
// Check that the user hasn't specified a value separator for an option
// that doesn't take a value.
if (!bCanHaveValue && (iColon != -1))
throw new ApplicationException(StringLoader.Get("Err_NoValueRequired", strOpt));
// Check that the user has put a colon if the option requires a value.
if (bRequiresValue && (iColon == -1))
throw new ApplicationException(StringLoader.Get("Err_ValueRequired", strOpt));
// Go look for a value if there is one.
if (bCanHaveValue && iColon != -1)
{
if (iColon == (aArgs[i].Length - 1))
{
// No value separator, or
// separator is at end of
// option; look for value in
// next command line arg.
if (i + 1 == aArgs.Length)
{
throw new ApplicationException(StringLoader.Get("Err_ValueRequired", strOpt));
}
else
{
if ((aArgs[i + 1].StartsWith( "/" ) || aArgs[i + 1].StartsWith( "-" )))
throw new ApplicationException(StringLoader.Get("Err_ValueRequired", strOpt));
strVal = aArgs[i+1];
i++;
}
}
else
{
// Value is in same command line
// arg as the option, substring
// it out.
strVal = aArgs[i].Substring(iColon + 1);
}
}
// Build the option value pair.
aOptList[iOpt++] = new Option(strOpt, strVal);
}
else
{
// Command line word is a raw argument.
aArgList[iArg++] = aArgs[i];
}
}
// Allocate the non-temporary arg and option lists at exactly
// the right size.
m_aArgList = new String[iArg];
m_aOptList = new Option[iOpt];
// Copy in the values we've calculated.
Array.Copy(aArgList, m_aArgList, iArg);
Array.Copy(aOptList, m_aOptList, iOpt);
}
public int NumArgs { get { return m_aArgList.Length; } }
public int NumOpts { get { return m_aOptList.Length; } }
public String GetNextArg()
{
if (m_iArgCursor >= m_aArgList.Length)
return null;
return m_aArgList[m_iArgCursor++];
}
public Option GetNextOption()
{
if (m_iOptCursor >= m_aOptList.Length)
return null;
return m_aOptList[m_iOptCursor++];
}
}
}