001 /*
002 * Copyright 2007 - 2007 JEuclid, http://jeuclid.sf.net
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 /* $Id: CommandLineParser.java 310 2007-05-18 20:26:36Z maxberger $ */
018
019 package net.sourceforge.jeuclid.app.support;
020
021 import java.io.File;
022 import java.util.HashMap;
023 import java.util.Map;
024
025 import net.sourceforge.jeuclid.ParameterKey;
026
027 /**
028 * This class contains a command line parser for JEuclid apps.
029 *
030 * @author Max Berger
031 * @version $Revision: 310 $
032 */
033 public final class CommandLineParser {
034
035 private CommandLineParser() {
036 // empty on purpose
037 }
038
039 /**
040 * Results from command line parsing.
041 */
042 public static class ParseResults {
043 private final File source;
044
045 private final File target;
046
047 private final Map<ParameterKey, String> params;
048
049 /**
050 * Construct a ParseResults Object.
051 *
052 * @param s
053 * source file
054 * @param t
055 * target file
056 * @param p
057 * rendering parameters
058 */
059 public ParseResults(final File s, final File t,
060 final Map<ParameterKey, String> p) {
061 this.source = s;
062 this.target = t;
063 this.params = p;
064 }
065
066 /**
067 * @return the params
068 */
069 public final Map<ParameterKey, String> getParams() {
070 return this.params;
071 }
072
073 /**
074 * @return the source
075 */
076 public final File getSource() {
077 return this.source;
078 }
079
080 /**
081 * @return the target
082 */
083 public final File getTarget() {
084 return this.target;
085 }
086
087 }
088
089 /**
090 * parses the command line and returns its values.
091 *
092 * @param args
093 * the command line
094 * @return a ParseResults instance
095 */
096 public static ParseResults parseCommandLine(final String[] args) {
097 File source = null;
098 File target = null;
099 int count = 0;
100 final Map<ParameterKey, String> params = new HashMap<ParameterKey, String>();
101 String option = null;
102 for (int i = 0; i < args.length; i++) {
103 final String curArg = args[i];
104 if (curArg.startsWith("-")) {
105 option = curArg.substring(1);
106 } else {
107 if (option != null) {
108 final String value = args[i];
109 final ParameterKey key = ParameterKey.valueOf(option);
110 params.put(key, value);
111 option = null;
112 } else if (count == 0) {
113 source = new File(curArg);
114 } else if (count == 1) {
115 target = new File(curArg);
116 } else {
117 throw new IllegalArgumentException("To many files given");
118 }
119 count++;
120 }
121 }
122 if (option != null) {
123 throw new IllegalArgumentException("No value given for " + option);
124 }
125 return new ParseResults(source, target, params);
126 }
127 }