Java Array Element Get getArgs(String[] args, String... singleArgs)

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

Description

Get CLI arguments.

License

Open Source License

Parameter

Parameter Description
args a parameter
singleArgs a parameter

Exception

Parameter Description
Exception an exception

Declaration

public static Map<String, String[]> getArgs(String[] args,
        String... singleArgs) throws Exception 

Method Source Code

//package com.java2s;

import java.util.HashMap;

import java.util.Map;

public class Main {
    /**//from  www  .java2  s. c o  m
     * Get CLI arguments.
     *
     * @param args
     * @param singleArgs
     * @return
     * @throws Exception
     */
    public static Map<String, String[]> getArgs(String[] args,
            String... singleArgs) throws Exception {
        Map<String, String[]> params = new HashMap<String, String[]>();
        for (int i = 0; i < args.length; i++) {
            String inArg = args[i];
            if (!inArg.startsWith("--")) {
                throw new Exception("Wrong argument syntax: " + inArg);
            } else {
                inArg = inArg.substring(2);
            }
            for (int j = 0; j < singleArgs.length; j++) {
                if (singleArgs[j].equals(inArg)) {
                    params.put(inArg, new String[] {});
                }
            }
            if (inArg.contains("=")) {
                String[] keyset = inArg.split("=", 2);
                if (keyset[1].startsWith("{") && keyset[1].endsWith("}")) {
                    params.put(
                            keyset[0],
                            new String[] { keyset[1].substring(1,
                                    keyset[1].length() - 1) });
                } else {
                    params.put(keyset[0], keyset[1].split(","));
                }
            }
        }
        return params;
    }
}

Related

  1. get(T[] array, int index)
  2. get(T[] array, int index)
  3. getAllMatches(String[] target, String[] pattern)
  4. getArgPairsSeparatedByChar(String[] args, String separator)
  5. getArgs(final String[] args, final String[] defaultValue, final String... flags)
  6. getArgsOption(String[] args, String option)
  7. getArrayNoNull(Object[] array)
  8. getArrayPreview(final Object[] array, final int previewSize)
  9. getArrays(byte[] inputArray, int arraySize, boolean zeroPad)