Skip to content

Commit c80d0a9

Browse files
authored
Merge pull request eugenp#6783 from FDPro/bael-2882
dupirefr/dupire.francois+pro@gmail.com [BAEL-2882] Create a Command Line Java Program with Picocli
2 parents 9b2148d + 382216b commit c80d0a9

File tree

11 files changed

+281
-0
lines changed

11 files changed

+281
-0
lines changed

picocli/pom.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>parent-java</artifactId>
9+
<version>0.0.1-SNAPSHOT</version>
10+
<relativePath>../parent-java</relativePath>
11+
</parent>
12+
13+
<artifactId>picocli</artifactId>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>info.picocli</groupId>
18+
<artifactId>picocli</artifactId>
19+
<version>3.9.6</version>
20+
</dependency>
21+
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter</artifactId>
25+
<version>2.1.4.RELEASE</version>
26+
</dependency>
27+
</dependencies>
28+
</project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.picocli.git;
2+
3+
import com.baeldung.picocli.git.commands.programmative.GitCommand;
4+
import com.baeldung.picocli.git.commands.subcommands.GitAddCommand;
5+
import com.baeldung.picocli.git.commands.subcommands.GitCommitCommand;
6+
import com.baeldung.picocli.git.commands.subcommands.GitConfigCommand;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.CommandLineRunner;
9+
import org.springframework.boot.SpringApplication;
10+
import org.springframework.boot.autoconfigure.SpringBootApplication;
11+
import picocli.CommandLine;
12+
13+
@SpringBootApplication
14+
public class Application implements CommandLineRunner {
15+
public static void main(String[] args) {
16+
SpringApplication.run(Application.class, args);
17+
}
18+
19+
private GitCommand gitCommand;
20+
private GitAddCommand addCommand;
21+
private GitCommitCommand commitCommand;
22+
private GitConfigCommand configCommand;
23+
24+
@Autowired
25+
public Application(GitCommand gitCommand, GitAddCommand addCommand, GitCommitCommand commitCommand, GitConfigCommand configCommand) {
26+
this.gitCommand = gitCommand;
27+
this.addCommand = addCommand;
28+
this.commitCommand = commitCommand;
29+
this.configCommand = configCommand;
30+
}
31+
32+
@Override
33+
public void run(String... args) {
34+
CommandLine commandLine = new CommandLine(gitCommand);
35+
commandLine.addSubcommand("add", addCommand);
36+
commandLine.addSubcommand("commit", commitCommand);
37+
commandLine.addSubcommand("config", configCommand);
38+
39+
commandLine.parseWithHandler(new CommandLine.RunLast(), args);
40+
}
41+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.picocli.git.commands.declarative;
2+
3+
import com.baeldung.picocli.git.model.ConfigElement;
4+
import com.baeldung.picocli.git.commands.subcommands.GitAddCommand;
5+
import com.baeldung.picocli.git.commands.subcommands.GitCommitCommand;
6+
import com.baeldung.picocli.git.commands.subcommands.GitConfigCommand;
7+
import picocli.CommandLine;
8+
9+
import static picocli.CommandLine.*;
10+
import static picocli.CommandLine.Command;
11+
12+
@Command(
13+
name = "git",
14+
subcommands = {
15+
GitAddCommand.class,
16+
GitCommitCommand.class,
17+
GitConfigCommand.class
18+
}
19+
)
20+
public class GitCommand implements Runnable {
21+
public static void main(String[] args) {
22+
CommandLine commandLine = new CommandLine(new GitCommand());
23+
commandLine.registerConverter(ConfigElement.class, ConfigElement::from);
24+
25+
commandLine.parseWithHandler(new RunLast(), args);
26+
}
27+
28+
@Override
29+
public void run() {
30+
System.out.println("The popular git command");
31+
}
32+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.picocli.git.commands.methods;
2+
3+
import picocli.CommandLine;
4+
5+
import static picocli.CommandLine.Command;
6+
7+
@Command(name = "git")
8+
public class GitCommand implements Runnable {
9+
public static void main(String[] args) {
10+
CommandLine.run(new GitCommand(), args);
11+
}
12+
13+
@Override
14+
public void run() {
15+
System.out.println("The popular git command");
16+
}
17+
18+
@Command(name = "add")
19+
public void addCommand() {
20+
System.out.println("Adding some files to the staging area");
21+
}
22+
23+
@Command(name = "commit")
24+
public void commitCommand() {
25+
System.out.println("Committing files in the staging area, how wonderful?");
26+
}
27+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.picocli.git.commands.programmative;
2+
3+
import com.baeldung.picocli.git.commands.subcommands.GitAddCommand;
4+
import com.baeldung.picocli.git.commands.subcommands.GitCommitCommand;
5+
import org.springframework.stereotype.Component;
6+
import picocli.CommandLine;
7+
8+
import static picocli.CommandLine.Command;
9+
import static picocli.CommandLine.RunLast;
10+
11+
@Command(name = "git")
12+
@Component
13+
public class GitCommand implements Runnable {
14+
public static void main(String[] args) {
15+
CommandLine commandLine = new CommandLine(new GitCommand());
16+
commandLine.addSubcommand("add", new GitAddCommand());
17+
commandLine.addSubcommand("commit", new GitCommitCommand());
18+
19+
commandLine.parseWithHandler(new RunLast(), args);
20+
}
21+
22+
@Override
23+
public void run() {
24+
System.out.println("The popular git command");
25+
}
26+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.picocli.git.commands.subcommands;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
import java.nio.file.Path;
6+
import java.util.List;
7+
8+
import static picocli.CommandLine.*;
9+
10+
@Command(
11+
name = "add"
12+
)
13+
@Component
14+
public class GitAddCommand implements Runnable {
15+
@Option(names = "-A")
16+
private boolean allFiles;
17+
18+
@Parameters(index = "0..*")
19+
private List<Path> files;
20+
21+
@Override
22+
public void run() {
23+
if (allFiles) {
24+
System.out.println("Adding all files to the staging area");
25+
}
26+
27+
if (files != null) {
28+
files.forEach(path -> System.out.println("Adding " + path + " to the staging area"));
29+
}
30+
}
31+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.picocli.git.commands.subcommands;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
import static picocli.CommandLine.Command;
6+
import static picocli.CommandLine.Option;
7+
8+
@Command(
9+
name = "commit"
10+
)
11+
@Component
12+
public class GitCommitCommand implements Runnable {
13+
@Option(names = {"-m", "--message"}, required = true)
14+
private String[] messages;
15+
16+
@Override
17+
public void run() {
18+
System.out.println("Committing files in the staging area, how wonderful?");
19+
if (messages != null) {
20+
System.out.println("The commit message is");
21+
for (String message : messages) {
22+
System.out.println(message);
23+
}
24+
}
25+
}
26+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.picocli.git.commands.subcommands;
2+
3+
import com.baeldung.picocli.git.model.ConfigElement;
4+
import org.springframework.stereotype.Component;
5+
6+
import static picocli.CommandLine.Command;
7+
import static picocli.CommandLine.Parameters;
8+
9+
@Command(
10+
name = "config"
11+
)
12+
@Component
13+
public class GitConfigCommand implements Runnable {
14+
@Parameters(index = "0")
15+
private ConfigElement element;
16+
17+
@Parameters(index = "1")
18+
private String value;
19+
20+
@Override
21+
public void run() {
22+
System.out.println("Setting " + element.value() + " to " + value);
23+
}
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.picocli.git.model;
2+
3+
import java.util.Arrays;
4+
5+
public enum ConfigElement {
6+
USERNAME("user.name"),
7+
EMAIL("user.email");
8+
9+
private final String value;
10+
11+
ConfigElement(String value) {
12+
this.value = value;
13+
}
14+
15+
public String value() {
16+
return value;
17+
}
18+
19+
public static ConfigElement from(String value) {
20+
return Arrays.stream(values())
21+
.filter(element -> element.value.equals(value))
22+
.findFirst()
23+
.orElseThrow(() -> new IllegalArgumentException("The argument " + value + " doesn't match any ConfigElement"));
24+
}
25+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.picocli.helloworld;
2+
3+
import picocli.CommandLine;
4+
5+
import static picocli.CommandLine.Command;
6+
7+
@Command(
8+
name = "hello",
9+
description = "Says hello"
10+
)
11+
public class HelloWorldCommand implements Runnable {
12+
public static void main(String[] args) {
13+
CommandLine.run(new HelloWorldCommand(), args);
14+
}
15+
16+
@Override
17+
public void run() {
18+
System.out.println("Hello World!");
19+
}
20+
}

0 commit comments

Comments
 (0)