1 package org.kohsuke.args4j.spi;
2
3 import org.kohsuke.args4j.CmdLineException;
4 import org.kohsuke.args4j.CmdLineParser;
5 import org.kohsuke.args4j.Option;
6
7 import java.util.ResourceBundle;
8
9
10 /***
11 * Code that parses operands of an option into Java.
12 *
13 * <p>
14 * This class can be extended by application to support additional Java datatypes in option operands.
15 *
16 * <p>
17 * Implementation of this class needs to be registered to args4j by using
18 * {@link CmdLineParser#registerHandler(Class,Class)}
19 *
20 * @author Kohsuke Kawaguchi
21 */
22 public abstract class OptionHandler {
23 /***
24 * The annotation.
25 */
26 public final Option option;
27 /***
28 * Object to be used for setting value.
29 */
30 public final Setter setter;
31 /***
32 * The owner to which this handler belongs to.
33 */
34 public final CmdLineParser owner;
35
36 protected OptionHandler(CmdLineParser parser, Option option, Setter setter) {
37 this.owner = parser;
38 this.option = option;
39 this.setter = setter;
40 }
41
42 /***
43 * Called if the option that this owner recognizes is found.
44 *
45 * @param params
46 * The rest of the arguments. This method can use this
47 * object to access the arguments of the option if necessary.
48 *
49 * The object is valid only during the method call.
50 *
51 * @return
52 * The number of arguments consumed. For example, return 0
53 * if this option doesn't take any parameter.
54 */
55 public abstract int parseArguments( Parameters params ) throws CmdLineException;
56
57 /***
58 * Gets the default meta variable name used to print the usage screen.
59 *
60 * @return null to hide a meta variable.
61 */
62 public abstract String getDefaultMetaVariable();
63
64 public final String getMetaVariable(ResourceBundle rb) {
65 String token = option.metaVar();
66 if(token.length()==0)
67 token = getDefaultMetaVariable();
68 if(token==null) return null;
69
70 if(rb!=null) {
71 String localized = rb.getString(token);
72 if(localized!=null)
73 token = localized;
74 }
75
76 return token;
77 }
78 }