package logfilefilter.internal.ui;

import logfilefilter.JavaLogFileFilter;

public class CommandLineUi {
	private final JavaLogFileFilter processor = new JavaLogFileFilter();

	public void parseCommandLineParameters(String[] args) {
		for (int i = 0; i < args.length; i++) {
			switch (args[i]) {
			case "-i":
				processor.setIncludePattern(args[++i]);
				break;
			case "-e":
				processor.setExcludePattern(args[++i]);
				break;
			case "-d":
				processor.setDatePattern(args[++i]);
				break;
			case "-f":
				processor.setSourceFilePath(args[++i]);
				break;
			case "-t":
				processor.setTargetFilePath(args[++i]);
				break;
			default:
				throw new IllegalArgumentException("Parameter '" + args[i] + "' is unknown.\n" //
						+ "The following parameters can be used:\n" //
						+ "  -i  Include regex pattern\n" //
						+ "  -e  Exclude regex pattern\n" //
						+ "  -d  Date pattern used to identify the begin of a log entry\n" //
						+ "  -f  Source file\n" //
						+ "  -t  Target file, to write the resulting logs");
			}
		}
	}

	public void execute(String[] args) throws Exception {
		parseCommandLineParameters(args);
		processor.execute();
	}

	public static void main(String[] args) {
		CommandLineUi clUi = new CommandLineUi();
		try {
			clUi.execute(args);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

@@ -227,16 +227,114 @@
 args) %7B%0A
+%09%09String errorMessage = %22No parameters defined%22;%0A%09%09boolean invalidParameters = args.length == 0;%0A%0A
 %09%09for (i
@@ -367,16 +367,16 @@
  i++) %7B%0A
-
 %09%09%09switc
@@ -751,91 +751,194 @@
 %09%09%09%09
-throw new IllegalArgumentException(%22Parameter '%22 + args%5Bi%5D + %22' is unknown.
+invalidParameters = true;%0A%09%09%09%09errorMessage = %22Parameter '%22 + args%5Bi%5D + %22' is unknown.%22;%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09if (invalidParameters) %7B%0A%09%09%09throw new IllegalArgumentException(errorMessage + %22
 %5Cn%22 //%0A
-%09
 %09%09%09%09
@@ -986,25 +986,24 @@
 %5Cn%22 //%0A%09%09%09%09%09
-%09
 + %22  -i  Inc
@@ -1019,33 +1019,32 @@
 x pattern%5Cn%22 //%0A
-%09
 %09%09%09%09%09+ %22  -e  Ex
@@ -1065,25 +1065,24 @@
 ttern%5Cn%22 //%0A
-%09
 %09%09%09%09%09+ %22  -d
@@ -1145,25 +1145,24 @@
 %5Cn%22 //%0A%09%09%09%09%09
-%09
 + %22  -f  Sou
@@ -1172,25 +1172,24 @@
  file%5Cn%22 //%0A
-%09
 %09%09%09%09%09+ %22  -t
@@ -1234,21 +1234,16 @@
 logs%22);%0A
-%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%0A

improve commandline parameter validation