My favorites | Sign in
Project Home Downloads Issues Source
Repository:
Checkout   Browse   Changes   Clones    
 
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#region Copyright 2008-2011 by Roger Knapp, Licensed under the Apache License, Version 2.0
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Text;

namespace CSharpTest.Net.Utils
{
/// <summary>
/// This is a private class as the means of sharing is to simply include the source file not
/// reference a library.
/// </summary>
[System.Diagnostics.DebuggerNonUserCode]
partial class ArgumentList : System.Collections.ObjectModel.KeyedCollection<string, ArgumentList.Item>
{
#region Static Configuration Options
static StringComparer _defaultCompare = StringComparer.OrdinalIgnoreCase;
static char[] _prefix = new char[] { '/', '-' };
static char[] _delim = new char[] { ':', '=' };
static readonly string[] EmptyList = new string[0];

/// <summary>
/// Controls the default string comparer used for this class
/// </summary>
public static StringComparer DefaultComparison
{
get { return _defaultCompare; }
set
{
if (value == null) throw new ArgumentNullException();
_defaultCompare = value;
}
}

/// <summary>
/// Controls the allowable prefix characters that will preceed named arguments
/// </summary>
public static char[] PrefixChars
{
get { return (char[])_prefix.Clone(); }
set
{
if (value == null) throw new ArgumentNullException();
if (value.Length == 0) throw new ArgumentOutOfRangeException();
_prefix = (char[])value.Clone();
}
}
/// <summary>
/// Controls the allowable delimeter characters seperate argument names from values
/// </summary>
public static char[] NameDelimeters
{
get { return (char[])_delim.Clone(); }
set
{
if (value == null) throw new ArgumentNullException();
if (value.Length == 0) throw new ArgumentOutOfRangeException();
_delim = (char[])value.Clone();
}
}
#endregion Static Configuration Options

readonly List<string> _unnamed;
/// <summary>
/// Initializes a new instance of the ArgumentList class using the argument list provided
/// </summary>
public ArgumentList(params string[] arguments) : this(DefaultComparison, arguments) { }
/// <summary>
/// Initializes a new instance of the ArgumentList class using the argument list provided
/// and using the string comparer provided, by default this is case-insensitive
/// </summary>
public ArgumentList(StringComparer comparer, params string[] arguments)
: base(comparer, 0)
{
_unnamed = new List<string>();
this.AddRange(arguments);
}

/// <summary>
/// Returns a list of arguments that did not start with a character in the PrefixChars
/// static collection. These arguments can be modified by the methods on the returned
/// collection, or you set this property to a new collection (a copy is made).
/// </summary>
public IList<string> Unnamed
{
get { return _unnamed; }
set
{
_unnamed.Clear();
if (value != null)
_unnamed.AddRange(value);
}
}

/// <summary>
/// Parses the strings provided for switch names and optionally values, by default in one
/// of the following forms: "/name=value", "/name:value", "-name=value", "-name:value"
/// </summary>
public void AddRange(params string[] arguments)
{
if (arguments == null) throw new ArgumentNullException();

foreach (string arg in arguments)
{
string name, value;
if (TryParseNameValue(arg, out name, out value))
Add(name, value);
else
_unnamed.Add(CleanArgument(arg));
}
}

/// <summary>
/// Adds a name/value pair to the collection of arguments, if value is null the name is
/// added with no values.
/// </summary>
public void Add(string name, string value)
{
if (name == null)
throw new ArgumentNullException();

Item item;
if (!TryGetValue(name, out item))
base.Add(item = new Item(name));

if (value != null)
item.Add(value);
}

/// <summary>
/// A string collection of all keys in the arguments
/// </summary>
public string[] Keys
{
get
{
if (Dictionary == null) return new string[0];
List<string> list = new List<string>(Dictionary.Keys);
list.Sort();
return list.ToArray();
}
}

/// <summary>
/// Returns true if the value was found by that name and set the output value
/// </summary>
public bool TryGetValue(string name, out Item value)
{
if (name == null)
throw new ArgumentNullException();

if (Dictionary != null)
return Dictionary.TryGetValue(name, out value);
value = null;
return false;
}

/// <summary>
/// Returns true if the value was found by that name and set the output value
/// </summary>
public bool TryGetValue(string name, out string value)
{
if (name == null)
throw new ArgumentNullException();

Item test;
if (Dictionary != null && Dictionary.TryGetValue(name, out test))
{
value = test.Value;
return true;
}
value = null;
return false;
}

/// <summary>
/// Returns an Item of name even if it does not exist
/// </summary>
public Item SafeGet(string name)
{
Item result;
if (TryGetValue(name, out result))
return result;
return new Item(name, null);
}

#region Protected / Private operations...

static string CleanArgument(string argument)
{
if (argument == null) throw new ArgumentNullException();
if (argument.Length >= 2 && argument[0] == '"' && argument[argument.Length - 1] == '"')
argument = argument.Substring(1, argument.Length - 2).Replace("\"\"", "\"");
return argument;
}

/// <summary>
/// Attempts to parse a name value pair from '/name=value' format
/// </summary>
public static bool TryParseNameValue(string argument, out string name, out string value)
{
argument = CleanArgument(argument);//strip quotes
name = value = null;

if (String.IsNullOrEmpty(argument) || 0 != argument.IndexOfAny(_prefix, 0, 1))
return false;

name = argument.Substring(1);
if (String.IsNullOrEmpty(name))
return false;

int endName = name.IndexOfAny(_delim, 1);

if (endName > 0)
{
value = name.Substring(endName + 1);
name = name.Substring(0, endName);
}

return true;
}

/// <summary>
/// Searches the arguments until it finds a switch or value by the name in find and
/// if found it will:
/// A) Remove the item from the arguments
/// B) Set the out parameter value to any value found, or null if just '/name'
/// C) Returns true that it was found and removed.
/// </summary>
public static bool Remove(ref string[] arguments, string find, out string value)
{
value = null;
for (int i = 0; i < arguments.Length; i++)
{
string name, setting;
if (TryParseNameValue(arguments[i], out name, out setting) &&
_defaultCompare.Equals(name, find))
{
List<string> args = new List<string>(arguments);
args.RemoveAt(i);
arguments = args.ToArray();
value = setting;
return true;
}
}
return false;
}

/// <summary>
/// Abract override for extracting key
/// </summary>
protected override string GetKeyForItem(ArgumentList.Item item)
{
return item.Name;
}

#endregion

#region Item class used for collection
/// <summary>
/// This is a single named argument within an argument list collection, this
/// can be implicitly assigned to a string, or a string[] array
/// </summary>
[System.Diagnostics.DebuggerNonUserCode]
public class Item : System.Collections.ObjectModel.Collection<string>
{
private readonly string _name;
private readonly List<string> _values;

/// <summary>
/// Constructs an item for the name and values provided.
/// </summary>
public Item(string name, params string[] items)
: this(new List<string>(), name, items) { }

private Item(List<string> impl, string name, string[] items)
: base(impl)
{
if (name == null)
throw new ArgumentNullException();

_name = name;
_values = impl;
if (items != null)
_values.AddRange(items);
}

/// <summary>
/// Returns the name of this item
/// </summary>
public string Name { get { return _name; } }

/// <summary>
/// Returns the first value of this named item or null if one doesn't exist
/// </summary>
public string Value
{
get { return _values.Count > 0 ? _values[0] : null; }
set
{
_values.Clear();
if (value != null)
_values.Add(value);
}
}

/// <summary>
/// Returns the collection of items in this named slot
/// </summary>
public string[] Values
{
get { return _values.ToArray(); }
set
{
_values.Clear();
if (value != null)
_values.AddRange(value);
}
}

/// <summary>
/// Same as the .Values property, returns the collection of items in this named slot
/// </summary>
/// <returns></returns>
public string[] ToArray() { return _values.ToArray(); }
/// <summary>
/// Add one or more values to this named item
/// </summary>
public void AddRange(IEnumerable<string> items) { _values.AddRange(items); }

/// <summary>
/// Converts this item to key-value pair to rem to a dictionary
/// </summary>
public static implicit operator KeyValuePair<string, string[]>(Item item)
{
if (item == null) throw new ArgumentNullException();
return new KeyValuePair<string, string[]>(item.Name, item.Values);
}

/// <summary>
/// Converts this item to a string by getting the first value or null if none
/// </summary>
public static implicit operator string(Item item) { return item == null ? null : item.Value; }

/// <summary>
/// Converts this item to array of strings
/// </summary>
public static implicit operator string[](Item item) { return item == null ? null : item.Values; }
}

#endregion Item class used for collection

private class ArgReader
{
const char CharEmpty = (char)0;
char[] _chars;
int _pos;
public ArgReader(string data)
{
_chars = data.ToCharArray();
_pos = 0;
}

public bool MoveNext() { _pos++; return _pos < _chars.Length; }
public char Current { get { return (_pos < _chars.Length) ? _chars[_pos] : CharEmpty; } }
public bool IsWhiteSpace { get { return Char.IsWhiteSpace(Current); } }
public bool IsQuote { get { return (Current == '"'); } }
public bool IsEOF { get { return _pos >= _chars.Length; } }
}

/// <summary> Parses the individual arguments from the given input string. </summary>
public static string[] Parse(string rawtext)
{
List<String> list = new List<string>();
if (rawtext == null)
throw new ArgumentNullException("rawtext");
ArgReader characters = new ArgReader(rawtext.Trim());

while (!characters.IsEOF)
{
if (characters.IsWhiteSpace)
{
characters.MoveNext();
continue;
}

StringBuilder sb = new StringBuilder();

if (characters.IsQuote)
{//quoted string
while (characters.MoveNext())
{
if (characters.IsQuote)
{
if (!characters.MoveNext() || characters.IsWhiteSpace)
break;
}
sb.Append(characters.Current);
}
}
else
{
sb.Append(characters.Current);
while (characters.MoveNext())
{
if (characters.IsWhiteSpace)
break;
sb.Append(characters.Current);
}
}

list.Add(sb.ToString());
}
return list.ToArray();
}

/// <summary> The inverse of Parse, joins the arguments together and properly escapes output </summary>
public static string Join(params string[] arguments)
{
if (arguments == null)
throw new ArgumentNullException("arguments");
char[] escaped = " \t\"&()[]{}^=;!'+,`~".ToCharArray();

StringBuilder sb = new StringBuilder();
foreach (string argument in arguments)
{
string arg = argument;

if( arg.IndexOfAny(escaped) >= 0 )
sb.AppendFormat("\"{0}\"", arg.Replace("\"", "\"\""));
else
sb.Append(arg);

sb.Append(' ');
}

return sb.ToString(0, Math.Max(0, sb.Length-1));
}
}
}

Change log

59104bd26e63 by rogerk on Apr 26, 2011   Diff
Release version 1.11.426.305
Go to: 
Project members, sign in to write a code review

Older revisions

a9af4ee4f5f9 by Grigand on Oct 24, 2010   Diff
*v1.10.1024.336*

  # Added
CSharpTest.Net.Generators.exe to
integrate with the CmdTool's VS
...
d55997422b7a by Grigand on Apr 20, 2010   Diff
Release version 1.10.420.164
f098da8efab8 by grigand on Oct 4, 2009   Diff
Released version 1.9.1004.144

Major updates include:
1. Removed dependency from Library to
Logging so that they can be
...
All revisions of this file

File info

Size: 13198 bytes, 453 lines
Powered by Google Project Hosting