-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandParser.cs
More file actions
78 lines (65 loc) · 2.44 KB
/
CommandParser.cs
File metadata and controls
78 lines (65 loc) · 2.44 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
using System;
using System.Collections.Generic;
namespace maple
{
static class CommandParser
{
public struct CommandInfo
{
public String PrimaryCommand { get; set; }
public List<String> Args { get; set; }
public List<String> Switches { get; set; }
public CommandInfo(String primaryCommand, List<String> args, List<String> switches)
{
PrimaryCommand = primaryCommand;
Args = args;
Switches = switches;
}
}
public static CommandInfo Parse(String input, String defaultPrimaryCommand = "", bool combineArgs = false)
{
String[] commands = input.Split(" ");
bool setPrimaryCommand = false;
if(defaultPrimaryCommand != "")
setPrimaryCommand = true;
bool inQuoteBlock = false;
String primaryCommand = defaultPrimaryCommand;
List<String> commandArgs = new List<String>();
List<String> commandSwitches = new List<String>();
foreach(String s in commands)
{
//first string is primary command
if(!setPrimaryCommand)
{
primaryCommand = s;
setPrimaryCommand = true;
continue;
}
//switches start with at least one '-'
if(s.StartsWith("-"))
{
commandSwitches.Add(s);
continue;
}
//nothing has been triggered, its an argument
if(!inQuoteBlock && !combineArgs)
commandArgs.Add(s); //append to list if not in quote block
else
{
if(commandArgs.Count > 0)
commandArgs[commandArgs.Count - 1] += " " + s; //append to last item if in quote block
else
commandArgs.Add(s);
}
//determine if it starts / ends a quote block
char[] commandChars = s.ToCharArray();
foreach(char c in commandChars)
{
if(c == '\"') //toggle for each quote found
inQuoteBlock = !inQuoteBlock;
}
}
return new CommandInfo(primaryCommand, commandArgs, commandSwitches);
}
}
}