summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Wu <peter@lekensteyn.nl>2014-05-09 11:24:26 +0200
committerPeter Wu <peter@lekensteyn.nl>2014-05-09 11:24:26 +0200
commit0ef2f13370e44834f7047313e03df4c149aea6c2 (patch)
tree18fd784b7372d6bbcb2082e533ff1043a99f7b2b
parentc98a8ad4326ef36fc4fe03ba710ab2ba962ad60c (diff)
downloadDatafiller-0ef2f13370e44834f7047313e03df4c149aea6c2.tar.gz
Fixup help text, allow to override DB settings
-rw-r--r--src/main/Main.java59
1 files changed, 34 insertions, 25 deletions
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 <ip> Specify the database ip address.",
- " --file <tweet> <profile> 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."
};
}