Package gnu.getopt

Examples of gnu.getopt.Getopt


        LongOpt[] lopts = { new LongOpt("capture", LongOpt.NO_ARGUMENT, null, 'c'),
            new LongOpt("killOnTimeout", LongOpt.NO_ARGUMENT, null, 'k'),
            new LongOpt("wait", LongOpt.REQUIRED_ARGUMENT, null, 'w'),
            new LongOpt("directory", LongOpt.REQUIRED_ARGUMENT, null, 'd') };

        Getopt getopt = new Getopt("execute", args, sopts, lopts);
        int code;

        while ((executable == null) && ((code = getopt.getopt()) != -1)) {
            switch (code) {
            case ':':
            case '?': {
                out.println(MSG.getMsg(AgentI18NResourceKeys.HELP_SYNTAX_LABEL, getSyntax()));
                return;
            }

            case 1: {
                // we found the executable name - stop processing arguments
                executable = getopt.getOptarg();
                break;
            }

            case 'c': {
                capture = true;
                break;
            }

            case 'd': {
                workingDir = getopt.getOptarg();
                break;
            }

            case 'k': {
                killOnTimeout = true;
                break;
            }

            case 'w': {
                String waitArg = getopt.getOptarg();

                try {
                    waitTime = Long.parseLong(waitArg);
                } catch (NumberFormatException nfe) {
                    out.println(MSG.getMsg(AgentI18NResourceKeys.EXECUTE_BAD_WAIT_ARG, waitArg));
                    return;
                }

                break;
            }

            case 'E': {
                if (environmentVars == null) {
                    environmentVars = new LinkedHashMap<String, String>();
                }

                // here we parse it just to see if we need to set the value to true if a value wasn't provided
                String envvar = getopt.getOptarg();
                int i = envvar.indexOf("=");
                String name;
                String value;

                if (i == -1) {
                    name = envvar;
                    value = "true";
                } else {
                    name = envvar.substring(0, i);
                    value = envvar.substring(i + 1, envvar.length());
                }

                environmentVars.put(name, value);
                break;
            }
            }
        }

        if (executable == null) {
            out.println(MSG.getMsg(AgentI18NResourceKeys.EXECUTE_MISSING_EXECUTABLE));
            return;
        }

        // determine what arguments, if any, need to be passed to the process
        List<String> procArgList = new ArrayList<String>();
        for (int i = getopt.getOptind(); i < args.length; i++) {
            procArgList.add(args[i]);
        }

        String[] procArgArray = procArgList.toArray(new String[procArgList.size()]);
View Full Code Here


            new LongOpt("enable", LongOpt.NO_ARGUMENT, null, 'e'), // enable agent updates
            new LongOpt("download", LongOpt.NO_ARGUMENT, null, 'o'), // downloads the agent update binary
            new LongOpt("status", LongOpt.NO_ARGUMENT, null, 's') // status as to whether its enabled/disabled
        };

        Getopt getopt = new Getopt(getPromptCommandString(), args, sopts, lopts);
        int code;

        while ((code = getopt.getopt()) != -1) {
            switch (code) {
            case ':':
            case '?':
            case 1: {
                out.println(MSG.getMsg(AgentI18NResourceKeys.HELP_SYNTAX_LABEL, getSyntax()));
                break;
            }

            case 'u': {
                // this method outputs messages to the user; it also will not return if successful
                AgentUpdateThread.updateAgentNow(agent, true);
                break;
            }

            case 'v': {
                URL url = null;
                try {
                    AgentUpdateVersion check = new AgentUpdateVersion(agent);
                    url = check.getVersionUrl();
                    AgentUpdateInformation info = check.getAgentUpdateInformation();
                    out.println(MSG.getMsg(AgentI18NResourceKeys.UPDATE_CHECK_INFO, url, info.getUpdateVersion(), info
                        .getUpdateBuild(), info.getAgentVersion(), info.getAgentBuild()));

                    if (info.isAgentOutOfDate()) {
                        out.println(MSG.getMsg(AgentI18NResourceKeys.UPDATE_CHECK_OOD));
                    } else {
                        if (info.isAgentOutOfDateStrict()) {
                            out.println(MSG.getMsg(AgentI18NResourceKeys.UPDATE_CHECK_OOD_STRICT));
                        } else {
                            out.println(MSG.getMsg(AgentI18NResourceKeys.UPDATE_CHECK_NOT_OOD));
                        }
                    }
                } catch (Exception e) {
                    out.println(MSG.getMsg(AgentI18NResourceKeys.UPDATE_CHECK_FAILED, url, e));
                }
                break;
            }

            case 'd': {
                Preferences prefs = agent.getConfiguration().getPreferences();
                String prefName = AgentConfigurationConstants.AGENT_UPDATE_ENABLED;
                boolean prefValue = false;
                prefs.putBoolean(prefName, prefValue);
                try {
                    prefs.flush();
                } catch (BackingStoreException e) {
                    out.println(MSG.getMsg(AgentI18NResourceKeys.CANNOT_STORE_PREFERENCES, prefName, prefValue));
                }
                out.println(MSG.getMsg(AgentI18NResourceKeys.UPDATE_DISABLED));
                break;
            }

            case 'e': {
                Preferences prefs = agent.getConfiguration().getPreferences();
                String prefName = AgentConfigurationConstants.AGENT_UPDATE_ENABLED;
                boolean prefValue = true;
                prefs.putBoolean(prefName, prefValue);
                try {
                    prefs.flush();
                } catch (BackingStoreException e) {
                    out.println(MSG.getMsg(AgentI18NResourceKeys.CANNOT_STORE_PREFERENCES, prefName, prefValue));
                }
                out.println(MSG.getMsg(AgentI18NResourceKeys.UPDATE_ENABLED));
                break;
            }

            case 'o': {
                try {
                    AgentUpdateDownload aud = new AgentUpdateDownload(agent);
                    aud.download();
                    aud.validate();
                    out.println(MSG.getMsg(AgentI18NResourceKeys.UPDATE_DOWNLOADED, aud.getAgentUpdateBinaryFile()));
                } catch (Exception e) {
                    out.println(MSG.getMsg(AgentI18NResourceKeys.UPDATE_DOWNLOAD_FAILED, ThrowableUtil
                        .getAllMessages(e)));
                }
                break;
            }

            case 's': {
                if (agent.getConfiguration().isAgentUpdateEnabled()) {
                    out.println(MSG.getMsg(AgentI18NResourceKeys.UPDATE_ENABLED));
                } else {
                    out.println(MSG.getMsg(AgentI18NResourceKeys.UPDATE_DISABLED));
                }
                break;
            }
            }
        }

        if ((getopt.getOptind() + 1) < args.length) {
            out.println(MSG.getMsg(AgentI18NResourceKeys.HELP_SYNTAX_LABEL, getSyntax()));
        }

        return;
    }
View Full Code Here

        String sopts = "-dvr";
        LongOpt[] lopts = { new LongOpt("verbose", LongOpt.NO_ARGUMENT, null, 'v'),
            new LongOpt("recurse", LongOpt.NO_ARGUMENT, null, 'r'),
            new LongOpt("delete", LongOpt.NO_ARGUMENT, null, 'd') };

        Getopt getopt = new Getopt(getPromptCommandString(), args, sopts, lopts);
        int code;
        boolean verbose = false;
        boolean recurse = false;
        boolean delete = false;
        String pathname = null;

        while ((pathname == null) && (code = getopt.getopt()) != -1) {
            switch (code) {
            case ':':
            case '?': {
                out.println(MSG.getMsg(AgentI18NResourceKeys.HELP_SYNTAX_LABEL, getSyntax()));
                break;
            }

            case 1: {
                // we found the path name - stop processing arguments
                pathname = getopt.getOptarg();
                break;
            }

            case 'v': {
                verbose = true;
                break;
            }

            case 'r': {
                recurse = true;
                break;
            }

            case 'd': {
                delete = true;
                break;
            }
            }
        }

        if (getopt.getOptind() < args.length || pathname == null) {
            // we got too many arguments on the command line
            out.println(MSG.getMsg(AgentI18NResourceKeys.HELP_SYNTAX_LABEL, getSyntax()));
            return;
        }
View Full Code Here

            new LongOpt("norecurse", LongOpt.NO_ARGUMENT, null, 'n'), //
            new LongOpt("sync", LongOpt.NO_ARGUMENT, null, 's'), //
            new LongOpt("types", LongOpt.NO_ARGUMENT, null, 't'), //
            new LongOpt("xml", LongOpt.NO_ARGUMENT, null, 'x') };

        Getopt getopt = new Getopt(getPromptCommandString(), args, sopts, lopts);
        int code;

        while ((inventoryBinaryFile == null) && ((code = getopt.getopt()) != -1)) {
            switch (code) {
            case ':':
            case '?': {
                out.println(MSG.getMsg(AgentI18NResourceKeys.HELP_SYNTAX_LABEL, getSyntax()));
                return;
            }

            case 1: {
                // we found the inventory binary file name - stop processing arguments
                inventoryBinaryFile = getopt.getOptarg();
                break;
            }

            case 's': {
                sync = true;
                break;
            }

            case 'e': {
                exportFile = getopt.getOptarg();
                break;
            }

            case 't': {
                dumpTypesOnly = true;
                break;
            }

            case 'n': {
                noRecurse = true;
                break;
            }

            case 'i': {
                String idString = getopt.getOptarg();
                try {
                    id = Integer.valueOf(idString);
                } catch (NumberFormatException e) {
                    out.println(MSG.getMsg(AgentI18NResourceKeys.INVENTORY_BAD_ID, idString));
                    return;
                }

                break;
            }

            case 'x': {
                dumpXml = true;
                break;
            }
            }
        }

        if (getopt.getOptind() < args.length) {
            // we got too many arguments on the command line
            out.println(MSG.getMsg(AgentI18NResourceKeys.HELP_SYNTAX_LABEL, getSyntax()));
            return;
        }
View Full Code Here

            new LongOpt("env", LongOpt.OPTIONAL_ARGUMENT, null, 'e'),
            new LongOpt("host", LongOpt.NO_ARGUMENT, null, 'h') };

        out.println(versionString);

        Getopt getopt = new Getopt(getPromptCommandString(), args, sopts, lopts);
        int code;

        while ((code = getopt.getopt()) != -1) {
            switch (code) {
            case ':':
            case '?':
            case 1: {
                out.println(MSG.getMsg(AgentI18NResourceKeys.HELP_SYNTAX_LABEL, getSyntax()));
                break;
            }

            case 'h': {
                out.println();
                out.println(MSG.getMsg(AgentI18NResourceKeys.VERSION_HOST_LABEL));

                try {
                    InetAddress localhost = InetAddress.getLocalHost();
                    String name = localhost.getCanonicalHostName();
                    String ip = localhost.getHostAddress();
                    out.println(name + '(' + ip + ')');
                } catch (Exception e) {
                    out.println(ThrowableUtil.getAllMessages(e));
                }
                break;
            }

            case 's': {
                out.println();
                out.println(MSG.getMsg(AgentI18NResourceKeys.VERSION_SYSPROPS_LABEL));

                String opt = getopt.getOptarg();

                Properties sysprops = System.getProperties();
                for (Map.Entry<Object, Object> sysprop : sysprops.entrySet()) {
                    if (opt == null || sysprop.getKey().toString().startsWith(opt)) {
                        out.println(sysprop.getKey() + "=" + sysprop.getValue());
                    }
                }
                break;
            }

            case 'e': {
                out.println();
                out.println(MSG.getMsg(AgentI18NResourceKeys.VERSION_ENV_LABEL));

                String opt = getopt.getOptarg();

                Map<String, String> envvars = System.getenv();
                if (envvars == null) {
                    envvars = new HashMap<String, String>();
                }
                for (Map.Entry<String, String> envvar : envvars.entrySet()) {
                    if (opt == null || envvar.getKey().startsWith(opt)) {
                        out.println(envvar.getKey() + "=" + envvar.getValue());
                    }
                }
                break;
            }
            }
        }

        if ((getopt.getOptind() + 1) < args.length) {
            out.println(MSG.getMsg(AgentI18NResourceKeys.HELP_SYNTAX_LABEL, getSyntax()));
        }

        return true;
    }
View Full Code Here

            new LongOpt("resourceType", LongOpt.REQUIRED_ARGUMENT, null, 'r'), //
            new LongOpt("full", LongOpt.NO_ARGUMENT, null, 'f'), //
            new LongOpt("verbose", LongOpt.NO_ARGUMENT, null, 'v'), //
            new LongOpt("blacklist", LongOpt.REQUIRED_ARGUMENT, null, 'b') };

        Getopt getopt = new Getopt("discovery", args, sopts, lopts);
        int code;

        while ((code = getopt.getopt()) != -1) {
            switch (code) {
            case ':':
            case '?':
            case 1: {
                out.println(MSG.getMsg(AgentI18NResourceKeys.HELP_SYNTAX_LABEL, getSyntax()));
                return;
            }

            case 'p': {
                pluginName = getopt.getOptarg();
                break;
            }

            case 'i': {
                resourceId = Integer.valueOf(getopt.getOptarg());
                break;
            }

            case 'r': {
                resourceTypeName = getopt.getOptarg();
                break;
            }

            case 'f': {
                full = true;
                break;
            }

            case 'b': {
                String opt = getopt.getOptarg();
                InventoryManager inventoryManager = PluginContainer.getInstance().getInventoryManager();
                DiscoveryComponentProxyFactory factory = inventoryManager.getDiscoveryComponentProxyFactory();
                if (opt.equalsIgnoreCase("list")) {
                    HashSet<ResourceType> blacklist = factory.getResourceTypeBlacklist();
                    out.println(MSG.getMsg(AgentI18NResourceKeys.DISCOVERY_BLACKLIST_LIST, blacklist));
                } else if (opt.equalsIgnoreCase("clear")) {
                    factory.clearResourceTypeBlacklist();
                    out.println(MSG.getMsg(AgentI18NResourceKeys.DISCOVERY_BLACKLIST_CLEAR));
                } else {
                    out.println(MSG.getMsg(AgentI18NResourceKeys.HELP_SYNTAX_LABEL, getSyntax()));
                }
                return;
            }

            case 'v': {
                verbose = true;
                break;
            }
            }
        }

        if ((getopt.getOptind() + 1) < args.length) {
            out.println(MSG.getMsg(AgentI18NResourceKeys.HELP_SYNTAX_LABEL, getSyntax()));
            return;
        }

        if (full) {
View Full Code Here

        String sopts = "dfv:";
        LongOpt[] lopts = { new LongOpt("verbose", LongOpt.REQUIRED_ARGUMENT, null, 'v'),
            new LongOpt("dump", LongOpt.NO_ARGUMENT, null, 'd'), new LongOpt("free", LongOpt.NO_ARGUMENT, null, 'f') };

        Getopt getopt = new Getopt(getPromptCommandString(), args, sopts, lopts);
        int code;
        Boolean verbose = null;
        Boolean free = null;
        Boolean dump = null;

        while ((code = getopt.getopt()) != -1) {
            switch (code) {
            case ':':
            case '?':
            case 1: {
                out.println(MSG.getMsg(AgentI18NResourceKeys.HELP_SYNTAX_LABEL, getSyntax()));
                break;
            }

            case 'v': {
                String verboseOpt = getopt.getOptarg();
                verbose = Boolean.valueOf(Boolean.parseBoolean(verboseOpt));
                break;
            }

            case 'd': {
                dump = Boolean.TRUE;
                break;
            }

            case 'f': {
                free = Boolean.TRUE;
                break;
            }
            }
        }

        if ((getopt.getOptind() + 1) < args.length) {
            out.println(MSG.getMsg(AgentI18NResourceKeys.HELP_SYNTAX_LABEL, getSyntax()));
        }

        if (Boolean.TRUE.equals(dump)) {
            printCurrentMemoryUsage(out);
View Full Code Here

        LongOpt[] lopts = { new LongOpt("changed", LongOpt.NO_ARGUMENT, null, 'c'),
            new LongOpt("verbose", LongOpt.NO_ARGUMENT, null, 'v'),
            new LongOpt("quiet", LongOpt.NO_ARGUMENT, null, 'q'),
            new LongOpt("force", LongOpt.NO_ARGUMENT, null, 'f') };

        Getopt getopt = new Getopt(getPromptCommandString(), args, sopts, lopts);
        int code;

        while ((code = getopt.getopt()) != -1) {
            switch (code) {
            case ':':
            case '?':
            case 1: {
                out.println(MSG.getMsg(AgentI18NResourceKeys.HELP_SYNTAX_LABEL, getSyntax()));
                return;
            }

            case 'c': {
                changedOnly = true;
                break;
            }

            case 'v': {
                verbose = true;
                break;
            }

            case 'q': {
                quiet = true;
                break;
            }

            case 'f': {
                force = true;
                break;
            }
            }
        }

        if (getopt.getOptind() < args.length) {
            // we got too many arguments on the command line
            out.println(MSG.getMsg(AgentI18NResourceKeys.HELP_SYNTAX_LABEL, getSyntax()));
            return;
        }
View Full Code Here

        boolean clean_token = false; // only used if clean_config = true
        boolean purge_data = false;
        boolean purge_plugins = false;
        AgentInputReaderFactory.ConsoleType console_type = null;

        Getopt getopt = new Getopt("agent", args, sopts, lopts);
        int code;

        while ((code = getopt.getopt()) != -1) {
            switch (code) {
            case ':':
            case '?': {
                // for now both of these should exit
                displayUsage();
                throw new IllegalArgumentException(MSG.getMsg(AgentI18NResourceKeys.BAD_ARGS));
            }

            case 1: {
                // this will catch non-option arguments (which we don't currently care about)
                System.err.println(MSG.getMsg(AgentI18NResourceKeys.UNUSED_OPTION, getopt.getOptarg()));
                break;
            }

            case 'h': {
                displayUsage();
                throw new HelpException(MSG.getMsg(AgentI18NResourceKeys.HELP_SHOWN));
            }

            case 'D': {
                // set a system property
                String sysprop = getopt.getOptarg();
                int i = sysprop.indexOf("=");
                String name;
                String value;

                if (i == -1) {
                    name = sysprop;
                    value = "true";
                } else {
                    name = sysprop.substring(0, i);
                    value = sysprop.substring(i + 1, sysprop.length());
                }

                System.setProperty(name, value);
                LOG.debug(AgentI18NResourceKeys.SYSPROP_SET, name, value);

                break;
            }

            case 'c': {
                config_file_name = getopt.getOptarg();
                break;
            }

            case 'l': {
                clean_config = true;
                purge_data = true;
                break;
            }

            case 'L': {
                clean_config = true;
                purge_data = true;
                clean_token = true;
                break;
            }

            case 'u': {
                purge_data = true;
                break;
            }

            case 'g': {
                purge_plugins = true;
                break;
            }

            case 'a': {
                m_advancedSetup = true;
                break;
            }

            case 's': {
                m_forcedSetup = true;
                break;
            }

            case 'n': {
                m_startAtBoot = false;
                break;
            }

            case 'p': {
                setConfigurationPreferencesNode(getopt.getOptarg());
                break;
            }

            case 'e': {
                console_type = AgentInputReaderFactory.ConsoleType.valueOf(getopt.getOptarg()); // throws IllegalArgumentException if invalid
                break;
            }

            case 'd': {
                m_daemonMode = true;
                break;
            }

            case 'i': {
                File script = new File(getopt.getOptarg());

                try {
                    m_input = AgentInputReaderFactory.create(this, script);
                    m_stdinInput = false;
                } catch (Exception e) {
                    throw new IllegalArgumentException(MSG.getMsg(AgentI18NResourceKeys.BAD_INPUT_FILE, script, e));
                }

                break;
            }

            case 'o': {
                File output = new File(getopt.getOptarg());

                try {
                    File parentDir = output.getParentFile();
                    if ((parentDir != null) && (!parentDir.exists())) {
                        parentDir.mkdirs();
View Full Code Here

            new LongOpt("log", LongOpt.REQUIRED_ARGUMENT, null, 'o'), // location of the log file
            new LongOpt("jar", LongOpt.REQUIRED_ARGUMENT, null, 'j'), // location of an external jar that has our agent
            new LongOpt("pause", LongOpt.OPTIONAL_ARGUMENT, null, 'p'), // pause (sleep) before updating
            new LongOpt("script", LongOpt.REQUIRED_ARGUMENT, null, 's') }; // switch immediately to the given server

        Getopt getopt = new Getopt(AgentUpdate.class.getSimpleName(), args, sopts, lopts);
        int code;
        long pause = -1L;

        while ((code = getopt.getopt()) != -1) {
            switch (code) {
            case ':':
            case '?':
            case 1: {
                throw new IllegalArgumentException("Bad argument!");
            }

            case 'u': {
                this.updateFlag = true;
                String value = getopt.getOptarg();
                if (value != null) {
                    this.oldAgentHomeArgument = value;
                }

                // make sure the directory actually exists
                File agentHome = new File(this.oldAgentHomeArgument);
                if (!agentHome.exists() || !agentHome.isDirectory()) {
                    throw new FileNotFoundException("There is no agent located at: " + this.oldAgentHomeArgument);
                }

                // be nice for the user - if the user specified the agent's parent directory,
                // then set the old agent home argument to the "real" agent home directory
                File possibleHomeDir = new File(agentHome, "rhq-agent");
                if (possibleHomeDir.exists() && possibleHomeDir.isDirectory()) {
                    agentHome = possibleHomeDir;
                }

                // make it an absolute path
                this.oldAgentHomeArgument = agentHome.getAbsolutePath();
                break;
            }

            case 'i': {
                this.installFlag = true;
                String value = getopt.getOptarg();
                if (value != null) {
                    this.newAgentHomeParentArgument = value;
                }
                break;
            }

            case 'q': {
                this.quietFlag = true;
                break;
            }

            case 'h': {
                throw new UnsupportedOperationException();
            }

            case 'v': {
                this.showVersion = true;
                break;
            }

            case 'o': {
                this.logFileArgument = getopt.getOptarg();
                break;
            }

            case 'l': {
                this.launchFlag = Boolean.valueOf(Boolean.parseBoolean(getopt.getOptarg()));
                break;
            }

            case 'j': {
                this.jarFileArgument = getopt.getOptarg();
                File jarFile = new File(this.jarFileArgument);
                if (!jarFile.exists() || !jarFile.isFile()) {
                    throw new FileNotFoundException("There is no agent jar located at: " + this.jarFileArgument);
                }
                break;
            }

            case 'p': {
                pause = 30000L;
                String value = getopt.getOptarg();
                if (value != null) {
                    try {
                        pause = Long.parseLong(value);
                    } catch (Exception e) {
                        pause = 30000L;
                    }
                }

                break;
            }
            case 's': {
                this.scriptFileArgument = getopt.getOptarg();
                break;
            }
            }
        }

        if (getopt.getOptind() < args.length) {
            throw new IllegalArgumentException("Bad arguments.");
        }

        if (this.showVersion) {
            return; // do not continue, this will exit the VM after showing the version info
View Full Code Here

TOP

Related Classes of gnu.getopt.Getopt

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.