26
26
import org .metafacture .framework .annotations .Out ;
27
27
import org .metafacture .framework .annotations .ReturnsAvailableArguments ;
28
28
29
+ import java .io .BufferedReader ;
30
+ import java .io .File ;
31
+ import java .io .FileReader ;
29
32
import java .io .IOException ;
30
33
import java .io .PrintStream ;
31
34
import java .lang .reflect .InvocationTargetException ;
34
37
import java .util .Arrays ;
35
38
import java .util .Collection ;
36
39
import java .util .Collections ;
40
+ import java .util .HashMap ;
37
41
import java .util .List ;
38
42
import java .util .Map ;
39
43
import java .util .Map .Entry ;
40
44
41
45
/**
42
- * Prints Flux help for a given {@link ObjectFactory}
46
+ * Prints Flux help for a given {@link ObjectFactory}.
47
+ * If the file at {@value #PATH_TO_EXAMPLES} exists it's taken to insert links to examples and to the source code.
43
48
*
44
49
* @author Markus Michael Geipel
45
50
*/
46
51
public final class HelpPrinter {
47
52
53
+ private static final String PATH_TO_EXAMPLES = "../metafacture-documentation/linksAndExamples.tsv" ;
54
+ private static final Map <String , String []> EXAMPLES_MAP = new HashMap <>();
55
+
48
56
private HelpPrinter () {
49
57
// no instances
50
58
}
@@ -55,8 +63,9 @@ private HelpPrinter() {
55
63
* @param args unused
56
64
*
57
65
* @see #print
66
+ * @throws IOException when an I/O error occurs
58
67
*/
59
- public static void main (final String [] args ) {
68
+ public static void main (final String [] args ) throws IOException {
60
69
FluxProgramm .printHelp (System .out );
61
70
}
62
71
@@ -66,9 +75,10 @@ public static void main(final String[] args) {
66
75
*
67
76
* @param factory the ObjectFactory
68
77
* @param out the PrintStream to print to
78
+ * @throws IOException when an I/O error occurs
69
79
*/
70
80
public static void print (final ObjectFactory <?> factory ,
71
- final PrintStream out ) {
81
+ final PrintStream out ) throws IOException {
72
82
out .println ("Welcome to Metafacture" );
73
83
out .println ("======================" );
74
84
out .println ();
@@ -77,9 +87,9 @@ public static void print(final ObjectFactory<?> factory,
77
87
out .println ("\n Usage:\t flux FLOW_FILE [VARNAME=VALUE ...]\n " );
78
88
out .println ("Available flux commands:\n " );
79
89
80
- final List <String > keyWords = new ArrayList <String >();
81
- keyWords .addAll (factory .keySet ());
90
+ final List <String > keyWords = new ArrayList <>(factory .keySet ());
82
91
Collections .sort (keyWords );
92
+ loadExamples ();
83
93
for (final String name : keyWords ) {
84
94
describe (name , factory , out );
85
95
}
@@ -111,8 +121,37 @@ private static <T> void describe(final String name, final ObjectFactory<T> facto
111
121
out .println ("- arguments:\t " + arguments );
112
122
}
113
123
114
- final Map <String , Method > attributes = configurableClass .getSetters ();
124
+ printAttributes (out , configurableClass , configurableClass .getSetters ());
125
+ printSignature (out , moduleClass );
126
+
127
+ final String [] examplesEntry = EXAMPLES_MAP .get (name );
128
+ if (examplesEntry != null && examplesEntry .length > 2 ) {
129
+ out .println ("- [example in Playground]" + "(" + examplesEntry [2 ] + ")" );
130
+ }
131
+ if (examplesEntry != null && examplesEntry .length > 1 ) {
132
+ out .println ("- java class:\t [" + moduleClass .getCanonicalName () + "](" + examplesEntry [1 ] + ")" );
133
+ }
134
+ else {
135
+ out .println ("- java class:\t " + moduleClass .getCanonicalName ());
136
+ }
137
+ out .println ();
138
+ }
139
+
140
+ private static <T > void printSignature (final PrintStream out , final Class <? extends T > moduleClass ) {
141
+ String inString = "<unknown>" ;
142
+ String outString = "" ;
143
+ final In inClass = moduleClass .getAnnotation (In .class );
144
+ if (inClass != null ) {
145
+ inString = inClass .value ().getSimpleName ();
146
+ }
147
+ final Out outClass = moduleClass .getAnnotation (Out .class );
148
+ if (outClass != null ) {
149
+ outString = outClass .value ().getSimpleName ();
150
+ }
151
+ out .println ("- signature:\t " + inString + " -> " + outString );
152
+ }
115
153
154
+ private static <T > void printAttributes (final PrintStream out , final ConfigurableClass <? extends T > configurableClass , final Map <String , Method > attributes ) {
116
155
if (!attributes .isEmpty ()) {
117
156
out .print ("- options:\t " );
118
157
final StringBuilder builder = new StringBuilder ();
@@ -140,20 +179,6 @@ private static <T> void describe(final String name, final ObjectFactory<T> facto
140
179
}
141
180
out .println (builder .substring (0 , builder .length () - 2 ));
142
181
}
143
-
144
- String inString = "<unknown>" ;
145
- String outString = "" ;
146
- final In inClass = moduleClass .getAnnotation (In .class );
147
- if (inClass != null ) {
148
- inString = inClass .value ().getSimpleName ();
149
- }
150
- final Out outClass = moduleClass .getAnnotation (Out .class );
151
- if (outClass != null ) {
152
- outString = outClass .value ().getSimpleName ();
153
- }
154
- out .println ("- signature:\t " + inString + " -> " + outString );
155
- out .println ("- java class:\t " + moduleClass .getCanonicalName ());
156
- out .println ();
157
182
}
158
183
159
184
@ SuppressWarnings ("unchecked" )
@@ -171,4 +196,20 @@ private static Collection<String> getAvailableArguments(final Class<?> moduleCla
171
196
return Collections .emptyList ();
172
197
}
173
198
199
+ private static void loadExamples () throws IOException {
200
+ final File f = new File (PATH_TO_EXAMPLES );
201
+ if (f .exists ()) {
202
+ final BufferedReader bufferedReader = new BufferedReader (new FileReader (f ));
203
+ String line ;
204
+ while ((line = bufferedReader .readLine ()) != null ) {
205
+ final String [] tsv = line .split ("\t " );
206
+ EXAMPLES_MAP .put (tsv [0 ], tsv );
207
+
208
+ if (tsv .length < 2 ) {
209
+ System .err .println ("Invalid command info: " + line );
210
+ }
211
+ }
212
+ }
213
+ }
214
+
174
215
}
0 commit comments