-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCLIParser.java
47 lines (41 loc) · 1.57 KB
/
CLIParser.java
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
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CLIParser
{
/*
A parser can be used to interpret a line in a CLI with this format:
command -param1 value1 -param2 "value in double quotes"
Example:
delete -path "my file.txt" -forced true
*/
/***
* Regex to for text in double quotes: "[^"\\]*(?:\\.[^"\\]*)*"
*/
private static final String STRING_PATTERN = "\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\"";
public static void main(String[] args)
{
parseAndPrintCommand("delete -path \"my file.txt\" -forced true");
Scanner scanner = new Scanner(System.in);
String inputLine;
while (scanner.hasNextLine() &&
!(inputLine = scanner.nextLine()).equalsIgnoreCase("end")) //Getting input till 'end' is not input
parseAndPrintCommand(inputLine);
}
private static void parseAndPrintCommand(String str)
{
Pattern commandPattern = Pattern.compile("^\\s*(?<command>\\w+)");
Matcher m = commandPattern.matcher(str);
if (!m.find())
{
System.out.println("Invalid input");
return;
}
System.out.println("command = " + m.group("command"));
Pattern paramsPattern = Pattern.compile("-(?<param>\\w+)\\s+(?<value>" + STRING_PATTERN + "|\\w+)");
m.usePattern(paramsPattern);
System.out.println("Parameters:");
while (m.find())
System.out.println(m.group("param") + " = " + m.group("value"));
}
}