Java Args Parse parse(String[] args)

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

Description

parse

License

LGPL

Declaration

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

Method Source Code


//package com.java2s;
/*//  ww  w . ja  v a2  s. co  m
 * This class was created by <AdrianTodt>. It's distributed as
 * part of the DavidBot. Get the Source Code in github:
 * https://github.com/adriantodt/David
 *
 * DavidBot is Open Source and distributed under the
 * GNU Lesser General Public License v2.1:
 * https://github.com/adriantodt/David/blob/master/LICENSE
 *
 * File Created @ [01/11/16 13:23]
 */

import java.util.*;

public class Main {
    public static Map<String, String> parse(String[] args) {
        Map<String, String> options = new HashMap<>();

        for (int i = 0; i < args.length; i++) {
            if (args[i].charAt(0) == '-' || args[i].charAt(0) == '/') //This start with - or /
            {
                args[i] = args[i].substring(1);
                if (i + 1 >= args.length || args[i + 1].charAt(0) == '-' || args[i + 1].charAt(0) == '/') //Next start with - (or last arg)
                {
                    options.put(args[i], "null");
                } else {
                    options.put(args[i], args[i + 1]);
                    i++;
                }
            } else {
                options.put(null, args[i]);
            }
        }

        return options;
    }
}

Related

  1. getPossibleCompletionsForGivenArgs(String[] args, String[] possibilitiesOfCompletion)
  2. getPossibleCompletionsForGivenArgs(String[] args, String[] possibilitiesOfCompletion)
  3. parse(String[] args)
  4. parseAndFillFittingValues(String[] prefixes, String[] values)
  5. parseArg(String[] args)
  6. parseArgs(String argvStart, String[] args)