Java命令行程序构建工具airlift使用之分组(group)

来源:互联网 发布:coc各级墙升级数据 编辑:程序博客网 时间:2024/06/05 11:58

闲话少说,直接上代码:

package com.ilucky.druid.airlift.test2;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;/** * v1.0:20161115 * 组的概念. * 右击Run As - Run Configurations - Arguments - Program arguments * 输入: g1 G1MyCommand test, g1 G1MyCommand2 test, * 或g2 G2MyCommand 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");        builder.withGroup("g1")            .withDescription("g1:Description")            .withDefaultCommand(Help.class)            .withCommands(Help.class, G1MyCommand.class, G1MyCommand2.class);        builder.withGroup("g2")            .withDescription("g2:Description")            .withDefaultCommand(Help.class)            .withCommands(Help.class, G2MyCommand.class);        Cli<Runnable> cliParser = builder.build();        try {            cliParser.parse(args).run();        } catch (ParseArgumentsUnexpectedException e) {            System.out.println("Invalid command:"+e.toString());        }    }}/**输入: g1 G1MyCommand test, 结果:===> [g1, G1MyCommand, test]G1MyCommand=test输入: g1 G1MyCommand2 test, 结果:===> [g1, G1MyCommand2, test]G1MyCommand2=test输入: g2 G2MyCommand test, 结果:===> [g2, G2MyCommand, test]G2MyCommand=test*/
package com.ilucky.druid.airlift.test2;import io.airlift.airline.Arguments;import io.airlift.airline.Command;@Command(name="G1MyCommand",description="This is G1MyCommand")public class G1MyCommand implements Runnable {    @Arguments    private String command;    @Override    public void run() {        System.out.println("G1MyCommand=" + command);    }}
package com.ilucky.druid.airlift.test2;import io.airlift.airline.Arguments;import io.airlift.airline.Command;@Command(name="G1MyCommand2",description="This is G1MyCommand2")public class G1MyCommand2 implements Runnable {    @Arguments    private String command;    @Override    public void run() {        System.out.println("G1MyCommand2=" + command);    }}
package com.ilucky.druid.airlift.test2;import io.airlift.airline.Arguments;import io.airlift.airline.Command;@Command(name="G2MyCommand",description="This is G2MyCommand")public class G2MyCommand implements Runnable {    @Arguments    private String command;    @Override    public void run() {        System.out.println("G2MyCommand=" + command);    }}
原创粉丝点击