1
2
3
4
5
6
7
8
9
10
11
12
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 }