Java Array Element Get getArgs(final String[] args, final String[] defaultValue, final String... flags)

Here you can find the source of getArgs(final String[] args, final String[] defaultValue, final String... flags)

Description

Retrieve multi argument after the given flag and until the next "-*".

License

LGPL

Parameter

Parameter Description
args the list of arguments.
defaultValue the default value if the flag is not present.
flags the flag (e.g. "-f", "--foo").

Return

the value after the flag, or the default value.

Declaration

public static String[] getArgs(final String[] args, final String[] defaultValue, final String... flags) 

Method Source Code


//package com.java2s;
//License from project: LGPL 

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**/*from w ww . j  a v a 2s.co  m*/
     * Retrieve multi argument after the given flag and until the next "-*".
     *
     * @param args the list of arguments.
     * @param defaultValue the default value if the flag is not present.
     * @param flags the flag (e.g. "-f", "--foo").
     * @return the value after the flag, or the default value.
     */
    public static String[] getArgs(final String[] args, final String[] defaultValue, final String... flags) {
        if (flags == null || flags.length == 0)
            return null;
        if (args == null || args.length == 0)
            return defaultValue;

        //final List<String> r = new ArrayList<>(); // J7
        final List<String> r = new ArrayList<String>(); // J6
        int i = 0;
        outer: for (; i < args.length; i++)
            for (final String flag : flags)
                if (flag.equalsIgnoreCase(args[i]))
                    break outer;
        i++;
        for (; i < args.length; i++) {
            if (args[i].startsWith("-"))
                break;
            r.add(args[i]);
        }
        return r.isEmpty() ? defaultValue : r.toArray(new String[r.size()]);
    }
}

Related

  1. get(T[] array, int index)
  2. get(T[] array, int index)
  3. get(T[] array, int index)
  4. getAllMatches(String[] target, String[] pattern)
  5. getArgPairsSeparatedByChar(String[] args, String separator)
  6. getArgs(String[] args, String... singleArgs)
  7. getArgsOption(String[] args, String option)
  8. getArrayNoNull(Object[] array)
  9. getArrayPreview(final Object[] array, final int previewSize)