Java Args Parse parseProgramArguments(String[] args)

Here you can find the source of parseProgramArguments(String[] args)

Description

Parses command line arguments to map.

License

Open Source License

Parameter

Parameter Description
args Command line parameters array.

Return

Argument list map.

Declaration

public static Map<String, List<String>> parseProgramArguments(String[] args) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.*;

public class Main {
    /**/*  w ww.ja va2s. com*/
     * Parses command line arguments to map.
     * Example:
     *      program input output -m 128 912 -f ALL
     *      Map Structure:
     *      arguments
     *      |
     *      |--> '_'    ['input','output']
     *      |--> '-m'   ['128','912']
     *      |--> '-f'   ['ALL']
     * @param args Command line parameters array.
     * @return Argument list map.
     */
    public static Map<String, List<String>> parseProgramArguments(String[] args) {
        Map<String, List<String>> parsedArgs = new LinkedHashMap<String, List<String>>();

        String lastParam = "_";
        for (String arg : args) {

            if (arg.startsWith("-")) {
                if (!lastParam.equals(arg)) {
                    parsedArgs.put(arg, new ArrayList<String>());
                    lastParam = arg;
                }

            } else {
                if (lastParam.equals("_")) {
                    if (!parsedArgs.containsKey("_")) {
                        parsedArgs.put("_", new ArrayList<String>());
                    }
                }

                parsedArgs.get(lastParam).add(arg);
            }

        }

        return parsedArgs;

    }
}

Related

  1. parseCLInput(String[] args)
  2. parseCommandLine(String[] args)
  3. parseCommandLineArguments(String[] args)
  4. parseCommandLineArguments(String[] args)
  5. parseLines(String[] lines)
  6. parseProviderOptions(String[] options)
  7. parseStringArrayToSet(String[] value)