Here you can find the source of parseArgumentString(String command)
Parameter | Description |
---|---|
command | a parameter |
public static String[] parseArgumentString(String command)
//package com.java2s; /*//ww w . j a v a 2 s .c o m * Copyright (c) 2012, the Dart project authors. * * Licensed under the Eclipse Public License v1.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at * * http://www.eclipse.org/legal/epl-v10.html * * Unless required by applicable law or agreed to in writing, software distributed under the License * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express * or implied. See the License for the specific language governing permissions and limitations under * the License. */ import java.util.ArrayList; import java.util.List; public class Main { /** * Convert an argument string into a list of arguments. This essentially splits on the space char, * with handling for quotes and escaped quotes. * <p> * <ul> * <li>foo bar baz ==> [foo][bar][baz] * <li>foo=three bar='one two' ==> [foo=three][bar='one two'] * * @param command * @return */ public static String[] parseArgumentString(String command) { List<String> args = new ArrayList<String>(); StringBuilder builder = new StringBuilder(); boolean inQuote = false; boolean prevWasSlash = false; for (final char c : command.toCharArray()) { if (!prevWasSlash && (c == '\'' || c == '"')) { inQuote = !inQuote; prevWasSlash = false; // Don't include the quote char. continue; } if (c == ' ' && !inQuote) { args.add(builder.toString()); builder.setLength(0); } else { builder.append(c); } prevWasSlash = c == '\\'; } if (builder.length() > 0) { args.add(builder.toString()); } return args.toArray(new String[args.size()]); } }