View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.creadur.whisker.cli;
20  
21  import org.apache.commons.cli.CommandLine;
22  import org.apache.commons.cli.Option;
23  import org.apache.commons.cli.OptionGroup;
24  import org.apache.commons.cli.Options;
25  
26  /**
27   * Describes an option on the command line.
28   */
29  public enum CommandLineOption {
30  
31      /** License descriptor command line argument. */
32      LICENSE_DESCRIPTION("license-descriptor", 'l',
33              "use given license descriptor", true, "file", false),
34      /** Application source command line argument. */
35      SOURCE("source", 's', "application source", false, "dir", false),
36      /** Generation command line argument. */
37      ACT_TO_GENERATE("generate", 'g',
38              "generate license and notice", false, null, true),
39      /** Audit command line argument. */
40      ACT_TO_AUDIT("audit", 'a', "report audit details", false, null, true),
41      /** Generate skeleton mete-data command line argument. */
42      ACT_TO_SKELETON("skeleton", 't',
43              "generates skeleton meta-data", false, null, true),
44      /** Print help then exit, ignoring other options. */
45      PRINT_HELP("help", 'h', "print help then exit, ignoring other options.", false, null, false);
46  
47      /**
48       * Creates options for the command line.
49       * @return not null
50       */
51      public static Options options() {
52          final Options options = new Options();
53          final OptionGroup acts = new OptionGroup();
54          acts.setRequired(true);
55          for (final CommandLineOption option : values()) {
56              final Option cliOption = option.create();
57              if (option.isAct) {
58                  acts.addOption(cliOption);
59              } else {
60                  options.addOption(cliOption);
61              }
62          }
63          options.addOptionGroup(acts);
64          return options;
65      }
66  
67      /** The long name used for this command line argument. */
68      private final String longName;
69      /** The character short for this command line argument.*/
70      private final char shortName;
71      /** A description of this command line argument suitable for user.*/
72      private final String description;
73      /** Is this a mandatory argument? */
74      private final boolean required;
75      /** The argument name. */
76      private final String argument;
77      /** Is this argument within the act group? */
78      private final boolean isAct;
79  
80      /**
81       * Describes one argument.
82       * @param longName not null
83       * @param shortName not null
84       * @param description not null
85       * @param required is this mandatory?
86       * @param argument possibly null
87       * @param isAct is this argument an act?
88       */
89      private CommandLineOption(final String longName,
90              final char shortName,
91              final String description,
92              final boolean required,
93              final String argument,
94              final boolean isAct) {
95          this.longName = longName;
96          this.shortName = shortName;
97          this.description = description;
98          this.required = required;
99          this.argument = argument;
100         this.isAct = isAct;
101     }
102 
103     /**
104      * Gets the long name of this command line argument.
105      * @return not null
106      */
107     public String getLongName() {
108         return longName;
109     }
110 
111     /**
112      * Gets the short name of this command line argument.
113      * @return the character short for this option
114      */
115     public char getShortName() {
116         return shortName;
117     }
118 
119     /**
120      * Gets the description for this option.
121      * @return not null
122      */
123     public String getDescription() {
124         return description;
125     }
126 
127     /**
128      * Builds a representation.
129      * @return not null
130      */
131     @SuppressWarnings("static-access")
132     public Option create() {
133         final Option.Builder builder = Option.builder(String.valueOf(getShortName()));
134             builder.required(required)
135                     .desc(getDescription())
136                     .longOpt(getLongName());
137         if (argument != null) {
138             builder.hasArg().argName(argument);
139         }
140         return builder.build();
141     }
142 
143     /**
144      * Gets an option value from the command line.
145      * @param commandLine not null
146      * @return not null
147      */
148     public String getOptionValue(final CommandLine commandLine) {
149         return commandLine.getOptionValue(getShortName());
150     }
151 
152     /**
153      * Is the short name option set?
154      * @param commandLine not null
155      * @return true when the option is present, false otherwise
156      */
157     public boolean isSetOn(final CommandLine commandLine) {
158         return commandLine.hasOption(getShortName());
159     }
160 }