package com.ilucky.airlift;
import java.util.Arrays;
import io.airlift.airline.Cli;
import io.airlift.airline.Cli.CliBuilder;
import io.airlift.airline.Help;
import io.airlift.airline.ParseArgumentsUnexpectedException;
/**
* Java命令行程序构建工具:io.airlift.airline.Cli:
* 以前开发Java命令行程序,需要解析很多参数,以及一些帮助信息, 今天可以使用airlfit工具快速构建命令行程序.
*
* v1.0:20161115
* 右击Run As - Run Configurations - Arguments - Program arguments
* 输入: MyCommand test, 运行, 查看Console窗口.
* @author Ilucky
*/
public class MainTest {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
System.out.println("===> " + Arrays.asList(args));
CliBuilder<Runnable> builder = Cli.<Runnable>builder("MyCommand:Builder")
.withDescription("MyCommand:Description")
.withDefaultCommand(Help.class)
.withCommands(Help.class, MyCommand.class);
Cli<Runnable> cliParser = builder.build();
System.out.println("getMetadata().getName()="+cliParser.getMetadata().getName());
System.out.println("getMetadata().getDescription()="+cliParser.getMetadata().getDescription());
System.out.println("getMetadata().getCommandGroups()="+Arrays.asList(cliParser.getMetadata().getCommandGroups()));
System.out.println("getMetadata().getOptions()="+cliParser.getMetadata().getOptions());
System.out.println("getMetadata().getDefaultCommand().getName()="+cliParser.getMetadata().getDefaultCommand().getName());
try {
cliParser.parse(args).run();
} catch (ParseArgumentsUnexpectedException e) {
System.out.println("Invalid command:"+e.toString());
}
}
}
/**
输入: MyCommand test, 结果:
===> [MyCommand, test]
getMetadata().getName()=MyCommand:Builder
getMetadata().getDescription()=MyCommand:Description
getMetadata().getCommandGroups()=[[]]
getMetadata().getOptions()=[]
getMetadata().getDefaultCommand().getName()=help
MyCommand=test
输入: 空, 结果:
===> []
getMetadata().getName()=MyCommand:Builder
getMetadata().getDescription()=MyCommand:Description
getMetadata().getCommandGroups()=[[]]
getMetadata().getOptions()=[]
getMetadata().getDefaultCommand().getName()=help
usage: MyCommand:Builder <command> [<args>]
The most commonly used MyCommand:Builder commands are:
MyCommand This is my command
help Display help information
See 'MyCommand:Builder help <command>' for more information on a specific
command.
输入: help, 结果:
===> [help]
getMetadata().getName()=MyCommand:Builder
getMetadata().getDescription()=MyCommand:Description
getMetadata().getCommandGroups()=[[]]
getMetadata().getOptions()=[]
getMetadata().getDefaultCommand().getName()=help
usage: MyCommand:Builder <command> [<args>]
The most commonly used MyCommand:Builder commands are:
MyCommand This is my command
help Display help information
See 'MyCommand:Builder help <command>' for more information on a specific
command.
*/
版权声明:本文为sidongxue2原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。