View Javadoc
1   package org.eclipse.jgit.pgm.opt;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import org.eclipse.jgit.pgm.internal.CLIText;
7   import org.kohsuke.args4j.CmdLineException;
8   import org.kohsuke.args4j.CmdLineParser;
9   import org.kohsuke.args4j.OptionDef;
10  import org.kohsuke.args4j.spi.OptionHandler;
11  import org.kohsuke.args4j.spi.Parameters;
12  import org.kohsuke.args4j.spi.Setter;
13  
14  /**
15   * Handler which allows to parse option with few values
16   *
17   * @since 4.2
18   */
19  public class OptionWithValuesListHandler extends OptionHandler<List<?>> {
20  
21  	/**
22  	 * Constructor for OptionWithValuesListHandler.
23  	 *
24  	 * @param parser
25  	 *            a {@link org.kohsuke.args4j.CmdLineParser} object.
26  	 * @param option
27  	 *            a {@link org.kohsuke.args4j.OptionDef} object.
28  	 * @param setter
29  	 *            a {@link org.kohsuke.args4j.spi.Setter} object.
30  	 */
31  	public OptionWithValuesListHandler(CmdLineParser parser,
32  			OptionDef option, Setter<List<?>> setter) {
33  		super(parser, option, setter);
34  	}
35  
36  	/** {@inheritDoc} */
37  	@Override
38  	public int parseArguments(Parameters params) throws CmdLineException {
39  		final List<String> list = new ArrayList<>();
40  		for (int idx = 0; idx < params.size(); idx++) {
41  			final String p;
42  			try {
43  				p = params.getParameter(idx);
44  			} catch (CmdLineException cle) {
45  				break;
46  			}
47  			list.add(p);
48  		}
49  		setter.addValue(list);
50  		return list.size();
51  	}
52  
53  	/** {@inheritDoc} */
54  	@Override
55  	public String getDefaultMetaVariable() {
56  		return CLIText.get().metaVar_values;
57  	}
58  
59  }