parent
d5d232fc42
commit
1b7718d420
@ -0,0 +1,30 @@
|
|||||||
|
package space.kklochko.jdbc_hospital_example.cli.parsers;
|
||||||
|
|
||||||
|
import lombok.NonNull;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.TreeMap;
|
||||||
|
|
||||||
|
public class ArgumentParser {
|
||||||
|
public Map<String, String> parse(@NonNull String args) {
|
||||||
|
Map<String, String> parsed = new LinkedHashMap<String, String>();
|
||||||
|
|
||||||
|
if(args == "") return parsed;
|
||||||
|
|
||||||
|
// remove last '
|
||||||
|
args = args.substring(0, args.length() - 1);
|
||||||
|
|
||||||
|
for(String arg : args.split("',")) {
|
||||||
|
String[] parts = arg.split("=");
|
||||||
|
parsed.put(parts[0].trim(), removeQuotes(parts[1]));
|
||||||
|
}
|
||||||
|
|
||||||
|
return parsed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String removeQuotes(@NonNull String withQuotes) {
|
||||||
|
return withQuotes.substring(1, withQuotes.length());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package space.kklochko.jdbc_hospital_example.cli.parsers;
|
||||||
|
|
||||||
|
import lombok.NonNull;
|
||||||
|
import lombok.ToString;
|
||||||
|
import space.kklochko.jdbc_hospital_example.cli.commands.CommandData;
|
||||||
|
|
||||||
|
@ToString
|
||||||
|
public class CommandParser {
|
||||||
|
public CommandData parse(@NonNull String command) {
|
||||||
|
ArgumentParser parser = new ArgumentParser();
|
||||||
|
String[] command_args = command.split("[()]");
|
||||||
|
String[] command_parts = command_args[0].split(" ");
|
||||||
|
|
||||||
|
String args="";
|
||||||
|
if(command_args.length > 1)
|
||||||
|
args = command_args[1];
|
||||||
|
|
||||||
|
return new CommandData(command_parts[0], command_parts[1], parser.parse(args));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue