blob: 6501f8fa32f6390138b008a2b4a792b471389352 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
package org.kohsuke.args4j.spi;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.OptionDef;
/**
* OptionHandler which handles an option with exactly one argument, like "-foo bar".
* @author Jan Materne
* @since 2.0.9
* @param <T> Type of the Setter-class
*/
public abstract class OneArgumentOptionHandler<T> extends OptionHandler<T> {
public OneArgumentOptionHandler(CmdLineParser parser, OptionDef option, Setter<? super T> setter) {
super(parser, option, setter);
}
@Override
public String getDefaultMetaVariable() {
return "N";
}
@Override
public int parseArguments(Parameters params) throws CmdLineException {
String token = params.getParameter(0);
try {
T value = parse(token);
setter.addValue(value);
}
catch (NumberFormatException ex) {
throw new CmdLineException(owner, Messages.ILLEGAL_OPERAND.format(params.getParameter(-1),token));
}
return 1;
}
/**
* Parses a string to a real value of Type <T>.
* @param argument String value to parse
* @return the parsed value
* @throws NumberFormatException if parsing is not possible
* @throws CmdLineException
* if the parsing encounters a failure that should be reported to the user.
*/
protected abstract T parse(String argument) throws NumberFormatException, CmdLineException;
}
|