简体   繁体   English

具有变量名称的命令行参数

[英]Command Line Arguments with variable name

Generally when we have to access command line arguments in Java we use, args[] But this will be fine when command line input is like this, 通常当我们必须访问Java中的命令行参数时,我们使用args []但是当命令行输入是这样时,这将是正常的,

//Demo is class
C:\java Demo 1 2

we can access 1 and 2 simply by using args[0] and args[1]. 我们只需使用args [0]和args [1]即可访问1和2。

But I have encountered a situation where the command line input will be like this, 但我遇到过命令行输入如下的情况,

C:\java Demo --num1=12 --num2=14

If I have to get above values of num1 and num2, how do I get it? 如果我必须得到num1和num2的值,我该如何得到它?

I googled a couple of times for command line arguments with minor modification, every time I got the simpler version only. 每次我只获得更简单的版本时,我用google搜索了几次命令行参数并进行了少量修改。

I came across this answer here 我在这里遇到了这个答案

How do I parse command line arguments in Java? 如何在Java中解析命令行参数? .

But I am looking for a simple Java version with no third party libraries. 但我正在寻找一个没有第三方库的简单Java版本。 Yes they make our life easy, but I want to learn Java way first. 是的,他们让我们的生活变得轻松,但我想先学习Java方法。

public static void main(String[] args) {
    //simpler way to access command line arguments
    int first_num = Integer.parseInt(args[0]);
    int second_num = Integer.parseInt(args[1]);

    System.out.println("num1= "+first_num+", num2= "+second_num);

}

Not actually sure if by using 实际上并不确定是否使用

C:\ java Demo --num1=12 --num2=13

we will be able to directly access num1 and num2 in our main method or not. 我们将能够直接访问main方法中的num1和num2。

But I need those values to be available in main method. 但我需要在main方法中提供这些值。

Hope I am clear, please let me know if any info needed. 希望我很清楚,如果需要任何信息,请告诉我。

To achieve what you want in a very simple manner you can try, 你可以尝试以一种非常简单的方式实现自己想要的目标,

    public static void main(String[] args) {
    Map<String, String> argsMap = new LinkedHashMap<>();
    for (String arg: args) {
        String[] parts = arg.split("=");
        argsMap.put(parts[0], parts[1]);
    }

    argsMap.entrySet().forEach(arg-> {
        System.out.println(arg.getKey().replace("--", "") + "=" + arg.getValue());
    });
}

Note: There are more advance ways as per the duplicate post. 注意:根据重复的帖子有更多先进的方法。 But I guess you are looking for a very simple way. 但我想你正在寻找一种非常简单的方法。

I would suggest to have a look at Apache Commons CLI that is exactly made for your requirements: 我建议你看看完全符合你要求的Apache Commons CLI:

http://commons.apache.org/proper/commons-cli/ http://commons.apache.org/proper/commons-cli/

Read the intro first: http://commons.apache.org/proper/commons-cli/introduction.html 首先阅读介绍: http//commons.apache.org/proper/commons-cli/introduction.html

It has a even a nice fluent API: 它有一个非常流畅的API:

Options options = new Options();
options.addOption(OptionBuilder.withLongOpt("num1")
                               .withDescription("some info about the usage")
                               .hasArg()
                               .withArgName("VALUE")
                               .create() );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM