Package gnu.getopt

Examples of gnu.getopt.Getopt


            new LongOpt("version", LongOpt.REQUIRED_ARGUMENT, null, 'v'),
            new LongOpt("class", LongOpt.REQUIRED_ARGUMENT, null, 'l'),
            new LongOpt("uri", LongOpt.REQUIRED_ARGUMENT, null, 'u'),
            new LongOpt("subsystem", LongOpt.REQUIRED_ARGUMENT, null, 's'), };

        Getopt getopt = new Getopt(programName, args, sopts, lopts);
        int code;
        String arg;

        while ((code = getopt.getopt()) != -1) {
            switch (code) {
            case ':':
            case '?': {
                // for now both of these should exit with error status
                return 1;
            }

            case 1: {
                // this will catch non-option arguments (which will be command params for a particular command)
                arg = getopt.getOptarg();
                String paramName;
                String paramValue;
                int i = arg.indexOf("=");
                if (i == -1) {
                    paramName = arg;
                    paramValue = "true";
                } else {
                    paramName = arg.substring(0, i);
                    paramValue = arg.substring(i + 1, arg.length());
                }

                // add the parameter to the returned map
                m_params.put(paramName, paramValue);

                LOG.debug(CommI18NResourceKeys.CMDLINE_CLIENT_CMDLINE_PARAM, programName, paramName, ((!"password"
                    .equalsIgnoreCase(paramName)) ? paramValue : "*"));

                break;
            }

            case 'h': {
                // show command line help
                System.out.println(programName + " " + getUsage());
                System.out.println();
                return 0;
            }

            case 'v': {
                arg = getopt.getOptarg();

                try {
                    m_commandVersion = new Integer(arg).intValue();
                } catch (NumberFormatException nfe) {
                    LOG.error(CommI18NResourceKeys.CMDLINE_CLIENT_INVALID_CMD_VERSION, arg, nfe);
                    return 1;
                }

                break;
            }

            case 'p': {
                arg = getopt.getOptarg();
                LOG.debug(CommI18NResourceKeys.CMDLINE_CLIENT_PACKAGES, programName, arg);

                ArrayList<String> packageList = new ArrayList<String>();
                StringTokenizer strtok = new StringTokenizer(arg, ":");
                while (strtok.hasMoreTokens()) {
                    packageList.add(strtok.nextToken());
                }

                m_packages = packageList.toArray(new String[packageList.size()]);

                break;
            }

            case 'l': {
                m_classname = getopt.getOptarg();

                LOG.debug(CommI18NResourceKeys.CMDLINE_CLIENT_CLASSNAME, programName, m_classname);

                break;
            }

            case 'c': {
                m_commandName = getopt.getOptarg();

                LOG.debug(CommI18NResourceKeys.CMDLINE_CLIENT_COMMAND, programName, m_commandName);

                break;
            }

            case 'u': {
                m_locatorUri = getopt.getOptarg();

                LOG.debug(CommI18NResourceKeys.CMDLINE_CLIENT_LOCATOR_URI, programName, m_locatorUri);

                break;
            }

            case 's': {
                m_subsystem = getopt.getOptarg();

                LOG.debug(CommI18NResourceKeys.CMDLINE_CLIENT_SUBSYSTEM, programName, m_subsystem);

                break;
            }
View Full Code Here


        String shortOpts = "-:f:";
        LongOpt[] longOpts = {
                new LongOpt("file", LongOpt.REQUIRED_ARGUMENT, null, 'f'),
                new LongOpt("args-style", LongOpt.REQUIRED_ARGUMENT, null, -2)
        };
        Getopt getopt = new Getopt("exec", args, shortOpts, longOpts, false);

        List<String> scriptArgs = new ArrayList<String>();
        String argStyle = "indexed";
        String scriptName = null;

        int code = getopt.getopt();
        while (code != -1) {
            switch (code) {
                case ':':
                case '?':
                   throw new IllegalArgumentException("Invalid options");
                case 1:
                    scriptArgs.add(getopt.getOptarg());
                    break;
                case 'f':
                    scriptName = getopt.getOptarg();
                    break;
                case -2://ClientMain.ARGS_STYLE_ID:
                    argStyle = getopt.getOptarg();
                    if (isInvalidArgStyle(argStyle)) {
                        throw new CommandLineParseException(argStyle + " - invalid value for style option");
                    }
                    break;
            }
            code = getopt.getopt();
        }

        return createScriptCmdLine(scriptName, argStyle, scriptArgs);
    }
View Full Code Here

        longOptions[6] = new LongOpt("import", LongOpt.NO_ARGUMENT, null, 'i');
        longOptions[7] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h');
        longOptions[8] = new LongOpt("file", LongOpt.REQUIRED_ARGUMENT, null, 'f');
        longOptions[9] = new LongOpt("format", LongOpt.REQUIRED_ARGUMENT, null, 'o');
       
        Getopt options = new Getopt("data", args, "eihr:u:p:d:c:f:o:", longOptions);
       
        String url = null;
        String user = null;
        String password = null;
        String driverClass = null;
        String configFile = null;
        String ioFileName = null;
        String format = "xml";
        boolean doExport = false;
        boolean doImport = false;
        List<String> entities = new ArrayList<String>();
       
        int option;
        while ((option = options.getopt()) != -1) {
            switch (option) {
            case 'r':
                url = options.getOptarg();
                break;
            case 'u':
                user = options.getOptarg();
                break;
            case 'p':
                password = options.getOptarg();
                break;
            case 'd':
                driverClass = options.getOptarg();
                break;
            case 'c':
                configFile = options.getOptarg();
                break;
            case 'e':
                doExport = true;
                break;
            case 'i':
                doImport = true;
                break;
            case 'h':
                usage();
                break;
            case 'f':
                ioFileName = options.getOptarg();
                break;
            case 'o':
                format = options.getOptarg();
                break;
               
            }
        }
       
        for (int i = options.getOptind(); i < args.length; i++) {
            entities.add(args[i]);
        }
       
        Properties settings = new Properties();
        putNotNull(settings, "url", url);
View Full Code Here

            new LongOpt("file", LongOpt.NO_ARGUMENT, null, 'f'),
            new LongOpt("version", LongOpt.NO_ARGUMENT, null, 'v'),
            new LongOpt("language", LongOpt.REQUIRED_ARGUMENT, null, 'l'),
            new LongOpt("args-style", LongOpt.REQUIRED_ARGUMENT, null, -2) };

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

        List<String> execCmdLine = new ArrayList<String>();
        execCmdLine.add("exec");

        while ((code = getopt.getopt()) != -1) {
            switch (code) {
            case ':':
            case '?': {
                config.invalidArgs = true;
                break;
            }

            case 1: {
                // this catches non-option arguments which can be passed when running a script in non-interactive mode
                // with -f or running a single command in non-interactive mode with -c.
                execCmdLine.add(getopt.getOptarg());
                break;
            }

            case 'h': {
                config.displayUsage = true;
                break;
            }

            case 'u': {
                this.user = getopt.getOptarg();
                break;
            }
            case 'p': {
                this.pass = getopt.getOptarg();
                break;
            }
            case 'P': {
                config.askForPassword = true;
                break;
            }
            case 'c': {
                interactiveMode = false;
                execCmdLine.add(getopt.getOptarg());
                break;
            }
            case 'f': {
                interactiveMode = false;
                execCmdLine.add("-f");
                execCmdLine.add(getopt.getOptarg());
                break;
            }
            case -2: {
                execCmdLine.add("--args-style=" + getopt.getOptarg());
                break;
            }
            case 's': {
                setHost(getopt.getOptarg());
                break;
            }
            case 'r': {
                setTransport(getopt.getOptarg());
                break;
            }
            case 't': {
                String portArg = getopt.getOptarg();
                try {
                    setPort(Integer.parseInt(portArg));
                } catch (Exception e) {
                    outputWriter.println("Invalid port [" + portArg + "]");
                    System.exit(1);
                }
                break;
            }
            case 'v': {
                config.showDetailedVersion = true;
                if (args.length == 1) {
                    config.showVersionAndExit = true;
                }
                break;
            }
            case 'l':
                this.language = getopt.getOptarg();
                break;
            }
        }

        if (!interactiveMode) {
View Full Code Here

                new LongOpt("file", LongOpt.REQUIRED_ARGUMENT, null, 'f'),
                new LongOpt("append", LongOpt.OPTIONAL_ARGUMENT, null, 'a'),
                new LongOpt("start", LongOpt.OPTIONAL_ARGUMENT, null, 'b'),
                new LongOpt("end", LongOpt.OPTIONAL_ARGUMENT, null, 'e')
        };
        Getopt getopt = new Getopt(getPromptCommandString(), args, shortOpts, longOpts);
        getopt.setOpterr(false);

        RecordArgs recordArgs = new RecordArgs();

        int code = getopt.getopt();
        while (code != -1) {
            switch (code) {
                case ':':
                case '?':
                   throw new CommandLineParseException("Invalid option");
                case 1:
                    break;
                case 'f':
                    recordArgs.file = new File(getopt.getOptarg());
                    break;
                case 'a':
                    recordArgs.append = true;
                    break;
                case 'b':
                    if (recordArgs.recordState == null) {
                        recordArgs.recordState = RecordState.START;
                    }
                    break;
                case 'e':
                    if (recordArgs.recordState == null) {
                        recordArgs.recordState = RecordState.STOP;
                    }
                    break;
            }
            code = getopt.getopt();
        }

        if (recordArgs.file == null) {
            throw new CommandLineParseException("The file option must be specified.");
        }
View Full Code Here

    private VersionArgs parseArgs(String[] args) {
        String shortOpts = "-:v";
        LongOpt[] longOpts = {
            new LongOpt("verbose", LongOpt.OPTIONAL_ARGUMENT, null, 'v')
        };
        Getopt getopt = new Getopt(getPromptCommandString(), args, shortOpts, longOpts);
        getopt.setOpterr(false);

        VersionArgs versionArgs = new VersionArgs();

        int code = getopt.getopt();
        while (code != -1) {
            switch (code) {
                case ':':
                    throw new CommandLineParseException("Invalid option");
                case 'v':
                    versionArgs.verbose = true;
                    break;
            }
            code = getopt.getopt();
        }

        return versionArgs;
    }
View Full Code Here

            new LongOpt("destination", LongOpt.REQUIRED_ARGUMENT, null, 'd') };

        String source = null;
        String destination = null;

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

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

            case 1: {
                throw new IllegalArgumentException("Bad recipe command!");
            }

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

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

            default: {
                throw new IllegalArgumentException("Unexpected error in recipe command");
View Full Code Here

        String sopts = ":f:";
        LongOpt[] lopts = { new LongOpt("file", LongOpt.REQUIRED_ARGUMENT, null, 'f') };

        String file = null;

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

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

            case 1: {
                throw new IllegalArgumentException("Bad recipe command!");
            }

            case 'f': {
                file = getopt.getOptarg();
                break;
            }

            default: {
                throw new IllegalArgumentException("Unexpected error in recipe command");
View Full Code Here

            new LongOpt("directory", LongOpt.REQUIRED_ARGUMENT, null, 'd') };

        String filename = null;
        String directory = null;

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

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

            case 1: {
                throw new IllegalArgumentException("Bad recipe command!");
            }

            case 'f': {
                filename = getopt.getOptarg();
                break;
            }

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

            default: {
                throw new IllegalArgumentException("Unexpected error in recipe command");
View Full Code Here

            new LongOpt("name", LongOpt.REQUIRED_ARGUMENT, null, 'n') };

        String replacementVariableName = null;
        String defaultValue = null;

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

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

            case 1: {
                throw new IllegalArgumentException("Bad recipe command!");
            }

            case 'n': {
                replacementVariableName = getopt.getOptarg();
                break;
            }

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

            default: {
                throw new IllegalArgumentException("Unexpected error in recipe command");
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.