use of org.apache.commons.cli.ParseException in project cytoscape-impl by cytoscape.
the class Parser method parseCommandLine.
private void parseCommandLine(String[] args) {
// try to parse the cmd line
CommandLineParser parser = new PosixParser();
CommandLine line = null;
// first load the simple exit options
try {
line = parser.parse(options, args);
} catch (ParseException e) {
logger.error("Parsing command line failed.", e);
printHelp();
shutdown.exit(1);
return;
}
if (line.hasOption("h")) {
printHelp();
} else if (line.hasOption("v")) {
System.out.println("Cytoscape version: " + version.getVersion());
} else if (line.hasOption("c")) {
startupConfig.setCommandFile(line.getOptionValue("c"));
} else {
logger.error("No arguments specified. See usage for details.");
printHelp();
}
}
use of org.apache.commons.cli.ParseException in project latke by b3log.
the class LatkeClient method main.
/**
* Main entry.
*
* @param args the specified command line arguments
* @throws Exception exception
*/
public static void main(String[] args) throws Exception {
// Backup Test:
// args = new String[] {
// "-h", "-backup", "-repository_names", "-verbose", "-s", "chevo2xs.appspot.com:80", "-u", "zane", "-p", "xxx", "-backup_dir",
// "C:/b3log_backup", "-w", "true"};
args = new String[] { "-h", "-restore", "-create_tables", "-verbose", "-s", "localhost:8080", "-u", "zane", "-p", "xxx", "-backup_dir", "C:/b3log_backup_devapi" };
final Options options = getOptions();
final CommandLineParser parser = new PosixParser();
try {
final CommandLine cmd = parser.parse(options, args);
serverAddress = cmd.getOptionValue("s");
backupDir = new File(cmd.getOptionValue("backup_dir"));
if (!backupDir.exists()) {
backupDir.mkdir();
}
userName = cmd.getOptionValue("u");
if (cmd.hasOption("verbose")) {
verbose = true;
}
password = cmd.getOptionValue("p");
if (verbose) {
System.out.println("Requesting server[" + serverAddress + "]");
}
final HttpClient httpClient = new DefaultHttpClient();
if (cmd.hasOption("repository_names")) {
getRepositoryNames();
}
if (cmd.hasOption("create_tables")) {
final List<NameValuePair> qparams = new ArrayList<NameValuePair>();
qparams.add(new BasicNameValuePair("userName", userName));
qparams.add(new BasicNameValuePair("password", password));
final URI uri = URIUtils.createURI("http", serverAddress, -1, CREATE_TABLES, URLEncodedUtils.format(qparams, "UTF-8"), null);
final HttpPut request = new HttpPut();
request.setURI(uri);
if (verbose) {
System.out.println("Starting create tables");
}
final HttpResponse httpResponse = httpClient.execute(request);
final InputStream contentStream = httpResponse.getEntity().getContent();
final String content = IOUtils.toString(contentStream).trim();
if (verbose) {
printResponse(content);
}
}
if (cmd.hasOption("w")) {
final String writable = cmd.getOptionValue("w");
final List<NameValuePair> qparams = new ArrayList<NameValuePair>();
qparams.add(new BasicNameValuePair("userName", userName));
qparams.add(new BasicNameValuePair("password", password));
qparams.add(new BasicNameValuePair("writable", writable));
final URI uri = URIUtils.createURI("http", serverAddress, -1, SET_REPOSITORIES_WRITABLE, URLEncodedUtils.format(qparams, "UTF-8"), null);
final HttpPut request = new HttpPut();
request.setURI(uri);
if (verbose) {
System.out.println("Setting repository writable[" + writable + "]");
}
final HttpResponse httpResponse = httpClient.execute(request);
final InputStream contentStream = httpResponse.getEntity().getContent();
final String content = IOUtils.toString(contentStream).trim();
if (verbose) {
printResponse(content);
}
}
if (cmd.hasOption("backup")) {
System.out.println("Make sure you have disabled repository writes with [-w false], continue? (y)");
final Scanner scanner = new Scanner(System.in);
final String input = scanner.next();
scanner.close();
if (!"y".equals(input)) {
return;
}
if (verbose) {
System.out.println("Starting backup data");
}
final Set<String> repositoryNames = getRepositoryNames();
for (final String repositoryName : repositoryNames) {
// You could specify repository manually
// if (!"archiveDate_article".equals(repositoryName)) {
// continue;
// }
int requestPageNum = 1;
// Backup interrupt recovery
final List<File> backupFiles = getBackupFiles(repositoryName);
if (!backupFiles.isEmpty()) {
final File latestBackup = backupFiles.get(backupFiles.size() - 1);
final String latestPageSize = getBackupFileNameField(latestBackup.getName(), "${pageSize}");
if (!PAGE_SIZE.equals(latestPageSize)) {
if (verbose) {
System.out.println("Repository [" + repositoryName + "] backup have completed");
}
continue;
}
final String latestPageNum = getBackupFileNameField(latestBackup.getName(), "${pageNum}");
// Prepare for the next page to request
requestPageNum = Integer.parseInt(latestPageNum) + 1;
if (verbose) {
System.out.println("Start tot backup interrupt recovery [pageNum=" + requestPageNum + "]");
}
}
int totalPageCount = requestPageNum + 1;
for (; requestPageNum <= totalPageCount; requestPageNum++) {
final List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("userName", userName));
params.add(new BasicNameValuePair("password", password));
params.add(new BasicNameValuePair("repositoryName", repositoryName));
params.add(new BasicNameValuePair("pageNum", String.valueOf(requestPageNum)));
params.add(new BasicNameValuePair("pageSize", PAGE_SIZE));
final URI uri = URIUtils.createURI("http", serverAddress, -1, GET_DATA, URLEncodedUtils.format(params, "UTF-8"), null);
final HttpGet request = new HttpGet(uri);
if (verbose) {
System.out.println("Getting data from repository [" + repositoryName + "] with pagination [pageNum=" + requestPageNum + ", pageSize=" + PAGE_SIZE + "]");
}
final HttpResponse httpResponse = httpClient.execute(request);
final InputStream contentStream = httpResponse.getEntity().getContent();
final String content = IOUtils.toString(contentStream, "UTF-8").trim();
contentStream.close();
if (verbose) {
printResponse(content);
}
final JSONObject resp = new JSONObject(content);
final JSONObject pagination = resp.getJSONObject("pagination");
totalPageCount = pagination.getInt("paginationPageCount");
final JSONArray results = resp.getJSONArray("rslts");
final String backupPath = backupDir.getPath() + File.separatorChar + repositoryName + File.separatorChar + requestPageNum + '_' + results.length() + '_' + System.currentTimeMillis() + ".json";
final File backup = new File(backupPath);
final FileWriter fileWriter = new FileWriter(backup);
IOUtils.write(results.toString(), fileWriter);
fileWriter.close();
if (verbose) {
System.out.println("Backup file[path=" + backupPath + "]");
}
}
}
}
if (cmd.hasOption("restore")) {
System.out.println("Make sure you have enabled repository writes with [-w true], continue? (y)");
final Scanner scanner = new Scanner(System.in);
final String input = scanner.next();
scanner.close();
if (!"y".equals(input)) {
return;
}
if (verbose) {
System.out.println("Starting restore data");
}
final Set<String> repositoryNames = getRepositoryNamesFromBackupDir();
for (final String repositoryName : repositoryNames) {
// You could specify repository manually
// if (!"archiveDate_article".equals(repositoryName)) {
// continue;
// }
final List<File> backupFiles = getBackupFiles(repositoryName);
if (verbose) {
System.out.println("Restoring repository[" + repositoryName + ']');
}
for (final File backupFile : backupFiles) {
final FileReader backupFileReader = new FileReader(backupFile);
final String dataContent = IOUtils.toString(backupFileReader);
backupFileReader.close();
final List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("userName", userName));
params.add(new BasicNameValuePair("password", password));
params.add(new BasicNameValuePair("repositoryName", repositoryName));
final URI uri = URIUtils.createURI("http", serverAddress, -1, PUT_DATA, URLEncodedUtils.format(params, "UTF-8"), null);
final HttpPost request = new HttpPost(uri);
final List<NameValuePair> data = new ArrayList<NameValuePair>();
data.add(new BasicNameValuePair("data", dataContent));
final UrlEncodedFormEntity entity = new UrlEncodedFormEntity(data, "UTF-8");
request.setEntity(entity);
if (verbose) {
System.out.println("Data[" + dataContent + "]");
}
final HttpResponse httpResponse = httpClient.execute(request);
final InputStream contentStream = httpResponse.getEntity().getContent();
final String content = IOUtils.toString(contentStream, "UTF-8").trim();
contentStream.close();
if (verbose) {
printResponse(content);
}
final String pageNum = getBackupFileNameField(backupFile.getName(), "${pageNum}");
final String pageSize = getBackupFileNameField(backupFile.getName(), "${pageSize}");
final String backupTime = getBackupFileNameField(backupFile.getName(), "${backupTime}");
final String restoredPath = backupDir.getPath() + File.separatorChar + repositoryName + File.separatorChar + pageNum + '_' + pageSize + '_' + backupTime + '_' + System.currentTimeMillis() + ".json";
final File restoredFile = new File(restoredPath);
backupFile.renameTo(restoredFile);
if (verbose) {
System.out.println("Backup file[path=" + restoredPath + "]");
}
}
}
}
if (cmd.hasOption("v")) {
System.out.println(VERSION);
}
if (cmd.hasOption("h")) {
printHelp(options);
}
// final File backup = new File(backupDir.getPath() + File.separatorChar + repositoryName + pageNum + '_' + pageSize + '_'
// + System.currentTimeMillis() + ".json");
// final FileEntity fileEntity = new FileEntity(backup, "application/json; charset=\"UTF-8\"");
} catch (final ParseException e) {
System.err.println("Parsing args failed, caused by: " + e.getMessage());
printHelp(options);
} catch (final ConnectException e) {
System.err.println("Connection refused");
}
}
use of org.apache.commons.cli.ParseException 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.ParseException 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.ParseException 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