| 1 | /********************************************************************** |
| 2 | * Copyright (c) 2013, 2014 Ericsson |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are |
| 5 | * made available under the terms of the Eclipse Public License v1.0 which |
| 6 | * accompanies this distribution, and is available at |
| 7 | * http://www.eclipse.org/legal/epl-v10.html |
| 8 | * |
| 9 | * Contributors: |
| 10 | * Matthew Khouzam- Initial API and implementation |
| 11 | **********************************************************************/ |
| 12 | |
| 13 | package org.eclipse.linuxtools.internal.tracing.rcp.ui.cli; |
| 14 | |
| 15 | import java.util.HashMap; |
| 16 | import java.util.Map; |
| 17 | |
| 18 | import org.eclipse.linuxtools.internal.tracing.rcp.ui.messages.Messages; |
| 19 | |
| 20 | /** |
| 21 | * Command line parser |
| 22 | * |
| 23 | * @author Matthew Khouzam |
| 24 | */ |
| 25 | public class CliParser { |
| 26 | |
| 27 | private static final String NOUI_ARG = "--NOUI"; //$NON-NLS-1$ |
| 28 | |
| 29 | private static final String OPEN_ARG = "--open"; //$NON-NLS-1$ |
| 30 | |
| 31 | private final Map<String, String> params = new HashMap<>(); |
| 32 | |
| 33 | /** Open key */ |
| 34 | public static final String OPEN_FILE_LOCATION = ".,-=open=-,."; //$NON-NLS-1$ |
| 35 | /** No ui key */ |
| 36 | public static final String NO_UI = ".,-=noui=-,."; //$NON-NLS-1$ |
| 37 | |
| 38 | /** |
| 39 | * Constructor |
| 40 | * |
| 41 | * @param args |
| 42 | * the command line arguments |
| 43 | * @throws TracingRCPCliException |
| 44 | * an error occurred parsing the cli |
| 45 | */ |
| 46 | public CliParser(final String[] args) throws TracingRCPCliException { |
| 47 | for (int i = 0; i < args.length; i++) { |
| 48 | if (args[i].equals(OPEN_ARG)) { |
| 49 | put(OPEN_FILE_LOCATION, args, i); |
| 50 | // skip since we have two args |
| 51 | i++; |
| 52 | } |
| 53 | else if (args[i].equals(NOUI_ARG)) { |
| 54 | params.put(NO_UI, new String()); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | private void put(String key, String[] args, int pos) throws TracingRCPCliException { |
| 60 | if (args.length <= pos) { |
| 61 | throw new TracingRCPCliException(Messages.CliParser_MalformedCommand + ':' + ' ' + args[pos]); |
| 62 | } |
| 63 | params.put(key, args[pos + 1]); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Get a parameter from the parsed command line |
| 68 | * @param key OPEN_FILE_LOCATION or NO_UI |
| 69 | * @return the value of the parameter, can be null |
| 70 | */ |
| 71 | public String getArgument(String key) { |
| 72 | return params.get(key); |
| 73 | } |
| 74 | |
| 75 | } |