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);
}
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);
}
}
}
Aggregations