use of gnu.getopt.LongOpt in project Xponents by OpenSextant.
the class XText method main.
public static void main(String[] args) {
LongOpt[] options = { new LongOpt("input", LongOpt.REQUIRED_ARGUMENT, null, 'i'), new LongOpt("output", LongOpt.REQUIRED_ARGUMENT, null, 'o'), new LongOpt("export", LongOpt.REQUIRED_ARGUMENT, null, 'x'), new LongOpt("strip-prefix", LongOpt.REQUIRED_ARGUMENT, null, 'p'), new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h'), new LongOpt("clean-html", LongOpt.NO_ARGUMENT, null, 'H'), new LongOpt("embed-conversion", LongOpt.NO_ARGUMENT, null, 'e'), new LongOpt("embed-children", LongOpt.NO_ARGUMENT, null, 'c'), new LongOpt("tika-pst", LongOpt.NO_ARGUMENT, null, 'T') };
// "hcex:i:o:p:"
gnu.getopt.Getopt opts = new gnu.getopt.Getopt("XText", args, "", options);
String input = null;
String output = null;
boolean embed = false;
boolean filter_html = false;
boolean saveChildrenWithInput = false;
String saveChildrenTo = null;
String prefix = null;
XText xt = new XText();
try {
int c;
while ((c = opts.getopt()) != -1) {
switch(c) {
case 0:
break;
case 'i':
input = opts.getOptarg();
break;
case 'o':
output = opts.getOptarg();
break;
case 'H':
filter_html = true;
break;
case 'c':
saveChildrenWithInput = true;
break;
case 'x':
saveChildrenTo = opts.getOptarg();
break;
case 'p':
prefix = opts.getOptarg();
break;
case 'e':
embed = true;
System.out.println("Saving conversions to Input folder. Output folder will be ignored.");
break;
case 'T':
xt.enableTikaPST(true);
break;
case 'h':
default:
XText.usage();
System.exit(1);
}
}
} catch (Exception err) {
XText.usage();
System.exit(1);
}
if (input == null) {
System.out.println("An input argument is required, e.g., -Dinput=/Folder/...");
System.exit(-1);
}
// Setting LANG=en_US in your shell.
//
// System.setProperty("LANG", "en_US");
// Given this is a test application, we will
xt.enableOverwrite(true);
// overwrite every time XText is called.
xt.enableSaving(embed || output != null);
// creates a ./text/ Folder locally in
xt.getPathManager().enableSaveWithInput(embed);
// directory.
xt.enableHTMLScrubber(filter_html);
xt.getPathManager().enableSaveChildrenWithInput(saveChildrenWithInput);
// If user wishes to strip input paths of some prefix
// Output will be dumped in the resulting relative path.
xt.getPathManager().setStripPrefixPath(prefix);
// ... others?
if (!saveChildrenWithInput && saveChildrenTo != null) {
xt.getPathManager().setExtractedChildrenCache(saveChildrenTo);
}
try {
if (!embed) {
if (output == null) {
output = "output";
// Will save to output dir.
xt.enableSaving(true);
FileUtility.makeDirectory(output);
xt.getPathManager().setConversionCache(output);
System.out.println("Default output folder is $PWD/" + output);
} else {
xt.enableSaving(true);
// Notice this main program requires an output path.
xt.getPathManager().setConversionCache(output);
}
}
// Set itself to listen, as this is the main program.
xt.setConversionListener(new MainProgramListener());
xt.setup();
xt.extractText(input);
} catch (IOException ioerr) {
XText.usage();
ioerr.printStackTrace();
} catch (ConfigException cfgerr) {
XText.usage();
cfgerr.printStackTrace();
}
}
use of gnu.getopt.LongOpt in project Xponents by OpenSextant.
the class XponentsTaggerDemo method run.
/**
* Returns 0 = SUCCESS, other than 0 is a FAILURE mode.
*
* @throws IOException
* @throws InterruptedException
* @throws ClassNotFoundException
*/
@Override
public int run(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
/*
* Run:
* /input/path /output/path phase
*
* phase = geotag | xtax
*/
LongOpt[] options = { new LongOpt("in", LongOpt.REQUIRED_ARGUMENT, null, 'i'), new LongOpt("out", LongOpt.REQUIRED_ARGUMENT, null, 'o'), new LongOpt("phase", LongOpt.REQUIRED_ARGUMENT, null, 'p'), new LongOpt("log4j-extra-config", LongOpt.REQUIRED_ARGUMENT, null, 'L') };
gnu.getopt.Getopt opts = new gnu.getopt.Getopt("xponents-mr", args, "", options);
String inPath = null;
String outPath = null;
String phase = null;
String log4jExtraConfig = null;
System.out.println(Arrays.toString(args));
int c;
while ((c = opts.getopt()) != -1) {
switch(c) {
case 0:
// 0 = Long opt processed.
break;
case 'i':
inPath = opts.getOptarg();
break;
case 'o':
outPath = opts.getOptarg();
break;
case 'p':
phase = opts.getOptarg();
break;
case 'L':
log4jExtraConfig = opts.getOptarg();
break;
default:
return -2;
}
}
/* Helper resources -- possibly just replaced by existing SocGeoBase utilities.
*/
XponentsTaggerDemo.initResources();
/* Job App Configuration
*/
Job job = Job.getInstance(getConf(), "Xponents-Tagging");
job.setJarByClass(XponentsTaggerDemo.class);
// REQUIRED:
if (phase == null) {
System.err.println("Phase and must be set");
usage();
return -2;
}
job.getConfiguration().set("phase", phase);
/* Mapper */
if (phase.equalsIgnoreCase("geotag")) {
job.setMapperClass(GeoTaggerMapper.class);
} else if (phase.equalsIgnoreCase("xtax")) {
job.setMapperClass(KeywordTaggerMapper.class);
}
if (log4jExtraConfig != null) {
job.getConfiguration().set(LOG4J_SUPPLEMENTAL_CONFIGURATION, log4jExtraConfig);
}
/* No Reduce step */
job.setNumReduceTasks(1);
/* Job Input */
job.setInputFormatClass(SequenceFileInputFormat.class);
/* Map Phase ouptut */
SequenceFileInputFormat.addInputPaths(job, inPath);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class);
/* Job Output */
job.getConfiguration().setBoolean("mapreduce.map.output.compress", true);
job.getConfiguration().set("mapreduce.map.output.compress.codec", "org.apache.hadoop.io.compress.SnappyCodec");
job.setOutputFormatClass(SequenceFileOutputFormat.class);
SequenceFileOutputFormat.setOutputPath(job, new Path(outPath));
SequenceFileOutputFormat.setCompressOutput(job, true);
SequenceFileOutputFormat.setOutputCompressionType(job, CompressionType.BLOCK);
job.getConfiguration().set("mapreduce.output.fileoutputformat.compress.codec", "org.apache.hadoop.io.compress.SnappyCodec");
/* Note that Job Output and Mapper Output should be in sync, if you have only Mapper Output */
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
return job.waitForCompletion(true) ? 0 : 1;
}
use of gnu.getopt.LongOpt in project ACS by ACS-Community.
the class CDBChecker method checkArgs.
/**
* Checks the command line arguments given to the program and capture the given flags.
* @param args command line arguments
* @return True if arguments are OK, false otherwise
*/
protected boolean checkArgs(String[] args) {
boolean retVal = true;
int c;
LongOpt[] longopts = new LongOpt[5];
longopts[0] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h');
longopts[1] = new LongOpt("network", LongOpt.NO_ARGUMENT, null, 'n');
longopts[2] = new LongOpt("verbose", LongOpt.NO_ARGUMENT, null, 'v');
longopts[3] = new LongOpt("recursive", LongOpt.NO_ARGUMENT, null, 'r');
longopts[4] = new LongOpt("checkIdlTypes", LongOpt.NO_ARGUMENT, null, 'c');
Getopt myGetOpt = new Getopt("cdbChecker", args, "rhncvaW;", longopts);
// We'll do our own error handling
myGetOpt.setOpterr(false);
while ((c = myGetOpt.getopt()) != -1) {
switch(c) {
case 'n':
this.network = true;
break;
case 'r':
this.recursive = false;
break;
case 'v':
this.verbose = true;
break;
case 'h':
retVal = false;
break;
case 'c':
this.checkidl = true;
break;
case 'W':
System.out.println("[Error] : you tried a -W with an incorrect long option name");
globalErrorFlag = true;
retVal = false;
break;
case '?':
if (0 == myGetOpt.getOptopt()) {
System.out.println("[Error] : the long option '" + args[myGetOpt.getOptind() - 1] + "' is not valid");
} else {
System.out.println("[Error] : the option '" + (char) myGetOpt.getOptopt() + "' is not valid");
}
globalErrorFlag = true;
retVal = false;
break;
default:
globalErrorFlag = true;
retVal = false;
break;
}
}
// do the following only if we aren't already needing to pretVal is not false)
if (retVal) {
// check for the additional (optional) command line arguments, XMLPath and XSDPath
for (int i = myGetOpt.getOptind(); i < args.length; i++) {
if (myGetOpt.getOptind() == i) {
if (args[i].startsWith("/") || args[i].matches("[a-zA-Z]:.*")) {
this.XMLPath = args[i];
} else {
System.out.println("[Error] : XMLPath must start with '/'");
globalErrorFlag = true;
retVal = false;
}
} else {
if (args[i].startsWith("/") || args[i].matches("[a-zA-Z]:.*")) {
this.XSDPath = args[i];
} else {
System.out.println("[Error] : XSDPath must start with '/'");
globalErrorFlag = true;
retVal = false;
}
break;
}
}
if (retVal && null == this.XMLPath) {
String acsCdbPath = System.getenv("ACS_CDB");
if (null != acsCdbPath) {
acsCdbPath += File.separator + "CDB";
System.out.println("*** XML path not specified; defaulting to $ACS_CDB" + File.separator + "CDB: " + acsCdbPath);
XMLPath = acsCdbPath;
} else {
System.out.println("\n[Error] XML path not specified and $ACS_CDB environment variable not set; no default possible.");
globalErrorFlag = true;
retVal = false;
}
}
}
return retVal;
}
Aggregations