View Javadoc

1   package com.lexicalscope.jewel.cli;
2   
3   /**
4    * Constructs a Cli from an annotated interface definition.
5    *
6    * @see com.lexicalscope.jewel.cli.Option
7    *
8    * @author Tim Wood
9    */
10  public abstract class CliFactory
11  {
12      /**
13       * Construct a Cli from an annotated interface definition
14       *
15       * @param <O>
16       *            The type of the interface that will be used to present the
17       *            arguments
18       * @param klass
19       *            The annotated interface definition
20       *
21       * @return A Cli configured to create instance of klass
22       *
23       * @throws ArgumentValidationException the arguments do not meet the CLI specification
24       * @throws InvalidOptionSpecificationException the CLI specification is not valid
25       */
26      public static <O> Cli<O> createCli(final Class<O> klass) throws InvalidOptionSpecificationException
27      {
28          return new CliInterfaceImpl<O>(klass);
29      }
30  
31      /**
32       * Construct a Cli from an annotated class
33       *
34       * @param <O>
35       *            The type of the class used to present the arguments
36       * @param options
37       *            The annotated class
38       *
39       * @return A Cli configured to configure the options
40       *
41       * @throws ArgumentValidationException the arguments do not meet the CLI specification
42       * @throws InvalidOptionSpecificationException the CLI specification is not valid
43       */
44      public static <O> Cli<O> createCliUsingInstance(final O options) throws InvalidOptionSpecificationException
45      {
46          return new CliInstanceImpl<O>(options);
47      }
48  
49      /**
50       * Parse arguments from an annotated interface definition
51       *
52       * @param <O>
53       *            The type of the interface that will be used to present the
54       *            arguments
55       * @param klass
56       *            The annotated interface definition
57       * @param arguments
58       *
59       * @return The parsed arguments
60       *
61       * @throws ArgumentValidationException the arguments do not meet the CLI specification
62       * @throws InvalidOptionSpecificationException the CLI specification is not valid
63       */
64      public static <O> O parseArguments(final Class<O> klass, final String... arguments)
65              throws ArgumentValidationException, InvalidOptionSpecificationException
66      {
67          return createCli(klass).parseArguments(arguments);
68      }
69  
70      /**
71       * Parse arguments from an annotated class instance
72       *
73       * @param <O>
74       *            The type of the class used to present the arguments
75       * @param klass
76       *            The annotated interface definition
77       * @param arguments
78       *
79       * @return The parsed arguments
80       *
81       * @throws ArgumentValidationException the arguments do not meet the CLI specification
82       * @throws InvalidOptionSpecificationException the CLI specification is not valid
83       */
84      public static <O> O parseArgumentsUsingInstance(final O options, final String... arguments)
85              throws ArgumentValidationException, InvalidOptionSpecificationException
86      {
87          return createCliUsingInstance(options).parseArguments(arguments);
88      }
89  }