1 /*
2 * Copyright 2007 - 2007 JEuclid, http://jeuclid.sf.net
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 /* $Id: CommandLineParser.java 310 2007-05-18 20:26:36Z maxberger $ */
18
19 package net.sourceforge.jeuclid.app.support;
20
21 import java.io.File;
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import net.sourceforge.jeuclid.ParameterKey;
26
27 /**
28 * This class contains a command line parser for JEuclid apps.
29 *
30 * @author Max Berger
31 * @version $Revision: 310 $
32 */
33 public final class CommandLineParser {
34
35 private CommandLineParser() {
36 // empty on purpose
37 }
38
39 /**
40 * Results from command line parsing.
41 */
42 public static class ParseResults {
43 private final File source;
44
45 private final File target;
46
47 private final Map<ParameterKey, String> params;
48
49 /**
50 * Construct a ParseResults Object.
51 *
52 * @param s
53 * source file
54 * @param t
55 * target file
56 * @param p
57 * rendering parameters
58 */
59 public ParseResults(final File s, final File t,
60 final Map<ParameterKey, String> p) {
61 this.source = s;
62 this.target = t;
63 this.params = p;
64 }
65
66 /**
67 * @return the params
68 */
69 public final Map<ParameterKey, String> getParams() {
70 return this.params;
71 }
72
73 /**
74 * @return the source
75 */
76 public final File getSource() {
77 return this.source;
78 }
79
80 /**
81 * @return the target
82 */
83 public final File getTarget() {
84 return this.target;
85 }
86
87 }
88
89 /**
90 * parses the command line and returns its values.
91 *
92 * @param args
93 * the command line
94 * @return a ParseResults instance
95 */
96 public static ParseResults parseCommandLine(final String[] args) {
97 File source = null;
98 File target = null;
99 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 }