Created
February 11, 2024 01:22
-
-
Save WizardlyBump17/b03f164159047e6ce39514341bbc1c19 to your computer and use it in GitHub Desktop.
a command parser sytem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import lombok.NonNull; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) { | |
try (Scanner scanner = new Scanner(System.in)) { | |
while (scanner.hasNextLine()) { | |
String line = scanner.nextLine(); | |
List<String> arguments = getArguments(line); | |
System.out.println(arguments); | |
} | |
} | |
} | |
public static @NonNull List<String> getArguments(@NonNull String command) { | |
String[] split = command.split(" "); | |
int splitLength = split.length; | |
List<String> arguments = new ArrayList<>(splitLength); | |
StringBuilder stringBuilder = new StringBuilder(); | |
for (int i = 0; i < splitLength; i++) { | |
String string = split[i]; | |
int length = string.length(); | |
if (length == 0) | |
continue; | |
if (string.charAt(0) == '"') { //possible start | |
if (length == 1) { //only a quote | |
if (i + 1 < splitLength) { //there are more strings | |
if (stringBuilder.isEmpty()) { | |
stringBuilder.append('"'); | |
continue; | |
} | |
arguments.add(stringBuilder.substring(1).trim()); | |
stringBuilder.setLength(0); | |
continue; | |
} | |
//no more strings | |
if (stringBuilder.isEmpty()) { | |
arguments.add("\""); //only a quote | |
continue; | |
} | |
arguments.add(stringBuilder.substring(1).trim()); //end of the string | |
stringBuilder.setLength(0); | |
continue; | |
} | |
if (string.charAt(length - 1) == '"') { | |
if (length >= 3 && string.charAt(length - 2) == '\\') { //escaped quote | |
stringBuilder.append(string, 0, length - 2).append(' '); | |
continue; | |
} | |
//end of the string | |
stringBuilder.append(string); | |
arguments.add(stringBuilder.substring(1, stringBuilder.length() - 1)); | |
stringBuilder.setLength(0); | |
continue; | |
} | |
//started the string | |
stringBuilder.append(string).append(' '); | |
continue; | |
} | |
if (string.charAt(length - 1) == '"') { //possible end | |
if (length >= 2 && string.charAt(length - 2) == '\\') { //escaped quote | |
if (stringBuilder.isEmpty()) { | |
arguments.add(string.substring(0, length - 2) + '"'); | |
continue; | |
} | |
stringBuilder.append(string, 0, length - 2).append("\" "); | |
continue; | |
} | |
//end of the string | |
if (stringBuilder.isEmpty()) { | |
arguments.add(string); | |
continue; | |
} | |
arguments.add(stringBuilder.append(string).substring(1, stringBuilder.length() - 1).trim()); | |
stringBuilder.setLength(0); | |
continue; | |
} | |
if (stringBuilder.isEmpty()) { | |
arguments.add(string); | |
continue; | |
} | |
stringBuilder.append(string).append(' '); | |
} | |
if (!stringBuilder.isEmpty()) | |
return Collections.emptyList(); | |
return arguments; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment