View Javadoc

1   package org.kohsuke.args4j.spi;
2   
3   import org.kohsuke.args4j.CmdLineException;
4   
5   /***
6    * List of arguments.
7    *
8    * <p>
9    * Object of this interface is passed to
10   * {@link OptionHandler}s to make it easy/safe to parse
11   * additional parameters for options.
12   */
13  public abstract class Parameters  {
14  
15      /***
16       * Returns the option name for which the current {@link OptionHandler} is invoked.
17       *
18       * @return always non-null.
19       */
20      public abstract String getOptionName();
21  
22      /***
23       * Gets the additional parameter to this option.
24       *
25       * @param idx
26       *      specifying 0 will retrieve the token next to the option.
27       *      For example, if the command line looks like "-o abc -d x",
28       *      then <code>getParameter(0)</code> for "-o" returns "abc"
29       *      and <code>getParameter(1)</code> will return "-d".
30       *
31       * @return
32       *      Always return non-null valid String. If an attempt is
33       *      made to access a non-existent index, this method throws
34       *      appropriate {@link org.kohsuke.args4j.CmdLineException}.
35       */
36      public abstract String getParameter(int idx) throws CmdLineException;
37  
38      /***
39       * Returns the number of available parameters.
40       *
41       * @return
42       *      the smallest integer for which {@link #getParameter(int)} reports
43       *      an error.
44       */
45      public abstract int getParameterCount();
46  
47      /***
48       * The convenience method of
49       * <code>Integer.parseInt(getParameter(idx))</code>
50       * with proper error handling.
51       *
52       * @exception CmdLineException
53       *      If the parameter is not an integer, it throws an
54       *      approrpiate {@link CmdLineException}.
55       */
56      public int getIntParameter(int idx) throws CmdLineException {
57          String token = getParameter(idx);
58          try {
59              return Integer.parseInt(token);
60          } catch( NumberFormatException e ) {
61              throw new CmdLineException(Messages.ILLEGAL_OPERAND.format(getOptionName(),token));
62          }
63      }
64  }