forked from agronomist/genomics-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandLine.java
130 lines (105 loc) · 4.75 KB
/
CommandLine.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
Copyright 2014 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.google.cloud.genomics.api.client;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.MissingCommandException;
import com.beust.jcommander.internal.Lists;
import com.google.api.client.repackaged.com.google.common.base.Strings;
import com.google.api.client.util.Maps;
import com.google.cloud.genomics.api.client.commands.*;
import com.google.cloud.genomics.api.client.commands.align.AlignBamsCommand;
import com.google.cloud.genomics.api.client.commands.align.AlignInterleavedFastqsCommand;
import com.google.cloud.genomics.api.client.commands.align.AlignPairedFastqsCommand;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
* Command line options handler for GenomicsSample
*/
class CommandLine {
private static final String JAR_COMMAND
= "java -jar genomics-tools-client-java-v1beta2.jar ";
private JCommander parser;
private BaseCommand command;
private Map<String, BaseCommand> registeredCommands = Maps.newLinkedHashMap();
public CommandLine() {
parser = new JCommander();
// The ordering of these commands is preserved in help messages
addCommand("listdatasets", new ListDatasetsCommand());
addCommand("getdataset", new GetDatasetsCommand());
addCommand("createdataset", new CreateDatasetCommand());
addCommand("updatedataset", new UpdateDatasetCommand());
addCommand("searchreferencesets", new SearchReferenceSetsCommand());
addCommand("searchreferences", new SearchReferencesCommand());
addCommand("getreferencebases", new GetReferenceBasesCommand());
addCommand("searchreadgroupsets", new SearchReadGroupSetsCommand());
addCommand("getreadgroupset", new GetReadGroupSetsCommand());
addCommand("searchreads", new SearchReadsCommand());
addCommand("importreads", new ImportReadsCommand());
addCommand("exportreads", new ExportReadsCommand());
addCommand("alignbam", new AlignBamsCommand());
addCommand("alignfastq", new AlignInterleavedFastqsCommand());
addCommand("alignpairedfastq", new AlignPairedFastqsCommand());
addCommand("callvariants", new CallVariantsCommand());
addCommand("getvariantset", new GetVariantSetsCommand());
addCommand("searchvariants", new SearchVariantsCommand());
addCommand("getvariant", new GetVariantsCommand());
addCommand("importvariants", new ImportVariantsCommand());
addCommand("exportvariants", new ExportVariantsCommand());
addCommand("searchcallsets", new SearchCallSetsCommand());
addCommand("updatecallset", new UpdateCallSetCommand());
addCommand("deletecallset", new DeleteCallSetCommand());
addCommand("listjobs", new ListJobsCommand());
addCommand("getjob", new GetJobsCommand());
addCommand("canceljob", new CancelJobCommand());
// Custom escape hatch
addCommand("custom", new CustomCommand());
}
private void addCommand(String name, BaseCommand command) {
registeredCommands.put(name, command);
parser.addCommand(name, command);
}
public BaseCommand getCommand() {
return command;
}
public void setArgs(String[] args) {
parser.parse(args);
if (Strings.isNullOrEmpty(parser.getParsedCommand())) {
throw new MissingCommandException("A command is required");
} else {
command = registeredCommands.get(parser.getParsedCommand());
}
}
public void printHelp(String headline, Appendable out) throws IOException {
out.append(headline).append("\n");
String parsedCommand = parser.getParsedCommand();
if (Strings.isNullOrEmpty(parsedCommand)) {
out.append("Usage: " + JAR_COMMAND + "<command> <options>")
.append("\n\n");
out.append("Valid commands are:\n");
describeCommands(out);
out.append("\n");
} else {
StringBuilder sb = new StringBuilder();
parser.getCommands().get(parsedCommand).setProgramName(JAR_COMMAND + parsedCommand);
parser.usage(parsedCommand, sb);
out.append(sb.toString());
}
}
private void describeCommands(Appendable out) throws IOException {
List<String> commands = Lists.newArrayList(registeredCommands.keySet());
for (String command : commands) {
out.append(String.format("%-20s%s%n", command, parser.getCommandDescription(command)));
}
}
}