refactoring: Cli-Parser
This commit is contained in:
2
cli-parser/build.gradle
Normal file
2
cli-parser/build.gradle
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
//file:noinspection GrUnresolvedAccess
|
||||||
|
apply from: rootDir.toPath().resolve('logic.gradle').toFile()
|
||||||
2
cli-parser/gradle.properties
Normal file
2
cli-parser/gradle.properties
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
# suppress inspection "UnusedProperty" for whole file
|
||||||
|
module.name=cli-parser
|
||||||
17
cli-parser/src/main/java/mc/cliparser/CommandLine.java
Normal file
17
cli-parser/src/main/java/mc/cliparser/CommandLine.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package mc.cliparser;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.ToString;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
@ToString
|
||||||
|
public class CommandLine {
|
||||||
|
|
||||||
|
private final Set<Option> options;
|
||||||
|
|
||||||
|
public boolean has(Option option) {
|
||||||
|
return options.contains(option);
|
||||||
|
}
|
||||||
|
}
|
||||||
52
cli-parser/src/main/java/mc/cliparser/CommandLineParser.java
Normal file
52
cli-parser/src/main/java/mc/cliparser/CommandLineParser.java
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package mc.cliparser;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
|
public class CommandLineParser {
|
||||||
|
|
||||||
|
private final Set<Option> options = new HashSet<>();
|
||||||
|
|
||||||
|
public void addOption(Option option) {
|
||||||
|
options.add(option);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommandLine parse(String[] args) {
|
||||||
|
|
||||||
|
Set<Option> foundOptions = new HashSet<>();
|
||||||
|
AtomicReference<Option> refCurrentOption = new AtomicReference<>(null);
|
||||||
|
|
||||||
|
for (String arg : args) {
|
||||||
|
if (refCurrentOption.get() != null) {
|
||||||
|
refCurrentOption.get().value(arg);
|
||||||
|
foundOptions.add(refCurrentOption.get());
|
||||||
|
refCurrentOption.set(null);
|
||||||
|
} else {
|
||||||
|
parseOptArgs(arg, foundOptions, refCurrentOption);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new CommandLine(foundOptions);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("java:S125")
|
||||||
|
private void parseOptArgs(String arg, Set<Option> foundOptions, AtomicReference<Option> refCurrentOption) {
|
||||||
|
String optName;
|
||||||
|
if (arg.startsWith("--")) {
|
||||||
|
optName = arg.substring(2);
|
||||||
|
} else /*if (args[i].startsWith("-"))*/ {
|
||||||
|
optName = arg.substring(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Option option : options) {
|
||||||
|
if (optName.equals(option.shortName()) || optName.equals(option.longName())) {
|
||||||
|
if (option.hasArgs()) {
|
||||||
|
refCurrentOption.set(option);
|
||||||
|
} else {
|
||||||
|
foundOptions.add(option);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
25
cli-parser/src/main/java/mc/cliparser/Option.java
Normal file
25
cli-parser/src/main/java/mc/cliparser/Option.java
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package mc.cliparser;
|
||||||
|
|
||||||
|
import lombok.*;
|
||||||
|
import lombok.experimental.Accessors;
|
||||||
|
|
||||||
|
@Accessors(fluent = true)
|
||||||
|
@Builder
|
||||||
|
@AllArgsConstructor(access = AccessLevel.PRIVATE)
|
||||||
|
@EqualsAndHashCode
|
||||||
|
@ToString
|
||||||
|
public class Option {
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private final String shortName;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private final String longName;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private final boolean hasArgs;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
private String value;
|
||||||
|
}
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
package mc.cliparser;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class CommandLineParserTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void optionTest() {
|
||||||
|
Option option = Option.builder().shortName("v").build();
|
||||||
|
assertNull(option.longName());
|
||||||
|
assertFalse(option.hasArgs());
|
||||||
|
|
||||||
|
option = Option.builder().longName("version").build();
|
||||||
|
assertNull(option.shortName());
|
||||||
|
assertFalse(option.hasArgs());
|
||||||
|
|
||||||
|
option = Option.builder().shortName("v").longName("value1").hasArgs(true).build();
|
||||||
|
assertNotNull(option.shortName());
|
||||||
|
assertNotNull(option.longName());
|
||||||
|
assertTrue(option.hasArgs());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shortOptionFlag() {
|
||||||
|
Option option = Option.builder().shortName("v").build();
|
||||||
|
|
||||||
|
var parser = new CommandLineParser();
|
||||||
|
parser.addOption(option);
|
||||||
|
|
||||||
|
CommandLine commandLine = parser.parse(new String[]{ "-v" });
|
||||||
|
assertTrue(commandLine.has(option));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void longOptionFlag() {
|
||||||
|
Option option = Option.builder().longName("version").build();
|
||||||
|
|
||||||
|
var parser = new CommandLineParser();
|
||||||
|
parser.addOption(option);
|
||||||
|
|
||||||
|
CommandLine commandLine = parser.parse(new String[]{ "--version" });
|
||||||
|
assertTrue(commandLine.has(option));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void argsOption() {
|
||||||
|
Option option1 = Option.builder().shortName("v").longName("value1").hasArgs(true).build();
|
||||||
|
Option option2 = Option.builder().shortName("a").longName("value2").hasArgs(true).build();
|
||||||
|
|
||||||
|
var parser = new CommandLineParser();
|
||||||
|
parser.addOption(option1);
|
||||||
|
parser.addOption(option2);
|
||||||
|
|
||||||
|
CommandLine commandLine = parser.parse(new String[]{ "--value1", "arg1", "-a", "arg2" });
|
||||||
|
assertTrue(commandLine.has(option1));
|
||||||
|
assertEquals("arg1", option1.value());
|
||||||
|
assertTrue(commandLine.has(option2));
|
||||||
|
assertEquals("arg2", option2.value());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void noPassOption() {
|
||||||
|
Option option = Option.builder().longName("version").build();
|
||||||
|
|
||||||
|
var parser = new CommandLineParser();
|
||||||
|
parser.addOption(option);
|
||||||
|
|
||||||
|
CommandLine commandLine = parser.parse(new String[]{ "--any-opt" });
|
||||||
|
assertFalse(commandLine.has(option));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,5 +13,6 @@ rootProject.name = map.get('project.name')
|
|||||||
include('utils')
|
include('utils')
|
||||||
include('protocol-new')
|
include('protocol-new')
|
||||||
include('server-new')
|
include('server-new')
|
||||||
|
include('cli-parser')
|
||||||
//include('protocol')
|
//include('protocol')
|
||||||
//include('server')
|
//include('server')
|
||||||
Reference in New Issue
Block a user