View Javadoc

1   /*
2    * Copyright 2007 Tim Wood
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  package com.lexicalscope.jewel.cli;
15  
16  import com.lexicalscope.jewel.cli.specification.ParsedOptionSpecification;
17  
18  class ParsedOptionSummary
19  {
20      private final ParsedOptionSpecification m_option;
21  
22      public ParsedOptionSummary(final ParsedOptionSpecification option)
23      {
24          m_option = option;
25      }
26  
27      private boolean hasCustomPattern()
28      {
29          return !m_option.getPattern().equals(".*");
30      }
31  
32      private boolean nullOrBlank(final String description)
33      {
34          return description == null || description.trim().equals("");
35      }
36  
37      public void describeOptionTo(final OptionHelpMessage helpMessage) {
38          if (m_option.isOptional())
39          {
40              helpMessage.startOptionalOption();
41          }
42          else
43          {
44              helpMessage.startMandatoryOption();
45          }
46  
47          helpMessage.longName(m_option.getLongName());
48          helpMessage.shortName(m_option.getShortNames());
49  
50          if (m_option.hasValue())
51          {
52              if (m_option.isMultiValued()) {
53                  if (hasCustomPattern())
54                  {
55                      helpMessage.multiValuedWithCustomPattern(m_option.getPattern());
56                  }
57                  else
58                  {
59                      helpMessage.multiValuedWithCustomPattern();
60                  }
61              }
62              else if (hasCustomPattern())
63              {
64                  helpMessage.singleValuedWithCustomPattern(m_option.getPattern());
65              }
66              else
67              {
68                  helpMessage.singleValued();
69              }
70          }
71          else
72          {
73              helpMessage.noValued();
74          }
75  
76          if (m_option.isOptional())
77          {
78              if (hasDescription())
79              {
80                  helpMessage.endOptionalOption(m_option.getDescription());
81              } else {
82                  helpMessage.endOptionalOption();
83              }
84          }
85          else
86          {
87              if (hasDescription())
88              {
89                  helpMessage.endMandatoryOption(m_option.getDescription());
90              } else {
91                  helpMessage.endMandatoryOption();
92              }
93          }
94      }
95  
96      private boolean hasDescription() {
97          return !nullOrBlank(m_option.getDescription());
98      }
99  
100     @Override public String toString() {
101         final OptionHelpMessage helpMessageOptionSummaryBuilderImpl =
102                 new HelpMessageOptionSummaryBuilderImpl();
103         this.describeOptionTo(helpMessageOptionSummaryBuilderImpl);
104         return helpMessageOptionSummaryBuilderImpl.toString();
105     }
106 }