Search in sources :

Example 51 with BasicParser

use of org.apache.commons.cli.BasicParser in project pentaho-kettle by pentaho.

the class Carte method parseAndRunCommand.

@SuppressWarnings("static-access")
private static void parseAndRunCommand(String[] args) throws Exception {
    options = new Options();
    options.addOption(OptionBuilder.withLongOpt("stop").withDescription(BaseMessages.getString(PKG, "Carte.ParamDescription.stop")).hasArg(false).isRequired(false).create('s'));
    options.addOption(OptionBuilder.withLongOpt("userName").withDescription(BaseMessages.getString(PKG, "Carte.ParamDescription.userName")).hasArg(true).isRequired(false).create('u'));
    options.addOption(OptionBuilder.withLongOpt("password").withDescription(BaseMessages.getString(PKG, "Carte.ParamDescription.password")).hasArg(true).isRequired(false).create('p'));
    options.addOption(OptionBuilder.withLongOpt("help").withDescription(BaseMessages.getString(PKG, "Carte.ParamDescription.help")).create('h'));
    CommandLineParser parser = new BasicParser();
    CommandLine cmd = parser.parse(options, args);
    if (cmd.hasOption('h')) {
        displayHelpAndAbort();
    }
    String[] arguments = cmd.getArgs();
    boolean usingConfigFile = false;
    // Load from an xml file that describes the complete configuration...
    // 
    SlaveServerConfig config = null;
    if (arguments.length == 1 && !Utils.isEmpty(arguments[0])) {
        if (cmd.hasOption('s')) {
            throw new Carte.CarteCommandException(BaseMessages.getString(PKG, "Carte.Error.illegalStop"));
        }
        usingConfigFile = true;
        FileObject file = KettleVFS.getFileObject(arguments[0]);
        Document document = XMLHandler.loadXMLFile(file);
        // Must stand up server now to allow decryption of password
        setKettleEnvironment();
        Node configNode = XMLHandler.getSubNode(document, SlaveServerConfig.XML_TAG);
        config = new SlaveServerConfig(new LogChannel("Slave server config"), configNode);
        if (config.getAutoSequence() != null) {
            config.readAutoSequences();
        }
        config.setFilename(arguments[0]);
    }
    if (arguments.length == 2 && !Utils.isEmpty(arguments[0]) && !Utils.isEmpty(arguments[1])) {
        String hostname = arguments[0];
        String port = arguments[1];
        if (cmd.hasOption('s')) {
            String user = cmd.getOptionValue('u');
            String password = cmd.getOptionValue('p');
            shutdown(hostname, port, user, password);
            System.exit(0);
        }
        SlaveServer slaveServer = new SlaveServer(hostname + ":" + port, hostname, port, null, null);
        config = new SlaveServerConfig();
        config.setSlaveServer(slaveServer);
    }
    // 
    if (config == null) {
        displayHelpAndAbort();
    }
    if (!usingConfigFile) {
        setKettleEnvironment();
    }
    runCarte(config);
}
Also used : Options(org.apache.commons.cli.Options) Node(org.w3c.dom.Node) LogChannel(org.pentaho.di.core.logging.LogChannel) Document(org.w3c.dom.Document) SlaveServer(org.pentaho.di.cluster.SlaveServer) BasicParser(org.apache.commons.cli.BasicParser) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) FileObject(org.apache.commons.vfs2.FileObject)

Example 52 with BasicParser

use of org.apache.commons.cli.BasicParser in project ranger by apache.

the class XmlConfigChanger method parseConfig.

@SuppressWarnings("static-access")
public void parseConfig(String[] args) {
    Options options = new Options();
    Option inputOption = OptionBuilder.hasArgs(1).isRequired().withLongOpt("input").withDescription("Input xml file name").create('i');
    options.addOption(inputOption);
    Option outputOption = OptionBuilder.hasArgs(1).isRequired().withLongOpt("output").withDescription("Output xml file name").create('o');
    options.addOption(outputOption);
    Option configOption = OptionBuilder.hasArgs(1).isRequired().withLongOpt("config").withDescription("Config file name").create('c');
    options.addOption(configOption);
    Option installPropOption = OptionBuilder.hasArgs(1).isRequired(false).withLongOpt("installprop").withDescription("install.properties").create('p');
    options.addOption(installPropOption);
    CommandLineParser parser = new BasicParser();
    CommandLine cmd = null;
    try {
        cmd = parser.parse(options, args);
    } catch (ParseException e) {
        String header = "ERROR: " + e;
        HelpFormatter helpFormatter = new HelpFormatter();
        helpFormatter.printHelp("java " + XmlConfigChanger.class.getName(), header, options, null, true);
        throw new RuntimeException(e);
    }
    String inputFileName = cmd.getOptionValue('i');
    this.inpFile = new File(inputFileName);
    if (!this.inpFile.canRead()) {
        String header = "ERROR: Input file [" + this.inpFile.getAbsolutePath() + "] can not be read.";
        HelpFormatter helpFormatter = new HelpFormatter();
        helpFormatter.printHelp("java " + XmlConfigChanger.class.getName(), header, options, null, true);
        throw new RuntimeException(header);
    }
    String outputFileName = cmd.getOptionValue('o');
    this.outFile = new File(outputFileName);
    if (this.outFile.exists()) {
        String header = "ERROR: Output file [" + this.outFile.getAbsolutePath() + "] already exists. Specify a filepath for creating new output file for the input [" + this.inpFile.getAbsolutePath() + "]";
        HelpFormatter helpFormatter = new HelpFormatter();
        helpFormatter.printHelp("java " + XmlConfigChanger.class.getName(), header, options, null, true);
        throw new RuntimeException(header);
    }
    String configFileName = cmd.getOptionValue('c');
    this.confFile = new File(configFileName);
    if (!this.confFile.canRead()) {
        String header = "ERROR: Config file [" + this.confFile.getAbsolutePath() + "] can not be read.";
        HelpFormatter helpFormatter = new HelpFormatter();
        helpFormatter.printHelp("java " + XmlConfigChanger.class.getName(), header, options, null, true);
        throw new RuntimeException(header);
    }
    String installPropFileName = (cmd.hasOption('p') ? cmd.getOptionValue('p') : null);
    if (installPropFileName != null) {
        this.propFile = new File(installPropFileName);
        if (!this.propFile.canRead()) {
            String header = "ERROR: Install Property file [" + this.propFile.getAbsolutePath() + "] can not be read.";
            HelpFormatter helpFormatter = new HelpFormatter();
            helpFormatter.printHelp("java " + XmlConfigChanger.class.getName(), header, options, null, true);
            throw new RuntimeException(header);
        }
    }
}
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) ParseException(org.apache.commons.cli.ParseException) File(java.io.File)

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