SubcommandHandler.java

  1. /*
  2.  * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */

  10. package org.eclipse.jgit.pgm.opt;

  11. import org.eclipse.jgit.pgm.CommandCatalog;
  12. import org.eclipse.jgit.pgm.CommandRef;
  13. import org.eclipse.jgit.pgm.TextBuiltin;
  14. import org.eclipse.jgit.pgm.internal.CLIText;
  15. import org.kohsuke.args4j.CmdLineException;
  16. import org.kohsuke.args4j.CmdLineParser;
  17. import org.kohsuke.args4j.OptionDef;
  18. import org.kohsuke.args4j.spi.OptionHandler;
  19. import org.kohsuke.args4j.spi.Parameters;
  20. import org.kohsuke.args4j.spi.Setter;

  21. /**
  22.  * Custom Argument handler for jgit command selection.
  23.  * <p>
  24.  * Translates a single argument string to a
  25.  * {@link org.eclipse.jgit.pgm.TextBuiltin} instance which we can execute at
  26.  * runtime with the remaining arguments of the parser.
  27.  */
  28. public class SubcommandHandler extends OptionHandler<TextBuiltin> {
  29.     private final org.eclipse.jgit.pgm.opt.CmdLineParser clp;

  30.     /**
  31.      * Create a new handler for the command name.
  32.      * <p>
  33.      * This constructor is used only by args4j.
  34.      *
  35.      * @param parser
  36.      *            a {@link org.kohsuke.args4j.CmdLineParser} object.
  37.      * @param option
  38.      *            a {@link org.kohsuke.args4j.OptionDef} object.
  39.      * @param setter
  40.      *            a {@link org.kohsuke.args4j.spi.Setter} object.
  41.      */
  42.     public SubcommandHandler(final CmdLineParser parser,
  43.             final OptionDef option, final Setter<? super TextBuiltin> setter) {
  44.         super(parser, option, setter);
  45.         clp = (org.eclipse.jgit.pgm.opt.CmdLineParser) parser;
  46.     }

  47.     /** {@inheritDoc} */
  48.     @Override
  49.     public int parseArguments(Parameters params) throws CmdLineException {
  50.         final String name = params.getParameter(0);
  51.         final CommandRef cr = CommandCatalog.get(name);
  52.         if (cr == null)
  53.             throw new CmdLineException(clp,
  54.                     CLIText.format(CLIText.get().notAJgitCommand), name);

  55.         // Force option parsing to stop. Everything after us should
  56.         // be arguments known only to this command and must not be
  57.         // recognized by the current parser.
  58.         //
  59.         owner.stopOptionParsing();
  60.         setter.addValue(cr.create());
  61.         return 1;
  62.     }

  63.     /** {@inheritDoc} */
  64.     @Override
  65.     public String getDefaultMetaVariable() {
  66.         return CLIText.get().metaVar_command;
  67.     }
  68. }