[BAEL-2882] Picocli

This commit is contained in:
dupirefr 2019-04-21 13:25:11 +02:00
parent 5e859a117f
commit 382216bdb1
11 changed files with 281 additions and 0 deletions

28
picocli/pom.xml Normal file
View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-java</relativePath>
</parent>
<artifactId>picocli</artifactId>
<dependencies>
<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
<version>3.9.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,41 @@
package com.baeldung.picocli.git;
import com.baeldung.picocli.git.commands.programmative.GitCommand;
import com.baeldung.picocli.git.commands.subcommands.GitAddCommand;
import com.baeldung.picocli.git.commands.subcommands.GitCommitCommand;
import com.baeldung.picocli.git.commands.subcommands.GitConfigCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import picocli.CommandLine;
@SpringBootApplication
public class Application implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
private GitCommand gitCommand;
private GitAddCommand addCommand;
private GitCommitCommand commitCommand;
private GitConfigCommand configCommand;
@Autowired
public Application(GitCommand gitCommand, GitAddCommand addCommand, GitCommitCommand commitCommand, GitConfigCommand configCommand) {
this.gitCommand = gitCommand;
this.addCommand = addCommand;
this.commitCommand = commitCommand;
this.configCommand = configCommand;
}
@Override
public void run(String... args) {
CommandLine commandLine = new CommandLine(gitCommand);
commandLine.addSubcommand("add", addCommand);
commandLine.addSubcommand("commit", commitCommand);
commandLine.addSubcommand("config", configCommand);
commandLine.parseWithHandler(new CommandLine.RunLast(), args);
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.picocli.git.commands.declarative;
import com.baeldung.picocli.git.model.ConfigElement;
import com.baeldung.picocli.git.commands.subcommands.GitAddCommand;
import com.baeldung.picocli.git.commands.subcommands.GitCommitCommand;
import com.baeldung.picocli.git.commands.subcommands.GitConfigCommand;
import picocli.CommandLine;
import static picocli.CommandLine.*;
import static picocli.CommandLine.Command;
@Command(
name = "git",
subcommands = {
GitAddCommand.class,
GitCommitCommand.class,
GitConfigCommand.class
}
)
public class GitCommand implements Runnable {
public static void main(String[] args) {
CommandLine commandLine = new CommandLine(new GitCommand());
commandLine.registerConverter(ConfigElement.class, ConfigElement::from);
commandLine.parseWithHandler(new RunLast(), args);
}
@Override
public void run() {
System.out.println("The popular git command");
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.picocli.git.commands.methods;
import picocli.CommandLine;
import static picocli.CommandLine.Command;
@Command(name = "git")
public class GitCommand implements Runnable {
public static void main(String[] args) {
CommandLine.run(new GitCommand(), args);
}
@Override
public void run() {
System.out.println("The popular git command");
}
@Command(name = "add")
public void addCommand() {
System.out.println("Adding some files to the staging area");
}
@Command(name = "commit")
public void commitCommand() {
System.out.println("Committing files in the staging area, how wonderful?");
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.picocli.git.commands.programmative;
import com.baeldung.picocli.git.commands.subcommands.GitAddCommand;
import com.baeldung.picocli.git.commands.subcommands.GitCommitCommand;
import org.springframework.stereotype.Component;
import picocli.CommandLine;
import static picocli.CommandLine.Command;
import static picocli.CommandLine.RunLast;
@Command(name = "git")
@Component
public class GitCommand implements Runnable {
public static void main(String[] args) {
CommandLine commandLine = new CommandLine(new GitCommand());
commandLine.addSubcommand("add", new GitAddCommand());
commandLine.addSubcommand("commit", new GitCommitCommand());
commandLine.parseWithHandler(new RunLast(), args);
}
@Override
public void run() {
System.out.println("The popular git command");
}
}

View File

@ -0,0 +1,31 @@
package com.baeldung.picocli.git.commands.subcommands;
import org.springframework.stereotype.Component;
import java.nio.file.Path;
import java.util.List;
import static picocli.CommandLine.*;
@Command(
name = "add"
)
@Component
public class GitAddCommand implements Runnable {
@Option(names = "-A")
private boolean allFiles;
@Parameters(index = "0..*")
private List<Path> files;
@Override
public void run() {
if (allFiles) {
System.out.println("Adding all files to the staging area");
}
if (files != null) {
files.forEach(path -> System.out.println("Adding " + path + " to the staging area"));
}
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.picocli.git.commands.subcommands;
import org.springframework.stereotype.Component;
import static picocli.CommandLine.Command;
import static picocli.CommandLine.Option;
@Command(
name = "commit"
)
@Component
public class GitCommitCommand implements Runnable {
@Option(names = {"-m", "--message"}, required = true)
private String[] messages;
@Override
public void run() {
System.out.println("Committing files in the staging area, how wonderful?");
if (messages != null) {
System.out.println("The commit message is");
for (String message : messages) {
System.out.println(message);
}
}
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.picocli.git.commands.subcommands;
import com.baeldung.picocli.git.model.ConfigElement;
import org.springframework.stereotype.Component;
import static picocli.CommandLine.Command;
import static picocli.CommandLine.Parameters;
@Command(
name = "config"
)
@Component
public class GitConfigCommand implements Runnable {
@Parameters(index = "0")
private ConfigElement element;
@Parameters(index = "1")
private String value;
@Override
public void run() {
System.out.println("Setting " + element.value() + " to " + value);
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.picocli.git.model;
import java.util.Arrays;
public enum ConfigElement {
USERNAME("user.name"),
EMAIL("user.email");
private final String value;
ConfigElement(String value) {
this.value = value;
}
public String value() {
return value;
}
public static ConfigElement from(String value) {
return Arrays.stream(values())
.filter(element -> element.value.equals(value))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("The argument " + value + " doesn't match any ConfigElement"));
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.picocli.helloworld;
import picocli.CommandLine;
import static picocli.CommandLine.Command;
@Command(
name = "hello",
description = "Says hello"
)
public class HelloWorldCommand implements Runnable {
public static void main(String[] args) {
CommandLine.run(new HelloWorldCommand(), args);
}
@Override
public void run() {
System.out.println("Hello World!");
}
}

View File

@ -12,6 +12,7 @@
<modules>
<module>lombok-custom</module>
<module>picocli</module>
</modules>
<dependencies>