Java Args Parse parseArguments(final String[] args)

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

Description

Parse commandline arguments

License

Open Source License

Parameter

Parameter Description
args The arg array from the main method, or manual args

Return

The map containing the args

Declaration

public static HashMap<String, Object> parseArguments(final String[] args) 

Method Source Code

//package com.java2s;
/**//from  w  ww . j av  a 2s.co  m
 * Sleeksnap, the open source cross-platform screenshot uploader
 * Copyright (C) 2012 Nikki <nikki@nikkii.us>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.HashMap;

public class Main {
    /**
     * Parse commandline arguments
     * 
     * @param args
     *            The arg array from the main method, or manual args
     * @return The map containing the args
     */
    public static HashMap<String, Object> parseArguments(final String[] args) {
        final HashMap<String, Object> arguments = new HashMap<String, Object>();
        for (String s : args) {
            if (s.startsWith("--")) {
                s = s.substring(2);
            } else if (s.startsWith("-")) {
                s = s.substring(1);
            }
            final int eqIdx = s.indexOf('=');
            final String key = s.substring(0, eqIdx != -1 ? eqIdx : s.length());
            Object value = true;
            if (eqIdx != -1) {
                value = s.substring(s.indexOf('=') + 1);
            }
            arguments.put(key, value);
        }
        return arguments;
    }
}

Related

  1. parseArgs(String argvStart, String[] args)
  2. parseArgs(String[] args)
  3. parseArgs(String[] args)
  4. parseArgs(String[] args)
  5. parseArgs(String[] args)
  6. parseArguments(String[] args)
  7. parseCLInput(String[] args)
  8. parseCommandLine(String[] args)
  9. parseCommandLineArguments(String[] args)