use of org.apache.commons.cli.BasicParser in project alluxio by Alluxio.
the class JournalTool method parseInputArgs.
/**
* Parses the input args with a command line format, using
* {@link org.apache.commons.cli.CommandLineParser}.
*
* @param args the input args
* @return true if parsing succeeded
*/
private static boolean parseInputArgs(String[] args) {
CommandLineParser parser = new BasicParser();
CommandLine cmd;
try {
cmd = parser.parse(OPTIONS, args);
} catch (ParseException e) {
System.out.println("Failed to parse input args: " + e);
return false;
}
sNoTimeout = cmd.hasOption("noTimeout");
sHelp = cmd.hasOption("help");
return true;
}
use of org.apache.commons.cli.BasicParser 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.BasicParser in project metron by apache.
the class PcapServiceCli method parse.
public void parse() {
CommandLineParser parser = new BasicParser();
CommandLine cmd = null;
try {
cmd = parser.parse(options, args);
} catch (ParseException e1) {
e1.printStackTrace();
}
if (cmd.hasOption("h")) {
help();
}
if (cmd.hasOption("log4j")) {
PropertyConfigurator.configure(cmd.getOptionValue("log4j"));
}
if (cmd.hasOption("port")) {
try {
port = Integer.parseInt(cmd.getOptionValue("port").trim());
} catch (Exception e) {
System.out.println("[Metron] Invalid value for port entered");
help();
}
}
if (cmd.hasOption("pcap_hdfs_path")) {
pcapHdfsPath = cmd.getOptionValue("pcap_hdfs_path");
} else {
throw new IllegalStateException("You must specify the pcap hdfs path");
}
if (cmd.hasOption("query_hdfs_path")) {
queryHdfsPath = cmd.getOptionValue("query_hdfs_path");
} else {
throw new IllegalStateException("You must specify the query temp hdfs path");
}
if (cmd.hasOption("endpoint_uri")) {
try {
if (uri == null || uri.equals(""))
throw new Exception("invalid uri");
uri = cmd.getOptionValue("uri").trim();
if (uri.charAt(0) != '/')
uri = "/" + uri;
if (uri.charAt(uri.length()) == '/')
uri = uri.substring(0, uri.length() - 1);
} catch (Exception e) {
System.out.println("[Metron] Invalid URI entered");
help();
}
}
}
use of org.apache.commons.cli.BasicParser in project accumulo by apache.
the class ShellPluginConfigurationCommand method getPluginClass.
public static <T> Class<? extends T> getPluginClass(final String tableName, final Shell shellState, final Class<T> clazz, final Property pluginProp) {
Iterator<Entry<String, String>> props;
try {
props = shellState.getConnector().tableOperations().getProperties(tableName).iterator();
} catch (AccumuloException | TableNotFoundException e) {
return null;
}
while (props.hasNext()) {
final Entry<String, String> ent = props.next();
if (ent.getKey().equals(pluginProp.toString())) {
Class<? extends T> pluginClazz;
String[] args = new String[2];
try {
Options o = new Options();
o.addOption(OptUtil.tableOpt());
args[0] = "-t";
args[1] = tableName;
CommandLine cl = new BasicParser().parse(o, args);
pluginClazz = shellState.getClassLoader(cl, shellState).loadClass(ent.getValue()).asSubclass(clazz);
} catch (ClassNotFoundException e) {
LoggerFactory.getLogger(ShellPluginConfigurationCommand.class).error("Class not found {}", e.getMessage());
return null;
} catch (ParseException e) {
LoggerFactory.getLogger(ShellPluginConfigurationCommand.class).error("Error parsing table: {} {}", Arrays.toString(args), e.getMessage());
return null;
} catch (TableNotFoundException e) {
LoggerFactory.getLogger(ShellPluginConfigurationCommand.class).error("Table not found: {} {}", tableName, e.getMessage());
return null;
} catch (Exception e) {
LoggerFactory.getLogger(ShellPluginConfigurationCommand.class).error("Error: {}", e.getMessage());
return null;
}
return pluginClazz;
}
}
return null;
}
use of org.apache.commons.cli.BasicParser in project bookkeeper by apache.
the class BookieShell method main.
public static void main(String[] argv) throws Exception {
BookieShell shell = new BookieShell();
// handle some common options for multiple cmds
Options opts = new Options();
opts.addOption(CONF_OPT, true, "configuration file");
opts.addOption(LEDGERID_FORMATTER_OPT, true, "format of ledgerId");
opts.addOption(ENTRY_FORMATTER_OPT, true, "format of entries");
BasicParser parser = new BasicParser();
CommandLine cmdLine = parser.parse(opts, argv, true);
// load configuration
CompositeConfiguration conf = new CompositeConfiguration();
if (cmdLine.hasOption(CONF_OPT)) {
String val = cmdLine.getOptionValue(CONF_OPT);
conf.addConfiguration(new PropertiesConfiguration(new File(val).toURI().toURL()));
}
shell.setConf(conf);
// ledgerid format
if (cmdLine.hasOption(LEDGERID_FORMATTER_OPT)) {
String val = cmdLine.getOptionValue(LEDGERID_FORMATTER_OPT);
shell.ledgerIdFormatter = LedgerIdFormatter.newLedgerIdFormatter(val, shell.bkConf);
} else {
shell.ledgerIdFormatter = LedgerIdFormatter.newLedgerIdFormatter(shell.bkConf);
}
LOG.debug("Using ledgerIdFormatter {}", shell.ledgerIdFormatter.getClass());
// entry format
if (cmdLine.hasOption(ENTRY_FORMATTER_OPT)) {
String val = cmdLine.getOptionValue(ENTRY_FORMATTER_OPT);
shell.entryFormatter = EntryFormatter.newEntryFormatter(val, shell.bkConf);
} else {
shell.entryFormatter = EntryFormatter.newEntryFormatter(shell.bkConf);
}
LOG.debug("Using entry formatter {}", shell.entryFormatter.getClass());
int res = shell.run(cmdLine.getArgs());
System.exit(res);
}
Aggregations