16
16
*
17
17
* CommandLinePrograms are listed in a single command line interface based on the java package specified to instanceMain.
18
18
*
19
- * If you want your own single command line program, extend this class and override {@link #getPackageList()}
20
- * to return a list of java packages in which to search for classes that extend CommandLineProgram
21
- * and/or {@link #getClassList()} to return a list of classes to include.
19
+ * If you want your own single command line program, extend this class and override if required:
20
+ *
21
+ * - {@link #getPackageList()} to return a list of java packages in which to search for classes that extend CommandLineProgram.
22
+ * - {@link #getClassList()} to return a list of single classes to include (e.g. required input pre-processing tools).
23
+ * - {@link #getCommandLineName()} for the name of the toolkit.
24
+ * - {@link #handleResult(Object)} for handle the result of the tool.
25
+ *
26
+ * Note: If any of the previous methods was overrided, {@link #main(String[])} should be implemented to instantiate your class
27
+ * and call {@link #mainEntry(String[])} to make the changes effective.
22
28
*/
23
29
public class Main {
24
30
@@ -65,6 +71,11 @@ protected List<Class<? extends CommandLineProgram>> getClassList() {
65
71
return Collections .emptyList ();
66
72
}
67
73
74
+ /** Returns the command line that will appear in the usage. */
75
+ protected String getCommandLineName () {
76
+ return "" ;
77
+ }
78
+
68
79
/**
69
80
* The main method.
70
81
* <p/>
@@ -87,19 +98,21 @@ public Object instanceMain(final String[] args, final List<String> packageList,
87
98
* This method is not intended to be used outside of the GATK framework and tests.
88
99
*/
89
100
public Object instanceMain (final String [] args ) {
90
- return instanceMain (args , getPackageList (), getClassList (), "" );
101
+ return instanceMain (args , getPackageList (), getClassList (), getCommandLineName () );
91
102
}
92
103
93
104
/**
94
- * The entry point to GATK from commandline.
105
+ * The entry point to the toolkit from commandline: it uses {@link #instanceMain(String[])} to run the command line
106
+ * program and handle the returned object with {@link #handleResult(Object)}, and exit with 0.
107
+ * If any error occurs, it handles the exception and exit with the concrete error exit value.
108
+ *
95
109
* Note: this is the only method that is allowed to call System.exit (because gatk tools may be run from test harness etc)
96
110
*/
97
- public static void main (final String [] args ) {
111
+ protected final void mainEntry (final String [] args ) {
98
112
try {
99
- final Object result = new Main ().instanceMain (args );
100
- if (result != null ) {
101
- System .out .println ("Tool returned:\n " + result );
102
- }
113
+ final Object result = instanceMain (args );
114
+ handleResult (result );
115
+ System .exit (0 );
103
116
} catch (final UserException .CommandLineException e ){
104
117
//the usage has already been printed so don't print it here.
105
118
if (printStackTraceOnUserExceptions ()) {
@@ -119,6 +132,21 @@ public static void main(final String[] args) {
119
132
}
120
133
}
121
134
135
+ /**
136
+ * Handle the result returned for a tool. Default implementation prints a message with the string value of the object if it is not null.
137
+ * @param result the result of the tool (may be null)
138
+ */
139
+ protected void handleResult (final Object result ) {
140
+ if (result != null ) {
141
+ System .out .println ("Tool returned:\n " + result );
142
+ }
143
+ }
144
+
145
+ /** The entry point to GATK from commandline. It calls {@link #mainEntry(String[])} from this instance. */
146
+ public static void main (final String [] args ) {
147
+ new Main ().mainEntry (args );
148
+ }
149
+
122
150
private static boolean printStackTraceOnUserExceptions () {
123
151
return "true" .equals (System .getenv (STACK_TRACE_ON_USER_EXCEPTION_PROPERTY )) || Boolean .getBoolean (STACK_TRACE_ON_USER_EXCEPTION_PROPERTY );
124
152
}
0 commit comments