View Javadoc
1   /**************************************************************************
2    *
3    * Copyright (c) 2015-2019 Yawg project contributors.
4    *
5    **************************************************************************/
6   
7   package com.varmateo.yawg.cli;
8   
9   import org.apache.commons.cli.Option;
10  import org.apache.commons.cli.OptionBuilder;
11  
12  
13  /**
14   * A description of a command line option.
15   */
16  public final class CliParameter {
17  
18  
19      private String  _shortName   = null;
20      private String  _longName    = null;
21      private String  _description = null;
22      private String  _argName     = null;
23  
24      // Derived from the other attributes.
25      private String  _name = null;
26      private String  _literal = null;
27      private boolean _isWithArg = false;
28  
29      private Option  _apacheOption = null;
30  
31  
32      /**
33       * Only for internal use.
34       */
35      private CliParameter(final Builder builder) {
36  
37          _shortName   = builder._shortName;
38          _longName    = builder._longName;
39          _description = builder._description;
40          _argName     = builder._argName;
41  
42          _name = (_longName!=null) ? _longName : _shortName;
43          _literal = (_longName!=null) ? ("--"+_longName) : ("-"+_shortName);
44          _isWithArg = (_argName!=null);
45  
46          _apacheOption = buildApacheOption(this);
47      }
48  
49  
50      /**
51       * Creates a new builder with no initializations.
52       *
53       * @return A newly created <code>Builder</code> instance.
54       */
55      public static Builder builder() {
56  
57          return new Builder();
58      }
59  
60  
61      /**
62       *
63       */
64      private static Option buildApacheOption(final CliParameter option) {
65  
66          final String longName = option.longName();
67          if ( longName != null ) {
68              OptionBuilder.withLongOpt(longName);
69          }
70  
71          final String description = option.description();
72          if ( description != null ) {
73              OptionBuilder.withDescription(description);
74          }
75  
76          final boolean isWithArg = option.isWithArg();
77          if ( isWithArg ) {
78              OptionBuilder.hasArg();
79          }
80  
81          final String argName = option.argName();
82          if ( argName != null ) {
83              OptionBuilder.withArgName(argName);
84          }
85  
86          final String shortName = option.shortName();
87  
88          return shortName != null
89                  ? OptionBuilder.create(shortName)
90                  : OptionBuilder.create();
91      }
92  
93  
94      /**
95       *
96       */
97      public String shortName() {
98  
99          return _shortName;
100     }
101 
102 
103     /**
104      *
105      */
106     public String longName() {
107 
108         return _longName;
109     }
110 
111 
112     /**
113      *
114      */
115     public String description() {
116 
117         return _description;
118     }
119 
120 
121     /**
122      *
123      */
124     public boolean isWithArg() {
125 
126         return _isWithArg;
127     }
128 
129 
130     /**
131      * Informative only.
132      */
133     public String argName() {
134 
135         return _argName;
136     }
137 
138 
139     /**
140      *
141      */
142     public String name() {
143 
144         return _name;
145     }
146 
147 
148     /**
149      *
150      */
151     public String literal() {
152 
153         return _literal;
154     }
155 
156 
157     /**
158      *
159      */
160     /* default */ Option apacheOption() {
161 
162         return _apacheOption;
163     }
164 
165 
166     /**
167      * A builder of <code>CliParameter</code> instances.
168      */
169     public static final class Builder {
170 
171 
172         private String  _shortName   = null;
173         private String  _longName    = null;
174         private String  _description = null;
175         private String  _argName     = null;
176 
177 
178         /**
179          *
180          */
181         private Builder() {
182             // Nothing to do.
183         }
184 
185 
186         /**
187          *
188          */
189         public Builder shortName(final String shortName) {
190 
191             _shortName = shortName;
192 
193             return this;
194         }
195 
196 
197         /**
198          *
199          */
200 
201         public Builder longName(final String longName) {
202 
203             _longName = longName;
204 
205             return this;
206         }
207 
208 
209 
210         /**
211          *
212          */
213 
214         public Builder description(final String description) {
215 
216             _description = description;
217 
218             return this;
219         }
220 
221 
222         /**
223          *
224          */
225         public Builder argName(final String argName) {
226 
227             _argName = argName;
228 
229             return this;
230         }
231 
232 
233         /**
234          *
235          */
236         public CliParameter build() {
237 
238             return new CliParameter(this);
239         }
240 
241 
242     }
243 
244 
245 }