View Javadoc

1   /*
2    * Copyright 2009 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  import java.util.ArrayList;
19  import java.util.List;
20  
21  class ParsedOptionSpecificationImpl extends AbstractOptionSpecification implements ParsedOptionSpecification {
22      private final OptionAnnotationAdapter optionAnnotation;
23  
24      ParsedOptionSpecificationImpl(final OptionAnnotationAdapter annotation) {
25          super(annotation);
26          optionAnnotation = annotation;
27          for (final String longName : annotation.longName()) {
28              if (longName.trim().isEmpty())
29              {
30                  throw new InvalidOptionSpecificationException(String.format(
31                          "option %s long name cannot be blank",
32                          getMethod()));
33              }
34          }
35      }
36  
37      @Override public List<String> getLongName() {
38          return optionAnnotation.longName();
39      }
40  
41      @Override public List<String> getShortNames() {
42          return optionAnnotation.shortName();
43      }
44  
45      @Override public List<String> getNames() {
46          final List<String> result = new ArrayList<String>(getShortNames());
47          result.addAll(getLongName());
48          return result;
49      }
50  
51      @Override public boolean hasShortName() {
52          return !getShortNames().isEmpty();
53      }
54  
55      @Override public boolean isHelpOption() {
56          return optionAnnotation.helpRequest();
57      }
58  
59      @Override public String getPattern() {
60          return annotation.pattern();
61      }
62  
63      @Override public boolean hasValue() {
64          return !isBoolean();
65      }
66  
67      @Override public final boolean isBoolean() {
68          return getType().isAssignableFrom(Boolean.class) || getType().isAssignableFrom(boolean.class);
69      }
70  
71      @Override public String toString() {
72          return new ParsedOptionSummary(this).toString();
73      }
74  
75      @Override public boolean allowedValue(final String value) {
76          return value.matches(getPattern());
77      }
78  
79      @Override public void reportMissingTo(final ValidationErrorBuilder validationErrorBuilder) {
80         validationErrorBuilder.missingOption(this);
81      }
82  }