JAVA/java
[java] picocli java sample
닉의네임
2022. 3. 22. 15:53
반응형
https://aragost.com/blog/java/picocli-introduction/
Picocli introduction
This is an introduction to creating user-friendly command-line interfaces to Java applications with the picocli framework.
aragost.com
maven
pom.xml
<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
<version>4.5.1</version>
</dependency>
gradle
gradle.build
// https://mvnrepository.com/artifact/info.picocli/picocli-spring-boot-starter
implementation group: 'info.picocli', name: 'picocli-spring-boot-starter', version: '4.6.3'
package com.curi.log.runner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.stereotype.Component;
import picocli.CommandLine;
@Component
public class AppCommandLineRunner implements CommandLineRunner, ExitCodeGenerator {
private final AppCommand appCommand;
private final CommandLine.IFactory factory;
private int exitCode;
public AppCommandLineRunner(AppCommand appCommand, CommandLine.IFactory factory) {
this.appCommand = appCommand;
this.factory = factory;
}
@Override
public void run(String... args) throws Exception {
exitCode = new CommandLine(appCommand, factory).setExitCodeExceptionMapper(appCommand).execute(args);
System.out.println("AppCommandLineRunner");
}
@Override
public int getExitCode() {
return exitCode;
}
}
package com.bbongdoo.doo.runner;
import com.bbongdoo.doo.service.DynamicIndex;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import picocli.CommandLine.*;
import java.io.IOException;
import java.util.concurrent.Callable;
@Component
@Command(name = "java -jar extract.jar", mixinStandardHelpOptions = true,
version = "1.0.0",
description = "extract"
)
@RequiredArgsConstructor
public class AppCommand implements Callable<Integer>, IExitCodeExceptionMapper {
private final DynamicIndex dynamicIndex;
@ArgGroup(exclusive = true, multiplicity = "3", validate = false)
Exclusive exclusive;
@Parameters(index = "0", paramLabel = "Last Name", description = "value:[L]")
private String lastName;
@Parameters(index = "1", paramLabel = "First Name", description = "value:[D]")
private String firstName;
@Parameters(index = "2", paramLabel = "Second Name", description = "value:[H]")
private String secondName;
@Override
public int getExitCode(Throwable exception) {
exception.printStackTrace();
return 0;
}
@Override
public Integer call() throws Exception {
System.out.println("Call");
if (!lastName.equals("L")) {
System.out.println("H 가 아님");
throw new IOException();
}
if (!firstName.equals("D")) {
System.out.println("H 가 아님");
throw new IOException();
}
if (!secondName.equals("H")) {
System.out.println("H 가 아님");
throw new IOException();
}
dynamicIndex.startDynamic();
return ExitCode.OK;
}
static class Exclusive {
@Option(names = {"-l", "--LastName"}, required = true, description = "last name value")
private boolean isLastName;
@Option(names = {"-d", "--FirstName"}, required = true, description = "first name value")
private boolean isFirstName;
@Option(names = {"-h", "--SecondName"}, required = true, description = "second name value")
private boolean isSecondName;
}
}
Program arguments 값 입력

반응형