use of org.apache.commons.cli.ParseException in project flink by apache.
the class CliFrontendParser method parseListCommand.
public static ListOptions parseListCommand(String[] args) throws CliArgsException {
try {
DefaultParser parser = new DefaultParser();
CommandLine line = parser.parse(LIST_OPTIONS, args, false);
return new ListOptions(line);
} catch (ParseException e) {
throw new CliArgsException(e.getMessage());
}
}
use of org.apache.commons.cli.ParseException in project hadoop by apache.
the class NodeInfo method main.
@SuppressWarnings("static-access")
public static void main(String[] args) throws IOException {
GenericOptionsParser genericParser = new GenericOptionsParser(args);
String[] remainingArgs = genericParser.getRemainingArgs();
Option conf = OptionBuilder.hasArg().create("conffile");
Option help = OptionBuilder.withLongOpt("help").create('h');
Options opts = new Options().addOption(conf).addOption(help);
CommandLineParser specificParser = new GnuParser();
CommandLine cmd = null;
try {
cmd = specificParser.parse(opts, remainingArgs);
} catch (MissingArgumentException e) {
terminate(1, "No argument specified for -conffile option");
} catch (ParseException e) {
terminate(1, USAGE);
}
if (cmd == null) {
terminate(1, "Failed to parse options");
}
if (cmd.hasOption('h')) {
terminate(0, USAGE);
}
List<File> files = new ArrayList<File>();
if (cmd.hasOption("conffile")) {
String[] values = cmd.getOptionValues("conffile");
for (String value : values) {
File confFile = new File(value);
if (confFile.isFile()) {
files.add(confFile);
} else if (confFile.isDirectory()) {
for (File file : listFiles(confFile)) {
files.add(file);
}
} else {
terminate(1, confFile.getAbsolutePath() + " is neither a file nor directory");
}
}
} else {
String confDirName = System.getenv(HADOOP_CONF_DIR);
if (confDirName == null) {
terminate(1, HADOOP_CONF_DIR + " is not defined");
}
File confDir = new File(confDirName);
if (!confDir.isDirectory()) {
terminate(1, HADOOP_CONF_DIR + " is not a directory");
}
files = Arrays.asList(listFiles(confDir));
}
if (files.isEmpty()) {
terminate(1, "No input file to validate");
}
boolean ok = true;
for (File file : files) {
String path = file.getAbsolutePath();
List<String> errors = checkConf(new FileInputStream(file));
if (errors.isEmpty()) {
System.out.println(path + ": valid");
} else {
ok = false;
System.err.println(path + ":");
for (String error : errors) {
System.err.println("\t" + error);
}
}
}
if (ok) {
System.out.println("OK");
} else {
terminate(1, "Invalid file exists");
}
}
use of org.apache.commons.cli.ParseException in project ignite by apache.
the class DecisionTreesExample method main.
/**
* Launches example.
*
* @param args Program arguments.
*/
public static void main(String[] args) throws IOException {
System.out.println(">>> Decision trees example started.");
String igniteCfgPath;
CommandLineParser parser = new BasicParser();
String trainingImagesPath;
String trainingLabelsPath;
String testImagesPath;
String testLabelsPath;
Map<String, String> mnistPaths = new HashMap<>();
mnistPaths.put(MNIST_TRAIN_IMAGES, "train-images-idx3-ubyte");
mnistPaths.put(MNIST_TRAIN_LABELS, "train-labels-idx1-ubyte");
mnistPaths.put(MNIST_TEST_IMAGES, "t10k-images-idx3-ubyte");
mnistPaths.put(MNIST_TEST_LABELS, "t10k-labels-idx1-ubyte");
try {
// Parse the command line arguments.
CommandLine line = parser.parse(buildOptions(), args);
if (line.hasOption(MLExamplesCommonArgs.UNATTENDED)) {
System.out.println(">>> Skipped example execution because 'unattended' mode is used.");
System.out.println(">>> Decision trees example finished.");
return;
}
igniteCfgPath = line.getOptionValue(CONFIG, DEFAULT_CONFIG);
} catch (ParseException e) {
e.printStackTrace();
return;
}
if (!getMNIST(mnistPaths.values())) {
System.out.println(">>> You should have MNIST dataset in " + MNIST_DIR + " to run this example.");
return;
}
trainingImagesPath = Objects.requireNonNull(IgniteUtils.resolveIgnitePath(MNIST_DIR + "/" + mnistPaths.get(MNIST_TRAIN_IMAGES))).getPath();
trainingLabelsPath = Objects.requireNonNull(IgniteUtils.resolveIgnitePath(MNIST_DIR + "/" + mnistPaths.get(MNIST_TRAIN_LABELS))).getPath();
testImagesPath = Objects.requireNonNull(IgniteUtils.resolveIgnitePath(MNIST_DIR + "/" + mnistPaths.get(MNIST_TEST_IMAGES))).getPath();
testLabelsPath = Objects.requireNonNull(IgniteUtils.resolveIgnitePath(MNIST_DIR + "/" + mnistPaths.get(MNIST_TEST_LABELS))).getPath();
try (Ignite ignite = Ignition.start(igniteCfgPath)) {
IgniteUtils.setCurrentIgniteName(ignite.configuration().getIgniteInstanceName());
int ptsCnt = 60000;
int featCnt = 28 * 28;
Stream<DenseLocalOnHeapVector> trainingMnistStream = MnistUtils.mnist(trainingImagesPath, trainingLabelsPath, new Random(123L), ptsCnt);
Stream<DenseLocalOnHeapVector> testMnistStream = MnistUtils.mnist(testImagesPath, testLabelsPath, new Random(123L), 10_000);
IgniteCache<BiIndex, Double> cache = createBiIndexedCache(ignite);
loadVectorsIntoBiIndexedCache(cache.getName(), trainingMnistStream.iterator(), featCnt + 1, ignite);
ColumnDecisionTreeTrainer<GiniSplitCalculator.GiniData> trainer = new ColumnDecisionTreeTrainer<>(10, ContinuousSplitCalculators.GINI.apply(ignite), RegionCalculators.GINI, RegionCalculators.MOST_COMMON, ignite);
System.out.println(">>> Training started");
long before = System.currentTimeMillis();
DecisionTreeModel mdl = trainer.train(new BiIndexedCacheColumnDecisionTreeTrainerInput(cache, new HashMap<>(), ptsCnt, featCnt));
System.out.println(">>> Training finished in " + (System.currentTimeMillis() - before));
IgniteTriFunction<Model<Vector, Double>, Stream<IgniteBiTuple<Vector, Double>>, Function<Double, Double>, Double> mse = Estimators.errorsPercentage();
Double accuracy = mse.apply(mdl, testMnistStream.map(v -> new IgniteBiTuple<>(v.viewPart(0, featCnt), v.getX(featCnt))), Function.identity());
System.out.println(">>> Errs percentage: " + accuracy);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(">>> Decision trees example finished.");
}
use of org.apache.commons.cli.ParseException in project nifi by apache.
the class SiteToSiteCliMain method parseCli.
/**
* Parses command line options into a CliParse object
*
* @param options an empty options object (so callers can print usage if the parse fails
* @param args the string array of arguments
* @return a CliParse object containing the constructed SiteToSiteClient.Builder and a TransferDirection
* @throws ParseException if there is an error parsing the command line
*/
public static CliParse parseCli(Options options, String[] args) throws ParseException {
options.addOption("u", URL_OPTION, true, "NiFI URL to connect to (default: " + URL_OPTION_DEFAULT + ")");
options.addOption("d", DIRECTION_OPTION, true, "Direction (valid directions: " + Arrays.stream(TransferDirection.values()).map(Object::toString).collect(Collectors.joining(", ")) + ") (default: " + DIRECTION_OPTION_DEFAULT + ")");
options.addOption("n", PORT_NAME_OPTION, true, "Port name");
options.addOption("i", PORT_IDENTIFIER_OPTION, true, "Port id");
options.addOption(null, TIMEOUT_OPTION, true, "Timeout");
options.addOption(null, PENALIZATION_OPTION, true, "Penalization period");
options.addOption(null, KEYSTORE_OPTION, true, "Keystore");
options.addOption(null, KEY_STORE_TYPE_OPTION, true, "Keystore type (default: " + KEYSTORE_TYPE_OPTION_DEFAULT + ")");
options.addOption(null, KEY_STORE_PASSWORD_OPTION, true, "Keystore password");
options.addOption(null, TRUST_STORE_OPTION, true, "Truststore");
options.addOption(null, TRUST_STORE_TYPE_OPTION, true, "Truststore type (default: " + KEYSTORE_TYPE_OPTION_DEFAULT + ")");
options.addOption(null, TRUST_STORE_PASSWORD_OPTION, true, "Truststore password");
options.addOption(null, NEED_CLIENT_AUTH_OPTION, false, "Need client auth");
options.addOption("c", COMPRESSION_OPTION, false, "Use compression");
options.addOption(null, PEER_PERSISTENCE_FILE_OPTION, true, "File to write peer information to so it can be recovered on restart");
options.addOption("p", TRANSPORT_PROTOCOL_OPTION, true, "Site to site transport protocol (default: " + TRANSPORT_PROTOCOL_OPTION_DEFAULT + ")");
options.addOption(null, BATCH_COUNT_OPTION, true, "Number of flow files in a batch");
options.addOption(null, BATCH_SIZE_OPTION, true, "Size of flow files in a batch");
options.addOption(null, BATCH_DURATION_OPTION, true, "Duration of a batch");
options.addOption(null, PROXY_HOST_OPTION, true, "Proxy hostname");
options.addOption(null, PROXY_PORT_OPTION, true, "Proxy port");
options.addOption(null, PROXY_USERNAME_OPTION, true, "Proxy username");
options.addOption(null, PROXY_PASSWORD_OPTION, true, "Proxy password");
options.addOption("h", HELP_OPTION, false, "Show help message and exit");
CommandLineParser parser = new DefaultParser();
CommandLine commandLine;
commandLine = parser.parse(options, args);
if (commandLine.hasOption(HELP_OPTION)) {
printUsage(null, options);
System.exit(1);
}
SiteToSiteClient.Builder builder = new SiteToSiteClient.Builder();
builder.url(commandLine.getOptionValue(URL_OPTION, URL_OPTION_DEFAULT));
if (commandLine.hasOption(PORT_NAME_OPTION)) {
builder.portName(commandLine.getOptionValue(PORT_NAME_OPTION));
}
if (commandLine.hasOption(PORT_IDENTIFIER_OPTION)) {
builder.portIdentifier(commandLine.getOptionValue(PORT_IDENTIFIER_OPTION));
}
if (commandLine.hasOption(TIMEOUT_OPTION)) {
builder.timeout(FormatUtils.getTimeDuration(commandLine.getOptionValue(TIMEOUT_OPTION), TimeUnit.NANOSECONDS), TimeUnit.NANOSECONDS);
}
if (commandLine.hasOption(PENALIZATION_OPTION)) {
builder.nodePenalizationPeriod(FormatUtils.getTimeDuration(commandLine.getOptionValue(PENALIZATION_OPTION), TimeUnit.NANOSECONDS), TimeUnit.NANOSECONDS);
}
if (commandLine.hasOption(KEYSTORE_OPTION)) {
builder.keystoreFilename(commandLine.getOptionValue(KEYSTORE_OPTION));
builder.keystoreType(KeystoreType.valueOf(commandLine.getOptionValue(KEY_STORE_TYPE_OPTION, KEYSTORE_TYPE_OPTION_DEFAULT).toUpperCase()));
if (commandLine.hasOption(KEY_STORE_PASSWORD_OPTION)) {
builder.keystorePass(commandLine.getOptionValue(KEY_STORE_PASSWORD_OPTION));
} else {
throw new ParseException("Must specify keystore password");
}
}
if (commandLine.hasOption(TRUST_STORE_OPTION)) {
builder.truststoreFilename(commandLine.getOptionValue(TRUST_STORE_OPTION));
builder.truststoreType(KeystoreType.valueOf(commandLine.getOptionValue(TRUST_STORE_TYPE_OPTION, KEYSTORE_TYPE_OPTION_DEFAULT).toUpperCase()));
if (commandLine.hasOption(TRUST_STORE_PASSWORD_OPTION)) {
builder.truststorePass(commandLine.getOptionValue(TRUST_STORE_PASSWORD_OPTION));
} else {
throw new ParseException("Must specify truststore password");
}
}
if (commandLine.hasOption(COMPRESSION_OPTION)) {
builder.useCompression(true);
} else {
builder.useCompression(false);
}
if (commandLine.hasOption(PEER_PERSISTENCE_FILE_OPTION)) {
builder.peerPersistenceFile(new File(commandLine.getOptionValue(PEER_PERSISTENCE_FILE_OPTION)));
}
if (commandLine.hasOption(BATCH_COUNT_OPTION)) {
builder.requestBatchCount(Integer.parseInt(commandLine.getOptionValue(BATCH_COUNT_OPTION)));
}
if (commandLine.hasOption(BATCH_SIZE_OPTION)) {
builder.requestBatchSize(Long.parseLong(commandLine.getOptionValue(BATCH_SIZE_OPTION)));
}
if (commandLine.hasOption(BATCH_DURATION_OPTION)) {
builder.requestBatchDuration(FormatUtils.getTimeDuration(commandLine.getOptionValue(BATCH_DURATION_OPTION), TimeUnit.NANOSECONDS), TimeUnit.NANOSECONDS);
}
if (commandLine.hasOption(PROXY_HOST_OPTION)) {
builder.httpProxy(new HttpProxy(commandLine.getOptionValue(PROXY_HOST_OPTION), Integer.parseInt(commandLine.getOptionValue(PROXY_PORT_OPTION, PROXY_PORT_OPTION_DEFAULT)), commandLine.getOptionValue(PROXY_USERNAME_OPTION), commandLine.getOptionValue(PROXY_PASSWORD_OPTION)));
}
builder.transportProtocol(SiteToSiteTransportProtocol.valueOf(commandLine.getOptionValue(TRANSPORT_PROTOCOL_OPTION, TRANSPORT_PROTOCOL_OPTION_DEFAULT).toUpperCase()));
TransferDirection transferDirection = TransferDirection.valueOf(commandLine.getOptionValue(DIRECTION_OPTION, DIRECTION_OPTION_DEFAULT));
return new CliParse() {
@Override
public SiteToSiteClient.Builder getBuilder() {
return builder;
}
@Override
public TransferDirection getTransferDirection() {
return transferDirection;
}
};
}
use of org.apache.commons.cli.ParseException in project streamsx.health by IBMStreams.
the class VinesAdapterService method main.
public static void main(String[] args) throws Exception {
Option contextOption = Option.builder("c").longOpt("context-type").hasArg().argName("context type").required().build();
Option hostOption = Option.builder("h").longOpt("host").hasArg().argName("host").required().build();
Option portOption = Option.builder("p").longOpt("port").hasArg().argName("port").required().build();
Option usernameOption = Option.builder("u").longOpt("username").hasArg().argName("username").required().build();
Option passwordOption = Option.builder("P").longOpt("password").hasArg().argName("password").required().build();
Option queueOption = Option.builder("q").longOpt("queue").hasArg().argName("queue").required().build();
Option exchangeOption = Option.builder("e").longOpt("exchange").hasArg().argName("exchange name").required().build();
Option debugOption = Option.builder("d").longOpt("debug").hasArg().argName("isDebugEnabled").required(false).type(Boolean.class).build();
Option mappingEnabledOption = Option.builder("m").longOpt("mappingEnabled").hasArg().argName("isMappingEnabled").required().build();
Options options = new Options();
options.addOption(contextOption);
options.addOption(hostOption);
options.addOption(portOption);
options.addOption(usernameOption);
options.addOption(passwordOption);
options.addOption(queueOption);
options.addOption(exchangeOption);
options.addOption(debugOption);
options.addOption(mappingEnabledOption);
CommandLineParser parser = new DefaultParser();
CommandLine cmd = null;
try {
cmd = parser.parse(options, args);
} catch (ParseException e) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("help", options);
throw (e);
}
boolean isDebug = Boolean.valueOf(cmd.getOptionValue("d", Boolean.FALSE.toString()));
HashMap<String, Object> params = new HashMap<String, Object>();
params.put("hostAndPort", cmd.getOptionValue("h") + ":" + cmd.getOptionValue("p"));
params.put("username", cmd.getOptionValue("u"));
params.put("password", cmd.getOptionValue("P"));
params.put("queueName", cmd.getOptionValue("q"));
params.put("exchangeName", cmd.getOptionValue("e", ""));
params.put("mappingEnabled", cmd.getOptionValue("m"));
HashMap<String, Object> config = new HashMap<>();
config.put(ContextProperties.SUBMISSION_PARAMS, params);
if (isDebug) {
config.put(ContextProperties.TRACING_LEVEL, TraceLevel.TRACE);
}
VinesAdapterService svc = new VinesAdapterService();
svc.run(Type.valueOf(cmd.getOptionValue("c", "DISTRIBUTED")), config);
if (isDebug) {
// launch a debug service to print raw messages to the console
Topology rawMsgTopo = new Topology("VinesRawMsgDebug");
rawMsgTopo.subscribe(VinesAdapterService.VINES_DEBUG_TOPIC, String.class).print();
StreamsContextFactory.getStreamsContext(Type.DISTRIBUTED).submit(rawMsgTopo).get();
// launch a debug service to print Observation tuples to the console
Topology obsTopo = new Topology("VinesObservationDebug");
SubscribeConnector.subscribe(obsTopo, VinesAdapterService.VINES_TOPIC).print();
StreamsContextFactory.getStreamsContext(Type.DISTRIBUTED).submit(obsTopo).get();
// launch a debug service to print errors to the console
Topology errTopo = new Topology("VinesErrorDebug");
errTopo.subscribe(VinesAdapterService.VINES_ERROR_TOPIC, String.class).print();
StreamsContextFactory.getStreamsContext(Type.DISTRIBUTED).submit(errTopo).get();
}
}
Aggregations