Search in sources :

Example 81 with PosixParser

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);
    }
}
Also used : Options(org.apache.commons.cli.Options) InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) PosixParser(org.apache.commons.cli.PosixParser) IOException(java.io.IOException) ZAuthToken(com.zimbra.common.auth.ZAuthToken) FileInputStream(java.io.FileInputStream) CommandLine(org.apache.commons.cli.CommandLine) ServiceException(com.zimbra.common.service.ServiceException) RightCommand(com.zimbra.cs.account.accesscontrol.RightCommand) BufferedReader(java.io.BufferedReader) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

Example 82 with PosixParser

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);
    }
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) PosixParser(org.apache.commons.cli.PosixParser) Option(org.apache.commons.cli.Option) ParseException(org.apache.commons.cli.ParseException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ParseException(org.apache.commons.cli.ParseException)

Example 83 with PosixParser

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

Example 84 with PosixParser

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

Example 85 with PosixParser

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

Aggregations

PosixParser (org.apache.commons.cli.PosixParser)103 CommandLine (org.apache.commons.cli.CommandLine)97 CommandLineParser (org.apache.commons.cli.CommandLineParser)67 Options (org.apache.commons.cli.Options)56 ParseException (org.apache.commons.cli.ParseException)51 File (java.io.File)24 IOException (java.io.IOException)20 HelpFormatter (org.apache.commons.cli.HelpFormatter)16 Optional (java.util.Optional)10 Test (org.junit.Test)9 List (java.util.List)8 Configuration (org.apache.hadoop.conf.Configuration)8 GenericOptionsParser (org.apache.hadoop.util.GenericOptionsParser)7 SystemExitException (org.apache.openejb.cli.SystemExitException)7 Option (org.apache.commons.cli.Option)6 Parser (org.apache.commons.cli.Parser)6 Properties (java.util.Properties)5 Path (org.apache.hadoop.fs.Path)5 ArrayList (java.util.ArrayList)4 HBaseConfiguration (org.apache.hadoop.hbase.HBaseConfiguration)4