Skip to content

Commit

Permalink
CLI options (apache commons cli) added. And it works.
Browse files Browse the repository at this point in the history
  • Loading branch information
hermann77 committed Sep 25, 2018
1 parent 3585d7c commit e614a91
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 40 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ dependencies {
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.21'
// runtime "mysql:mysql-connector-java:5.1.21"
compile group: 'commons-cli', name: 'commons-cli', version: '1.4'
}

98 changes: 59 additions & 39 deletions src/main/java/de/schwarz/emailparser/ParserMain.java
Original file line number Diff line number Diff line change
@@ -1,71 +1,91 @@
package de.schwarz.emailparser;

import java.sql.*;
import org.apache.commons.cli.*;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class ParserMain {


/**
* Main method
* @param args
* @param args cli arguments
*/
public static void main(String[] args) {

Parser parser = new Parser();
Scanner input = null;

// no file name given ==> use the standard place in src/main/resources/emails_folder_file.txt
if(args.length == 0) {
input = parser.getInputFromResource();
parser.readLines(input);
}
// file name given in args
else {

String inputFile = args[0];
input = parser.getInputFromFileInArgs(inputFile);
Options options = new Options();

// only emails list file passed
if(args.length == 1) {
parser.readLines(input);
}
// emails file AND DATABASE name + table passed in parameters
else if(args.length == 4) {
String databaseName = args[1];
String tableName = args[2];
String attributeName = args[3];
Option inputOpt = new Option("i", "input", true, "input file path");
inputOpt.setRequired(true);
options.addOption(inputOpt);

List<String> emailAddressesToDelete = new ArrayList<String>();
emailAddressesToDelete = parser.readLines(input);
Option dbNameOpt = new Option("d", "dbname", true, "database name where the email addresses are saved");
dbNameOpt.setRequired(true);
options.addOption(dbNameOpt);

deleteEmailAddressesFromDB(databaseName, tableName, attributeName, emailAddressesToDelete);
}
// DATABASE name AND port passed in parameters
else if(args.length == 5) {
String databaseName = args[1];
String tableName = args[2];
String attributeName = args[3];
String port = args[4];
Option tableOpt = new Option("t", "table", true, "database table name where the email addresses are saved");
tableOpt.setRequired(true);
options.addOption(tableOpt);

Option attrOpt = new Option("a", "attribute", true, "attribute name where the email addresses are saved");
attrOpt.setRequired(true);
options.addOption(attrOpt);

Option portOpt = new Option("p", "port", true, "database port");
portOpt.setRequired(false);
options.addOption(portOpt);

CommandLineParser cliParser = new DefaultParser();
HelpFormatter formatter = new HelpFormatter();
CommandLine cmd = null;

List<String> emailAddressesToDelete = new ArrayList<String>();
emailAddressesToDelete = parser.readLines(input);
try{
cmd = cliParser.parse(options, args);
}
catch (ParseException e) {
System.out.println(e.getMessage());
formatter.printHelp("java -jar build/libs/email-parser-all-<VERSION>.jar", options);

System.exit(1);
}

deleteEmailAddressesFromDB(databaseName, tableName, attributeName, emailAddressesToDelete, port);

if(args.length == 0) {
input = parser.getInputFromResource();
parser.readLines(input);
}
// file name given in args
else {
String inputFile = cmd.getOptionValue("input");
String databaseName = cmd.getOptionValue("dbname");
String tableName = cmd.getOptionValue("table");
String attributeName = cmd.getOptionValue("attribute");
String port = cmd.getOptionValue("port");

input = parser.getInputFromFileInArgs(inputFile);

List<String> emailAddressesToDelete = new ArrayList<String>();
emailAddressesToDelete = parser.readLines(input);

if(port == null) {
port = "3306";
}
deleteEmailAddressesFromDB(databaseName, tableName, attributeName, emailAddressesToDelete, port);
}

}


private static void deleteEmailAddressesFromDB(String databaseName, String tableName, String attributeName, List<String> emailAddressesToDelete) {
// use standard port 3306 (standard port on MacOS: 3307)
deleteEmailAddressesFromDB(databaseName, tableName, attributeName, emailAddressesToDelete, "3306");
}


private static void deleteEmailAddressesFromDB(String databaseName, String tableName, String attributeName, List<String> emailAddressesToDelete, String port) {
Expand Down

0 comments on commit e614a91

Please sign in to comment.