Repository navigation
Expand file tree
/
Copy pathCommandProcessor.java
More file actions
115 lines (108 loc) · 3.28 KB
/
Copy pathCommandProcessor.java
File metadata and controls
115 lines (108 loc) · 3.28 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
//package
import java.util.Scanner;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class CommandProcessor {
private String commandHeader;
private int argsCount;
private int flagCount;
private HashMap<String, String> flags;
private ArrayList<String> args;
private boolean caseInsensitive;
public CommandProcessor(){
caseInsensitive = false;
}
public CommandProcessor(boolean caseInsensitive){
this.caseInsensitive = caseInsensitive;
}
public boolean process(String command) {
if(caseInsensitive) command = command.toLowerCase();
command= command.replace("\\", "\\\\");
flags = new HashMap<String, String>();
args = new ArrayList<String>();
argsCount = 0;
flagCount = 0;
Scanner sc = new Scanner(command);
if(!sc.hasNext()) return false;
commandHeader = sc.next();
if (sc.hasNextLine())
command = sc.nextLine();
else {
sc.close();
return true;
}
sc.close();
Pattern p = Pattern.compile("(-\\w+) (-\\w+)"); //"(-\w) (\S*)");
Matcher m = p.matcher(command);
while (m.find()) {
String stray = m.group(1);
command = command.replaceAll(stray, "");
flags.put(stray, "");
flagCount++;
}
p = Pattern.compile("(-\\w+) \"(\\S+)\""); //"(-\w) (\S*)");
m = p.matcher(command);
while (m.find()) {
String arg = m.group(1);
String val = m.group(2);
String search = m.group(0).replace("\\","\\\\");
command = command.replaceAll(search, "");
val = val.replace("\\\\","\\");
flags.put(arg, val);
flagCount++;
}
p = Pattern.compile("(-\\w+) (\\S+)"); //"(-\w) (\S*)");
m = p.matcher(command);
while (m.find()) {
String arg = m.group(1);
String val = m.group(2);
command = command.replaceAll(m.group(1) + " " + m.group(2), "");
flags.put(arg, val);
flagCount++;
}
p = Pattern.compile("(-\\w+)$"); //"(-\w) (\S*)");
m = p.matcher(command);
if (m.find()) {
String arg = m.group(1);
command = command.replaceAll(m.group(1), "");
flags.put(arg, "");
flagCount++;
}
sc = new Scanner(command);
while (sc.hasNext()) {
args.add(sc.next());
argsCount++;
}
sc.close();
return true;
}
public HashMap<String,String> getAllFlags(){
HashMap<String,String> bro = new HashMap<>();
bro.putAll(flags);
return bro;
}
public String getArg(int index) {
try{
return args.get(index);
}catch(IndexOutOfBoundsException e){
return null;
}
}
public int getArgsCount() {
return argsCount;
}
public String getCommand() {
return commandHeader;
}
public String getFlag(String arg) {
return flags.get(arg);
}
public boolean hasFlag(String arg) {
return !(flags.get(arg) == null);
}
public int getFlagCount() {
return flagCount;
}
}