Search in sources :

Example 1 with BasicParser

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;
}
Also used : BasicParser(org.apache.commons.cli.BasicParser) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

Example 2 with BasicParser

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.");
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) URL(java.net.URL) Scanner(java.util.Scanner) Random(java.util.Random) BiIndex(org.apache.ignite.ml.trees.trainers.columnbased.BiIndex) ExampleNodeStartup(org.apache.ignite.examples.ExampleNodeStartup) Vector(org.apache.ignite.ml.math.Vector) Estimators(org.apache.ignite.ml.estimators.Estimators) Map(java.util.Map) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) Collection(java.util.Collection) IgniteTriFunction(org.apache.ignite.ml.math.functions.IgniteTriFunction) Collectors(java.util.stream.Collectors) IgniteCache(org.apache.ignite.IgniteCache) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) Objects(java.util.Objects) List(java.util.List) Stream(java.util.stream.Stream) ParseException(org.apache.commons.cli.ParseException) RegionCalculators(org.apache.ignite.ml.trees.trainers.columnbased.regcalcs.RegionCalculators) NotNull(org.jetbrains.annotations.NotNull) DecisionTreeModel(org.apache.ignite.ml.trees.models.DecisionTreeModel) Model(org.apache.ignite.ml.Model) Options(org.apache.commons.cli.Options) HashMap(java.util.HashMap) Function(java.util.function.Function) GiniSplitCalculator(org.apache.ignite.ml.trees.trainers.columnbased.contsplitcalcs.GiniSplitCalculator) BiIndexedCacheColumnDecisionTreeTrainerInput(org.apache.ignite.ml.trees.trainers.columnbased.BiIndexedCacheColumnDecisionTreeTrainerInput) OptionBuilder(org.apache.commons.cli.OptionBuilder) CacheWriteSynchronizationMode(org.apache.ignite.cache.CacheWriteSynchronizationMode) IgniteUtils(org.apache.ignite.internal.util.IgniteUtils) BasicParser(org.apache.commons.cli.BasicParser) CommandLine(org.apache.commons.cli.CommandLine) MnistUtils(org.apache.ignite.ml.util.MnistUtils) Option(org.apache.commons.cli.Option) ReadableByteChannel(java.nio.channels.ReadableByteChannel) Iterator(java.util.Iterator) ContinuousSplitCalculators(org.apache.ignite.ml.trees.trainers.columnbased.contsplitcalcs.ContinuousSplitCalculators) CommandLineParser(org.apache.commons.cli.CommandLineParser) Channels(java.nio.channels.Channels) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Ignite(org.apache.ignite.Ignite) MLExamplesCommonArgs(org.apache.ignite.examples.ml.MLExamplesCommonArgs) Ignition(org.apache.ignite.Ignition) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) IgniteDataStreamer(org.apache.ignite.IgniteDataStreamer) ColumnDecisionTreeTrainer(org.apache.ignite.ml.trees.trainers.columnbased.ColumnDecisionTreeTrainer) HashMap(java.util.HashMap) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) DecisionTreeModel(org.apache.ignite.ml.trees.models.DecisionTreeModel) IgniteTriFunction(org.apache.ignite.ml.math.functions.IgniteTriFunction) Function(java.util.function.Function) Random(java.util.Random) Ignite(org.apache.ignite.Ignite) GZIPInputStream(java.util.zip.GZIPInputStream) Stream(java.util.stream.Stream) FileOutputStream(java.io.FileOutputStream) FileInputStream(java.io.FileInputStream) CommandLineParser(org.apache.commons.cli.CommandLineParser) Vector(org.apache.ignite.ml.math.Vector) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector) ColumnDecisionTreeTrainer(org.apache.ignite.ml.trees.trainers.columnbased.ColumnDecisionTreeTrainer) BiIndex(org.apache.ignite.ml.trees.trainers.columnbased.BiIndex) IOException(java.io.IOException) BiIndexedCacheColumnDecisionTreeTrainerInput(org.apache.ignite.ml.trees.trainers.columnbased.BiIndexedCacheColumnDecisionTreeTrainerInput) BasicParser(org.apache.commons.cli.BasicParser) CommandLine(org.apache.commons.cli.CommandLine) DecisionTreeModel(org.apache.ignite.ml.trees.models.DecisionTreeModel) Model(org.apache.ignite.ml.Model) ParseException(org.apache.commons.cli.ParseException) DenseLocalOnHeapVector(org.apache.ignite.ml.math.impls.vector.DenseLocalOnHeapVector)

Example 3 with BasicParser

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();
        }
    }
}
Also used : BasicParser(org.apache.commons.cli.BasicParser) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) ParseException(org.apache.commons.cli.ParseException)

Example 4 with BasicParser

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;
}
Also used : AccumuloException(org.apache.accumulo.core.client.AccumuloException) Options(org.apache.commons.cli.Options) AccumuloException(org.apache.accumulo.core.client.AccumuloException) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) ParseException(org.apache.commons.cli.ParseException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) TableNotFoundException(org.apache.accumulo.core.client.TableNotFoundException) BasicParser(org.apache.commons.cli.BasicParser) Entry(java.util.Map.Entry) CommandLine(org.apache.commons.cli.CommandLine) ParseException(org.apache.commons.cli.ParseException)

Example 5 with BasicParser

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);
}
Also used : Options(org.apache.commons.cli.Options) BasicParser(org.apache.commons.cli.BasicParser) CommandLine(org.apache.commons.cli.CommandLine) CompositeConfiguration(org.apache.commons.configuration.CompositeConfiguration) PropertiesConfiguration(org.apache.commons.configuration.PropertiesConfiguration) File(java.io.File) Checkpoint(org.apache.bookkeeper.bookie.CheckpointSource.Checkpoint)

Aggregations

BasicParser (org.apache.commons.cli.BasicParser)52 CommandLine (org.apache.commons.cli.CommandLine)49 Options (org.apache.commons.cli.Options)37 CommandLineParser (org.apache.commons.cli.CommandLineParser)32 ParseException (org.apache.commons.cli.ParseException)32 HelpFormatter (org.apache.commons.cli.HelpFormatter)22 IOException (java.io.IOException)15 File (java.io.File)14 Option (org.apache.commons.cli.Option)10 FileNotFoundException (java.io.FileNotFoundException)3 ClientConfig (co.cask.cdap.client.config.ClientConfig)2 BufferedReader (java.io.BufferedReader)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 FileReader (java.io.FileReader)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 AccumuloException (org.apache.accumulo.core.client.AccumuloException)2 AccumuloSecurityException (org.apache.accumulo.core.client.AccumuloSecurityException)2 TableNotFoundException (org.apache.accumulo.core.client.TableNotFoundException)2