View Javadoc
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  
11  package org.eclipse.jgit.pgm.opt;
12  
13  import org.eclipse.jgit.pgm.internal.CLIText;
14  import org.eclipse.jgit.transport.RefSpec;
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  /**
23   * Custom argument handler {@link org.eclipse.jgit.transport.RefSpec} from
24   * string values.
25   * <p>
26   * Assumes the parser has been initialized with a Repository.
27   */
28  public class RefSpecHandler extends OptionHandler<RefSpec> {
29  	/**
30  	 * Create a new handler for the command name.
31  	 * <p>
32  	 * This constructor is used only by args4j.
33  	 *
34  	 * @param parser
35  	 *            a {@link org.kohsuke.args4j.CmdLineParser} object.
36  	 * @param option
37  	 *            a {@link org.kohsuke.args4j.OptionDef} object.
38  	 * @param setter
39  	 *            a {@link org.kohsuke.args4j.spi.Setter} object.
40  	 */
41  	public RefSpecHandler(final CmdLineParser parser, final OptionDef option,
42  			final Setter<? super RefSpec> setter) {
43  		super(parser, option, setter);
44  	}
45  
46  	/** {@inheritDoc} */
47  	@Override
48  	public int parseArguments(Parameters params) throws CmdLineException {
49  		setter.addValue(new RefSpec(params.getParameter(0)));
50  		return 1;
51  	}
52  
53  	/** {@inheritDoc} */
54  	@Override
55  	public String getDefaultMetaVariable() {
56  		return CLIText.get().metaVar_refspec;
57  	}
58  }