View Javadoc
1   /**************************************************************************
2    *
3    * Copyright (c) 2015-2019 Yawg project contributors.
4    *
5    **************************************************************************/
6   
7   package com.varmateo.yawg.cli;
8   
9   import java.io.PrintWriter;
10  import java.text.MessageFormat;
11  
12  import io.vavr.collection.Set;
13  import org.apache.commons.cli.HelpFormatter;
14  import org.apache.commons.cli.Options;
15  
16  import com.varmateo.yawg.cli.CliParameter;
17  
18  
19  /**
20   * Provides utility methods for printing help and varied info intended
21   * for end users.
22   */
23  /* package private */ final class CliInfoPrinter {
24  
25  
26      private static final String DEFAULT_ARGV0 = "";
27      private static final String DEFAULT_VERSION = "";
28      private static final String DEFAULT_USAGE_HEADER = "";
29      private static final String DEFAULT_USAGE_FOOTER = ""
30          + "\n";
31  
32      private static final String ERROR_FMT = ""
33          + "\n"
34          + "{0}: {1}\n";
35  
36      private String _argv0 = null;
37      private String _versionMessage = null;
38      private String _usageMessageHeader = null;
39      private String _usageMessageFooter = null;
40  
41  
42      /**
43       * Only used internally.
44       *
45       */
46      private CliInfoPrinter(final Builder builder) {
47  
48          _argv0 = builder._argv0;
49          _versionMessage = builder._versionMessage;
50          _usageMessageHeader = builder._usageMessageHeader;
51          _usageMessageFooter = builder._usageMessageFooter;
52      }
53  
54  
55      /**
56       * Creates a new builder with no initializations.
57       *
58       * @return A newly created <code>Builder</code> instance.
59       */
60      public static Builder builder() {
61  
62          return new Builder();
63      }
64  
65  
66      /**
67       *
68       */
69      public void printUsage(
70              final PrintWriter output,
71              final Set<CliParameter> options) {
72  
73          final String header = MessageFormat.format(_usageMessageHeader, _argv0);
74          final String footer = _usageMessageFooter;
75  
76          output.println(header);
77  
78          final Options       apacheOptions = buildApacheOptions(options);
79          final HelpFormatter formatter = new HelpFormatter();
80  
81          formatter.printOptions(
82                  output,
83                  HelpFormatter.DEFAULT_WIDTH,
84                  apacheOptions,
85                  HelpFormatter.DEFAULT_LEFT_PAD,
86                  HelpFormatter.DEFAULT_DESC_PAD);
87  
88          output.println(footer);
89      }
90  
91  
92      /**
93       *
94       */
95      private static Options
96          buildApacheOptions(final Set<CliParameter> options) {
97  
98          return options
99                  .map(CliParameter::apacheOption)
100                 .foldLeft(
101                         new Options(),
102                         (xs, x) -> xs.addOption(x));
103     }
104 
105 
106     /**
107      *
108      */
109     public void printVersion(final PrintWriter output) {
110 
111         output.println(_versionMessage);
112     }
113 
114 
115     /**
116      *
117      */
118     public void printError(
119             final PrintWriter  output,
120             final Throwable error) {
121 
122         final String errorMsg = error.getMessage();
123         final String msg      = MessageFormat.format(ERROR_FMT, _argv0, errorMsg);
124 
125         output.println(msg);
126     }
127 
128 
129     /**
130      * A builder of <code>CliInfoPrinter</code> instances.
131      */
132     public static final class Builder {
133 
134 
135         private String _argv0 = DEFAULT_ARGV0;
136         private String _versionMessage = DEFAULT_VERSION;
137         private String _usageMessageHeader = DEFAULT_USAGE_HEADER;
138         private String _usageMessageFooter = DEFAULT_USAGE_FOOTER;
139 
140 
141         /**
142          *
143          */
144         private Builder() {
145             // Nothing to do.
146         }
147 
148 
149         /**
150          *
151          */
152         public Builder argv0(final String argv0) {
153 
154             _argv0 = argv0;
155 
156             return this;
157         }
158 
159 
160         /**
161          *
162          */
163         public Builder versionMessage(final String versionMessage) {
164 
165             _versionMessage = versionMessage;
166 
167             return this;
168         }
169 
170 
171         /**
172          *
173          */
174         public Builder usageMessageHeader(final String usageMessageHeader) {
175 
176             _usageMessageHeader = usageMessageHeader;
177 
178             return this;
179         }
180 
181 
182         /**
183          *
184          */
185         public Builder usageMessageFooter(final String usageMessageFooter) {
186 
187             _usageMessageFooter = usageMessageFooter;
188 
189             return this;
190         }
191 
192 
193         /**
194          *
195          */
196         public CliInfoPrinter build() {
197 
198             return new CliInfoPrinter(this);
199         }
200 
201     }
202 
203 }