show info basics v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Mon, 16 Dec 2013 15:15:32 +0100
branchv_0
changeset 14 189b1260b942
parent 13 599aad77e986
child 15 bbd335b5410c
show info basics
java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java
java/sql-dk/src/info/globalcode/sql/dk/CLIParser.java
java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java
java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java
java/sql-dk/test/info/globalcode/sql/dk/CLIParserTest.java
--- a/java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java	Mon Dec 16 12:10:45 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java	Mon Dec 16 15:15:32 2013 +0100
@@ -1,8 +1,10 @@
 package info.globalcode.sql.dk;
 
 import static info.globalcode.sql.dk.Functions.isNotEmpty;
+import static info.globalcode.sql.dk.Functions.equalz;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.EnumSet;
 import java.util.List;
 
 /**
@@ -15,13 +17,24 @@
 	private String sql;
 	private String databaseName;
 	private String namePrefix = DEFAULT_NAME_PREFIX;
+	private String formatterName;
 	private boolean batch;
 
 	public enum MODE {
 
 		QUERY_NOW,
 		PREPARE_BATCH,
-		EXECUTE_BATCH
+		EXECUTE_BATCH,
+		JUST_SHOW_INFO
+	}
+
+	public enum INFO_TYPE {
+
+		HELP,
+		VERSION,
+		LICENSE,
+		FORMATTERS,
+		TYPES
 	}
 
 	public enum COMMAND_TYPE {
@@ -34,12 +47,36 @@
 	private COMMAND_TYPE commandType;
 	private final Collection<NamedParameter> namedParameters = new ArrayList<>();
 	private final List<Parameter> numberedParameters = new ArrayList<>();
+	private final EnumSet<INFO_TYPE> showInfo = EnumSet.noneOf(INFO_TYPE.class);
 
 	public void validate() throws InvalidOptionsException {
 		InvalidOptionsException e = new InvalidOptionsException();
 
-		if (getMode() == null) {
+		MODE mode = getMode();
+		if (mode == null) {
 			e.addProblem(new InvalidOptionsException.OptionProblem("Invalid combination of DB, SQL and BATCH – please specify just 2 of this 3 options"));
+		} else if (mode == MODE.JUST_SHOW_INFO) {
+			if (!namedParameters.isEmpty()) {
+				e.addProblem(new InvalidOptionsException.OptionProblem("Do not use named parameters if just showing info."));
+			}
+			if (!numberedParameters.isEmpty()) {
+				e.addProblem(new InvalidOptionsException.OptionProblem("Do not use numbered parameters if just showing info."));
+			}
+			if (isNotEmpty(sql, false)) {
+				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify SQL if just showing info."));
+			}
+			if (isNotEmpty(databaseName, false)) {
+				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify database if just showing info."));
+			}
+			if (batch) {
+				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify batch if just showing info."));
+			}
+			if (isNotEmpty(formatterName, false)) {
+				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify formatter if just showing info."));
+			}
+			if (!equalz(namePrefix, DEFAULT_NAME_PREFIX)) {
+				e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name prefix if just showing info."));
+			}
 		}
 
 		if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) {
@@ -74,7 +111,7 @@
 		} else if (hasDb() && batch && !hasSql()) {
 			return MODE.EXECUTE_BATCH;
 		} else {
-			return null;
+			return showInfo.isEmpty() ? null : MODE.JUST_SHOW_INFO;
 		}
 	}
 
@@ -129,4 +166,20 @@
 	public void setNamePrefix(String namePrefix) {
 		this.namePrefix = namePrefix;
 	}
+
+	public String getFormatterName() {
+		return formatterName;
+	}
+
+	public void setFormatterName(String formatterName) {
+		this.formatterName = formatterName;
+	}
+
+	public void addShowInfo(INFO_TYPE info) {
+		showInfo.add(info);
+	}
+
+	public EnumSet<INFO_TYPE> getShowInfo() {
+		return showInfo;
+	}
 }
--- a/java/sql-dk/src/info/globalcode/sql/dk/CLIParser.java	Mon Dec 16 12:10:45 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIParser.java	Mon Dec 16 15:15:32 2013 +0100
@@ -96,6 +96,24 @@
 						}
 					}
 					break;
+				case Tokens.FORMATTER:
+					options.setFormatterName(fetchNext(args, ++i));
+					break;
+				case Tokens.INFO_HELP:
+					options.addShowInfo(CLIOptions.INFO_TYPE.HELP);
+					break;
+				case Tokens.INFO_FORMATTERS:
+					options.addShowInfo(CLIOptions.INFO_TYPE.FORMATTERS);
+					break;
+				case Tokens.INFO_LICENSE:
+					options.addShowInfo(CLIOptions.INFO_TYPE.LICENSE);
+					break;
+				case Tokens.INFO_TYPES:
+					options.addShowInfo(CLIOptions.INFO_TYPE.TYPES);
+					break;
+				case Tokens.INFO_VERSION:
+					options.addShowInfo(CLIOptions.INFO_TYPE.VERSION);
+					break;
 				default:
 					throw new CLIParserException("Unknown option: " + arg);
 			}
@@ -121,6 +139,12 @@
 		public static final String DATA = "--data";
 		public static final String NAME_PREFIX = "--name-prefix";
 		public static final String TYPES = "--types";
+		public static final String FORMATTER = "--formatter";
+		public static final String INFO_HELP = "--help";
+		public static final String INFO_VERSION = "--version";
+		public static final String INFO_LICENSE = "--license";
+		public static final String INFO_FORMATTERS = "--list-formatters";
+		public static final String INFO_TYPES = "--list-types";
 
 		private Tokens() {
 		}
--- a/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Mon Dec 16 12:10:45 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Mon Dec 16 15:15:32 2013 +0100
@@ -13,10 +13,23 @@
 
 	public static void main(String[] args) {
 		try {
+			/** Parse options */
 			CLIParser parser = new CLIParser();
 			CLIOptions options = parser.parseOptions(args);
+
+
+			options.validate();
+
+			/** Show info */
+			if (!options.getShowInfo().isEmpty()) {
+				InfoLister infoLister = new InfoLister();
+				infoLister.showInfo(options.getShowInfo(), System.err);
+			}
+
 		} catch (CLIParserException e) {
-			log.log(Level.SEVERE, null, e);
+			log.log(Level.SEVERE, "Unable to parse CLI options", e);
+		} catch (InvalidOptionsException e) {
+			log.log(Level.SEVERE, "Invalid CLI options", e);
 		}
 	}
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java	Mon Dec 16 15:15:32 2013 +0100
@@ -0,0 +1,39 @@
+package info.globalcode.sql.dk;
+
+import java.io.PrintStream;
+import java.util.EnumSet;
+
+/**
+ * Displays info like help, version etc.
+ *
+ * @author Ing. František Kučera (frantovo.cz)
+ */
+public class InfoLister {
+
+	public void showInfo(EnumSet<CLIOptions.INFO_TYPE> infoTypes, PrintStream out) {
+		for (CLIOptions.INFO_TYPE infoType : infoTypes) {
+			switch (infoType) {
+				/**
+				 * TODO: implement show info
+				 */
+				case FORMATTERS:
+					out.println("TODO: list available formatters");
+					break;
+				case HELP:
+					out.println("TODO: show some help");
+					break;
+				case LICENSE:
+					out.println("TODO: show license");
+					break;
+				case TYPES:
+					out.println("TODO: list supported types");
+					break;
+				case VERSION:
+					out.println("TODO: show version");
+					break;
+				default:
+					throw new IllegalArgumentException("Unsupported INFO_TYPE: " + infoType);
+			}
+		}
+	}
+}
--- a/java/sql-dk/test/info/globalcode/sql/dk/CLIParserTest.java	Mon Dec 16 12:10:45 2013 +0100
+++ b/java/sql-dk/test/info/globalcode/sql/dk/CLIParserTest.java	Mon Dec 16 15:15:32 2013 +0100
@@ -159,4 +159,15 @@
 		assertEquals(options.getDatabaseName(), DATABASE_NAME_1);
 		assertEquals(options.getMode(), CLIOptions.MODE.EXECUTE_BATCH);
 	}
+
+	@Test
+	public void testParseOptions_ShowInfo_Help() throws InvalidOptionsException, CLIParserException {
+		String[] args = new String[]{Tokens.INFO_HELP};
+		CLIOptions options = parser.parseOptions(args);
+		options.validate();
+
+		assertEquals(options.getMode(), CLIOptions.MODE.JUST_SHOW_INFO);
+		assertEquals(options.getShowInfo().size(), 1);
+		assertTrue(options.getShowInfo().contains(CLIOptions.INFO_TYPE.HELP));
+	}
 }
\ No newline at end of file