java使用commons-cli构造命令行工具

4,115 阅读1分钟
  • commons-cli是apache的一个实现java命令行的工具,通过他,我们就可以使用java实现一个命令行工具。

  • pom依赖

        <dependency>
            <groupId>commons-cli</groupId>
            <artifactId>commons-cli</artifactId>
            <version>1.2</version>
        </dependency>

  • 使用:java -jar Hs-2.1.8.RELEASE.jar -h
  • 代码
public class CommandLineTest {

    public static void main(String[] args) {
        new CommandLineTest().parseArgs(args);
    }

    public void parseArgs(String[] args)  {


        Runtime.getRuntime().addShutdownHook(new Thread(()->{
            System.out.println("谢谢使用噶");
        }));

        try {
            CommandLineParser parser = new GnuParser( );

            Options options = new Options();
            options.addOption("help","help",false,"Print this usage information");
            options.addOption("c", "cfg", false, "config Absolute Path");

            CommandLine commandLine = null;
            commandLine = parser.parse( options, args );
            if( commandLine.hasOption("help") ) {
                System.out.println("这是一个优秀的帮助文档");
                System.exit(0);
            }

            if( commandLine.hasOption("c") ) {
                System.out.println("所以我每一个都选c");
                System.exit(0);
            }

        } catch (ParseException e) {
            System.out.println("请输入正确的参数");
            System.exit(0);
        }catch (Exception e){
            System.out.println("遇到了意外的情况,请重新输入");
        }

    }
}