-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCommandParser.cs
More file actions
56 lines (49 loc) · 1.62 KB
/
CommandParser.cs
File metadata and controls
56 lines (49 loc) · 1.62 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
using System.Collections.Generic;
using System.Linq;
using CommandCore.Library.Interfaces;
namespace CommandCore.Library
{
public class CommandParser : ICommandParser
{
public ParsedVerb ParseCommand(string[] arguments)
{
if (arguments == null)
{
return new ParsedVerb() {VerbName = "default"};
}
var argumentsClone = (string[]) arguments.Clone();
var parsedVerb = new ParsedVerb();
var firstArgIsVerb = argumentsClone.Length > 0 && !argumentsClone[0].StartsWith("-");
var startingPoint = 0;
if (firstArgIsVerb)
{
startingPoint = 1;
parsedVerb.VerbName = arguments[0];
}
else
{
// If there is no verb defined, we need a default verb that handles all of the coming requests.
parsedVerb.VerbName = "default";
}
if (argumentsClone.Length <= 1)
{
return parsedVerb;
}
var options = new Dictionary<string, List<string>>();
for (int i = startingPoint; i < arguments.Length; i++)
{
var arg = arguments[i];
if (arg.StartsWith("-") || arg.StartsWith("--"))
{
options.Add(arg.Trim('-'), new List<string>());
}
else
{
options[options.Keys.Last()].Add(arg);
}
}
parsedVerb.Options = options;
return parsedVerb;
}
}
}