@Test public void checkParsing() { assertEquals(asList("-Dfoo=bar"), Args.parse("-Dfoo=bar")); assertEquals(asList("-Dfoo=bar", "-D2=3", "-Dty"), Args.parse("-Dfoo=bar -D2=3 -Dty")); assertEquals(asList("-Dfoo=bar", "-D2=3", "-Dty"), Args.parse("-Dfoo=bar -D2=3 -Dty")); assertEquals(asList("-Dkey with space=value"), Args.parse("\"-Dkey with space=value\"")); assertEquals(asList("-Dkey with space"), Args.parse("\"-Dkey with space\"")); assertEquals(asList("-Dkey with space=value with space"), Args.parse("\"-Dkey with space=value with space\"")); assertEquals(asList("-Dfoo1=bar", "-Dkey with space=value with space", "-Dfoo=bar"), Args.parse("-Dfoo1=bar \"-Dkey with space=value with space\" -Dfoo=bar")); }
public static Collection<String> parse(final String raw) { final Collection<String> result = new LinkedList<>(); Character end = null; boolean escaped = false; final StringBuilder current = new StringBuilder(); for (int i = 0; i < raw.length(); i++) { final char c = raw.charAt(i); if (escaped) { escaped = false; current.append(c); } else if ((end != null && end == c) || (c == ' ' && end == null)) { if (current.length() > 0) { result.add(current.toString()); current.setLength(0); } end = null; } else if (c == '\\') { escaped = true; } else if (c == '"' || c == '\'') { end = c; } else { current.append(c); } } if (current.length() > 0) { result.add(current.toString()); } return result; }
Args { public static Collection<String> parse(final String raw) { final Collection<String> result = new LinkedList<>(); Character end = null; boolean escaped = false; final StringBuilder current = new StringBuilder(); for (int i = 0; i < raw.length(); i++) { final char c = raw.charAt(i); if (escaped) { escaped = false; current.append(c); } else if ((end != null && end == c) || (c == ' ' && end == null)) { if (current.length() > 0) { result.add(current.toString()); current.setLength(0); } end = null; } else if (c == '\\') { escaped = true; } else if (c == '"' || c == '\'') { end = c; } else { current.append(c); } } if (current.length() > 0) { result.add(current.toString()); } return result; } }
Args { public static Collection<String> parse(final String raw) { final Collection<String> result = new LinkedList<>(); Character end = null; boolean escaped = false; final StringBuilder current = new StringBuilder(); for (int i = 0; i < raw.length(); i++) { final char c = raw.charAt(i); if (escaped) { escaped = false; current.append(c); } else if ((end != null && end == c) || (c == ' ' && end == null)) { if (current.length() > 0) { result.add(current.toString()); current.setLength(0); } end = null; } else if (c == '\\') { escaped = true; } else if (c == '"' || c == '\'') { end = c; } else { current.append(c); } } if (current.length() > 0) { result.add(current.toString()); } return result; } private  Args(); }
Args { public static Collection<String> parse(final String raw) { final Collection<String> result = new LinkedList<>(); Character end = null; boolean escaped = false; final StringBuilder current = new StringBuilder(); for (int i = 0; i < raw.length(); i++) { final char c = raw.charAt(i); if (escaped) { escaped = false; current.append(c); } else if ((end != null && end == c) || (c == ' ' && end == null)) { if (current.length() > 0) { result.add(current.toString()); current.setLength(0); } end = null; } else if (c == '\\') { escaped = true; } else if (c == '"' || c == '\'') { end = c; } else { current.append(c); } } if (current.length() > 0) { result.add(current.toString()); } return result; } private  Args(); static Collection<String> parse(final String raw); }
Args { public static Collection<String> parse(final String raw) { final Collection<String> result = new LinkedList<>(); Character end = null; boolean escaped = false; final StringBuilder current = new StringBuilder(); for (int i = 0; i < raw.length(); i++) { final char c = raw.charAt(i); if (escaped) { escaped = false; current.append(c); } else if ((end != null && end == c) || (c == ' ' && end == null)) { if (current.length() > 0) { result.add(current.toString()); current.setLength(0); } end = null; } else if (c == '\\') { escaped = true; } else if (c == '"' || c == '\'') { end = c; } else { current.append(c); } } if (current.length() > 0) { result.add(current.toString()); } return result; } private  Args(); static Collection<String> parse(final String raw);  }