Skip to content

Commit 6d884c6

Browse files
magicDGSlbergelson
authored andcommitted
improving Main to be easier for custom toolkits to extend (#2246)
making changes to Main to make it easier for custom toolkits to extend it adding several protected methods to customize some parameters of the toolkit: String getCommandLineName(): returns the name from the command line toolkit void handleResult(Object): handle the output of the tool main(String[] args) code moved to a non-static final method (mainEntry(String[])) called inside the static one. This allow that the changes from override the customization methods could be apply with custom instances.
1 parent 127f9f1 commit 6d884c6

File tree

1 file changed

+38
-10
lines changed
  • src/main/java/org/broadinstitute/hellbender

1 file changed

+38
-10
lines changed

src/main/java/org/broadinstitute/hellbender/Main.java

+38-10
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,15 @@
1616
*
1717
* CommandLinePrograms are listed in a single command line interface based on the java package specified to instanceMain.
1818
*
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.
2228
*/
2329
public class Main {
2430

@@ -65,6 +71,11 @@ protected List<Class<? extends CommandLineProgram>> getClassList() {
6571
return Collections.emptyList();
6672
}
6773

74+
/** Returns the command line that will appear in the usage. */
75+
protected String getCommandLineName() {
76+
return "";
77+
}
78+
6879
/**
6980
* The main method.
7081
* <p/>
@@ -87,19 +98,21 @@ public Object instanceMain(final String[] args, final List<String> packageList,
8798
* This method is not intended to be used outside of the GATK framework and tests.
8899
*/
89100
public Object instanceMain(final String[] args) {
90-
return instanceMain(args, getPackageList(), getClassList(), "");
101+
return instanceMain(args, getPackageList(), getClassList(), getCommandLineName());
91102
}
92103

93104
/**
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+
*
95109
* Note: this is the only method that is allowed to call System.exit (because gatk tools may be run from test harness etc)
96110
*/
97-
public static void main(final String[] args) {
111+
protected final void mainEntry(final String[] args) {
98112
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);
103116
} catch (final UserException.CommandLineException e){
104117
//the usage has already been printed so don't print it here.
105118
if(printStackTraceOnUserExceptions()) {
@@ -119,6 +132,21 @@ public static void main(final String[] args) {
119132
}
120133
}
121134

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+
122150
private static boolean printStackTraceOnUserExceptions() {
123151
return "true".equals(System.getenv(STACK_TRACE_ON_USER_EXCEPTION_PROPERTY)) || Boolean.getBoolean(STACK_TRACE_ON_USER_EXCEPTION_PROPERTY);
124152
}

0 commit comments

Comments
 (0)