Here you can find the source of getArgs(final String[] args, final String[] defaultValue, final String... flags)
Parameter | Description |
---|---|
args | the list of arguments. |
defaultValue | the default value if the flag is not present. |
flags | the flag (e.g. "-f", "--foo"). |
public static String[] getArgs(final String[] args, final String[] defaultValue, final String... flags)
//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()]); } }