Java command line splitting

What is the recommended way to parse command line in Java. At the same time, I do not mean processing options when they are already in the form of an array (for example, processing "-x", etc.). There are already many questions and answers.

No, I mean splitting the full command line into "tokens." I need to convert a string, for example:

user 123712378 suspend "They are \"bad guys\"" Or\ are\ they? 

... to list / array:

 user 123712378 suspend They are "bad guys" Or are they? 

I am currently just doing a space division, but this obviously cannot handle quotes and escaped spaces.

(quote processing is most important. Hidden spaces would be nice)

Note. My command line is the input from a shell-like web interface. It is not built from main(String[] args)

+6
source share
3 answers

You will need to implement a state machine. You will need to read the character of the string by character and find the next state depending on your next or previous character.
For example, " indicates the beginning of the line, but if it is preceded by the \ character, the current state does not change and is read until the next token, which puts you in the next state.
That is essentially in your example you will have

 read string -> read number ^ - - - | 

Of course, you will need to identify all conditions and special characters that affect or do not affect your condition.
Honestly, I'm not sure why you would like to provide such functionality to the end user.
Traditionally, all cli programs accept input in the standard format -x or --x or --x=s , etc.
This format is well known to ordinary users and is easy to use and is checked as correct.
Traditionally, if we need to provide more "flexible" input for the user, it is best to create a graphical interface. This is what I would suggest.

+1
source

Create args [] back to the string, and then tokenize with regexp:

 public static void main(String[] args) { String commandline = ""; for(String arg : args) { commandline += arg; commandline += " "; } System.out.println(commandline); List<String> list = new ArrayList<String>(); Matcher m = Pattern.compile("([^\"]\\S*|\".+?\")\\s*").matcher(commandline); while (m.find()) list.add(m.group(1)); // Add .replace("\"", "") to remove surrounding quotes. System.out.println(list); } 

The last part I took from here .

0
source

DrJava's ArgumentTokenizer parses the command line so that the Bourne shell and its derivatives execute .

It supports escapes correctly, so bash -c 'echo "\"escaped '\''single'\'' quote\""' gets tokenized in [bash, -c, echo "\"escaped 'single' quote\""] .

0
source

Source: https://habr.com/ru/post/945720/


All Articles