views:

35

answers:

3

Hi there,

in small program I'm writing, I have to parse a line of user input. Basically what needs to be done is to split the line into an array of strings in the same way as is done with the arguments to main(), ie I'm looking for something like this:

String[] splitArgs(String cmdLine);

I just wonder, if the main methods' arguments are parsed in this way prior to invoking main itself, wouldn't it be possible to call that one instead of writing your own? So, does anyone know where to find that method?

Thanks, Axel

+2  A: 

No, that parsing is done by the shell or system library (on Windows), not by Java. You could use ProcessBuilder and sh -c (or cmd on Windows). Something like:

new ProcessBuilder("sh", "-c", "java Program " + cmdLine).start();
Matthew Flaschen
Oh, thanks... Could have figured out that one myself... ;-)
Axel
@Matthew: I must have completely misunderstood the question (but +1 anyway) but what problem is being solved by using this technique? Can you elaborate?
polygenelubricants
@poly, the simplest way to parse a command line the exact same way as the shell is to use the shell. So if he has a command line meant for Program, this is the easiest way to pass it.
Matthew Flaschen
@Matthew: got it. If there's no real `Program` to speak of, though (i.e. "I" need the `String[]`, not `Program`) it would be interesting to use this technique to have `Program` pass its `String[]` from `main` back to "me" somehow. I'm not sure how to to do the interprocess communication, though. Probably RMI.
polygenelubricants
+3  A: 

On globbing

Command line argument is parsed by the shell; this is why * is usually expanded to a list of files, for example. This is called "globbing", and it happens outside of your program, before the JVM even starts.

See also

Related questions


On splitting strings

As for splitting strings into array of strings, your most basic option is String.split(String regex).

Here's a very simple example:

    String[] parts = "one two three".split(" ");
    for (String part : parts) {
        System.out.println("[" + part + "]");
    }

The above prints:

[one]
[two]
[three]

The String argument to split is a regular expression.

References


Scanner option

Another option you can use is java.util.Scanner. This is a much more improved version of the legacy StringTokenizer.

Related questions


Guava option

For a more powerful String splitting functionality, you can use e.g. Splitter from Guava.

polygenelubricants
Yes, thank you. Forgot about the shell's command line parsing.The reason why I don't want to use String.split is that it will get difficult when trying to handle quotes and escaped quotes within/without quoted strings etc.
Axel
@Axel: I'm not aware of a library that does this for you, but I think regex is the traditional tool for the job. You can write regex that can handle escaped quotes within quotes etc. This is quite a common task that a well-tested pattern is probably available in some regex patterns repository.
polygenelubricants
@Axel: have a look at the `Strings` pattern here: http://www.regular-expressions.info/examplesprogrammer.html
polygenelubricants
A: 

That sort of parsing is usually done by the shell. So the function you're looking for would be part of a completely separate program, which is probably written in C. By the time the Java VM even gets started, the arguments have already been parsed.

David Zaslavsky