1

I have a program where commands are executed through Runtime.getRuntime().exec(cmdArray[]). The user can append additional switches to these commands by entering them in a textbox.

Example:

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

Is there a java function that would allow me to do this? or do I have to write it myself (sounds tedious since I have to handle the single and double quotes, all the escaping etc...)?

Thanks

EDIT: Didn't want the extra dependency so I wrote a simple method, it doesn't cover everything but it does what I want

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;
}
user2711115
  • 457
  • 3
  • 18

2 Answers2

0

Use Apache Common CLI library. It can parse options and arguments very well. http://commons.apache.org/proper/commons-cli/

KKKCoder
  • 903
  • 9
  • 18
0

There are numerous Java libraries "out there" that can help you with command line parsing; see How to parse command line arguments in Java? for examples.

However, parsing command arguments "intelligently" depends on where you are coming from:

  • If you want something that will parse command arguments according to a consistent syntax or meta-syntax, then some of those examples will surely do that.

  • If you want something that will magically understand what the user really means, then you are dreaming

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216