解析可以包含空格的命令行参数

问题描述 投票:0回答:1

我正在为我的组织中的团队构建一个用 java 编写的基于 spring boot 的命令行实用程序。我必须允许团队传递可以包含空格的任意命令行参数。

我尝试查找任何可以执行此操作的第三方库或 Spring 中的库,但我找不到任何可以按照我想要的方式解析参数的库。

java -jar client.jar --args='a=Car b=Truck c="Car faster than Truck" d="Car faster than all"'

在我的命令行实用程序中,我想获取参数。

a=Car
b=Truck
c=Car faster than Truck
d=Car faster than all

我尝试使用正则表达式按空格分割,正如你所知,它不起作用。我认为根据空间来分割它也是行不通的。

解析这些参数的有效方法是什么?

java spring-boot parsing command-line-arguments
1个回答
0
投票

现在我对 Spring-Boot 一无所知,但我要冒个险,命令行与其他任何东西都类似。这里的总体目标是基于空格 but 分割命令行字符串,而不是在双引号内。您可以使用正则表达式使用 Positive Lookahead 表达式和 String#split() 方法来完成此操作。下面是一个简单的例子(一定要阅读代码中的注释):

/* Splits the supplied command-line string based on one (or more) 
   whitespace(s) but, not those that are contained within double 
   quote marks. This utilizes a Positive Lookahead regular expression
   for the String#split() method. If you want an explanation of 
   the regex used below, then copy/Paste: 
            \s{1,}(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)
   into the Regular Expression bar in https://regex101.com/      */
String argsString = "'a=Car b=Truck c=\"Car faster than Truck\"  d=\"Car faster than all\"'";
    
String regex = "\\s{1,}(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)";
    
/* Not exactly sure if the single quotes at the beginnind and end 
   of of the overall command-line is actually delivered, so the `if` 
   block below removes them. If they are not part of the overall 
   command-line string then you can just delete this `if` block. */
if (argsString.startsWith("'") && argsString.endsWith("'")) {
    argsString = argsString.substring(1, argsString.length() - 1);
}
    
// Create an array of the provided command-line arguments:
String[] args = argsString.split(regex, -1);
    
/* If you want, remove the double-quote marks from tag values 
   within command-line elements:        */
for (int i = 0; i < args.length; i++) {
    /* Done this way in case there are nested double-quote marks
       (for whatever reason) within the command line Tag Values.
       nested double-quote marks are NOT parsed out: */
    String[] tmp = args[i].split("=");
    if (tmp[1].startsWith("\"") && tmp[1].endsWith("\"")) {
        args[i] = tmp[0] + "=" + tmp[1].substring(1, tmp[1].length() - 1);
    }
}
    
// Display the command-line elements within the console window:
for (String str : args) {
    System.out.println(str);
}
    
System.out.println();
    
/* Further parsing would be to split each arg element 
   based on the "=" character:         */
System.out.println("Tag/Value Delimiter:   =");
StringBuilder sb = new StringBuilder("");
for (String arg : args) {
    String[] elements = arg.split("=");
    sb.append(String.format(
              "Argument Tag: -> %-5s Argument Value: -> %-25s%n", 
              elements[0], elements[1]
            ));
}
System.out.println(sb.toString());

如果此代码已执行任务,那么您应该在控制台窗口中看到类似于下面显示的内容:

a=Car
b=Truck
c=Car faster than Truck
d=Car faster than all

Tag/Value Delimiter:   =
Argument Tag: -> a     Argument Value: -> Car                      
Argument Tag: -> b     Argument Value: -> Truck                    
Argument Tag: -> c     Argument Value: -> Car faster than Truck    
Argument Tag: -> d     Argument Value: -> Car faster than all      
© www.soinside.com 2019 - 2024. All rights reserved.