From 0ef2f13370e44834f7047313e03df4c149aea6c2 Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Fri, 9 May 2014 11:24:26 +0200 Subject: Fixup help text, allow to override DB settings --- src/main/Main.java | 59 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/main/Main.java b/src/main/Main.java index ba59f8b..bc5c9d5 100644 --- a/src/main/Main.java +++ b/src/main/Main.java @@ -25,30 +25,29 @@ public class Main { public static void main(String[] args) { try { Main main = new Main(args); + main.run(); } catch (IllegalArgumentException ex) { System.err.println(ex.getMessage()); System.exit(1); } } - private String m_hostaddress; - private String m_filename; + private final ConnectionBuilder cb; public Main(String[] args) { - /* parse the global options. */ - parseGlobalOptions(args); - - if (m_hostaddress == null) { - throw new IllegalArgumentException("Missing --dbhost to specify the hostaddress."); - } - - ConnectionBuilder cb = new ConnectionBuilder() - .setServerName(m_hostaddress) + // default connection properties + cb = new ConnectionBuilder() + .setServerName("localhost") .setUsername("postgres") .setPassword("2IOC02") .setDbName("Twitter"); + /* parse the global options. */ + parseGlobalOptions(args); + } + + public void run() { try (Connection connection = cb.create()) { /* create the object that fills the database */ @@ -77,7 +76,7 @@ public class Main { } } catch (SQLException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, - "Query error", ex); + "DB error", ex); } } @@ -86,13 +85,20 @@ public class Main { for (int i = 0; i < args.length; i++) { if ("--help".equals(args[i])) { printHelp(); + System.exit(0); } else if ("--dbhost".equals(args[i])) { - m_hostaddress = getParam(args, ++i); + cb.setServerName(getArg(args, ++i, "--dbhost")); + } else if ("--dbuser".equals(args[i])) { + cb.setUsername(getArg(args, ++i, "--dbuser")); + } else if ("--dbpass".equals(args[i])) { + cb.setPassword(getArg(args, ++i, "--dbpass")); + } else if ("--dbname".equals(args[i])) { + cb.setDbName(getArg(args, ++i, "--dbname")); } else if (args[i].startsWith("-")) { throw new IllegalArgumentException("Invalid option: " + args[i]); } else { /* This should be the filename */ - m_filename = getParam(args, i); + m_filename = args[i]; } } @@ -104,13 +110,12 @@ public class Main { /** * Read an extra option for a command. */ - private String getParam(String[] args, Integer index) { - if (index + 1 <= args.length) { - index++; - return args[index - 1]; - } else { - throw new IllegalArgumentException("An extra option was missing."); + private String getArg(String[] params, int index, String name) { + if (index >= params.length) { + System.err.println("Missing argument for parameter " + name); + System.exit(1); } + return params[index]; } /** @@ -123,11 +128,15 @@ public class Main { } private final static String[] HELP = { + "Usage: java -jar DataFiller.jar [options] [tweets-file]", + "", "Global options:", - " --help Print this help text.", - " --ip Specify the database ip address.", - " --file Specify the tweet and profile filenames ", - " ", - " if no --file was specified read from standard input." + " --help Print this help text.", + " --dbhost HOST Database host (defaults to 'localhost')", + " --dbuser USER Database username (defaults to 'postgres')", + " --dbpass PASS Database password (defaults to '2IOC02')", + " --dbname NAME Database name (defaults to 'Twitter')", + "", + "If no tweets file is given, data will be read from standard input." }; } -- cgit v1.2.1