【问题标题】:"intelligently" parsing user input arguments [duplicate]“智能”解析用户输入参数
【发布时间】:2013-08-26 13:57:41
【问题描述】:

我有一个程序通过 Runtime.getRuntime().exec(cmdArray[]) 执行命令。 用户可以通过在文本框中输入这些命令来为这些命令附加额外的开关。

例子:

cmdArray[] = {"someprogram", "--something", "123"} //this is the initial command
//textbox is   -A "bla bla bla"   notice the quotes

//do something...

cmdArray[] = {"someprogram", "--something", "123", "-A", "bla bla bla"} //parsed array

是否有允许我这样做的 java 函数?还是我必须自己写(听起来很乏味,因为我必须处理单引号和双引号,所有转义等等......)?

谢谢

编辑: 不想要额外的依赖,所以我写了一个简单的方法,它并没有涵盖所有内容,但它可以满足我的需求

public String[] getCmdArray(String cmd) { // Parses a regular command line and returns it as a string array for use with Runtime.exec()
    ArrayList<String> cmdArray = new ArrayList<String>();
    StringBuffer argBuffer = new StringBuffer();
    char[] quotes = {'"', '\''};
    char currentChar = 0, protect = '\\', separate = ' ';
    int cursor = 0;
    cmd = cmd.trim();
    while(cursor < cmd.length()) {
        currentChar = cmd.charAt(cursor);

        // Handle protected characters
        if(currentChar == protect) {
            if(cursor + 1 < cmd.length()) {
                char protectedChar = cmd.charAt(cursor + 1);
                argBuffer.append(protectedChar);
                cursor += 2;
            }
            else
                return null; // Unprotected \ at end of cmd
        }

        // Handle quoted args
        else if(inArray(currentChar, quotes)) {
            int nextQuote = cmd.indexOf(currentChar, cursor + 1);
            if(nextQuote != -1) {
                cmdArray.add(cmd.substring(cursor + 1, nextQuote));
                cursor = nextQuote + 1;
            }
            else
                return null; // Unprotected, unclosed quote
        }

        // Handle separator
        else if(currentChar == separate) {
            if(argBuffer.length() != 0)
                cmdArray.add(argBuffer.toString());
            argBuffer.setLength(0);
            cursor++;
        }

        else {
            argBuffer.append(currentChar);
            cursor++;
        }
    }

    if(currentChar != 0) // Handle the last argument (doesn't have a space after it)
        cmdArray.add(argBuffer.toString());

    return cmdArray.toArray(new String[cmdArray.size()]);
}

public boolean inArray(char needle, char[] stack) {
    for(char c : stack)
        if(needle == c)
            return true;
    return false;
}

【问题讨论】:

  • 我很确定您必须自己处理。

标签: java parsing escaping arguments quotes


【解决方案1】:

使用 Apache 通用 CLI 库。它可以很好地解析选项和参数。 http://commons.apache.org/proper/commons-cli/

【讨论】:

    【解决方案2】:

    “那里”有许多 Java 库可以帮助您进行命令行解析;有关示例,请参见 How to parse command line arguments in Java?

    但是,“智能”解析命令参数取决于您来自哪里:

    • 如果您希望根据一致的语法或元语法解析命令参数,那么其中一些示例肯定会做到这一点。

    • 如果您想要一些能够神奇地理解用户真正意思的东西,那么您就是在做梦

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-02
      • 1970-01-01
      • 2014-03-14
      • 2020-06-05
      • 2011-07-24
      • 2010-09-11
      • 2020-04-18
      相关资源
      最近更新 更多