Search in sources :

Example 76 with CommandLine

use of org.apache.commons.cli.CommandLine in project zm-mailbox by Zimbra.

the class MetadataDump method parseArgs.

private static CommandLine parseArgs(String[] args) {
    CommandLineParser parser = new GnuParser();
    CommandLine cl = null;
    try {
        cl = parser.parse(sOptions, args);
    } catch (ParseException pe) {
        usage(pe.getMessage());
        System.exit(1);
    }
    return cl;
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) GnuParser(org.apache.commons.cli.GnuParser) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

Example 77 with CommandLine

use of org.apache.commons.cli.CommandLine in project zm-mailbox by Zimbra.

the class TestLdapReadTimeout method main.

/**
     * /Users/pshao/dev/workspace/sandbox/sandbox/bin>/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/bin/java  LdapReadTimeout
     * /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/bin/java LdapReadTimeout
     * 
     * zmjava com.zimbra.qa.unittest.TestLdapReadTimeout -s 5
     * zmjava com.zimbra.qa.unittest.TestLdapReadTimeout -H ldap://localhost:389 -D uid=zimbra,cn=admins,cn=zimbra -w zimbra -s 5
     * 
     * @param args
     */
public static void main(String[] args) throws Exception {
    Options options = new Options();
    options.addOption(O_HELP, false, "print usage");
    options.addOption(O_SLEEP, true, "upon hitting an Exception, minutes to wait until exiting the program.  " + "If not specified, will exit immediately.");
    options.addOption(O_URI, true, "URI. e.g. ldap://localhost:389");
    options.addOption(O_BINDDN, true, "bind DN");
    options.addOption(O_PASSWORD, true, "password");
    CommandLine cl = null;
    try {
        CommandLineParser parser = new GnuParser();
        cl = parser.parse(options, args);
        if (cl == null) {
            throw new ParseException("");
        }
    } catch (ParseException e) {
        usage(options);
        e.printStackTrace();
        System.exit(1);
    }
    if (cl.hasOption(O_HELP)) {
        usage(options);
        System.exit(0);
    }
    String uri = null;
    String bindDN = null;
    String password = null;
    Integer minutesToWait = null;
    if (cl.hasOption(O_URI)) {
        uri = cl.getOptionValue(O_URI);
    } else {
        uri = "ldap://localhost:389";
    }
    if (cl.hasOption(O_BINDDN)) {
        bindDN = cl.getOptionValue(O_BINDDN);
    } else {
        bindDN = "uid=zimbra,cn=admins,cn=zimbra";
    }
    if (cl.hasOption(O_PASSWORD)) {
        password = cl.getOptionValue(O_PASSWORD);
    } else {
        password = "zimbra";
    }
    if (cl.hasOption(O_SLEEP)) {
        String wait = cl.getOptionValue(O_SLEEP);
        try {
            minutesToWait = Integer.valueOf(wait);
        } catch (NumberFormatException e) {
            usage(options);
            e.printStackTrace();
            System.exit(1);
        }
    }
    LdapReadTimeoutTester tester = null;
    try {
        // tester = new JNDITest(uri, bindDN, password);  // fails
        // works
        tester = new UnboundIDTest(uri, bindDN, password);
        System.out.println("=============");
        System.out.println(tester.getClass().getCanonicalName());
        System.out.println("LDAP server URI: " + uri);
        System.out.println("bind DN: " + bindDN);
        System.out.println("=============");
        System.out.println();
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
    long startTime = System.currentTimeMillis();
    test(tester);
    long endTime = System.currentTimeMillis();
    long elapsed = endTime - startTime;
    SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    System.out.println();
    System.out.println(tester.getClass().getCanonicalName());
    System.out.println("Started at: " + fmt.format(new Date(startTime)));
    System.out.println("Ended at: " + fmt.format(new Date(endTime)));
    System.out.println("Elapsed = " + (elapsed / 1000) + " seconds");
    System.out.println();
    if (minutesToWait != null) {
        System.out.println("Sleeping for " + minutesToWait + " minutes before exiting...");
        Thread.sleep(minutesToWait * 60 * 1000);
    }
}
Also used : Options(org.apache.commons.cli.Options) LDAPConnectionOptions(com.unboundid.ldap.sdk.LDAPConnectionOptions) GnuParser(org.apache.commons.cli.GnuParser) ParseException(org.apache.commons.cli.ParseException) Date(java.util.Date) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 78 with CommandLine

use of org.apache.commons.cli.CommandLine in project zm-mailbox by Zimbra.

the class LuceneViewer method main.

public static void main(String[] args) throws Exception {
    CLI cli = new CLI();
    CommandLine cl = cli.getCommandLine(args);
    if (!cl.hasOption(CLI.O_ACTION)) {
        cli.usage(new ParseException("missing required option " + CLI.O_ACTION), true);
    }
    String action = cl.getOptionValue(CLI.O_ACTION);
    if ("dump".equals(action)) {
        doDump(cl);
    } else if ("check".equals(action)) {
        doCheck(cl);
    } else {
        cli.usage(new ParseException("invalid option " + action), true);
    }
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) ParseException(org.apache.commons.cli.ParseException)

Example 79 with CommandLine

use of org.apache.commons.cli.CommandLine in project zm-mailbox by Zimbra.

the class LocalConfigCLI method exec.

private void exec(String[] args) {
    CommandLine cl = null;
    CommandLineParser parser = new GnuParser();
    try {
        cl = parser.parse(mOptions, args);
    } catch (ParseException pe) {
        Logging.error("Failed to parse command line: " + pe);
        System.exit(1);
    }
    if (cl.hasOption("q")) {
        Logging.setQuietMode(true);
    }
    if (cl.hasOption("h")) {
        usage();
    }
    // Load known keys from BackupLC if available
    loadExtensionLC("com.zimbra.cs.backup.BackupLC");
    // Load known keys from ZimbraOpenOfficeExt if available
    loadExtensionLC("com.zimbra.openoffice.config.OpenOfficeLC");
    // Load known keys from ZimbraVoice if available
    loadExtensionLC("com.zimbra.cs.voice.VoiceLC");
    // info/docs for supported keys
    if (cl.hasOption("i")) {
        checkCompatibleOptions("i", "q", cl);
        LocalConfig.printDoc(System.out, cl.getArgs(), false);
        return;
    }
    // info/docs for all keys (hidden option)
    if (cl.hasOption("all")) {
        checkCompatibleOptions("all", "q", cl);
        LocalConfig.printDoc(System.out, cl.getArgs(), true);
        return;
    }
    LocalConfig lc = null;
    try {
        lc = new LocalConfig(cl.getOptionValue("c"));
    } catch (DocumentException de) {
        error("failed when reading config file", de);
    } catch (ConfigException ce) {
        error("failed with error in config file", ce);
    }
    // edit
    if (cl.hasOption("e")) {
        checkNotRoot("-e");
        checkCompatibleOptions("e", "qfrc", cl);
        String[] av = cl.getArgs();
        if (av == null || av.length == 0) {
            error("insufficient arguments", null);
        }
        for (int i = 0; i < av.length; i++) {
            String key = null;
            String value = null;
            if (cl.hasOption("r")) {
                key = av[i];
                value = RandomPassword.generate();
            } else {
                int eqidx = av[i].indexOf("=");
                if (eqidx <= 0) {
                    // <= 0 also catches first char being =, ie no key specified
                    error("argument '" + av[i] + "' not in key=value form", null);
                }
                key = av[i].substring(0, eqidx);
                value = av[i].substring(eqidx + 1, av[i].length());
            }
            if (KnownKey.needForceToEdit(key) && !cl.hasOption("f")) {
                error("can not edit key " + key, null);
            }
            lc.set(key, value);
        }
        try {
            lc.save();
        } catch (Exception e) {
            error("save to " + lc.getConfigFile() + " failed", e);
        }
        return;
    }
    // unset
    if (cl.hasOption("u")) {
        checkNotRoot("-u");
        checkCompatibleOptions("u", "qfc", cl);
        String[] av = cl.getArgs();
        if (av == null || av.length == 0) {
            error("insufficient arguments", null);
        }
        for (int i = 0; i < av.length; i++) {
            String key = av[i];
            if (!lc.isSet(key)) {
                error("key " + key + " is not set", null);
            }
            lc.remove(key);
        }
        try {
            lc.save();
        } catch (Exception e) {
            error("save to " + lc.getConfigFile() + " failed", e);
        }
        return;
    }
    // show path
    if (cl.hasOption("p")) {
        checkCompatibleOptions("p", "qc", cl);
        System.out.println(lc.getConfigFile());
        return;
    }
    if (cl.hasOption("l")) {
        // reset logging and run native lib load
        CliUtil.toolSetup("WARN");
        try {
            reload();
        } catch (ServiceException e) {
            if (e.getCause() instanceof ConnectException) {
                error("'" + Provisioning.SERVICE_MAILBOX + "' service is not running", null);
            } else {
                error(e.getMessage(), e);
            }
        }
        return;
    }
    // print values
    String format = cl.getOptionValue("m");
    ConfigWriter cwriter = null;
    try {
        cwriter = ConfigWriter.getInstance(format, cl.hasOption("x"), !cl.hasOption("s"));
    } catch (ConfigException iae) {
        error("failed to create writer " + format, iae);
    }
    try {
        // changed
        if (cl.hasOption("n")) {
            checkCompatibleOptions("n", "qscmx", cl);
            lc.printChanged(System.out, cwriter, cl.getArgs());
            return;
        }
        // default
        if (cl.hasOption("d")) {
            checkCompatibleOptions("d", "qscmx", cl);
            lc.printDefaults(System.out, cwriter, cl.getArgs());
            return;
        }
        // current
        checkCompatibleOptions("", "qscmx", cl);
        lc.print(System.out, cwriter, cl.getArgs());
    } catch (Exception e) {
        error("exception occurred when printing", e);
    }
}
Also used : LocalConfig(com.zimbra.common.localconfig.LocalConfig) GnuParser(org.apache.commons.cli.GnuParser) ConfigException(com.zimbra.common.localconfig.ConfigException) ServiceException(com.zimbra.common.service.ServiceException) DocumentException(org.dom4j.DocumentException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) ParseException(org.apache.commons.cli.ParseException) ConfigException(com.zimbra.common.localconfig.ConfigException) ZClientException(com.zimbra.common.zclient.ZClientException) ConfigWriter(com.zimbra.common.localconfig.ConfigWriter) CommandLine(org.apache.commons.cli.CommandLine) ServiceException(com.zimbra.common.service.ServiceException) DocumentException(org.dom4j.DocumentException) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) ConnectException(java.net.ConnectException)

Example 80 with CommandLine

use of org.apache.commons.cli.CommandLine in project zm-mailbox by Zimbra.

the class SmtpClient method parseArguments.

@Override
protected void parseArguments(String[] args, MailConfig config) {
    CommandLineParser parser = new GnuParser();
    CommandLine cl = null;
    try {
        cl = parser.parse(options, args);
    } catch (ParseException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    if (cl.hasOption('p')) {
        config.setPort(Integer.parseInt(cl.getOptionValue('p')));
    } else {
        config.setPort(SmtpConfig.DEFAULT_PORT);
    }
    if (cl.hasOption('m')) {
        config.setMechanism(cl.getOptionValue('m').toUpperCase());
    }
    if (cl.hasOption('u')) {
        config.setAuthenticationId(cl.getOptionValue('u'));
    }
    if (cl.hasOption('w')) {
        setPassword(cl.getOptionValue('w'));
    }
    if (cl.hasOption('r')) {
        config.setRealm(cl.getOptionValue('r'));
    }
    if (cl.hasOption('s')) {
        config.setSecurity(MailConfig.Security.SSL);
    }
    if (cl.hasOption('d')) {
        config.getLogger().setLevel(Log.Level.trace);
    }
    if (cl.hasOption('h')) {
        usage();
        System.exit(0);
    }
    String[] remaining = cl.getArgs();
    config.setHost(remaining.length > 0 ? remaining[0] : "localhost");
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) GnuParser(org.apache.commons.cli.GnuParser) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

Aggregations

CommandLine (org.apache.commons.cli.CommandLine)968 Options (org.apache.commons.cli.Options)560 ParseException (org.apache.commons.cli.ParseException)464 CommandLineParser (org.apache.commons.cli.CommandLineParser)459 HelpFormatter (org.apache.commons.cli.HelpFormatter)259 GnuParser (org.apache.commons.cli.GnuParser)230 DefaultParser (org.apache.commons.cli.DefaultParser)213 PosixParser (org.apache.commons.cli.PosixParser)177 File (java.io.File)165 IOException (java.io.IOException)162 Test (org.junit.Test)144 Option (org.apache.commons.cli.Option)131 ArrayList (java.util.ArrayList)63 Path (org.apache.hadoop.fs.Path)60 BasicParser (org.apache.commons.cli.BasicParser)50 Configuration (org.apache.hadoop.conf.Configuration)41 Configuration (org.apache.flink.configuration.Configuration)37 HashMap (java.util.HashMap)34 List (java.util.List)34 Properties (java.util.Properties)33