The Airline CLI is a Java library providing an annotation-based framework for parsing command line interfaces. This project aims to integrate the Airline into Spring and serve as an application entrypoint.
Install the latest org.framefork:airline-cli-spring-boot-starter:*
and you're good to go.
import static org.framefork.cli.airline.CliMainClass.runOneCommand;
@SpringBootApplication
public class TestingSpringApplicationMock
{
public static void main(final String[] args)
{
runOneCommand(TestingSpringApplicationMock.class, args);
}
}
If you need to, you can use the third argument to customize the SpringApplicationBuilder
public static void main(final String[] args)
{
runOneCommand(TestingSpringApplicationMock.class, args, builder -> {
builder.profiles("cli");
});
}
The command must implement org.framefork.cli.airline.ExecutableCommand
, so that there is a single main method that will be called for the command.
And be annotated with org.framefork.cli.airline.CliCommand
, which makes it a Spring bean with prototype scope.
@CliCommand
public class MyCommand implements ExecutableCommand
{
@Option(name = {"--dry-run"}, description = "An option that requires no values")
private boolean dryRun = false;
@Override
public void execute()
{
if (dryRun) {
// dry run
} else {
// real run
}
}
}
Now you should be able to, using your new main class, execute one-off commands.
When you execute a command
- the application is started in non-web mode, the
runOneCommand()
takes care of that - the console arguments are parsed and validated
- Airline uses the bridge in this project to ask spring for the command instance, which is created from the prototype bean
- Airline binds all the options and arguments to the command, making them available to the command
- the
execute()
method is called - if at any point the handling ends with an error, its printed