From: SVN by r. <sv...@ca...> - 2009-01-23 16:27:33
|
Author: roy Date: 2009-01-23 17:27:25 +0100 (Fri, 23 Jan 2009) New Revision: 362 Modified: src/main/java/nl/improved/sqlclient/SQLShell.java Log: added -engine support more help information Modified: src/main/java/nl/improved/sqlclient/SQLShell.java =================================================================== --- src/main/java/nl/improved/sqlclient/SQLShell.java 2009-01-19 15:43:49 UTC (rev 361) +++ src/main/java/nl/improved/sqlclient/SQLShell.java 2009-01-23 16:27:25 UTC (rev 362) @@ -38,21 +38,78 @@ */ public class SQLShell { + public static final String INPUT = "-i"; + public static final String OUTPUT = "-o"; + public static final String ENGINE = "-engine"; + public static final String ENGINE_CHARVA = "charva"; + public static final String ENGINE_JCURSES = "jcurses"; + + public static Map<String, String> parseHelp(String[] args) throws IOException { + Map<String, String> argsMap = new HashMap<String, String>(); + if (args[0].equals("--help") || args.length %2 == 1) { + System.err.println("Usage: "); + System.err.println(" java nl.improved.sqlclient.SQLShell [-i inputfile] [-o outputfile] [-engine charva/jcurses]"); + System.exit(-1); + } + for (int i = 0; i < args.length; i+=2) { + if (args[i].equals(INPUT)) { + if (args.length < i+1) { + System.err.println("Missing filename parameter for -i argument"); + System.exit(-1); + } + File f = new File(args[i+1]); + if (!f.exists()) { + System.err.println("Filename '"+f.getAbsolutePath()+"' does not exist.\nAborting..."); + System.exit(-1); + } + if (!f.canRead()) { + System.err.println("Filename '"+f.getAbsolutePath()+"' cannot be read.\nAborting..."); + System.exit(-1); + } + argsMap.put(args[i], args[i+1]); + } else if (args[i].equals(OUTPUT)) { + if (args.length < i+1) { + System.err.println("Missing filename parameter for -i argument"); + System.exit(-1); + } + File f = new File(args[i+1]); + if (f.exists()) { + if (f.canWrite()) { + System.err.println("Filename already exists.. overwriting"); + } else { + System.err.println("Filename '"+f.getAbsolutePath()+"' cannot be written.\nAborting..."); + System.exit(-1); + } + } else if (f.createNewFile()) { + System.err.println("Filename '"+f.getAbsolutePath()+"' cannot be created.\nAborting..."); + System.exit(-1); + } + argsMap.put(args[i], args[i+1]); + } else if (args[i].equals(ENGINE)) { + if (args.length < i+1) { + System.err.println("Missing engine type parameter for -i argument."); + System.err.println("Possible values are 'jcurses' and 'charva'"); + System.exit(-1); + } + if (!(args[i+1].equals(ENGINE_CHARVA)) && !(args[i+1].equals(ENGINE_JCURSES))) { + System.err.println("Unknown parameter for engine type '"+ args[i+1]+"'"); + System.err.println("Possible values are 'jcurses' and 'charva'"); + System.exit(-1); + } + argsMap.put(args[i], args[i+1]); + } else { + System.err.println("Uknown option: "+ args[i]+"\nAborting..."); + System.exit(-1); + } + } + return argsMap; + } public static void main(String[] args) throws InterruptedException, IOException { PrintStream errorStream = System.err; PrintStream outStream = System.out; - final Map<String, String> argsMap = new HashMap<String, String>(); - if (args.length > 0) { - if (args[0].equals("--help") || args.length %2 == 1) { - System.err.println("Usage: "); - System.exit(-1); - } - for (int i = 0; i < args.length; i+=2) { - argsMap.put(args[i], args[i+1]); - } - } + final Map<String, String> argsMap = parseHelp(args); AbstractSQLShellWindow sqlshellWindow; - if (argsMap.get("-i") != null) { + if (argsMap.get(INPUT) != null) { sqlshellWindow = new AbstractSQLShellWindow() { @Override public int getScreenWidth() { @@ -93,7 +150,7 @@ @Override protected void output(CharSequence data) { - if (!argsMap.containsKey("-o")) { + if (!argsMap.containsKey(OUTPUT)) { System.out.println(data); } } @@ -102,8 +159,8 @@ }; Command cmd = new nl.improved.sqlclient.AbstractSQLShellWindow.ExecuteBatchCommand(sqlshellWindow); CommandResult result = cmd.execute(new SQLCommand("@"+ argsMap.get("-i"))); - if (argsMap.containsKey("-o")) { - File f = new File(argsMap.get("-o")); + if (argsMap.containsKey(OUTPUT)) { + File f = new File(argsMap.get(OUTPUT)); FileOutputStream fout = new FileOutputStream(f); for (CharSequence s : sqlshellWindow.getScreen().getScreenBuffer()) { fout.write(s.toString().getBytes()); @@ -122,9 +179,19 @@ } } else { //sqlshellWindow = new SQLShellWindow(); - try { - sqlshellWindow = new CharvaSQLShellWindow(); - } catch(Error e) { + if (!argsMap.containsKey(ENGINE) || argsMap.get(ENGINE).equals(ENGINE_CHARVA)) { + try { + sqlshellWindow = new CharvaSQLShellWindow(); + } catch(Error e) { + if (!argsMap.containsKey(ENGINE)) { + sqlshellWindow = new SQLShellWindow(); + } else { + System.err.println("Failed to instantiate charva version of sqlshell.\nCaused by: "+ e); + e.printStackTrace(); + return; + } + } + } else { sqlshellWindow = new SQLShellWindow(); } } |