Java命令行参数类 Commons CLI使用

本文介绍了Apache Commons CLI库的基本用法,包括不同类型的选项支持(如POSIX、GNU、Java属性等),并提供了一个Java示例程序,展示了如何解析命令行参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、介绍

Commons CLI库是apache的一个子项目,比较起来,其使用方法相对规范。
http://commons.apache.org/proper/commons-cli/

Commons CLI supports different types of options:

POSIX like options (ie. tar -zxvf foo.tar.gz)
GNU like long options (ie. du --human-readable --max-depth=1)
Java like properties (ie. java -Djava.awt.headless=true -Djava.net.useSystemProxies=true Foo)
Short options with value attached (ie. gcc -O2 foo.c)
long options with single hyphen (ie. ant -projecthelp)
A typical help message displayed by Commons CLI looks like this:

usage: ls
 -A,--almost-all          do not list implied . and ..
 -a,--all                 do not hide entries starting with .
 -B,--ignore-backups      do not list implied entried ending with ~
 -b,--escape              print octal escapes for nongraphic characters
    --block-size <SIZE>   use SIZE-byte blocks
 -c                       with -lt: sort by, and show, ctime (time of last
                          modification of file status information) with
                          -l:show ctime and sort by name otherwise: sort
                          by ctime
 -C                       list entries by columns

二、例子

package Test;

import org.apache.commons.cli.BasicParser;
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.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;

public class CLITest {
    public static void main(String[] args) throws ParseException {
          Options options = new Options();
          //短选项,长选项,选项后是否有参数,描述
          Option option = new Option("n", "name", true, "the name of this agent");
          option.setRequired(true);//必须设置
          options.addOption(option);

          option = new Option("f", "conf-file", true,
              "specify a config file (required if -z missing)");
          option.setRequired(false);
          options.addOption(option);

          option = new Option("h", "help", false, "display help text");
          options.addOption(option);

          //CommandLineParser parser = new PosixParser();//Posix风格
          CommandLineParser parser = new GnuParser();//gun风格
          CommandLine commandLine = parser.parse(options, args);

         //判断
          if (commandLine.hasOption('h')) {
            //格式化输出
            new HelpFormatter().printHelp("flume-ng agent", options, true);
            return;
          }
          if (commandLine.hasOption('f')) {
          //获取参数
          String file = commandLine.getOptionValue('f');
          }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值