commons-cli jar包的使用

一、简介

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

Apache Commons CLI library为用户提供了一个解释命令行的API。它在解释命令行时主要有三个状态,即:定义、解释和询问交互。下面的部分中将会详细的讨论这三个部分的内容,以及怎么样利用CLI实现它们。

很多时候我们想自己维护命令行操作,常常考虑不全面,而且也是在重复发明轮子,commons-cli 为我们提供了完整的命令行全套解决方案,下面就用heritri3里面使用commons-cli的例子来介绍这个工具的使用。


第一步:我们需要设置我们的命令

private static Options options() {
        Options options = new Options();
        options.addOption("h", "help", true, "Usage information." );
        options.addOption("a", "web-admin", true,  "REQUIRED. Specifies the " +
                "authorization username and password which must be supplied to " +
                "access the web interface. This may be of the form " +
                "\"password\" (which leaves username as the default 'admin'), " +
                "\"username:password\", or \"@filename\" for a file that " +
                "includes the single line \"username:password\". ");
        options.addOption("j", "jobs-dir", true, "The jobs directory.  " +
                        "Defaults to ./jobs");
        options.addOption("l", "logging-properties", true, 
                "The full path to the logging properties file " + 
                "(eg, conf/logging.properties).  If present, this file " +
                "will be used to configure Java logging.  Defaults to " +
                "${heritrix.home}/conf/logging.properties or if no " +
                "heritrix.home property set, ./conf/logging.properties");
        options.addOption("b", "web-bind-hosts", true, 
                "A comma-separated list of addresses/hostnames for the " +
                "web interface to bind to.");
        options.addOption("p", "web-port", true, "The port the web interface " +
                "should listen on.");
        options.addOption("s", "ssl-params", true,  "Specify a keystore " +
                "path, keystore password, and key password for HTTPS use. " +
                "Separate with commas, no whitespace.");
        return options;
    }
    

第二步:我们需要获取用户的命令,如果用户的命令出错了,我们需要打印错误信息

private static CommandLine getCommandLine(PrintStream out, String[] args) {
        CommandLineParser clp = new GnuParser();
        CommandLine cl;
        try {
            cl = clp.parse(options(), args);
        } catch (ParseException e) {
            usage(out, args);
            return null;
        }
        
        if (cl.getArgList().size() != 0) {
            usage(out, args);
            return null;
        }

        return cl;
    }


第三步:根据得到的命令,做出相应的处理,下面给出一个框架

CommandLine cl = getCommandLine(startupOut, args);
 CommandLine cl = getCommandLine(startupOut, args);
        if (cl == null) return;


        if (cl.hasOption('h')) {
          usage(startupOut, args);
          return ;
        }


在解析的过程中如果碰到没有提供的命令,或者错误的命令,commons自动维护错误信息,其他操作的处理由用户自己决定,命令行格式支持许多种,具体请看官网,下一步将学习commons-io包的使用。



你可能感兴趣的:(java)