Skip to content

Commit 9ff8509

Browse files
committed
Add better help description.
1 parent 89e152d commit 9ff8509

2 files changed

Lines changed: 59 additions & 17 deletions

File tree

src/main/java/com/comphenix/rema1000/Application.java

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,18 @@ public class Application {
2020
private static class ArgumentParser {
2121
private Path source;
2222
private Path destination;
23-
private DestinationFormat format = DestinationFormat.XLSX; // Default
23+
private DestinationFormat format = null; // Deduce from file extension
2424

2525
private boolean streamMode;
2626
private int pathCount;
2727

28+
private boolean showHelp;
29+
2830
public void parse(String[] args) {
31+
if (args == null || args.length == 0) {
32+
showHelp = true;
33+
return;
34+
}
2935
for (int i = 0; i < args.length; i++) {
3036
String arg = args[i];
3137

@@ -40,6 +46,11 @@ public void parse(String[] args) {
4046
// Allow no source or destination path
4147
streamMode = true;
4248
break;
49+
case "-?":
50+
case "-h":
51+
case "--help":
52+
showHelp = true;
53+
return;
4354
default:
4455
throw new IllegalArgumentException("Unknown format " + arg);
4556
}
@@ -60,6 +71,27 @@ public void parse(String[] args) {
6071
if (!streamMode && pathCount != 2) {
6172
throw new IllegalArgumentException("Must supply source and destination path (or enable stream mode).");
6273
}
74+
if (streamMode && format == null && pathCount < 2) {
75+
throw new IllegalArgumentException("Must specify a format in stream mode with no output file");
76+
}
77+
computeFormat();
78+
}
79+
80+
private void computeFormat() {
81+
if (format == null) {
82+
String fileExtension = getFileExtension(destination).toUpperCase();
83+
84+
// Deduce format from extension
85+
try {
86+
format = DestinationFormat.fromExtension(fileExtension);
87+
} catch (IllegalArgumentException e) {
88+
throw new IllegalArgumentException("Unable to output JSON to file extension " + fileExtension, e);
89+
}
90+
}
91+
}
92+
93+
public boolean isShowHelp() {
94+
return showHelp;
6395
}
6496

6597
public boolean isStreamMode() {
@@ -80,13 +112,24 @@ public DestinationFormat getFormat() {
80112
}
81113

82114
public static void main(String[] args) throws IOException {
83-
if (args.length <= 0) {
84-
System.out.println("RemaTransactionParser [-f format] [-s] source destination");
85-
return;
86-
}
87115
ArgumentParser parser = new ArgumentParser();
88116
parser.parse(args);
89117

118+
if (parser.isShowHelp()) {
119+
System.out.println("RemaTransactionParser [-f format] [-s] [-h] source destination");
120+
System.out.println(" -f format Specify the output format, either XLSX (Excel 2003) or SQL ");
121+
System.out.println(" (Database Export script for SQLite). If not specified, the");
122+
System.out.println(" output file extension will be used instead.");
123+
System.out.println(" -s Enable stream mode, allowing the program to use standard");
124+
System.out.println(" output or standard input instead of the file system. Format must");
125+
System.out.println(" be specified if no output file is specified.");
126+
System.out.println(" -h Show this help text.");
127+
System.out.println(" source Path to the JSON-file with the exported Rema 1000 data.");
128+
System.out.println(" May be omitted in stream mode.");
129+
System.out.println(" destination Path to the output XLSX- or SQL-file where the conversion");
130+
System.out.println(" output will be written. May be omitted in stream mode.");
131+
return;
132+
}
90133
Gson gson = new Gson();
91134

92135
try (BufferedReader reader = getInput(parser)) {
@@ -97,7 +140,7 @@ public static void main(String[] args) throws IOException {
97140
}
98141
}
99142
if (!parser.isStreamMode()) {
100-
System.out.println("Data converted.");
143+
System.out.println("Data converted to " + parser.getFormat());
101144
}
102145
}
103146

@@ -123,22 +166,17 @@ private static BufferedReader getInput(ArgumentParser parser) throws IOException
123166
}
124167

125168
private static OutputStream getOutput(ArgumentParser parser) throws IOException {
126-
String extension = parser.getFormat().getExtension();
127169
Path outputFile = parser.getDestination();
128170

129-
if (extension != null && outputFile != null) {
130-
String fileName = outputFile.getFileName().toString();
131-
int dotIndex = fileName.lastIndexOf('.');
132-
String fileExtension = dotIndex != -1 ? fileName.substring(dotIndex + 1) : "";
133-
134-
// Error
135-
if (!extension.equalsIgnoreCase(fileExtension)) {
136-
throw new IllegalArgumentException("Extension must be " + extension);
137-
}
138-
}
139171
return parser.getDestination() != null ? Files.newOutputStream(outputFile) : System.out;
140172
}
141173

174+
private static String getFileExtension(Path outputFile) {
175+
String fileName = outputFile.getFileName().toString();
176+
int dotIndex = fileName.lastIndexOf('.');
177+
return dotIndex != -1 ? fileName.substring(dotIndex + 1) : "";
178+
}
179+
142180
private static void writeOutput(DestinationFormat format, OutputStream output, DataRoot dataRoot) throws IOException {
143181
switch (format) {
144182
case XLSX:

src/main/java/com/comphenix/rema1000/DestinationFormat.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ public enum DestinationFormat {
1111
this.extension = extension;
1212
}
1313

14+
public static DestinationFormat fromExtension(String extension) {
15+
return valueOf(extension.toUpperCase());
16+
}
17+
1418
public String getExtension() {
1519
return extension;
1620
}

0 commit comments

Comments
 (0)