如何解析 Java 中的命令行参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/367706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 13:53:20  来源:igfitidea点击:

How do I parse command line arguments in Java?

javacommand-linecommand-line-argumentspicocli

提问by lindelof

What is a good way of parsing command line arguments in Java?

在 Java 中解析命令行参数的好方法是什么?

采纳答案by Vinko Vrsalovic

Check these out:

检查这些:

Or roll your own:

或者自己动手:



For instance,this is how you use commons-clito parse 2 string arguments:

例如,这是您commons-cli用来解析 2 个字符串参数的方式:

import org.apache.commons.cli.*;

public class Main {


    public static void main(String[] args) throws Exception {

        Options options = new Options();

        Option input = new Option("i", "input", true, "input file path");
        input.setRequired(true);
        options.addOption(input);

        Option output = new Option("o", "output", true, "output file");
        output.setRequired(true);
        options.addOption(output);

        CommandLineParser parser = new DefaultParser();
        HelpFormatter formatter = new HelpFormatter();
        CommandLine cmd;

        try {
            cmd = parser.parse(options, args);
        } catch (ParseException e) {
            System.out.println(e.getMessage());
            formatter.printHelp("utility-name", options);

            System.exit(1);
        }

        String inputFilePath = cmd.getOptionValue("input");
        String outputFilePath = cmd.getOptionValue("output");

        System.out.println(inputFilePath);
        System.out.println(outputFilePath);

    }

}

usage from command line:

从命令行使用:

$> java -jar target/my-utility.jar -i asd                                                                                       
Missing required option: o

usage: utility-name
 -i,--input <arg>    input file path
 -o,--output <arg>   output file

回答by Marc Novakowski

Take a look at the Commons CLIproject, lots of good stuff in there.

看看Commons CLI项目,里面有很多好东西。

回答by mepcotterell

Maybe these

也许这些

回答by GaryF

I've used JOpt and found it quite handy: http://jopt-simple.sourceforge.net/

我使用过 JOpt 并发现它非常方便:http://jopt-simple.sourceforge.net/

The front page also provides a list of about 8 alternative libraries, check them out and pick the one that most suits your needs.

首页还提供了大约 8 个替代库的列表,请查看它们并选择最适合您需求的库。

回答by OscarRyz

Yeap.

是的。

I think you're looking for something like this: http://commons.apache.org/cli

我想你正在寻找这样的东西:http: //commons.apache.org/cli

The Apache Commons CLI library provides an API for processing command line interfaces.

Apache Commons CLI 库提供了一个用于处理命令行界面的 API。

回答by Alex Miller

You might find this meta-article of unhappiness interesting as a jumping off point:

你可能会发现这篇关于不快乐的元文章很有趣,作为一个起点:

http://furiouspurpose.blogspot.com/2008/07/command-line-parsing-libraries-for-java.html

http://furious purpose.blogspot.com/2008/07/command-line-parsing-libraries-for-java.html

回答by André

Someone pointed me to args4jlately which is annotation based. I really like it!

最近有人向我指出了基于注释的args4j。我很喜欢!

回答by Cedric Beust

Take a look at the more recent JCommander.

看看最近的JCommander

I created it. I'm happy to receive questions or feature requests.

我创造了它。我很高兴收到问题或功能请求。