Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: af6a52844d87672cee77b72d7bd2920acbd193a9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*******************************************************************************
 * Copyright (c) 2007 Symbian Software Systems and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Andrew Ferguson (Symbian) - Initial implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.core.pdom.export;

import com.ibm.icu.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.cdt.core.index.export.Messages;
import org.eclipse.core.runtime.CoreException;

/**
 * Helper methods for command-line options
 * <br>
 * <b>Non-API</b> Should a more suitable way for manipulating command-line arguments become usable
 * in the future we will switch to that.
 */
public class CLIUtil {
	public static final String UNQUALIFIED_PARAMETERS= "UNQUALIFIED_PARAMETERS"; //$NON-NLS-1$
	
	/**
	 * Returns the list of options associated with the specified option
	 * @param arguments the arguments map
	 * @param opt the option name to check
	 * @param number the number of parameters
	 * @throws CoreException if the number of parameters is not the specified expected number
	 */
	public static List<String> getArg(Map<String, List<String>> arguments, String opt, int number) throws CoreException {
		List<String> list = arguments.get(opt);
		if(list==null || list.size()!=number) {
			String msg= MessageFormat.format(Messages.CLIUtil_OptionParametersMismatch, new Object[] {opt, ""+number}); //$NON-NLS-1$
			GeneratePDOMApplication.fail(msg);
		}
		return list;
	}

	/**
	 * Returns a map of String option to List of String parameters.
	 */
	public static Map<String,List<String>> parseToMap(String[] args) {
		Map<String,List<String>> result = new HashMap<String,List<String>>();
		String current = null;
		for (String arg : args) {
			if(arg.startsWith("-")) { //$NON-NLS-1$
				current = arg;
				result.put(current, new ArrayList<String>());
			} else {
				if(current==null) {
					current= UNQUALIFIED_PARAMETERS;
					result.put(current, new ArrayList<String>());
				}
				(result.get(current)).add(arg);
			}
		}
		return result;
	}
}

Back to the top