Coverage Report - com.lexicalscope.jewel.cli.ParsedOptionSummary
 
Classes in this File Line Coverage Branch Coverage Complexity
ParsedOptionSummary
100%
31/31
95%
23/24
2.5
 
 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  180
     {
 24  180
         m_option = option;
 25  180
     }
 26  
 
 27  
     private boolean hasCustomPattern()
 28  
     {
 29  128
         return !m_option.getPattern().equals(".*");
 30  
     }
 31  
 
 32  
     private boolean nullOrBlank(final String description)
 33  
     {
 34  180
         return description == null || description.trim().equals("");
 35  
     }
 36  
 
 37  
     public void describeOptionTo(final OptionHelpMessage helpMessage) {
 38  180
         if (m_option.isOptional())
 39  
         {
 40  66
             helpMessage.startOptionalOption();
 41  
         }
 42  
         else
 43  
         {
 44  114
             helpMessage.startMandatoryOption();
 45  
         }
 46  
 
 47  180
         helpMessage.longName(m_option.getLongName());
 48  180
         helpMessage.shortName(m_option.getShortNames());
 49  
 
 50  180
         if (m_option.hasValue())
 51  
         {
 52  128
             if (m_option.isMultiValued()) {
 53  26
                 if (hasCustomPattern())
 54  
                 {
 55  2
                     helpMessage.multiValuedWithCustomPattern(m_option.getPattern());
 56  
                 }
 57  
                 else
 58  
                 {
 59  24
                     helpMessage.multiValuedWithCustomPattern();
 60  
                 }
 61  
             }
 62  102
             else if (hasCustomPattern())
 63  
             {
 64  12
                 helpMessage.singleValuedWithCustomPattern(m_option.getPattern());
 65  
             }
 66  
             else
 67  
             {
 68  90
                 helpMessage.singleValued();
 69  
             }
 70  
         }
 71  
         else
 72  
         {
 73  52
             helpMessage.noValued();
 74  
         }
 75  
 
 76  180
         if (m_option.isOptional())
 77  
         {
 78  66
             if (hasDescription())
 79  
             {
 80  36
                 helpMessage.endOptionalOption(m_option.getDescription());
 81  
             } else {
 82  30
                 helpMessage.endOptionalOption();
 83  
             }
 84  
         }
 85  
         else
 86  
         {
 87  114
             if (hasDescription())
 88  
             {
 89  30
                 helpMessage.endMandatoryOption(m_option.getDescription());
 90  
             } else {
 91  84
                 helpMessage.endMandatoryOption();
 92  
             }
 93  
         }
 94  180
     }
 95  
 
 96  
     private boolean hasDescription() {
 97  180
         return !nullOrBlank(m_option.getDescription());
 98  
     }
 99  
 
 100  
     @Override public String toString() {
 101  90
         final OptionHelpMessage helpMessageOptionSummaryBuilderImpl =
 102  
                 new HelpMessageOptionSummaryBuilderImpl();
 103  90
         this.describeOptionTo(helpMessageOptionSummaryBuilderImpl);
 104  90
         return helpMessageOptionSummaryBuilderImpl.toString();
 105  
     }
 106  
 }