Skip to content

Commit

Permalink
add -h option to print usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ryepup committed Dec 10, 2013
1 parent 24c2125 commit e8782db
Showing 1 changed file with 40 additions and 29 deletions.
69 changes: 40 additions & 29 deletions src/access2csv/Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Arrays;
import java.util.List;

import au.com.bytecode.opencsv.CSVWriter;

Expand All @@ -19,20 +21,20 @@ static int export(Database db, String tableName, Writer csv) throws IOException{
CSVWriter writer = new CSVWriter(new BufferedWriter(csv));
int rows = 0;
try{
for(Row row : table){
int i = 0;
for (Object object : row.values()) {
buffer[i++] = object == null ? null : object.toString();
for(Row row : table){
int i = 0;
for (Object object : row.values()) {
buffer[i++] = object == null ? null : object.toString();
}
writer.writeNext(buffer);
rows++;
}
writer.writeNext(buffer);
rows++;
}
}finally{
writer.close();
}
return rows;
}

static void export(String filename, String tableName) throws IOException{
Database db = DatabaseBuilder.open(new File(filename));
try{
Expand All @@ -41,9 +43,9 @@ static void export(String filename, String tableName) throws IOException{
db.close();
}
}

static void schema(String filename) throws IOException{

Database db = DatabaseBuilder.open(new File(filename));
try{
for(String tableName : db.getTableNames()){
Expand All @@ -58,9 +60,9 @@ static void schema(String filename) throws IOException{
}finally{
db.close();
}

}

static void exportAll(String filename) throws IOException{
Database db = DatabaseBuilder.open(new File(filename));
try{
Expand All @@ -79,21 +81,39 @@ static void exportAll(String filename) throws IOException{
}catch(IOException ex){}
}
}

}finally{
db.close();

}

}


static void printUsage(){
System.out.println("Usage:");
System.out.println(" java -jar access2csv.jar [ACCESS FILE] [OPTIONS]");
System.out.println("");
System.out.println("Options:");
System.out.println("");
System.out.println(" * if no options are provided, all tables will be exported to CSV files,");
System.out.println(" one file per table. Output file paths will be printed to stdout");
System.out.println(" * '--schema' - prints the database schema");
System.out.println(" * [TABLENAME] - prints the given table as CSV to stdout");
}

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub

List<String> helpCommands = Arrays.asList(new String[]{"-h", "--help", "-H", "/?"});

if(args.length == 1 && helpCommands.contains(args[0])){
printUsage();
System.exit(0);
}
if(args.length == 1 && args[0].equals("--schema")){
exportAll(args[0]);
System.exit(0);
}
if(args.length == 1){
exportAll(args[0]);
System.exit(0);
Expand All @@ -106,17 +126,8 @@ else if(args.length == 2){
export(args[0], args[1]);
System.exit(0);
}

System.out.println("Invalid arguments. Usage:");
System.out.println(" java -jar access2csv.jar [ACCESS FILE] [OPTIONS]");
System.out.println("");
System.out.println("Options:");
System.out.println("");
System.out.println(" * if no options are provided, all tables will be exported to CSV files,");
System.out.println(" one file per table. Output file paths will be printed to stdout");
System.out.println(" * '--schema' - prints the database schema");
System.out.println(" * [TABLENAME] - prints the given table as CSV to stdout");

System.err.println("Invalid arguments.");
printUsage();
System.exit(1);
}

Expand Down

0 comments on commit e8782db

Please sign in to comment.