View Javadoc

1   /*
2    VerboseLevelOption.java
3    Creation date : 31/10/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.verbose;
26  
27  import static java.util.Arrays.asList;
28  import static java.util.Collections.unmodifiableSet;
29  import static java.util.logging.Level.INFO;
30  import static java.util.logging.Level.WARNING;
31  import static net.sourceforge.plantumldependency.common.constants.CharacterConstants.SPACE_CHAR;
32  import static net.sourceforge.plantumldependency.common.utils.check.ParameterCheckerUtils.checkNull;
33  import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.COMMAND_LINE_NULL_ERROR;
34  import static net.sourceforge.plantumldependency.commoncli.option.status.OptionStatus.ACTIVE_OPTIONAL_OPTION_STATUS;
35  
36  import java.util.Set;
37  import java.util.TreeSet;
38  import java.util.logging.Level;
39  
40  import net.sourceforge.plantumldependency.commoncli.command.CommandLine;
41  import net.sourceforge.plantumldependency.commoncli.exception.CommandLineException;
42  import net.sourceforge.plantumldependency.commoncli.option.AbstractOptionWithArgument;
43  import net.sourceforge.plantumldependency.commoncli.option.argument.OptionArgument;
44  import net.sourceforge.plantumldependency.commoncli.option.argument.impl.log.LevelOptionArgumentImpl;
45  import net.sourceforge.plantumldependency.commoncli.option.status.OptionStatus;
46  
47  /**
48   * A more advanced implementation managing the "-verbose" option with a logging level. <i>Note : no
49   * option should have the same main or secondary names</i>.
50   *
51   * @author Benjamin Croizet (<a href="mailto:graffity2199@yahoo.fr>graffity2199@yahoo.fr</a>)
52   * @since 1.3.0
53   * @version 1.3.0
54   */
55  public class VerboseLevelOption extends AbstractOptionWithArgument < Level > {
56  
57      /** Serial version UID. */
58      private static final long serialVersionUID = -3207782116090972597L;
59  
60      /** Option main synopsis. */
61      public static final String OPTION_MAIN_SYNOPSIS = "-v";
62  
63      /** Option synopsis alias. */
64      public static final Set < String > OPTION_SYNOPSIS = unmodifiableSet(new TreeSet < String >(
65              asList(new String[] {"--verbose"})));
66  
67      /**
68       * Default constructor.
69       *
70       * @since 1.3.0
71       */
72      public VerboseLevelOption() {
73          this(new LevelOptionArgumentImpl(false), new StringBuilder("To display log information."), SPACE_CHAR,
74                  ACTIVE_OPTIONAL_OPTION_STATUS);
75      }
76  
77      /**
78       * Full constructor.
79       *
80       * @param argument
81       *            the option argument, mustn't be <code>null</code>.
82       * @param fullOptionDescription
83       *            the full option usage description, explaining what the option does (used for
84       *            helping message). <i>Note : a new {@link StringBuilder} is created.</i>
85       * @param valSepararator
86       *            the option - argument separator. Most of the time, it is a single space " " but it
87       *            may be "=" or "-", mustn't be <code>null</code>.
88       * @param optionStatus
89       *            the option status, telling if the option is active, inactive or hidden, mustn't be
90       *            <code>null</code>.
91       * @since 1.3.0
92       */
93      public VerboseLevelOption(final OptionArgument < Level > argument, final StringBuilder fullOptionDescription,
94              final String valSepararator, final OptionStatus optionStatus) {
95          super(OPTION_MAIN_SYNOPSIS, OPTION_SYNOPSIS, argument, fullOptionDescription, valSepararator, optionStatus);
96      }
97  
98      /**
99       * {@inheritDoc}
100      *
101      * @since 1.3.0
102      */
103     @Override
104     public VerboseLevelOption deepClone() {
105         return (VerboseLevelOption) super.deepClone();
106     }
107 
108     /**
109      * {@inheritDoc}
110      *
111      * @since 1.3.0
112      */
113     @Override
114     public String getDefaultArgumentAsStringIfOptionNotSpecified(final CommandLine commandLine)
115             throws CommandLineException {
116         checkNull(commandLine, COMMAND_LINE_NULL_ERROR);
117 
118         return WARNING.getName();
119     }
120 
121     /**
122      * {@inheritDoc}
123      *
124      * @since 1.3.0
125      */
126     @Override
127     public String getDefaultArgumentAsStringIfOptionSpecified(final CommandLine commandLine)
128             throws CommandLineException {
129         checkNull(commandLine, COMMAND_LINE_NULL_ERROR);
130 
131         return INFO.getName();
132     }
133 }