You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CmdLineParser.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. package org.eclipse.jgit.pgm.opt;
  44. import java.lang.reflect.Field;
  45. import java.util.ArrayList;
  46. import org.kohsuke.args4j.Argument;
  47. import org.kohsuke.args4j.CmdLineException;
  48. import org.kohsuke.args4j.IllegalAnnotationError;
  49. import org.kohsuke.args4j.NamedOptionDef;
  50. import org.kohsuke.args4j.Option;
  51. import org.kohsuke.args4j.OptionDef;
  52. import org.kohsuke.args4j.spi.OptionHandler;
  53. import org.kohsuke.args4j.spi.Setter;
  54. import org.eclipse.jgit.lib.ObjectId;
  55. import org.eclipse.jgit.lib.Repository;
  56. import org.eclipse.jgit.pgm.TextBuiltin;
  57. import org.eclipse.jgit.pgm.internal.CLIText;
  58. import org.eclipse.jgit.revwalk.RevCommit;
  59. import org.eclipse.jgit.revwalk.RevTree;
  60. import org.eclipse.jgit.revwalk.RevWalk;
  61. import org.eclipse.jgit.transport.RefSpec;
  62. import org.eclipse.jgit.treewalk.AbstractTreeIterator;
  63. /**
  64. * Extended command line parser which handles --foo=value arguments.
  65. * <p>
  66. * The args4j package does not natively handle --foo=value and instead prefers
  67. * to see --foo value on the command line. Many users are used to the GNU style
  68. * --foo=value long option, so we convert from the GNU style format to the
  69. * args4j style format prior to invoking args4j for parsing.
  70. */
  71. public class CmdLineParser extends org.kohsuke.args4j.CmdLineParser {
  72. static {
  73. registerHandler(AbstractTreeIterator.class,
  74. AbstractTreeIteratorHandler.class);
  75. registerHandler(ObjectId.class, ObjectIdHandler.class);
  76. registerHandler(RefSpec.class, RefSpecHandler.class);
  77. registerHandler(RevCommit.class, RevCommitHandler.class);
  78. registerHandler(RevTree.class, RevTreeHandler.class);
  79. }
  80. private final Repository db;
  81. private RevWalk walk;
  82. /**
  83. * Creates a new command line owner that parses arguments/options and set
  84. * them into the given object.
  85. *
  86. * @param bean
  87. * instance of a class annotated by {@link Option} and
  88. * {@link Argument}. this object will receive values.
  89. *
  90. * @throws IllegalAnnotationError
  91. * if the option bean class is using args4j annotations
  92. * incorrectly.
  93. */
  94. public CmdLineParser(final Object bean) {
  95. this(bean, null);
  96. }
  97. /**
  98. * Creates a new command line owner that parses arguments/options and set
  99. * them into the given object.
  100. *
  101. * @param bean
  102. * instance of a class annotated by {@link Option} and
  103. * {@link Argument}. this object will receive values.
  104. * @param repo
  105. * repository this parser can translate options through.
  106. * @throws IllegalAnnotationError
  107. * if the option bean class is using args4j annotations
  108. * incorrectly.
  109. */
  110. public CmdLineParser(final Object bean, Repository repo) {
  111. super(bean);
  112. if (repo == null && bean instanceof TextBuiltin)
  113. repo = ((TextBuiltin) bean).getRepository();
  114. this.db = repo;
  115. }
  116. @Override
  117. public void parseArgument(final String... args) throws CmdLineException {
  118. final ArrayList<String> tmp = new ArrayList<String>(args.length);
  119. for (int argi = 0; argi < args.length; argi++) {
  120. final String str = args[argi];
  121. if (str.equals("--")) { //$NON-NLS-1$
  122. while (argi < args.length)
  123. tmp.add(args[argi++]);
  124. break;
  125. }
  126. if (str.startsWith("--")) { //$NON-NLS-1$
  127. final int eq = str.indexOf('=');
  128. if (eq > 0) {
  129. tmp.add(str.substring(0, eq));
  130. tmp.add(str.substring(eq + 1));
  131. continue;
  132. }
  133. }
  134. tmp.add(str);
  135. }
  136. super.parseArgument(tmp.toArray(new String[tmp.size()]));
  137. }
  138. /**
  139. * Get the repository this parser translates values through.
  140. *
  141. * @return the repository, if specified during construction.
  142. */
  143. public Repository getRepository() {
  144. if (db == null)
  145. throw new IllegalStateException(CLIText.get().noGitRepositoryConfigured);
  146. return db;
  147. }
  148. /**
  149. * Get the revision walker used to support option parsing.
  150. *
  151. * @return the revision walk used by this option parser.
  152. */
  153. public RevWalk getRevWalk() {
  154. if (walk == null)
  155. walk = new RevWalk(getRepository());
  156. return walk;
  157. }
  158. /**
  159. * Get the revision walker used to support option parsing.
  160. * <p>
  161. * This method does not initialize the RevWalk and may return null.
  162. *
  163. * @return the revision walk used by this option parser, or null.
  164. */
  165. public RevWalk getRevWalkGently() {
  166. return walk;
  167. }
  168. static class MyOptionDef extends OptionDef {
  169. public MyOptionDef(OptionDef o) {
  170. super(o.usage(), o.metaVar(), o.required(), o.handler(), o
  171. .isMultiValued());
  172. }
  173. @Override
  174. public String toString() {
  175. if (metaVar() == null)
  176. return "ARG"; //$NON-NLS-1$
  177. try {
  178. Field field = CLIText.class.getField(metaVar());
  179. String ret = field.get(CLIText.get()).toString();
  180. return ret;
  181. } catch (Exception e) {
  182. e.printStackTrace(System.err);
  183. return metaVar();
  184. }
  185. }
  186. }
  187. @Override
  188. protected OptionHandler createOptionHandler(OptionDef o, Setter setter) {
  189. if (o instanceof NamedOptionDef)
  190. return super.createOptionHandler(o, setter);
  191. else
  192. return super.createOptionHandler(new MyOptionDef(o), setter);
  193. }
  194. }