Package gnu.getopt

Examples of gnu.getopt.Getopt


        String name = null;
        String version = null;
        String description = 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': {
                name = getopt.getOptarg();
                break;
            }

            case 'v': {
                version = getopt.getOptarg();
                break;
            }

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

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


    private void processJBossArgs(String currentWorkingDir, String args[]) {
        String programName = this.sysProps.getProperty("program.name", "jboss");
        String shortOpts = "-:b:c:D:P:";
        LongOpt longOpts[] = { new LongOpt("configuration", 1, null, 'c'), new LongOpt("properties", 1, null, 'P'),
            new LongOpt("host", 1, null, 'b'), };
        Getopt options = new Getopt(programName, args, shortOpts, longOpts);
        // Tell Getopt not to complain to stderr about unrecognized options...
        options.setOpterr(false);
        int c;
        while ((c = options.getopt()) != -1) {
            switch (c) {
            case 'b': // 'b' (--host)
            {
                String arg = options.getOptarg();
                this.sysProps.setProperty(JBossProperties.BIND_ADDRESS, arg);
                break;
            }

            case 'c': // 'c' (--configuration)
            {
                String arg = options.getOptarg();
                this.sysProps.setProperty(JBossProperties.SERVER_NAME, arg);
                break;
            }

            case 'D': // 'D'
            {
                String arg = options.getOptarg();
                String name = addPropArgToProps(arg, this.sysProps);
                String value = this.sysProps.getProperty(name);
                if (value.equals("")) {
                    // unlike the JVM, org.jboss.Main interprets -Dfoo as foo="true", rather than as foo=""
                    this.sysProps.setProperty(name, Boolean.TRUE.toString());
                }
                break;
            }

            case 'P': // 'P' (--properties)
            {
                String arg = options.getOptarg();

                // cwd can be null if native support (i.e. SIGAR) is not able to determine it.
                if (!new File(arg).isAbsolute() && currentWorkingDir == null) {
                    LOG.error("Could not determine current working directory. Failed to parse relative path argument to --properties option: "
                        + options.getOptarg());
                    break;
                }

                URL url;
                try {
                    File workingDir = new File(currentWorkingDir);
                    url = JBossConfigurationUtility.makeURL(arg, workingDir);
                } catch (Exception e) {
                    LOG.error("Failed to parse argument to --properties option: " + options.getOptarg() + ": " + e);
                    break;
                }
                Properties props = new Properties();
                InputStream inputStream = null;
                try {
View Full Code Here

    private void processJBossArgs(String currentWorkingDir, String args[]) {
        String programName = this.sysProps.getProperty("program.name", "jboss");
        String shortOpts = "-:b:c:D:P:";
        LongOpt longOpts[] = { new LongOpt("configuration", 1, null, 'c'), new LongOpt("properties", 1, null, 'P'),
            new LongOpt("host", 1, null, 'b'), };
        Getopt options = new Getopt(programName, args, shortOpts, longOpts);
        // Tell Getopt not to complain to stderr about unrecognized options...
        options.setOpterr(false);
        int c;
        while ((c = options.getopt()) != -1) {
            switch (c) {
            case 'b': // 'b' (--host)
            {
                String arg = options.getOptarg();
                this.sysProps.setProperty(JBossProperties.BIND_ADDRESS, arg);
                break;
            }

            case 'c': // 'c' (--configuration)
            {
                String arg = options.getOptarg();
                this.sysProps.setProperty(JBossProperties.SERVER_NAME, arg);
                break;
            }

            case 'D': // 'D'
            {
                String arg = options.getOptarg();
                String name = addPropArgToProps(arg, this.sysProps);
                String value = this.sysProps.getProperty(name);
                if (value.equals("")) {
                    // unlike the JVM, org.jboss.Main interprets -Dfoo as foo="true", rather than as foo=""
                    this.sysProps.setProperty(name, Boolean.TRUE.toString());
                }
                break;
            }

            case 'P': // 'P' (--properties)
            {
                String arg = options.getOptarg();

                // cwd can be null if native support (i.e. SIGAR) is not able to determine it.
                if (!new File(arg).isAbsolute() && currentWorkingDir == null) {
                    log.error("Could not determine current working directory. Failed to parse relative path argument to --properties option: "
                        + options.getOptarg());
                    break;
                }

                URL url;
                try {
                    File workingDir = new File(currentWorkingDir);
                    url = JBossConfigurationUtility.makeURL(arg, workingDir);
                } catch (Exception e) {
                    log.error("Failed to parse argument to --properties option: " + options.getOptarg());
                    break;
                }
                Properties props = new Properties();
                InputStream inputStream = null;
                try {
View Full Code Here

        longOptions[3] = new LongOpt("username", LongOpt.OPTIONAL_ARGUMENT, null, 'u');
        longOptions[4] = new LongOpt("password", LongOpt.OPTIONAL_ARGUMENT, null, 'p');
        longOptions[5] = new LongOpt("jars", LongOpt.OPTIONAL_ARGUMENT, null, 'j');
        longOptions[6] = new LongOpt("persistence-unit", LongOpt.OPTIONAL_ARGUMENT, null, 's');

        Getopt options = new Getopt("iventory-serializer serializer", args, "d:i:c:u:p:j:s:", longOptions);

        String driverClass = "org.postgresql.Driver";
        String dialect = "org.hibernate.dialect.PostgreSQLDialect";
        String connectionUrl = null;
        String username = null;
        String password = null;
        String persistenceUnit = "rhqpu";
        List<String> jars = new ArrayList<String>();
        List<String> queries = new ArrayList<String>();

        int option;
        while ((option = options.getopt()) != -1) {
            switch (option) {
            case 'd':
                driverClass = options.getOptarg();
                break;
            case 'i':
                dialect = options.getOptarg();
                break;
            case 'c':
                connectionUrl = options.getOptarg();
                break;
            case 'u':
                username = options.getOptarg();
                break;
            case 'p':
                password = options.getOptarg();
                break;
            case 'j':
                jars.addAll(extractCommaSeparated(options.getOptarg()));
                break;
            case 's':
                persistenceUnit = options.getOptarg();
            }
        }

        for (int i = options.getOptind(); i < args.length; i++) {
            queries.add(args[i]);
        }

        if (driverClass == null || dialect == null || connectionUrl == null || queries.isEmpty()) {
            usage();
View Full Code Here

        longopts[3] = new LongOpt("puller-param", LongOpt.REQUIRED_ARGUMENT, null, 'p');
        longopts[4] = new LongOpt("icons-url", LongOpt.REQUIRED_ARGUMENT, null, 'i');
        longopts[5] = new LongOpt("loggeduser", LongOpt.REQUIRED_ARGUMENT, null, 'u');
        longopts[6] = new LongOpt("loggedpass", LongOpt.REQUIRED_ARGUMENT, null, 'w');
       
        Getopt g = new Getopt("nexsm", args, "+:hn:c:p:i:u:w:", longopts);
        //g.setOpterr(false); // We'll do our own error handling
       
        while ((c = g.getopt()) != -1)
        {
            switch (c)
            {
                case 'h':
                    printHelp();
                    System.exit(0);
                    break;
                   
                case 'n':
                    _param_nagiosbase = g.getOptarg();
                    break;
                   
                case 'c':
                    _param_defaultpuller = g.getOptarg();
                    break;
                   
                case 'p':
                    _param_defaultpullerparam = g.getOptarg();
                    break;
                   
                case 'i':
                    _param_icons_url = g.getOptarg();
                    break;

                case 'u':
                    _param_loggeduser = g.getOptarg();
                    break;
                   
                case 'w':
                    _param_loggedpass = g.getOptarg();
                    break;
                   
                case '?':
                    //Invalid parameter passed
                    printUsage();
View Full Code Here

            new LongOpt("quiet", LongOpt.NO_ARGUMENT, null, 'q'),
            new LongOpt("user", LongOpt.REQUIRED_ARGUMENT, null, 'u'),
            new LongOpt("password", LongOpt.REQUIRED_ARGUMENT, null, 'p'),
         };

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

      PROCESS_ARGUMENTS:

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

                 // non-option arguments
              case 1:
                 {
                    // create the command
                    commandName = getopt.getOptarg();
                    log.debug("Command name: " + commandName);

                    // pass the remaining arguments (if any) to the command for processing
                    int i = getopt.getOptind();

                    if (args.length > i)
                    {
                       commandArgs = new String[args.length - i];
                       System.arraycopy(args, i, commandArgs, 0, args.length - i);
                    }
                    else
                    {
                       commandArgs = new String[0];
                    }
                    // Log the command options
                    log.debug("Command arguments: " + Strings.join(commandArgs, ","));

                    // We are done, execute the command
                    break PROCESS_ARGUMENTS;
                 }

                 // show command line help
              case 'h':
                 displayHelp();
                 System.exit(0);
                 break; // for completeness

                 // Show command help
              case 'H':
                 commandName = getopt.getOptarg();
                 commandHelp = true;
                 break PROCESS_ARGUMENTS;

                 // help-commands
              case 0x1000:
                 twiddle.displayCommandList();
                 System.exit(0);
                 break; // for completeness

              case 'c':
                 // Try value as a URL
                 String props = getopt.getOptarg();
                 try
                 {
                    cmdProps = new URL(props);
                 }
                 catch (MalformedURLException e)
                 {
                    log.debug("Failed to use cmd props as url", e);
                    File path = new File(props);
                    if( path.exists() == false )
                    {
                       String msg = "Failed to locate command props: " + props
                          + " as URL or file";
                       throw new IllegalArgumentException(msg);
                    }
                    cmdProps = path.toURL();
                 }
                 break;
                 // set a system property
              case 'D':
                 {
                    String arg = getopt.getOptarg();
                    String name, value;
                    int i = arg.indexOf("=");
                    if (i == -1)
                    {
                       name = arg;
                       value = "true";
                    }
                    else
                    {
                       name = arg.substring(0, i);
                       value = arg.substring(i + 1, arg.length());
                    }
                    System.setProperty(name, value);
                    break;
                 }

                 // Set the JNDI server URL
              case 's':
                 twiddle.setServerURL(getopt.getOptarg());
                 break;

                 // Set the adapter JNDI name
              case 'a':
                 twiddle.setAdapterName(getopt.getOptarg());
                 break;
              case 'u':
                 String username = getopt.getOptarg();
                 SecurityAssociation.setPrincipal(new SimplePrincipal(username));
                 break;
              case 'p':
                 String password = getopt.getOptarg();
                 SecurityAssociation.setCredential(password);
                 break;

              // Enable quiet operations
              case 'q':
View Full Code Here

      LongOpt[] lopts =
      {
         new LongOpt("loader", LongOpt.OPTIONAL_ARGUMENT, null, 'l'),
      };

      Getopt getopt = new Getopt(null, args, sopts, lopts);
      getopt.setOpterr(false);
     
      int code;
      int argidx = 0;
     
      while ((code = getopt.getopt()) != -1)
      {
         switch (code)
         {
            case ':':
               throw new CommandException
                  ("Option requires an argument: "+ args[getopt.getOptind() - 1]);

            case '?':
               throw new CommandException
                  ("Invalid (or ambiguous) option: " + args[getopt.getOptind() - 1]);

            // non-option arguments
            case 1:
            {
               String arg = getopt.getOptarg();
              
               switch (argidx++) {
                  case 0:
                     className = arg;
                     log.debug("class name: " + className);
                     break;

                  case 1:
                  {
                     try {
                        objectName = new ObjectName(arg);
                        log.debug("mbean name: " + objectName);
                     }
                     catch (MalformedObjectNameException e) {
                        throw new CommandException("Invalid object name: " + arg);
                     }
                     break;
                  }
                 
                  default:
                     throw new CommandException("Unused argument: " + arg);
               }
               break;
            }

            // Set the loader name
            case 'l':
            {
               String arg = getopt.getOptarg();
              
               try {
                  loaderName = new ObjectName(arg);
                  log.debug("loader name: " + loaderName);
               }
View Full Code Here

      LongOpt[] lopts =
         {
            new LongOpt("noprefix", LongOpt.NO_ARGUMENT, null, 0x1000),
         };

      Getopt getopt = new Getopt(null, args, sopts, lopts);
      getopt.setOpterr(false);

      int code;
      int argidx = 0;

      while ((code = getopt.getopt()) != -1)
      {
         switch (code)
         {
            case ':':
               throw new CommandException
                  ("Option requires an argument: " + args[getopt.getOptind() - 1]);

            case '?':
               throw new CommandException
                  ("Invalid (or ambiguous) option: " + args[getopt.getOptind() - 1]);

            case 0x1000:
               break;
                 
               // non-option arguments
            case 1:
               {
                  String arg = getopt.getOptarg();

                  switch (argidx++)
                  {
                     case 0:
                        objectName = createObjectName(arg);
View Full Code Here

      LongOpt[] lopts =
      {
         // new LongOpt("count", LongOpt.NO_ARGUMENT, null, 'c'),
      };

      Getopt getopt = new Getopt(null, args, sopts, lopts);
      getopt.setOpterr(false);
     
      int code;
      int argidx = 0;
     
      while ((code = getopt.getopt()) != -1)
      {
         switch (code)
         {
            case ':':
               throw new CommandException
                  ("Option requires an argument: "+ args[getopt.getOptind() - 1]);

            case '?':
               throw new CommandException
                  ("Invalid (or ambiguous) option: " + args[getopt.getOptind() - 1]);

            // non-option arguments
            case 1:
            {
               String arg = getopt.getOptarg();
              
               switch (argidx++) {
                  default:
                     names.add(createObjectName(arg));
                     break;
View Full Code Here

        {
            new LongOpt( "count", LongOpt.NO_ARGUMENT, null, 'c' ),
            new LongOpt( "filter", LongOpt.REQUIRED_ARGUMENT, null, 'f' ),
        };

    Getopt getopt = new Getopt( null, args, sopts, lopts );
    getopt.setOpterr( false );

    int code;
    int argidx = 0;

    while ( ( code = getopt.getopt() ) != -1 ) {
      switch ( code ) {
        case ':':
          throw new CommandException
              ( "Option requires an argument: " + args[getopt.getOptind() - 1] );

        case '?':
          throw new CommandException
              ( "Invalid (or ambiguous) option: " + args[getopt.getOptind() - 1] );

          // non-option arguments
        case 1: {
          String arg = getopt.getOptarg();

          switch ( argidx++ ) {
            case 0:
              query = arg;
              log.debug( "query: " + query );
              break;

            default:
              throw new CommandException( "Unused argument: " + arg );
          }
          break;
        }

        // Show count
        case 'c':
          displayCount = true;
          break;

        case 'f':
          filter = getopt.getOptarg();
          log.debug( "filter: " + filter );
          break;
      }
    }
  }
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.