View Javadoc

1   /*
2    HelpOption.java
3    Creation date : 2/06/2010
4    Copyright © Benjamin Croizet (graffity2199@yahoo.fr)
5   
6    This program is free software; you can redistribute it and/or
7    modify it under the terms of the GNU General Public License
8    or GNU Lesser General Public License as published by the
9    Free Software Foundation; either version 3 of the License,
10   or (at your option) any later version.
11  
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16  
17   You should have received copies of the GNU General Public License
18   and GNU Lesser General Public License along with this program;
19   if not, write to the Free Software Foundation, Inc.,
20   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21   http://www.fsf.org/licensing/licenses/gpl.html
22   http://www.gnu.org/licenses/lgpl.html
23   */
24  
25  package net.sourceforge.plantumldependency.commoncli.option.impl.help;
26  
27  import static java.util.Arrays.asList;
28  import static java.util.Collections.unmodifiableSet;
29  import static java.util.logging.Level.FINE;
30  import static java.util.logging.Logger.getLogger;
31  import static net.sourceforge.plantumldependency.common.utils.check.ParameterCheckerUtils.checkNull;
32  import static net.sourceforge.plantumldependency.common.utils.log.LogUtils.buildLogString;
33  import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.COMMAND_LINE_NULL_ERROR;
34  import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.JAVA_PROGRAM_NULL_ERROR;
35  import static net.sourceforge.plantumldependency.commoncli.constants.log.FineConstants.OPTION_NOT_SPECIFIED_FINE;
36  import static net.sourceforge.plantumldependency.commoncli.option.status.OptionStatus.ACTIVE_OPTIONAL_OPTION_STATUS;
37  
38  import java.util.Set;
39  import java.util.TreeSet;
40  import java.util.logging.Logger;
41  
42  import net.sourceforge.plantumldependency.commoncli.command.CommandLine;
43  import net.sourceforge.plantumldependency.commoncli.exception.CommandLineException;
44  import net.sourceforge.plantumldependency.commoncli.option.AbstractOption;
45  import net.sourceforge.plantumldependency.commoncli.option.execution.ExecutableOption;
46  import net.sourceforge.plantumldependency.commoncli.option.execution.OptionExecution;
47  import net.sourceforge.plantumldependency.commoncli.option.status.OptionStatus;
48  import net.sourceforge.plantumldependency.commoncli.program.JavaProgram;
49  
50  /**
51   * A default implementation managing the "-help" option, displaying the program usage information.
52   * <i>Note : no option should have the same main or secondary names</i>.
53   *
54   * @author Benjamin Croizet (<a href="mailto:graffity2199@yahoo.fr>graffity2199@yahoo.fr</a>)
55   * @since 1.3.0
56   * @version 1.3.0
57   */
58  public class HelpOption extends AbstractOption implements ExecutableOption {
59  
60      /** Serial version UID. */
61      private static final long serialVersionUID = 3929762825343812282L;
62  
63      /** The class logger. */
64      private static final transient Logger LOGGER = getLogger(HelpOption.class.getName());
65  
66      /** Option main synopsis. */
67      public static final String OPTION_MAIN_SYNOPSIS = "-h";
68  
69      /** Option synopsis alias. */
70      public static final Set < String > OPTION_SYNOPSIS = unmodifiableSet(new TreeSet < String >(asList(new String[] {
71              "--help", "-?"})));
72  
73      /** The java program to display help information. */
74      private JavaProgram javaProgram;
75  
76      /** The option priority. */
77      private int priority;
78  
79      /**
80       * Default constructor.
81       *
82       * @param program
83       *            the java program to display help information, mustn't be <code>null</code>.
84       * @param optionPriority
85       *            the option priority as an integer. <i>Note : the priority must be unique amongst
86       *            all options</i>.
87       * @since 1.3.0
88       */
89      public HelpOption(final JavaProgram program, final int optionPriority) {
90          this(new StringBuilder("To display this help message."), program, optionPriority, ACTIVE_OPTIONAL_OPTION_STATUS);
91      }
92  
93      /**
94       * Full constructor.
95       *
96       * @param fullOptionDescription
97       *            the full option usage description, explaining what the option does (used for
98       *            helping message). <i>Note : a new {@link StringBuilder} is created.</i>
99       * @param program
100      *            the java program to display authors information, mustn't be <code>null</code>.
101      * @param optionPriority
102      *            the option priority as an integer. <i>Note : the priority must be unique amongst
103      *            all options</i>.
104      * @param optionStatus
105      *            the option status, telling if the option is active, inactive or hidden, mustn't be
106      *            <code>null</code>.
107      * @since 1.3.0
108      */
109     public HelpOption(final StringBuilder fullOptionDescription, final JavaProgram program, final int optionPriority,
110             final OptionStatus optionStatus) {
111         super(OPTION_MAIN_SYNOPSIS, OPTION_SYNOPSIS, fullOptionDescription, optionStatus);
112         setJavaProgram(program);
113         setPriority(optionPriority);
114     }
115 
116     /**
117      * {@inheritDoc}
118      *
119      * @since 1.3.0
120      */
121     @Override
122     public HelpOption deepClone() {
123         final HelpOption a = (HelpOption) super.deepClone();
124         a.javaProgram = getJavaProgram().deepClone();
125         return a;
126     }
127 
128     /**
129      * {@inheritDoc}
130      *
131      * @since 1.3.0
132      */
133     @Override
134     protected StringBuilder getFullUsageAdditions() {
135         return new StringBuilder();
136     }
137 
138     /**
139      * {@inheritDoc}
140      *
141      * @since 1.3.0
142      */
143     @Override
144     protected StringBuilder getFullUsageDescriptionAdditions() {
145         return new StringBuilder();
146     }
147 
148     /**
149      * Gets the value of <code>javaProgram</code>.
150      *
151      * @return the value of <code>javaProgram</code>.
152      * @see #setJavaProgram(JavaProgram)
153      * @since 1.3.0
154      */
155     private JavaProgram getJavaProgram() {
156         return javaProgram;
157     }
158 
159     /**
160      * {@inheritDoc}
161      *
162      * @since 1.3.0
163      */
164     @Override
165     protected StringBuilder getMainUsageAdditions() {
166         return new StringBuilder();
167     }
168 
169     /**
170      * {@inheritDoc}
171      *
172      * @since 1.3.0
173      */
174     @Override
175     public int getPriority() {
176         return priority;
177     }
178 
179     /**
180      * {@inheritDoc}
181      *
182      * @since 1.3.0
183      */
184     @Override
185     public OptionExecution parseCommandLine(final CommandLine commandLine) throws CommandLineException {
186         checkNull(commandLine, COMMAND_LINE_NULL_ERROR);
187 
188         OptionExecution optionExecution = null;
189 
190         if (commandLine.isOptionActiveAndSpecified(this)) {
191             optionExecution = new HelpOptionExecution(getJavaProgram(), getPriority());
192         } else {
193             LOGGER.log(FINE, buildLogString(OPTION_NOT_SPECIFIED_FINE, getAllNames()));
194         }
195 
196         return optionExecution;
197     }
198 
199     /**
200      * Sets the value of <code>javaProgram</code>.
201      *
202      * @param value
203      *            the <code>javaProgram</code> to set, can be <code>null</code>.
204      * @see #getJavaProgram()
205      * @since 1.3.0
206      */
207     private void setJavaProgram(final JavaProgram value) {
208         checkNull(value, JAVA_PROGRAM_NULL_ERROR);
209 
210         javaProgram = value;
211     }
212 
213     /**
214      * Sets the value of <code>priority</code>.
215      *
216      * @param value
217      *            the <code>priority</code> to set.
218      * @see #getPriority()
219      * @since 1.3.0
220      */
221     private void setPriority(final int value) {
222         priority = value;
223     }
224 
225     /**
226      * {@inheritDoc}
227      *
228      * @since 1.3.0
229      */
230     @Override
231     public String toString() {
232         return getClass().getSimpleName() + " [" + super.toString() + ", javaProgram=" + javaProgram + ", priority="
233                 + priority + "]";
234     }
235 }