Buat Program Java Command Line dengan Picocli

1. Pengenalan

Dalam tutorial ini, kita akan mendekati perpustakaan picocli , yang memungkinkan kita membuat program baris perintah dengan mudah di Java.

Kami mula-mula memulakan dengan membuat arahan Hello World. Kami kemudian akan menyelami ciri utama perpustakaan dengan mereproduksi, sebahagiannya, perintah git .

2. Hello World Command

Mari mulakan dengan sesuatu yang mudah: arahan Hello World!

Pertama, kita perlu menambahkan kebergantungan pada projek picocli :

 info.picocli picocli 3.9.6 

Seperti yang kita lihat, kita akan menggunakan perpustakaan versi 3.9.6 , walaupun versi 4.0.0 sedang dalam pembinaan (kini tersedia dalam ujian alpha).

Sekarang kebergantungan telah disiapkan, mari buat perintah Hello World kami. Untuk melakukannya, kami akan menggunakan anotasi @Command dari perpustakaan :

@Command( name = "hello", description = "Says hello" ) public class HelloWorldCommand { }

Seperti yang kita lihat, anotasi boleh mengambil parameter. Kami hanya menggunakan dua daripadanya di sini. Tujuan mereka adalah untuk memberikan maklumat mengenai perintah dan teks semasa untuk mesej bantuan automatik.

Pada masa ini, tidak banyak yang dapat kita lakukan dengan perintah ini. Untuk membuat ia melakukan sesuatu, kita perlu menambah utama kaedah memanggil kemudahan CommandLine.run (Runnable, String []) kaedah . Ini memerlukan dua parameter: contoh perintah kami, yang dengan itu harus melaksanakan antara muka Runnable , dan array String yang mewakili argumen perintah (pilihan, parameter, dan subkomando):

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!"); } }

Sekarang, apabila kita menjalankan kaedah utama , kita akan melihat bahawa konsol mengeluarkan "Hello World!"

Apabila dikemas ke balang, kita dapat menjalankan perintah Hello World menggunakan perintah java :

java -cp "pathToPicocliJar;pathToCommandJar" com.baeldung.picoli.helloworld.HelloWorldCommand

Tanpa mengejutkan, itu juga menghasilkan "Hello World!" rentetan ke konsol.

3. Kes Penggunaan Konkrit

Sekarang setelah kita melihat asasnya, kita akan menyelami perpustakaan picocli secara mendalam . Untuk melakukan itu, kita akan menghasilkan, sebahagiannya, perintah yang popular: git .

Sudah tentu, tujuannya bukan untuk melaksanakan tingkah laku perintah git tetapi untuk menghasilkan kemungkinan perintah git - subkomando mana yang ada dan pilihan mana yang tersedia untuk subkomen yang pelik.

Pertama, kita harus membuat kelas GitCommand seperti yang kita lakukan untuk perintah Hello World:

@Command 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"); } }

4. Menambah Subkomando

The git tawaran perintah banyak subcommands - add, melakukan, jauh , dan banyak lagi. Kami akan menumpukan perhatian pada penambahan dan komitmen .

Oleh itu, tujuan kami di sini adalah untuk menyatakan kedua-dua subkomando tersebut kepada perintah utama. Picocli menawarkan tiga cara untuk mencapainya.

4.1. Menggunakan Anotasi @Command di Kelas

The @Command anotasi tawaran kemungkinan untuk mendaftar subcommands melalui subcommands parameter :

@Command( subcommands = { GitAddCommand.class, GitCommitCommand.class } )

Dalam kes kami, kami menambah dua kelas baru: GitAddCommand dan GitCommitCommand . Kedua-duanya diberi penjelasan dengan @Command dan melaksanakan Runnable . Penting untuk memberi mereka nama, kerana nama-nama tersebut akan digunakan oleh picocli untuk mengenali subkomen mana yang harus dilaksanakan:

@Command( name = "add" ) public class GitAddCommand implements Runnable { @Override public void run() { System.out.println("Adding some files to the staging area"); } }
@Command( name = "commit" ) public class GitCommitCommand implements Runnable { @Override public void run() { System.out.println("Committing files in the staging area, how wonderful?"); } }

Oleh itu, jika kita menjalankan perintah utama kita dengan menambahkan sebagai argumen, konsol akan mengeluarkan "Menambah beberapa fail ke kawasan pementasan" .

4.2. Menggunakan Kaedah @Anotasi Komen

Kaedah lain untuk menyatakan subkomando adalah dengan membuat kaedah @Command -notated yang mewakili perintah tersebut di kelas GitCommand :

@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?"); }

Dengan cara itu, kita dapat secara langsung menerapkan logik perniagaan kita ke dalam kaedah dan tidak membuat kelas yang berasingan untuk mengatasinya.

4.3. Menambah Subkomersial Secara Berprogram

Akhirnya, picocli memberi kami kemungkinan untuk mendaftarkan subkomando kami secara teratur . Ini agak sukar, kerana kita harus membuat objek CommandLine yang membungkus perintah kita dan kemudian menambahkan subkomando kepadanya:

CommandLine commandLine = new CommandLine(new GitCommand()); commandLine.addSubcommand("add", new GitAddCommand()); commandLine.addSubcommand("commit", new GitCommitCommand());

Selepas itu, kita masih harus menjalankan perintah kita, tetapi kita tidak dapat menggunakan kaedah CommandLine.run () lagi . Sekarang, kita harus memanggil kaedah parseWithHandler () pada objek C ommandLine yang baru kita buat :

commandLine.parseWithHandler(new RunLast(), args);

Kita harus perhatikan penggunaan kelas RunLast , yang memberitahu picocli untuk menjalankan subkomen yang paling spesifik. Terdapat dua pengendali arahan lain yang disediakan oleh picocli : RunFirst dan RunAll . Yang pertama menjalankan perintah paling atas, sementara yang terakhir menjalankan semua perintah.

Semasa menggunakan kaedah kemudahan CommandLine.run () , pengendali RunLast digunakan secara lalai.

5. Menguruskan Pilihan Menggunakan Anotasi @Option

5.1. Pilihan dengan Tidak Berhujah

Let's now see how to add some options to our commands. Indeed, we would like to tell our add command that it should add all modified files. To achieve that, we'll add a field annotated with the @Option annotation to our GitAddCommand class:

@Option(names = {"-A", "--all"}) private boolean allFiles; @Override public void run() { if (allFiles) { System.out.println("Adding all files to the staging area"); } else { System.out.println("Adding some files to the staging area"); } }

As we can see, the annotation takes a names parameter, which gives the different names of the option. Therefore, calling the add command with either -A or –all will set the allFiles field to true. So, if we run the command with the option, the console will show “Adding all files to the staging area”.

5.2. Option with an Argument

As we just saw, for options without arguments, their presence or absence is always evaluated to a boolean value.

However, it's possible to register options that take arguments. We can do this simply by declaring our field to be of a different type. Let's add a message option to our commit command:

@Option(names = {"-m", "--message"}) private String message; @Override public void run() { System.out.println("Committing files in the staging area, how wonderful?"); if (message != null) { System.out.println("The commit message is " + message); } }

Unsurprisingly, when given the message option, the command will show the commit message on the console. Later in the article, we'll cover which types are handled by the library and how to handle other types.

5.3. Option with Multiple Arguments

But now, what if we want our command to take multiple messages, as is done with the real git commit command? No worries, let's make our field be an array or a Collection, and we're pretty much done:

@Option(names = {"-m", "--message"}) 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); } } }

Now, we can use the message option multiple times:

commit -m "My commit is great" -m "My commit is beautiful"

However, we might also want to give the option only once and separate the different parameters by a regex delimiter. Hence, we can use the split parameter of the @Option annotation:

@Option(names = {"-m", "--message"}, split = ",") private String[] messages;

Now, we can pass -m “My commit is great”,”My commit is beautiful” to achieve the same result as above.

5.4. Required Option

Sometimes, we might have an option that is required. The required argument, which defaults to false, allows us to do that:

@Option(names = {"-m", "--message"}, required = true) private String[] messages;

Now it's impossible to call the commit command without specifying the message option. If we try to do that, picocli will print an error:

Missing required option '--message=' Usage: git commit -m= [-m=]... -m, --message=

6. Managing Positional Parameters

6.1. Capture Positional Parameters

Now, let's focus on our add command because it's not very powerful yet. We can only decide to add all files, but what if we wanted to add specific files?

We could use another option to do that, but a better choice here would be to use positional parameters. Indeed, positional parameters are meant to capture command arguments that occupy specific positions and are neither subcommands nor options.

In our example, this would enable us to do something like:

add file1 file2

In order to capture positional parameters, we'll make use of the @Parameters annotation:

@Parameters private List 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")); } }

Now, our command from earlier would print:

Adding file1 to the staging area Adding file2 to the staging area

6.2. Capture a Subset of Positional Parameters

It's possible to be more fine-grained about which positional parameters to capture, thanks to the index parameter of the annotation. The index is zero-based. Thus, if we define:

@Parameters(index="2..*")

This would capture arguments that don't match options or subcommands, from the third one to the end.

The index can be either a range or a single number, representing a single position.

7. A Word About Type Conversion

As we've seen earlier in this tutorial, picocli handles some type conversion by itself. For example, it maps multiple values to arrays or Collections, but it can also map arguments to specific types like when we use the Path class for the add command.

As a matter of fact, picocli comes with a bunch of pre-handled types. This means we can use those types directly without having to think about converting them ourselves.

However, we might need to map our command arguments to types other than those that are already handled. Fortunately for us, this is possible thanks to the ITypeConverter interface and the CommandLine#registerConverter method, which associates a type to a converter.

Let's imagine we want to add the config subcommand to our git command, but we don't want users to change a configuration element that doesn't exist. So, we decide to map those elements to an enum:

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")); } }

Plus, in our newly created GitConfigCommand class, let's add two positional parameters:

@Parameters(index = "0") private ConfigElement element; @Parameters(index = "1") private String value; @Override public void run() { System.out.println("Setting " + element.value() + " to " + value); }

This way, we make sure that users won't be able to change non-existent configuration elements.

Finally, we have to register our converter. What's beautiful is that, if using Java 8 or higher, we don't even have to create a class implementing the ITypeConverter interface. We can just pass a lambda or method reference to the registerConverter() method:

CommandLine commandLine = new CommandLine(new GitCommand()); commandLine.registerConverter(ConfigElement.class, ConfigElement::from); commandLine.parseWithHandler(new RunLast(), args);

This happens in the GitCommand main() method. Note that we had to let go of the convenience CommandLine.run() method.

When used with an unhandled configuration element, the command would show the help message plus a piece of information telling us that it wasn't possible to convert the parameter to a ConfigElement:

Invalid value for positional parameter at index 0 (): cannot convert 'user.phone' to ConfigElement (java.lang.IllegalArgumentException: The argument user.phone doesn't match any ConfigElement) Usage: git config    

8. Integrating with Spring Boot

Finally, let's see how to Springify all that!

Indeed, we might be working within a Spring Boot environment and want to benefit from it in our command-line program. In order to do that, we must create a SpringBootApplicationimplementing the CommandLineRunner interface:

@SpringBootApplication public class Application implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override public void run(String... args) { } }

Plus, let's annotate all our commands and subcommands with the Spring @Component annotation and autowire all that in our Application:

private GitCommand gitCommand; private GitAddCommand addCommand; private GitCommitCommand commitCommand; private GitConfigCommand configCommand; public Application(GitCommand gitCommand, GitAddCommand addCommand, GitCommitCommand commitCommand, GitConfigCommand configCommand) { this.gitCommand = gitCommand; this.addCommand = addCommand; this.commitCommand = commitCommand; this.configCommand = configCommand; }

Note that we had to autowire every subcommand. Unfortunately, this is because, for now, picocli is not yet able to retrieve subcommands from the Spring context when declared declaratively (with annotations). Thus, we'll have to do that wiring ourselves, in a programmatic way:

@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); }

And now, our command line program works like a charm with Spring components. Therefore, we could create some service classes and use them in our commands, and let Spring take care of the dependency injection.

9. Conclusion

In this article, we've seen some key features of the picocli library. We've learned how to create a new command and add some subcommands to it. We've seen many ways to deal with options and positional parameters. Plus, we've learned how to implement our own type converters to make our commands strongly typed. Finally, we've seen how to bring Spring Boot into our commands.

Of course, there are many things more to discover about it. The library provides complete documentation.

As for the full code of this article, it can be found on our GitHub.