View Javadoc
1   /*
2    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.pgm.opt;
45  
46  import java.io.IOException;
47  import java.io.Writer;
48  import java.lang.reflect.Field;
49  import java.util.ArrayList;
50  import java.util.Collections;
51  import java.util.Iterator;
52  import java.util.List;
53  import java.util.ResourceBundle;
54  
55  import org.eclipse.jgit.lib.ObjectId;
56  import org.eclipse.jgit.lib.Repository;
57  import org.eclipse.jgit.pgm.Die;
58  import org.eclipse.jgit.pgm.TextBuiltin;
59  import org.eclipse.jgit.pgm.internal.CLIText;
60  import org.eclipse.jgit.revwalk.RevCommit;
61  import org.eclipse.jgit.revwalk.RevTree;
62  import org.eclipse.jgit.revwalk.RevWalk;
63  import org.eclipse.jgit.transport.RefSpec;
64  import org.eclipse.jgit.treewalk.AbstractTreeIterator;
65  import org.kohsuke.args4j.Argument;
66  import org.kohsuke.args4j.CmdLineException;
67  import org.kohsuke.args4j.IllegalAnnotationError;
68  import org.kohsuke.args4j.NamedOptionDef;
69  import org.kohsuke.args4j.Option;
70  import org.kohsuke.args4j.OptionDef;
71  import org.kohsuke.args4j.spi.OptionHandler;
72  import org.kohsuke.args4j.spi.RestOfArgumentsHandler;
73  import org.kohsuke.args4j.spi.Setter;
74  
75  /**
76   * Extended command line parser which handles --foo=value arguments.
77   * <p>
78   * The args4j package does not natively handle --foo=value and instead prefers
79   * to see --foo value on the command line. Many users are used to the GNU style
80   * --foo=value long option, so we convert from the GNU style format to the
81   * args4j style format prior to invoking args4j for parsing.
82   */
83  public class CmdLineParser extends org.kohsuke.args4j.CmdLineParser {
84  	static {
85  		registerHandler(AbstractTreeIterator.class,
86  				AbstractTreeIteratorHandler.class);
87  		registerHandler(ObjectId.class, ObjectIdHandler.class);
88  		registerHandler(RefSpec.class, RefSpecHandler.class);
89  		registerHandler(RevCommit.class, RevCommitHandler.class);
90  		registerHandler(RevTree.class, RevTreeHandler.class);
91  		registerHandler(List.class, OptionWithValuesListHandler.class);
92  	}
93  
94  	private final Repository db;
95  
96  	private RevWalk walk;
97  
98  	private boolean seenHelp;
99  
100 	private TextBuiltin cmd;
101 
102 	/**
103 	 * Creates a new command line owner that parses arguments/options and set
104 	 * them into the given object.
105 	 *
106 	 * @param bean
107 	 *            instance of a class annotated by {@link Option} and
108 	 *            {@link Argument}. this object will receive values.
109 	 *
110 	 * @throws IllegalAnnotationError
111 	 *             if the option bean class is using args4j annotations
112 	 *             incorrectly.
113 	 */
114 	public CmdLineParser(final Object bean) {
115 		this(bean, null);
116 	}
117 
118 	/**
119 	 * Creates a new command line owner that parses arguments/options and set
120 	 * them into the given object.
121 	 *
122 	 * @param bean
123 	 *            instance of a class annotated by {@link Option} and
124 	 *            {@link Argument}. this object will receive values.
125 	 * @param repo
126 	 *            repository this parser can translate options through.
127 	 * @throws IllegalAnnotationError
128 	 *             if the option bean class is using args4j annotations
129 	 *             incorrectly.
130 	 */
131 	public CmdLineParser(final Object bean, Repository repo) {
132 		super(bean);
133 		if (bean instanceof TextBuiltin) {
134 			cmd = (TextBuiltin) bean;
135 		}
136 		if (repo == null && cmd != null) {
137 			repo = cmd.getRepository();
138 		}
139 		this.db = repo;
140 	}
141 
142 	@Override
143 	public void parseArgument(final String... args) throws CmdLineException {
144 		final ArrayList<String> tmp = new ArrayList<String>(args.length);
145 		for (int argi = 0; argi < args.length; argi++) {
146 			final String str = args[argi];
147 			if (str.equals("--")) { //$NON-NLS-1$
148 				while (argi < args.length)
149 					tmp.add(args[argi++]);
150 				break;
151 			}
152 
153 			if (str.startsWith("--")) { //$NON-NLS-1$
154 				final int eq = str.indexOf('=');
155 				if (eq > 0) {
156 					tmp.add(str.substring(0, eq));
157 					tmp.add(str.substring(eq + 1));
158 					continue;
159 				}
160 			}
161 
162 			tmp.add(str);
163 
164 			if (containsHelp(args)) {
165 				// suppress exceptions on required parameters if help is present
166 				seenHelp = true;
167 				// stop argument parsing here
168 				break;
169 			}
170 		}
171 		List<OptionHandler> backup = null;
172 		if (seenHelp) {
173 			backup = unsetRequiredOptions();
174 		}
175 
176 		try {
177 			super.parseArgument(tmp.toArray(new String[tmp.size()]));
178 		} catch (Die e) {
179 			if (!seenHelp) {
180 				throw e;
181 			}
182 			printToErrorWriter(CLIText.fatalError(e.getMessage()));
183 		} finally {
184 			// reset "required" options to defaults for correct command printout
185 			if (backup != null && !backup.isEmpty()) {
186 				restoreRequiredOptions(backup);
187 			}
188 			seenHelp = false;
189 		}
190 	}
191 
192 	private void printToErrorWriter(String error) {
193 		if (cmd == null) {
194 			System.err.println(error);
195 		} else {
196 			try {
197 				cmd.getErrorWriter().println(error);
198 			} catch (IOException e1) {
199 				System.err.println(error);
200 			}
201 		}
202 	}
203 
204 	private List<OptionHandler> unsetRequiredOptions() {
205 		List<OptionHandler> options = getOptions();
206 		List<OptionHandler> backup = new ArrayList<>(options);
207 		for (Iterator<OptionHandler> iterator = options.iterator(); iterator
208 				.hasNext();) {
209 			OptionHandler handler = iterator.next();
210 			if (handler.option instanceof NamedOptionDef
211 					&& handler.option.required()) {
212 				iterator.remove();
213 			}
214 		}
215 		return backup;
216 	}
217 
218 	private void restoreRequiredOptions(List<OptionHandler> backup) {
219 		List<OptionHandler> options = getOptions();
220 		options.clear();
221 		options.addAll(backup);
222 	}
223 
224 	/**
225 	 * @param args
226 	 *            non null
227 	 * @return true if the given array contains help option
228 	 * @since 4.2
229 	 */
230 	protected boolean containsHelp(final String... args) {
231 		return TextBuiltin.containsHelp(args);
232 	}
233 
234 	/**
235 	 * Get the repository this parser translates values through.
236 	 *
237 	 * @return the repository, if specified during construction.
238 	 */
239 	public Repository getRepository() {
240 		if (db == null)
241 			throw new IllegalStateException(CLIText.get().noGitRepositoryConfigured);
242 		return db;
243 	}
244 
245 	/**
246 	 * Get the revision walker used to support option parsing.
247 	 *
248 	 * @return the revision walk used by this option parser.
249 	 */
250 	public RevWalk getRevWalk() {
251 		if (walk == null)
252 			walk = new RevWalk(getRepository());
253 		return walk;
254 	}
255 
256 	/**
257 	 * Get the revision walker used to support option parsing.
258 	 * <p>
259 	 * This method does not initialize the RevWalk and may return null.
260 	 *
261 	 * @return the revision walk used by this option parser, or null.
262 	 */
263 	public RevWalk getRevWalkGently() {
264 		return walk;
265 	}
266 
267 	class MyOptionDef extends OptionDef {
268 
269 		public MyOptionDef(OptionDef o) {
270 			super(o.usage(), o.metaVar(), o.required(), o.handler(), o
271 					.isMultiValued());
272 		}
273 
274 		@Override
275 		public String toString() {
276 			if (metaVar() == null)
277 				return "ARG"; //$NON-NLS-1$
278 			try {
279 				Field field = CLIText.class.getField(metaVar());
280 				String ret = field.get(CLIText.get()).toString();
281 				return ret;
282 			} catch (Exception e) {
283 				e.printStackTrace(System.err);
284 				return metaVar();
285 			}
286 		}
287 
288 		@Override
289 		public boolean required() {
290 			return seenHelp ? false : super.required();
291 		}
292 	}
293 
294 	@Override
295 	protected OptionHandler createOptionHandler(OptionDef o, Setter setter) {
296 		if (o instanceof NamedOptionDef)
297 			return super.createOptionHandler(o, setter);
298 		else
299 			return super.createOptionHandler(new MyOptionDef(o), setter);
300 
301 	}
302 
303 	@SuppressWarnings("unchecked")
304 	private List<OptionHandler> getOptions() {
305 		List<OptionHandler> options = null;
306 		try {
307 			Field field = org.kohsuke.args4j.CmdLineParser.class
308 					.getDeclaredField("options"); //$NON-NLS-1$
309 			field.setAccessible(true);
310 			options = (List<OptionHandler>) field.get(this);
311 		} catch (NoSuchFieldException | SecurityException
312 				| IllegalArgumentException | IllegalAccessException e) {
313 			// ignore
314 		}
315 		if (options == null) {
316 			return Collections.emptyList();
317 		}
318 		return options;
319 	}
320 
321 	@Override
322 	public void printSingleLineUsage(Writer w, ResourceBundle rb) {
323 		List<OptionHandler> options = getOptions();
324 		if (options.isEmpty()) {
325 			super.printSingleLineUsage(w, rb);
326 			return;
327 		}
328 		List<OptionHandler> backup = new ArrayList<>(options);
329 		boolean changed = sortRestOfArgumentsHandlerToTheEnd(options);
330 		try {
331 			super.printSingleLineUsage(w, rb);
332 		} finally {
333 			if (changed) {
334 				options.clear();
335 				options.addAll(backup);
336 			}
337 		}
338 	}
339 
340 	private boolean sortRestOfArgumentsHandlerToTheEnd(
341 			List<OptionHandler> options) {
342 		for (int i = 0; i < options.size(); i++) {
343 			OptionHandler handler = options.get(i);
344 			if (handler instanceof RestOfArgumentsHandler
345 					|| handler instanceof PathTreeFilterHandler) {
346 				options.remove(i);
347 				options.add(handler);
348 				return true;
349 			}
350 		}
351 		return false;
352 	}
353 }