Commit c0891c6b authored by Christian MOMON's avatar Christian MOMON
Browse files

Improved CLI.

parent 17d404bb
......@@ -63,7 +63,7 @@ public class FlatDB4GeoNamesLauncher
// Manage parameters.
if (args.length > 0)
{
FlatDB4GeoNamesCLI.main(args);
FlatDB4GeoNamesCLI.run(args);
}
else if (args.length == 0)
{
......
......@@ -21,6 +21,7 @@ package org.tip.flatdb4geonames.cli;
import java.io.File;
import java.io.IOException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tip.flatdb4geonames.model.FlatDB4GeoNamesException;
......@@ -28,6 +29,10 @@ import org.tip.flatdb4geonames.model.FlatDB4GeoNamesFactory;
import org.tip.flatdb4geonames.model.GeoNamesFlatDatabase;
import org.tip.flatdb4geonames.model.GeoNamesLine;
import org.tip.flatdb4geonames.model.GeoNamesLines;
import org.tip.flatdb4geonames.util.BuildInformation;
import org.tip.flatdb4geonames.util.ToolBox;
import fr.devinsy.util.StringList;
/**
*
......@@ -37,14 +42,28 @@ public class FlatDB4GeoNamesCLI
{
private static Logger logger = LoggerFactory.getLogger(FlatDB4GeoNamesCLI.class);
/**
*
* @param results
*/
public static void println(final GeoNamesLines results)
{
for (GeoNamesLine result : results)
{
System.out.println("-------------------------------------------");
System.out.println(result.toLine());
}
}
/**
*
* @param args
* @throws IOException
* @throws FlatDB4GeoNamesException
*/
public static void main(final String[] args) throws IOException, FlatDB4GeoNamesException
public static void run(final String[] args) throws IOException, FlatDB4GeoNamesException
{
//
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler()
{
/**
......@@ -72,69 +91,96 @@ public class FlatDB4GeoNamesCLI
}
});
if ((args.length == 1) && (args[0].equals("-h")))
logger.debug("args={}{}", args.length, StringList.toStringWithBrackets(args));
// This following code has high cyclomatic because it is implementing a
// transducer of the parameters.
if (args.length == 0)
{
showHelp();
}
else if ((args.length > 1) && (args[0].equals("-cli")))
else
{
if ((args.length == 2) && (args[1].equals("download")))
if (ToolBox.matchAny(args[0], "-h", "-help", "--help"))
{
FlatDB4GeoNamesFactory.downloadGeoNamesFiles(new File(System.getProperty("user.dir")));
showHelp();
}
else if ((args.length == 3) && (args[1].equals("download")))
else if (StringUtils.equals(args[0], "download"))
{
FlatDB4GeoNamesFactory.downloadGeoNamesFiles(new File(args[2]));
}
else if ((args.length == 2) && (args[1].equals("buildIndex")))
{
FlatDB4GeoNamesFactory.buildIndex(new File(System.getProperty("user.dir")));
}
else if ((args.length == 3) && (args[1].equals("buildIndex")))
{
FlatDB4GeoNamesFactory.buildIndex(new File(args[2]));
if (args.length == 1)
{
FlatDB4GeoNamesFactory.downloadGeoNamesFiles(new File(System.getProperty("user.dir")));
}
else if (args.length == 2)
{
FlatDB4GeoNamesFactory.downloadGeoNamesFiles(ToolBox.normalizePath(args[1]));
}
else
{
System.err.println("Bad parameters.");
showHelp();
}
}
else if ((args.length == 2) && (args[1].equals("create")))
else if (StringUtils.equals(args[0], "buildIndex"))
{
FlatDB4GeoNamesFactory.create(new File(System.getProperty("user.dir")));
if (args.length == 1)
{
FlatDB4GeoNamesFactory.buildIndex(new File(System.getProperty("user.dir")));
}
else if (args.length == 2)
{
FlatDB4GeoNamesFactory.buildIndex(ToolBox.normalizePath(args[1]));
}
else
{
System.err.println("Bad parameters.");
showHelp();
}
}
else if ((args.length == 3) && (args[1].equals("create")))
else if (StringUtils.equals(args[0], "create"))
{
FlatDB4GeoNamesFactory.create(new File(args[2]));
if (args.length == 1)
{
FlatDB4GeoNamesFactory.create(new File(System.getProperty("user.dir")));
}
else if (args.length == 2)
{
FlatDB4GeoNamesFactory.create(ToolBox.normalizePath(args[1]));
}
else
{
System.err.println("Bad parameters.");
showHelp();
}
}
else if ((args.length == 3) && (args[1].equals("search")))
else if (StringUtils.equals(args[0], "search"))
{
GeoNamesFlatDatabase database = FlatDB4GeoNamesFactory.open(new File(System.getProperty("user.dir")));
GeoNamesLines results = database.search(args[2]);
println(results);
database.close();
if (args.length == 2)
{
GeoNamesFlatDatabase database = FlatDB4GeoNamesFactory.open(new File(System.getProperty("user.dir")));
GeoNamesLines results = database.search(args[1]);
println(results);
database.close();
}
else if (args.length == 3)
{
GeoNamesFlatDatabase database = FlatDB4GeoNamesFactory.open(ToolBox.normalizePath(args[1]));
GeoNamesLines results = database.search(args[2]);
println(results);
database.close();
}
else
{
System.err.println("Bad parameters.");
showHelp();
}
}
else if ((args.length == 4) && (args[1].equals("search")))
else
{
GeoNamesFlatDatabase database = FlatDB4GeoNamesFactory.open(args[2]);
GeoNamesLines results = database.search(args[3]);
println(results);
database.close();
System.err.println("Bad parameters.");
showHelp();
}
}
else
{
System.err.println("Bad parameters.");
showHelp();
}
}
/**
*
* @param results
*/
public static void println(final GeoNamesLines results)
{
for (GeoNamesLine result : results)
{
System.out.println("-------------------------------------------");
System.out.println(result.toLine());
}
}
/**
......@@ -142,6 +188,23 @@ public class FlatDB4GeoNamesCLI
*/
public static void showHelp()
{
System.out.println("flatdb4geonames [ -h | -cli [ download [directory] | buildIndex [directory] | create [directory] | search input ] ]");
StringList message = new StringList();
message.append("FlatDB4GeoNames, version ").append(new BuildInformation().version());
message.append(", JVM ").append(System.getProperty("java.version"));
message.append(", ").append(System.getProperty("os.name")).append(" ").append(System.getProperty("os.version")).append(" ").append(System.getProperty("sun.arch.data.model")).appendln("bits");
message.appendln();
message.appendln("FlatDB4GeoNames provides an easy, local and fast Java access to GeoNames data.");
message.appendln();
message.appendln("Usage:");
message.appendln(" flatdb4geonames run FlatDB4GeoNames GUI");
message.appendln(" flatdb4geonames [ -h | -help | --help ] display this help message");
message.appendln(" flatdb4geonames download [directory] download GeoNames files");
message.appendln(" flatdb4geonames buildIndex [directory] build FlatDB4GeoNames indexes");
message.appendln(" flatdb4geonames create [directory] download + buildIndex");
message.appendln(" flatdb4geonames search [directory] input search input in FlatDB4GeoNames database");
message.appendln();
message.appendln("If directory is not set, the current directory is defined as the FlatDB4GeoNames database location.");
System.out.println(message.toString());
}
}
......@@ -77,15 +77,15 @@ public class GeoNamesFlatDatabase
}
else if (!repository.exists())
{
throw new IllegalArgumentException("Bad repository (not existing).");
throw new IllegalArgumentException("Bad repository (not existing) " + repository.getAbsolutePath());
}
else if (!repository.isDirectory())
{
throw new IllegalArgumentException("Bad repository (not directory).");
throw new IllegalArgumentException("Bad repository (not directory) " + repository.getAbsolutePath());
}
else if (!repository.canRead())
{
throw new IllegalArgumentException("Bad repository (not readable).");
throw new IllegalArgumentException("Bad repository (not readable) " + repository.getAbsolutePath());
}
else
{
......
/*
* Copyright (C) 2016 Christian Pierre MOMON <christian.momon@devinsy.fr>
*
* This file is part of FlatDB4GeoNames.
*
* FlatDB4GeoNames is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.tip.flatdb4geonames.util;
import java.io.File;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author Christian Pierre MOMON (christian.momon@devinsy.fr)
*/
public class ToolBox
{
private static Logger logger = LoggerFactory.getLogger(ToolBox.class);
/**
*
*/
private ToolBox()
{
}
/**
*
* @param seq
* @return
*/
public static boolean matchAny(final CharSequence seq, final CharSequence... searchSeqs)
{
boolean result;
boolean ended = false;
int index = 0;
result = false;
while (!ended)
{
if (index < searchSeqs.length)
{
CharSequence searchSeq = searchSeqs[index];
if (StringUtils.equals(seq, searchSeq))
{
ended = true;
result = true;
}
else
{
index += 1;
}
}
else
{
ended = true;
result = false;
}
}
//
return result;
}
/**
*
* @param sourcePath
* @return
*/
public static File normalizePath(final String sourcePath)
{
File result;
if (StringUtils.isBlank(sourcePath))
{
result = null;
}
else
{
String targetPath;
if (sourcePath.startsWith("~"))
{
targetPath = sourcePath.replaceFirst("~", System.getProperty("user.home"));
}
else
{
targetPath = sourcePath;
}
result = new File(targetPath);
}
//
return result;
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment