Search in sources :

Example 76 with Option

use of org.apache.commons.cli.Option in project tika by apache.

the class CommandLineParserBuilder method build.

public Options build(InputStream is) throws IOException {
    Document doc = null;
    try {
        DocumentBuilder docBuilder = new ParseContext().getDocumentBuilder();
        doc = docBuilder.parse(is);
    } catch (TikaException | SAXException e) {
        throw new IOExceptionWithCause(e);
    }
    Node docElement = doc.getDocumentElement();
    NodeList children = docElement.getChildNodes();
    Node commandlineNode = null;
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        String nodeName = child.getNodeName();
        if (nodeName.equals("commandline")) {
            commandlineNode = child;
            break;
        }
    }
    Options options = new Options();
    if (commandlineNode == null) {
        return options;
    }
    NodeList optionNodes = commandlineNode.getChildNodes();
    for (int i = 0; i < optionNodes.getLength(); i++) {
        Node optionNode = optionNodes.item(i);
        if (optionNode.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        Option opt = buildOption(optionNode);
        if (opt != null) {
            options.addOption(opt);
        }
    }
    return options;
}
Also used : Options(org.apache.commons.cli.Options) TikaException(org.apache.tika.exception.TikaException) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) IOExceptionWithCause(org.apache.tika.io.IOExceptionWithCause) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParseContext(org.apache.tika.parser.ParseContext) Option(org.apache.commons.cli.Option)

Example 77 with Option

use of org.apache.commons.cli.Option in project tika by apache.

the class CommandLineParserBuilder method buildOption.

private Option buildOption(Node optionNode) {
    NamedNodeMap map = optionNode.getAttributes();
    String opt = getString(map, "opt", "");
    String description = getString(map, "description", "");
    String longOpt = getString(map, "longOpt", "");
    boolean isRequired = getBoolean(map, "required", false);
    boolean hasArg = getBoolean(map, "hasArg", false);
    if (opt.trim().length() == 0 || description.trim().length() == 0) {
        throw new IllegalArgumentException("Must specify at least option and description");
    }
    Option option = new Option(opt, description);
    if (longOpt.trim().length() > 0) {
        option.setLongOpt(longOpt);
    }
    if (isRequired) {
        option.setRequired(true);
    }
    if (hasArg) {
        option.setArgs(1);
    }
    return option;
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Option(org.apache.commons.cli.Option)

Example 78 with Option

use of org.apache.commons.cli.Option in project cdap by caskdata.

the class UpgradeTool method main.

public static void main(String[] args) throws Exception {
    Options options = new Options().addOption(new Option("h", "help", false, "Print this usage message.")).addOption(new Option("u", "uri", true, "CDAP instance URI to interact with in the format " + "[http[s]://]<hostname>:<port>. Defaults to localhost:11015.")).addOption(new Option("a", "accesstoken", true, "File containing the access token to use when interacting " + "with a secure CDAP instance.")).addOption(new Option("t", "timeout", true, "Timeout in milliseconds to use when interacting with the " + "CDAP RESTful APIs. Defaults to " + DEFAULT_READ_TIMEOUT_MILLIS + ".")).addOption(new Option("n", "namespace", true, "Namespace to perform the upgrade in. If none is given, " + "pipelines in all namespaces will be upgraded.")).addOption(new Option("p", "pipeline", true, "Name of the pipeline to upgrade. If specified, a namespace " + "must also be given.")).addOption(new Option("f", "configfile", true, "File containing old application details to update. " + "The file contents are expected to be in the same format as the request body for creating an " + "ETL application from one of the etl artifacts. " + "It is expected to be a JSON Object containing 'artifact' and 'config' fields." + "The value for 'artifact' must be a JSON Object that specifies the artifact scope, name, and version. " + "The value for 'config' must be a JSON Object specifies the source, transforms, and sinks of the pipeline, " + "as expected by older versions of the etl artifacts.")).addOption(new Option("o", "outputfile", true, "File to write the converted application details provided in " + "the configfile option. If none is given, results will be written to the input file + '.converted'. " + "The contents of this file can be sent directly to CDAP to update or create an application.")).addOption(new Option("e", "errorDir", true, "Optional directory to write any upgraded pipeline configs that " + "failed to upgrade. The problematic configs can then be manually edited and upgraded separately. " + "Upgrade errors may happen for pipelines that use plugins that are not backwards compatible. " + "This directory must be writable by the user that is running this tool."));
    CommandLineParser parser = new BasicParser();
    CommandLine commandLine = parser.parse(options, args);
    String[] commandArgs = commandLine.getArgs();
    // if help is an option, or if there isn't a single 'upgrade' command, print usage and exit.
    if (commandLine.hasOption("h") || commandArgs.length != 1 || !"upgrade".equalsIgnoreCase(commandArgs[0])) {
        HelpFormatter helpFormatter = new HelpFormatter();
        helpFormatter.printHelp(UpgradeTool.class.getName() + " upgrade", "Upgrades old pipelines to the current version. If the plugins used are not backward-compatible, " + "the attempted upgrade config will be written to the error directory for a manual upgrade.", options, "");
        System.exit(0);
    }
    ClientConfig clientConfig = getClientConfig(commandLine);
    if (commandLine.hasOption("f")) {
        String inputFilePath = commandLine.getOptionValue("f");
        String outputFilePath = commandLine.hasOption("o") ? commandLine.getOptionValue("o") : inputFilePath + ".new";
        convertFile(inputFilePath, outputFilePath, new Upgrader(new ArtifactClient(clientConfig)));
        System.exit(0);
    }
    File errorDir = commandLine.hasOption("e") ? new File(commandLine.getOptionValue("e")) : null;
    if (errorDir != null) {
        if (!errorDir.exists()) {
            if (!errorDir.mkdirs()) {
                LOG.error("Unable to create error directory {}.", errorDir.getAbsolutePath());
                System.exit(1);
            }
        } else if (!errorDir.isDirectory()) {
            LOG.error("{} is not a directory.", errorDir.getAbsolutePath());
            System.exit(1);
        } else if (!errorDir.canWrite()) {
            LOG.error("Unable to write to error directory {}.", errorDir.getAbsolutePath());
            System.exit(1);
        }
    }
    UpgradeTool upgradeTool = new UpgradeTool(clientConfig, errorDir);
    String namespace = commandLine.getOptionValue("n");
    String pipelineName = commandLine.getOptionValue("p");
    if (pipelineName != null) {
        if (namespace == null) {
            throw new IllegalArgumentException("Must specify a namespace when specifying a pipeline.");
        }
        ApplicationId appId = new ApplicationId(namespace, pipelineName);
        if (upgradeTool.upgrade(appId)) {
            LOG.info("Successfully upgraded {}.", appId);
        } else {
            LOG.info("{} did not need to be upgraded.", appId);
        }
        System.exit(0);
    }
    if (namespace != null) {
        printUpgraded(upgradeTool.upgrade(new NamespaceId(namespace)));
        System.exit(0);
    }
    printUpgraded(upgradeTool.upgrade());
}
Also used : Options(org.apache.commons.cli.Options) Upgrader(co.cask.cdap.etl.tool.config.Upgrader) HelpFormatter(org.apache.commons.cli.HelpFormatter) BasicParser(org.apache.commons.cli.BasicParser) CommandLine(org.apache.commons.cli.CommandLine) ArtifactClient(co.cask.cdap.client.ArtifactClient) Option(org.apache.commons.cli.Option) CommandLineParser(org.apache.commons.cli.CommandLineParser) NamespaceId(co.cask.cdap.proto.id.NamespaceId) ClientConfig(co.cask.cdap.client.config.ClientConfig) ApplicationId(co.cask.cdap.proto.id.ApplicationId) File(java.io.File)

Example 79 with Option

use of org.apache.commons.cli.Option in project cdap by caskdata.

the class AbstractProgramTwillRunnable method createOption.

private Option createOption(String opt, String desc) {
    Option option = new Option(opt, true, desc);
    option.setRequired(true);
    return option;
}
Also used : Option(org.apache.commons.cli.Option)

Example 80 with Option

use of org.apache.commons.cli.Option in project cdap by caskdata.

the class JobQueueDebugger method main.

public static void main(String[] args) throws Exception {
    Options options = new Options().addOption(new Option("h", "help", false, "Print this usage message.")).addOption(new Option("p", "partition", true, "JobQueue partition to debug. Defaults to all partitions.")).addOption(new Option("t", "trace", false, "Trace mode. Prints all of the jobs being debugged."));
    CommandLineParser parser = new BasicParser();
    CommandLine commandLine = parser.parse(options, args);
    String[] commandArgs = commandLine.getArgs();
    // if help is an option, or if there is a command, print usage and exit.
    if (commandLine.hasOption("h") || commandArgs.length != 0) {
        HelpFormatter helpFormatter = new HelpFormatter();
        helpFormatter.printHelp(JobQueueDebugger.class.getName(), "Scans the JobQueueDataset and prints statistics about the Jobs in it.", options, "");
        System.exit(0);
    }
    Integer partition = null;
    if (commandLine.hasOption("p")) {
        String partitionString = commandLine.getOptionValue("p");
        partition = Integer.valueOf(partitionString);
    }
    boolean trace = false;
    if (commandLine.hasOption("t")) {
        trace = true;
    }
    JobQueueDebugger debugger = createDebugger();
    debugger.startAndWait();
    debugger.printTopicMessageIds();
    if (partition == null) {
        debugger.scanPartitions(trace);
    } else {
        debugger.scanPartition(partition, trace);
    }
    debugger.stopAndWait();
}
Also used : HelpFormatter(org.apache.commons.cli.HelpFormatter) Options(org.apache.commons.cli.Options) BasicParser(org.apache.commons.cli.BasicParser) CommandLine(org.apache.commons.cli.CommandLine) Option(org.apache.commons.cli.Option) CommandLineParser(org.apache.commons.cli.CommandLineParser)

Aggregations

Option (org.apache.commons.cli.Option)152 Options (org.apache.commons.cli.Options)105 CommandLine (org.apache.commons.cli.CommandLine)53 CommandLineParser (org.apache.commons.cli.CommandLineParser)52 ParseException (org.apache.commons.cli.ParseException)41 GnuParser (org.apache.commons.cli.GnuParser)39 HelpFormatter (org.apache.commons.cli.HelpFormatter)30 File (java.io.File)13 OptionGroup (org.apache.commons.cli.OptionGroup)13 FileInputStream (java.io.FileInputStream)10 IOException (java.io.IOException)10 HashMap (java.util.HashMap)9 DefaultParser (org.apache.commons.cli.DefaultParser)9 Properties (java.util.Properties)8 BasicParser (org.apache.commons.cli.BasicParser)6 ConsoleAppender (org.apache.log4j.ConsoleAppender)6 PatternLayout (org.apache.log4j.PatternLayout)6 ArrayList (java.util.ArrayList)5 PosixParser (org.apache.commons.cli.PosixParser)5 List (java.util.List)3