Register most of our OptionHandler implementations for automatic use
[egit/florian.git] / org.spearce.jgit.pgm / src / org / spearce / jgit / pgm / opt / CmdLineParser.java
blob62197e4dfebea8efa8bb7d585db762958cbaec7a
1 /*
2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
21 * written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org.spearce.jgit.pgm.opt;
40 import java.util.ArrayList;
42 import org.kohsuke.args4j.Argument;
43 import org.kohsuke.args4j.CmdLineException;
44 import org.kohsuke.args4j.IllegalAnnotationError;
45 import org.kohsuke.args4j.Option;
46 import org.spearce.jgit.lib.ObjectId;
47 import org.spearce.jgit.lib.Repository;
48 import org.spearce.jgit.pgm.TextBuiltin;
49 import org.spearce.jgit.revwalk.RevCommit;
50 import org.spearce.jgit.revwalk.RevTree;
51 import org.spearce.jgit.revwalk.RevWalk;
52 import org.spearce.jgit.transport.RefSpec;
53 import org.spearce.jgit.treewalk.AbstractTreeIterator;
55 /**
56 * Extended command line parser which handles --foo=value arguments.
57 * <p>
58 * The args4j package does not natively handle --foo=value and instead prefers
59 * to see --foo value on the command line. Many users are used to the GNU style
60 * --foo=value long option, so we convert from the GNU style format to the
61 * args4j style format prior to invoking args4j for parsing.
63 public class CmdLineParser extends org.kohsuke.args4j.CmdLineParser {
64 static {
65 registerHandler(AbstractTreeIterator.class,
66 AbstractTreeIteratorHandler.class);
67 registerHandler(ObjectId.class, ObjectIdHandler.class);
68 registerHandler(RefSpec.class, RefSpecHandler.class);
69 registerHandler(RevCommit.class, RevCommitHandler.class);
70 registerHandler(RevTree.class, RevTreeHandler.class);
73 private final Repository db;
75 private RevWalk walk;
77 /**
78 * Creates a new command line owner that parses arguments/options and set
79 * them into the given object.
81 * @param bean
82 * instance of a class annotated by {@link Option} and
83 * {@link Argument}. this object will receive values.
85 * @throws IllegalAnnotationError
86 * if the option bean class is using args4j annotations
87 * incorrectly.
89 public CmdLineParser(final Object bean) {
90 this(bean, null);
93 /**
94 * Creates a new command line owner that parses arguments/options and set
95 * them into the given object.
97 * @param bean
98 * instance of a class annotated by {@link Option} and
99 * {@link Argument}. this object will receive values.
100 * @param repo
101 * repository this parser can translate options through.
102 * @throws IllegalAnnotationError
103 * if the option bean class is using args4j annotations
104 * incorrectly.
106 public CmdLineParser(final Object bean, Repository repo) {
107 super(bean);
108 if (repo == null && bean instanceof TextBuiltin)
109 repo = ((TextBuiltin) bean).getRepository();
110 this.db = repo;
113 @Override
114 public void parseArgument(final String... args) throws CmdLineException {
115 final ArrayList<String> tmp = new ArrayList<String>(args.length);
116 for (int argi = 0; argi < args.length; argi++) {
117 final String str = args[argi];
118 if (str.equals("--")) {
119 while (argi < args.length)
120 tmp.add(args[argi++]);
121 break;
124 if (str.startsWith("--")) {
125 final int eq = str.indexOf('=');
126 if (eq > 0) {
127 tmp.add(str.substring(0, eq));
128 tmp.add(str.substring(eq + 1));
129 continue;
133 tmp.add(str);
136 super.parseArgument(tmp.toArray(new String[tmp.size()]));
140 * Get the repository this parser translates values through.
142 * @return the repository, if specified during construction.
144 public Repository getRepository() {
145 if (db == null)
146 throw new IllegalStateException("No Git repository configured.");
147 return db;
151 * Get the revision walker used to support option parsing.
153 * @return the revision walk used by this option parser.
155 public RevWalk getRevWalk() {
156 if (walk == null)
157 walk = new RevWalk(getRepository());
158 return walk;
162 * Get the revision walker used to support option parsing.
163 * <p>
164 * This method does not initialize the RevWalk and may return null.
166 * @return the revision walk used by this option parser, or null.
168 public RevWalk getRevWalkGently() {
169 return walk;