use of org.apache.commons.cli.PosixParser in project zm-mailbox by Zimbra.
the class ProvUtil method main.
public static void main(String[] args) throws IOException, ServiceException {
CliUtil.setCliSoapHttpTransportTimeout();
// send all logs to stderr
ZimbraLog.toolSetupLog4jConsole("INFO", true, false);
SocketFactories.registerProtocols();
SoapTransport.setDefaultUserAgent("zmprov", BuildInfo.VERSION);
ProvUtil pu = new ProvUtil();
CommandLineParser parser = new PosixParser();
Options options = new Options();
options.addOption("h", "help", false, "display usage");
options.addOption("f", "file", true, "use file as input stream");
options.addOption("s", "server", true, "host[:port] of server to connect to");
options.addOption("l", "ldap", false, "provision via LDAP");
options.addOption("L", "logpropertyfile", true, "log4j property file");
options.addOption("a", "account", true, "account name (not used with --ldap)");
options.addOption("p", "password", true, "password for account");
options.addOption("P", "passfile", true, "filename with password in it");
options.addOption("z", "zadmin", false, "use zimbra admin name/password from localconfig for account/password");
options.addOption("v", "verbose", false, "verbose mode");
options.addOption("d", "debug", false, "debug mode (SOAP request and response payload)");
options.addOption("D", "debughigh", false, "debug mode (SOAP req/resp payload and http headers)");
options.addOption("m", "master", false, "use LDAP master (has to be used with --ldap)");
options.addOption("t", "temp", false, "write binary values to files in temporary directory specified in localconfig key zmprov_tmp_directory");
options.addOption("r", "replace", false, "allow replacement of multi-valued attr value");
options.addOption("fd", "forcedisplay", false, "force display attr value");
options.addOption(SoapCLI.OPT_AUTHTOKEN);
options.addOption(SoapCLI.OPT_AUTHTOKENFILE);
CommandLine cl = null;
boolean err = false;
try {
cl = parser.parse(options, args, true);
} catch (ParseException pe) {
printError("error: " + pe.getMessage());
err = true;
}
if (err || cl.hasOption('h')) {
pu.usage();
}
if (cl.hasOption('l') && cl.hasOption('s')) {
printError("error: cannot specify both -l and -s at the same time");
System.exit(2);
}
pu.setVerbose(cl.hasOption('v'));
if (cl.hasOption('l')) {
pu.setUseLdap(true, cl.hasOption('m'));
}
if (cl.hasOption('L')) {
if (cl.hasOption('l')) {
ZimbraLog.toolSetupLog4j("INFO", cl.getOptionValue('L'));
} else {
printError("error: cannot specify -L when -l is not specified");
System.exit(2);
}
}
if (cl.hasOption('z')) {
pu.setAccount(LC.zimbra_ldap_user.value());
pu.setPassword(LC.zimbra_ldap_password.value());
}
if (cl.hasOption(SoapCLI.O_AUTHTOKEN) && cl.hasOption(SoapCLI.O_AUTHTOKENFILE)) {
printError("error: cannot specify " + SoapCLI.O_AUTHTOKEN + " when " + SoapCLI.O_AUTHTOKENFILE + " is specified");
System.exit(2);
}
if (cl.hasOption(SoapCLI.O_AUTHTOKEN)) {
ZAuthToken zat = ZAuthToken.fromJSONString(cl.getOptionValue(SoapCLI.O_AUTHTOKEN));
pu.setAuthToken(zat);
}
if (cl.hasOption(SoapCLI.O_AUTHTOKENFILE)) {
String authToken = StringUtil.readSingleLineFromFile(cl.getOptionValue(SoapCLI.O_AUTHTOKENFILE));
ZAuthToken zat = ZAuthToken.fromJSONString(authToken);
pu.setAuthToken(zat);
}
if (cl.hasOption('s')) {
pu.setServer(cl.getOptionValue('s'));
}
if (cl.hasOption('a')) {
pu.setAccount(cl.getOptionValue('a'));
}
if (cl.hasOption('p')) {
pu.setPassword(cl.getOptionValue('p'));
}
if (cl.hasOption('P')) {
pu.setPassword(StringUtil.readSingleLineFromFile(cl.getOptionValue('P')));
}
if (cl.hasOption('d') && cl.hasOption('D')) {
printError("error: cannot specify both -d and -D at the same time");
System.exit(2);
}
if (cl.hasOption('D')) {
pu.setDebug(SoapDebugLevel.high);
} else if (cl.hasOption('d')) {
pu.setDebug(SoapDebugLevel.normal);
}
if (!pu.useLdap() && cl.hasOption('m')) {
printError("error: cannot specify -m when -l is not specified");
System.exit(2);
}
if (cl.hasOption('t')) {
pu.setOutputBinaryToFile(true);
}
if (cl.hasOption('r')) {
pu.setAllowMultiValuedAttrReplacement(true);
}
if (cl.hasOption("fd")) {
pu.setForceDisplayAttrValue(true);
}
args = recombineDecapitatedAttrs(cl.getArgs(), options, args);
try {
if (args.length < 1) {
pu.initProvisioning();
InputStream is = null;
if (cl.hasOption('f')) {
pu.setBatchMode(true);
is = new FileInputStream(cl.getOptionValue('f'));
} else {
if (LC.command_line_editing_enabled.booleanValue()) {
try {
CliUtil.enableCommandLineEditing(LC.zimbra_home.value() + "/.zmprov_history");
} catch (IOException e) {
errConsole.println("Command line editing will be disabled: " + e);
if (pu.verboseMode) {
e.printStackTrace(errConsole);
}
}
}
// This has to happen last because JLine modifies System.in.
is = System.in;
}
pu.interactive(new BufferedReader(new InputStreamReader(is, "UTF-8")));
} else {
Command cmd = pu.lookupCommand(args[0]);
if (cmd == null) {
pu.usage();
}
if (cmd.isDeprecated()) {
pu.deprecated();
}
if (pu.forceLdapButDontRequireUseLdapOption(cmd)) {
pu.setUseLdap(true, false);
}
if (pu.needProvisioningInstance(cmd)) {
pu.initProvisioning();
}
try {
if (!pu.execute(args)) {
pu.usage();
}
} catch (ArgException e) {
pu.usage();
}
}
} catch (ServiceException e) {
Throwable cause = e.getCause();
String errText = "ERROR: " + e.getCode() + " (" + e.getMessage() + ")" + (cause == null ? "" : " (cause: " + cause.getClass().getName() + " " + cause.getMessage() + ")");
printError(errText);
if (pu.verboseMode) {
e.printStackTrace(errConsole);
}
System.exit(2);
}
}
use of org.apache.commons.cli.PosixParser in project symmetric-ds by JumpMind.
the class AbstractCommandLauncher method execute.
public void execute(String[] args) {
PosixParser parser = new PosixParser();
Options options = new Options();
buildOptions(options);
try {
CommandLine line = parser.parse(options, args);
if (line.hasOption(HELP) || (line.getArgList().contains(HELP)) || ((args == null || args.length == 0) && line.getOptions().length == 0 && printHelpIfNoOptionsAreProvided())) {
printHelp(line, options);
System.exit(2);
}
configureLogging(line);
configurePropertiesFile(line);
if (line.getOptions() != null) {
for (Option option : line.getOptions()) {
log.info("Option: name={}, value={}", new Object[] { option.getLongOpt() != null ? option.getLongOpt() : option.getOpt(), ArrayUtils.toString(option.getValues()) });
}
}
executeWithOptions(line);
} catch (ParseException e) {
System.err.println(e.getMessage());
printUsage(options);
System.exit(4);
} catch (Exception e) {
System.err.println("-------------------------------------------------------------------------------");
System.err.println("An exception occurred. Please see the following for details:");
System.err.println("-------------------------------------------------------------------------------");
ExceptionUtils.printRootCauseStackTrace(e, System.err);
System.err.println("-------------------------------------------------------------------------------");
System.exit(1);
}
}
use of org.apache.commons.cli.PosixParser in project SSM by Intel-bigdata.
the class SmartServer method parseHelpArgument.
public static boolean parseHelpArgument(String[] args, String helpDescription, PrintStream out, boolean printGenericCommandUsage) {
if (args.length == 1) {
try {
CommandLineParser parser = new PosixParser();
CommandLine cmdLine = parser.parse(helpOptions, args);
if (cmdLine.hasOption(helpOpt.getOpt()) || cmdLine.hasOption(helpOpt.getLongOpt())) {
// should print out the help information
out.println(helpDescription + "\n");
return true;
}
} catch (ParseException pe) {
LOG.warn("Parse help exception", pe);
return false;
}
}
return false;
}
use of org.apache.commons.cli.PosixParser in project phoenix by apache.
the class PhoenixConsumerTool method parseOptions.
public static CommandLine parseOptions(String[] args) {
Options options = getOptions();
CommandLineParser parser = new PosixParser();
CommandLine cmdLine = null;
try {
cmdLine = parser.parse(options, args);
} catch (ParseException e) {
printHelpAndExit("Error parsing command line options: " + e.getMessage(), options);
}
if (cmdLine.hasOption(HELP_OPT.getOpt())) {
printHelpAndExit(options, 0);
}
if (!cmdLine.hasOption(FILE_PATH_OPT.getOpt())) {
throw new IllegalStateException(FILE_PATH_OPT.getLongOpt() + " is a mandatory " + "parameter");
}
if (!cmdLine.getArgList().isEmpty()) {
throw new IllegalStateException("Got unexpected extra parameters: " + cmdLine.getArgList());
}
return cmdLine;
}
use of org.apache.commons.cli.PosixParser in project apex-core by apache.
the class ApexCli method getGetOperatorClassesCommandLineInfo.
static GetOperatorClassesCommandLineInfo getGetOperatorClassesCommandLineInfo(String[] args) throws ParseException {
CommandLineParser parser = new PosixParser();
GetOperatorClassesCommandLineInfo result = new GetOperatorClassesCommandLineInfo();
CommandLine line = parser.parse(GET_OPERATOR_CLASSES_OPTIONS.options, args);
result.parent = line.getOptionValue("parent");
result.args = line.getArgs();
return result;
}
Aggregations