use of org.apache.commons.cli.DefaultParser in project MassBank-web by MassBank.
the class Validator method main.
public static void main(String[] arguments) {
// load version and print
final Properties properties = new Properties();
try {
properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("project.properties"));
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
System.out.println("Validator version: " + properties.getProperty("version"));
// parse command line
Options options = new Options();
options.addOption(null, "db", false, "also read record from database and compare with original Record; Developer Feature!");
options.addOption(null, "legacy", false, "less strict mode for legacy records with minor problems.");
options.addOption(null, "online", false, "also do online checks, like PubChem CID check.");
CommandLine cmd = null;
try {
cmd = new DefaultParser().parse(options, arguments);
} catch (ParseException e) {
// oops, something went wrong
System.err.println("Parsing command line failed. Reason: " + e.getMessage());
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("Validator [OPTIONS] <FILE|DIR> [<FILE|DIR> ...]", options);
System.exit(1);
}
if (cmd.getArgList().size() == 0) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("Validator [OPTIONS] <FILE|DIR> [<FILE|DIR> ...]", options);
System.exit(1);
}
if (cmd.hasOption("legacy"))
System.out.println("Validation mode: legacy");
// find all files in arguments and all *.txt files in directories and subdirectories
// specified in arguments
List<File> recordfiles = new ArrayList<>();
for (String argument : cmd.getArgList()) {
File argumentf = new File(argument);
if (argumentf.isFile() && FilenameUtils.getExtension(argument).equals("txt")) {
recordfiles.add(argumentf);
} else if (argumentf.isDirectory()) {
recordfiles.addAll(FileUtils.listFiles(argumentf, new String[] { "txt" }, true));
} else {
logger.warn("Argument " + argument + " could not be processed.");
}
}
if (recordfiles.size() == 0) {
logger.error("No files found for validation.");
System.exit(1);
}
// validate all files
logger.trace("Validating " + recordfiles.size() + " files");
AtomicBoolean haserror = new AtomicBoolean(false);
AtomicBoolean doDatbase = new AtomicBoolean(cmd.hasOption("db"));
AtomicBoolean legacyMode = new AtomicBoolean(cmd.hasOption("legacy"));
AtomicBoolean onlineMode = new AtomicBoolean(cmd.hasOption("online"));
List<String> accessions = recordfiles.parallelStream().map(filename -> {
String recordString;
String accession = null;
logger.info("Working on " + filename + ".");
try {
recordString = FileUtils.readFileToString(filename, StandardCharsets.UTF_8);
if (hasNonStandardChars(recordString)) {
logger.warn("Check " + filename + ".");
}
;
// basic validation
Set<String> config = new HashSet<String>();
if (legacyMode.get())
config.add("legacy");
if (onlineMode.get())
config.add("online");
Record record = validate(recordString, "", config);
if (record == null) {
logger.error("Error in \'" + filename + "\'.");
haserror.set(true);
} else // additional tests
{
logger.trace("validation passed for " + filename);
// compare ACCESSION with filename
accession = record.ACCESSION();
if (!accession.equals(FilenameUtils.getBaseName(filename.toString()))) {
logger.error("Error in \'" + filename.getName().toString() + "\'.");
logger.error("ACCESSION \'" + record.ACCESSION() + "\' does not match filename \'" + filename.getName().toString() + "\'");
haserror.set(true);
}
// validate correct serialization: String <-> (String -> Record class -> String)
String recordStringFromRecord = record.toString();
int position = StringUtils.indexOfDifference(new String[] { recordString, recordStringFromRecord });
if (position != -1) {
logger.error("Error in \'" + filename + "\'.");
logger.error("File content differs from generated record string.\nThis might be a code problem. Please Report!");
String[] tokens = recordStringFromRecord.split("\\n");
int line = 0, col = 0, offset = 0;
for (String token : tokens) {
offset = offset + token.length() + 1;
if (position < offset) {
col = position - (offset - (token.length() + 1));
logger.error("Error in line " + (line + 1) + ".");
logger.error(tokens[line]);
StringBuilder error_at = new StringBuilder(StringUtils.repeat(" ", col));
error_at.append('^');
logger.error(error_at);
haserror.set(true);
break;
}
line++;
}
}
// validate correct serialization with db: String <-> (db -> Record class -> String)
if (doDatbase.get()) {
Record recordDatabase = null;
try {
DatabaseManager dbMan = new DatabaseManager("MassBank");
recordDatabase = dbMan.getAccessionData(record.ACCESSION());
dbMan.closeConnection();
} catch (SQLException | ConfigurationException e) {
e.printStackTrace();
System.exit(1);
}
if (recordDatabase == null) {
String errormsg = "retrieval of '" + record.ACCESSION() + "' from database failed";
logger.error(errormsg);
System.exit(1);
}
String recordStringFromDB = recordDatabase.toString();
position = StringUtils.indexOfDifference(new String[] { recordString, recordStringFromDB });
if (position != -1) {
logger.error("Error in \'" + filename + "\'.");
logger.error("File content differs from generated record string from database content.\nThis might be a code problem. Please Report!");
String[] tokens = recordStringFromDB.split("\\n");
int line = 0, col = 0, offset = 0;
for (String token : tokens) {
offset = offset + token.length() + 1;
if (position < offset) {
col = position - (offset - (token.length() + 1));
logger.error("Error in line " + (line + 1) + ".");
logger.error(tokens[line]);
StringBuilder error_at = new StringBuilder(StringUtils.repeat(" ", col));
error_at.append('^');
logger.error(error_at);
haserror.set(true);
break;
}
line++;
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
return accession;
}).filter(Objects::nonNull).collect(Collectors.toList());
// check duplicates
Set<String> duplicates = new LinkedHashSet<String>();
Set<String> uniques = new HashSet<String>();
for (String c : accessions) {
// System.out.println(c);
if (!uniques.add(c)) {
duplicates.add(c);
}
}
if (duplicates.size() > 0) {
logger.error("There are duplicates in all accessions:");
logger.error(duplicates.toString());
haserror.set(true);
}
// return 1 if there were errors
if (haserror.get())
System.exit(1);
else
System.exit(0);
}
use of org.apache.commons.cli.DefaultParser in project streaming-samples by ashvina.
the class TopologyArgParser method parseOptions.
private void parseOptions(String[] args) throws Exception {
Options options = new Options();
compNames.stream().forEach(comp -> {
options.addOption(Option.builder(comp).desc("instance count for component: " + comp).hasArg().build());
});
options.addOption(Option.builder("workers").desc("number of workers").hasArg().required().build());
CommandLineParser parser = new DefaultParser();
CommandLine cmd = parser.parse(options, args);
compNames.stream().filter(comp -> cmd.hasOption(comp)).forEach(comp -> compCounts.put(comp, getIntValue(cmd, comp)));
numWorkers = getIntValue(cmd, "workers");
}
use of org.apache.commons.cli.DefaultParser in project java-docs-samples by GoogleCloudPlatform.
the class Inspect method main.
// [END dlp_inspect_bigquery]
/**
* Command line application to inspect data using the Data Loss Prevention API. Supported data
* formats: string, file, text file on GCS, BigQuery table, and Datastore entity
*/
public static void main(String[] args) throws Exception {
OptionGroup optionsGroup = new OptionGroup();
optionsGroup.setRequired(true);
Option stringOption = new Option("s", "string", true, "inspect string");
optionsGroup.addOption(stringOption);
Option fileOption = new Option("f", "file path", true, "inspect input file path");
optionsGroup.addOption(fileOption);
Option gcsOption = new Option("gcs", "Google Cloud Storage", false, "inspect GCS file");
optionsGroup.addOption(gcsOption);
Option datastoreOption = new Option("ds", "Google Datastore", false, "inspect Datastore kind");
optionsGroup.addOption(datastoreOption);
Option bigqueryOption = new Option("bq", "Google BigQuery", false, "inspect BigQuery table");
optionsGroup.addOption(bigqueryOption);
Options commandLineOptions = new Options();
commandLineOptions.addOptionGroup(optionsGroup);
Option minLikelihoodOption = Option.builder("minLikelihood").hasArg(true).required(false).build();
commandLineOptions.addOption(minLikelihoodOption);
Option maxFindingsOption = Option.builder("maxFindings").hasArg(true).required(false).build();
commandLineOptions.addOption(maxFindingsOption);
Option infoTypesOption = Option.builder("infoTypes").hasArg(true).required(false).build();
infoTypesOption.setArgs(Option.UNLIMITED_VALUES);
commandLineOptions.addOption(infoTypesOption);
Option includeQuoteOption = Option.builder("includeQuote").hasArg(true).required(false).build();
commandLineOptions.addOption(includeQuoteOption);
Option bucketNameOption = Option.builder("bucketName").hasArg(true).required(false).build();
commandLineOptions.addOption(bucketNameOption);
Option gcsFileNameOption = Option.builder("fileName").hasArg(true).required(false).build();
commandLineOptions.addOption(gcsFileNameOption);
Option datasetIdOption = Option.builder("datasetId").hasArg(true).required(false).build();
commandLineOptions.addOption(datasetIdOption);
Option tableIdOption = Option.builder("tableId").hasArg(true).required(false).build();
commandLineOptions.addOption(tableIdOption);
Option projectIdOption = Option.builder("projectId").hasArg(true).required(false).build();
commandLineOptions.addOption(projectIdOption);
Option topicIdOption = Option.builder("topicId").hasArg(true).required(false).build();
commandLineOptions.addOption(topicIdOption);
Option subscriptionIdOption = Option.builder("subscriptionId").hasArg(true).required(false).build();
commandLineOptions.addOption(subscriptionIdOption);
Option datastoreNamespaceOption = Option.builder("namespace").hasArg(true).required(false).build();
commandLineOptions.addOption(datastoreNamespaceOption);
Option datastoreKindOption = Option.builder("kind").hasArg(true).required(false).build();
commandLineOptions.addOption(datastoreKindOption);
CommandLineParser parser = new DefaultParser();
HelpFormatter formatter = new HelpFormatter();
CommandLine cmd;
try {
cmd = parser.parse(commandLineOptions, args);
} catch (ParseException e) {
System.out.println(e.getMessage());
formatter.printHelp(Inspect.class.getName(), commandLineOptions);
System.exit(1);
return;
}
Likelihood minLikelihood = Likelihood.valueOf(cmd.getOptionValue(minLikelihoodOption.getOpt(), Likelihood.LIKELIHOOD_UNSPECIFIED.name()));
int maxFindings = Integer.parseInt(cmd.getOptionValue(maxFindingsOption.getOpt(), "0"));
boolean includeQuote = Boolean.parseBoolean(cmd.getOptionValue(includeQuoteOption.getOpt(), "true"));
String projectId = cmd.getOptionValue(projectIdOption.getOpt(), ServiceOptions.getDefaultProjectId());
String topicId = cmd.getOptionValue(topicIdOption.getOpt());
String subscriptionId = cmd.getOptionValue(subscriptionIdOption.getOpt());
List<InfoType> infoTypesList = Collections.emptyList();
if (cmd.hasOption(infoTypesOption.getOpt())) {
infoTypesList = new ArrayList<>();
String[] infoTypes = cmd.getOptionValues(infoTypesOption.getOpt());
for (String infoType : infoTypes) {
infoTypesList.add(InfoType.newBuilder().setName(infoType).build());
}
}
// string inspection
if (cmd.hasOption("s")) {
String val = cmd.getOptionValue(stringOption.getOpt());
inspectString(val, minLikelihood, maxFindings, infoTypesList, includeQuote, projectId);
} else if (cmd.hasOption("f")) {
String filePath = cmd.getOptionValue(fileOption.getOpt());
inspectFile(filePath, minLikelihood, maxFindings, infoTypesList, includeQuote, projectId);
// gcs file inspection
} else if (cmd.hasOption("gcs")) {
String bucketName = cmd.getOptionValue(bucketNameOption.getOpt());
String fileName = cmd.getOptionValue(gcsFileNameOption.getOpt());
inspectGcsFile(bucketName, fileName, minLikelihood, infoTypesList, maxFindings, topicId, subscriptionId, projectId);
// datastore kind inspection
} else if (cmd.hasOption("ds")) {
String namespaceId = cmd.getOptionValue(datastoreNamespaceOption.getOpt(), "");
String kind = cmd.getOptionValue(datastoreKindOption.getOpt());
// use default project id when project id is not specified
inspectDatastore(projectId, namespaceId, kind, minLikelihood, infoTypesList, maxFindings, topicId, subscriptionId);
} else if (cmd.hasOption("bq")) {
String datasetId = cmd.getOptionValue(datasetIdOption.getOpt());
String tableId = cmd.getOptionValue(tableIdOption.getOpt());
// use default project id when project id is not specified
inspectBigquery(projectId, datasetId, tableId, minLikelihood, infoTypesList, maxFindings, topicId, subscriptionId);
}
}
use of org.apache.commons.cli.DefaultParser in project java-docs-samples by GoogleCloudPlatform.
the class Redact method main.
// [END dlp_redact_image]
/**
* Command line application to redact strings, images using the Data Loss Prevention API.
*/
public static void main(String[] args) throws Exception {
Options commandLineOptions = new Options();
Option minLikelihoodOption = Option.builder("minLikelihood").hasArg(true).required(false).build();
commandLineOptions.addOption(minLikelihoodOption);
Option infoTypesOption = Option.builder("infoTypes").hasArg(true).required(false).build();
infoTypesOption.setArgs(Option.UNLIMITED_VALUES);
commandLineOptions.addOption(infoTypesOption);
Option inputFilePathOption = Option.builder("f").hasArg(true).longOpt("inputFilePath").required(false).build();
commandLineOptions.addOption(inputFilePathOption);
Option outputFilePathOption = Option.builder("o").hasArg(true).longOpt("outputFilePath").required(false).build();
commandLineOptions.addOption(outputFilePathOption);
Option projectIdOption = Option.builder("projectId").hasArg(true).required(false).build();
CommandLineParser parser = new DefaultParser();
HelpFormatter formatter = new HelpFormatter();
CommandLine cmd;
try {
cmd = parser.parse(commandLineOptions, args);
} catch (ParseException e) {
System.out.println(e.getMessage());
formatter.printHelp(Redact.class.getName(), commandLineOptions);
System.exit(1);
return;
}
List<InfoType> infoTypesList = new ArrayList<>();
String[] infoTypes = cmd.getOptionValues(infoTypesOption.getOpt());
if (infoTypes != null) {
for (String infoType : infoTypes) {
infoTypesList.add(InfoType.newBuilder().setName(infoType).build());
}
}
Likelihood minLikelihood = Likelihood.valueOf(cmd.getOptionValue(minLikelihoodOption.getOpt(), Likelihood.LIKELIHOOD_UNSPECIFIED.name()));
String inputFilePath = cmd.getOptionValue(inputFilePathOption.getOpt());
String outputFilePath = cmd.getOptionValue(outputFilePathOption.getOpt());
String projectId = cmd.getOptionValue(projectIdOption.getOpt(), ServiceOptions.getDefaultProjectId());
redactImage(inputFilePath, minLikelihood, infoTypesList, outputFilePath, projectId);
}
use of org.apache.commons.cli.DefaultParser in project java-docs-samples by GoogleCloudPlatform.
the class Triggers method main.
// [END dlp_delete_trigger]
/**
* Command line application to crate, list and delete triggers.
*/
public static void main(String[] args) throws Exception {
OptionGroup optionsGroup = new OptionGroup();
optionsGroup.setRequired(true);
Option createTriggerOption = new Option("c", "create", false, "Create trigger to scan a GCS bucket");
optionsGroup.addOption(createTriggerOption);
Option listTriggersOption = new Option("l", "list", false, "List triggers");
optionsGroup.addOption(listTriggersOption);
Option deleteTriggerOption = new Option("d", "delete", false, "Delete trigger");
optionsGroup.addOption(deleteTriggerOption);
Options commandLineOptions = new Options();
commandLineOptions.addOptionGroup(optionsGroup);
Option bucketNameOption = Option.builder("bucketName").hasArg(true).required(false).build();
commandLineOptions.addOption(bucketNameOption);
Option gcsFileNameOption = Option.builder("fileName").hasArg(true).required(false).build();
commandLineOptions.addOption(gcsFileNameOption);
Option minLikelihoodOption = Option.builder("minLikelihood").hasArg(true).required(false).build();
commandLineOptions.addOption(minLikelihoodOption);
Option maxFindingsOption = Option.builder("maxFindings").hasArg(true).required(false).build();
commandLineOptions.addOption(maxFindingsOption);
Option infoTypesOption = Option.builder("infoTypes").hasArg(true).required(false).build();
infoTypesOption.setArgs(Option.UNLIMITED_VALUES);
commandLineOptions.addOption(infoTypesOption);
Option projectIdOption = Option.builder("projectId").hasArg(true).required(false).build();
commandLineOptions.addOption(projectIdOption);
Option triggerIdOption = Option.builder("triggerId").hasArg(true).required(false).build();
commandLineOptions.addOption(triggerIdOption);
Option displayNameOption = Option.builder("displayName").hasArg(true).required(false).build();
commandLineOptions.addOption(displayNameOption);
Option descriptionOption = Option.builder("description").hasArg(true).required(false).build();
commandLineOptions.addOption(descriptionOption);
Option scanPeriodOption = Option.builder("scanPeriod").hasArg(true).required(false).build();
commandLineOptions.addOption(scanPeriodOption);
CommandLineParser parser = new DefaultParser();
HelpFormatter formatter = new HelpFormatter();
CommandLine cmd;
try {
cmd = parser.parse(commandLineOptions, args);
} catch (ParseException e) {
System.out.println(e.getMessage());
formatter.printHelp(DeIdentification.class.getName(), commandLineOptions);
System.exit(1);
return;
}
String projectId = cmd.getOptionValue(projectIdOption.getOpt(), ServiceOptions.getDefaultProjectId());
if (cmd.hasOption("c")) {
Likelihood minLikelihood = Likelihood.valueOf(cmd.getOptionValue(minLikelihoodOption.getOpt(), Likelihood.LIKELIHOOD_UNSPECIFIED.name()));
int maxFindings = Integer.parseInt(cmd.getOptionValue(maxFindingsOption.getOpt(), "0"));
String triggerId = cmd.getOptionValue(triggerIdOption.getOpt());
String displayName = cmd.getOptionValue(displayNameOption.getOpt(), "");
String description = cmd.getOptionValue(descriptionOption.getOpt(), "");
String bucketName = cmd.getOptionValue(bucketNameOption.getOpt());
String fileName = cmd.getOptionValue(gcsFileNameOption.getOpt());
int scanPeriod = Integer.valueOf(cmd.getOptionValue(scanPeriodOption.getOpt()));
List<InfoType> infoTypesList = new ArrayList<>();
if (cmd.hasOption(infoTypesOption.getOpt())) {
infoTypesList = new ArrayList<>();
String[] infoTypes = cmd.getOptionValues(infoTypesOption.getOpt());
for (String infoType : infoTypes) {
infoTypesList.add(InfoType.newBuilder().setName(infoType).build());
}
}
createTrigger(triggerId, displayName, description, bucketName, fileName, scanPeriod, infoTypesList, minLikelihood, maxFindings, projectId);
} else if (cmd.hasOption("l")) {
// list triggers
listTriggers(projectId);
} else if (cmd.hasOption("d")) {
String triggerId = cmd.getOptionValue(triggerIdOption.getOpt());
deleteTrigger(projectId, triggerId);
}
}
Aggregations