@@ -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 :
0 commit comments