Java中带有变量名的特定格式的命令行参数。

huangapple go评论59阅读模式
英文:

command line argument with variable name in java in specific format

问题

我正在尝试获得一个类似于以下格式的命令行界面(CLI)以用于我的课程:

java demo --age 20 --name Amr --school Academy_of_technology --course CS

我该如何实现这个?

我看到了一个解决方案,并尝试了这个链接中的方法:https://stackoverflow.com/questions/54092354/command-line-arguments-with-variable-name

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());
});

但是上述代码的输入格式是类似于javac demo --age=20 --name=Amar school=AOT course=CS的。

而我希望我的输入格式像这样:

java demo --age 20 --name Amr --school Academy_of_technology --course CS

所以我用空格替换了"=",结果得到了预期之外的数组越界。我在考虑是否可以使用正则表达式。

该代码始终期望有4个输入参数。

英文:

I am trying to get a CLI for my class in the format of

java demo --age 20 --name Amr --school Academy_of_technology --course CS

how do I achieve this.

I saw one of the solutions and tried that over here https://stackoverflow.com/questions/54092354/command-line-arguments-with-variable-name


Map&lt;String, String&gt; argsMap = new LinkedHashMap&lt;&gt;();
    for (String arg: args) {
        String[] parts = arg.split(&quot;=&quot;);
        argsMap.put(parts[0], parts[1]);
    }

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

but the above code's input format was like javac demo --age=20 --name=Amar school=AOT course=CS

and i want my i/p format to be like this
java demo --age 20 --name Amr --school Academy_of_technology --course CS

so i replaced the "=" with " " and i got array out of bounds as epected.
I was thinking if regex would be the way.

The code always expects 4 input.

答案1

得分: 1

以下代码将在仅以空格分隔键值对的情况下起作用。

public static void main(String[] args) {

    Map<String, String> argMap = new LinkedHashMap<>();

    for(int ind=0; ind<args.length; ind+=2) {
        argMap.put(args[ind], args[ind+1]);
    }

    for(Map.Entry<String, String> entrySet: argMap.entrySet()) {
        String property = entrySet.getKey().substring(2);
        String value = entrySet.getValue();
        System.out.println("key = " + property + ", value = " + value);
    }

}
英文:

The below code will work if key and value pairs are only space-separated.

public static void main(String[] args) {

	Map&lt;String, String&gt; argMap = new LinkedHashMap&lt;&gt;();

	for(int ind=0; ind&lt;args.length; ind+=2) {
		argMap.put(args[ind], args[ind+1]);
	}

	for(Map.Entry&lt;String, String&gt; entrySet: argMap.entrySet()) {
		String property = entrySet.getKey().substring(2);
		String value = entrySet.getValue();
		System.out.println(&quot;key = &quot; + property + &quot;, value = &quot; + value);
	}

}

huangapple
  • 本文由 发表于 2020年9月28日 14:10:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/64096819.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定