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 5.8KB

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