-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConsoleCommand.cs
More file actions
71 lines (61 loc) · 2.38 KB
/
ConsoleCommand.cs
File metadata and controls
71 lines (61 loc) · 2.38 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
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace NopCommand
{
public class ConsoleCommand
{
public ConsoleCommand(string input)
{
// Ugly regex to split string on spaces, but preserve quoted text intact:
var stringArray =
Regex.Split(input, "(?<=^[^\"]*(?:\"[^\"]*\"[^\"]*)*) (?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
_arguments = new List<string>();
for (var i = 0; i < stringArray.Length; i++)
{
// The first element is always the command:
if (i == 0)
{
Name = stringArray[i];
// Set the default:
LibraryClassName = "DataCommands";
var s = stringArray[0].Split('.');
if (s.Length == 2)
{
LibraryClassName = s[0];
Name = s[1];
}
}
else
{
var inputArgument = stringArray[i];
// Assume that most of the time, the input argument is NOT quoted text:
var argument = inputArgument;
// Is the argument a quoted text string?
var regex = new Regex("\"(.*?)\"", RegexOptions.Singleline);
var match = regex.Match(inputArgument);
// If it IS quoted, there will be at least one capture:
if (match.Captures.Count > 0)
{
// Get the unquoted text from within the qoutes:
var captureQuotedText = new Regex("[^\"]*[^\"]");
var quoted = captureQuotedText.Match(match.Captures[0].Value);
// The argument should include all text from between the quotes
// as a single string:
argument = quoted.Captures[0].Value;
}
_arguments.Add(argument);
}
}
}
public string Name { get; set; }
public string LibraryClassName { get; set; }
private List<string> _arguments;
public IEnumerable<string> Arguments
{
get
{
return _arguments;
}
}
}
}