![JAR search and dependency download from the Maven repository](/logo.png)
com.sonalake.utah.cli.CommandLineInterface Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of utah-parser Show documentation
Show all versions of utah-parser Show documentation
A Java library for parsing semi-structured text files
The newest version!
package com.sonalake.utah.cli;
import com.google.gson.GsonBuilder;
import com.sonalake.utah.Parser;
import com.sonalake.utah.config.Config;
import org.apache.commons.cli.*;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.lang3.StringUtils;
import java.io.*;
import java.util.*;
public class CommandLineInterface {
private static final String FORMAT_PARAM = "o";
private static final String CONFIG_PARAM = "f";
public static void main(String[] args) {
CommandLineInterface iface = new CommandLineInterface();
Reader source = new InputStreamReader(System.in);
PrintStream target = System.out;
iface.processArgs(args, source, target);
}
void processArgs(String[] args, Reader source, PrintStream target) {
try {
CLIConfig cliConfig = parse(args);
try (BufferedReader reader = new BufferedReader(source)) {
Parser parser = parseInput(cliConfig, reader);
String format = cliConfig.getFormat().toString();
switch (format.toUpperCase()) {
case "JSON":
printToJSON(parser, target);
break;
case "CSV":
printToCSV(parser, target);
break;
}
}
} catch (Exception e) {
HelpFormatter formatter = new HelpFormatter();
PrintWriter pw = new PrintWriter(target);
formatter.printHelp(pw, formatter.getWidth(), "utah", "",
buildOptions(), formatter.getLeftPadding(), formatter.getDescPadding(),
"", true);
pw.flush();
}
}
/**
* Print the content of the parsed records as json to the target
*
* @param parser
*/
void printToJSON(Parser parser, PrintStream target) {
Map curr = parser.next();
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy