/*
 * Copyright (C) 2009 the original author(s).
 *
 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
 *
 * 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.
 */

package org.sonatype.gshell.util;

import java.lang.reflect.Array;

/**
 * Utils for command-line arguments.
 *
 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
 * @since 2.0
 */
public class Arguments
{
    public static Object[] shift(final Object[] args) {
        return shift(args, 1);
    }

    public static Object[] shift(final Object[] source, int pos) {
        assert source != null;
        assert source.length >= pos;

        Object[] target = (Object[]) Array.newInstance(source.getClass().getComponentType(), source.length - pos);

        System.arraycopy(source, pos, target, 0, target.length);

        return target;
    }

    public static String[] toStringArray(final Object[] args) {
        assert args != null;

        String[] strings = new String[args.length];

        for (int i = 0; i < args.length; i++) {
            strings[i] = String.valueOf(args[i]);
        }

        return strings;
    }
}

@@ -667,16 +667,67 @@
 t.Array;
+%0Aimport java.util.ArrayList;%0Aimport java.util.List;
 %0A%0A/**%0A *
@@ -1598,11 +1598,2778 @@
 s;%0A    %7D
+%0A%0A    public static String%5B%5D clean(final String%5B%5D args) %7B%0A        assert args != null;%0A%0A        List%3CString%3E cleaned = new ArrayList%3CString%3E();%0A%0A        StringBuilder currentArg = null;%0A%0A        for (int i = 0; i %3C args.length; i++) %7B%0A            String arg = args%5Bi%5D;%0A%0A            boolean addedToBuffer = false;%0A%0A            if (arg.startsWith(%22%5C%22%22)) %7B%0A                // if we're in the process of building up another arg, push it and start over.%0A                // this is for the case: %22-Dfoo=bar %22-Dfoo2=bar two%22 (note the first unterminated quote)%0A                if (currentArg != null) %7B%0A                    cleaned.add(currentArg.toString());%0A                %7D%0A%0A                // start building an argument here.%0A                currentArg = new StringBuilder(arg.substring(1));%0A                addedToBuffer = true;%0A            %7D%0A%0A            // this has to be a separate %22if%22 statement, to capture the case of: %22-Dfoo=bar%22%0A            if (arg.endsWith(%22%5C%22%22)) %7B%0A                String cleanArgPart = arg.substring(0, arg.length() - 1);%0A%0A                // if we're building an argument, keep doing so.%0A                if (currentArg != null) %7B%0A                    // if this is the case of %22-Dfoo=bar%22, then we need to adjust the buffer.%0A                    if (addedToBuffer) %7B%0A                        currentArg.setLength(currentArg.length() - 1);%0A                    %7D%0A                    // otherwise, we trim the trailing %22 and append to the buffer.%0A                    else %7B%0A                        // TODO: introducing a space here...not sure what else to do but collapse whitespace%0A                        currentArg.append(' ').append(cleanArgPart);%0A                    %7D%0A%0A                    cleaned.add(currentArg.toString());%0A                %7D%0A                else %7B%0A                    cleaned.add(cleanArgPart);%0A                %7D%0A%0A                currentArg = null;%0A%0A                continue;%0A            %7D%0A%0A            // if we haven't added this arg to the buffer, and we ARE building an argument%0A            // buffer, then append it with a preceding space...again, not sure what else to%0A            // do other than collapse whitespace.%0A            // NOTE: The case of a trailing quote is handled by nullifying the arg buffer.%0A            if (!addedToBuffer) %7B%0A                if (currentArg != null) %7B%0A                    currentArg.append(' ').append(arg);%0A                %7D%0A                else %7B%0A                    cleaned.add(arg);%0A                %7D%0A            %7D%0A        %7D%0A%0A        if (currentArg != null) %7B%0A            cleaned.add(currentArg.toString());%0A        %7D%0A%0A        if (cleaned.isEmpty()) %7B%0A            return args;%0A        %7D%0A        else %7B%0A            return cleaned.toArray(new String%5Bcleaned.size()%5D);%0A        %7D%0A    %7D
 %0A%7D%0A

Add argument cleaner from maven