code: private static CommandLine parseArgs(Options opts, String ...args) 
    throws IllegalArgumentException{
  
      OptionBuilder.withArgName("NameNode|DataNode");
      OptionBuilder.hasArg();
      OptionBuilder.withDescription("specify jmx service (NameNode by default)");
      Option jmx_service = OptionBuilder.create("service");
  
      OptionBuilder.withArgName("mbean server");
      OptionBuilder.hasArg();
      OptionBuilder.withDescription("specify mbean server (localhost by default)");
      Option jmx_server = OptionBuilder.create("server");
  
      OptionBuilder.withDescription("print help");
      Option jmx_help = OptionBuilder.create("help");
  
      OptionBuilder.withArgName("mbean server port");
      OptionBuilder.hasArg();
      OptionBuilder.withDescription("specify mbean server port, " +
      "if missing - it will try to connect to MBean Server in the same VM");
      Option jmx_port = OptionBuilder.create("port");
  
      OptionBuilder.withArgName("VM's pid");
      OptionBuilder.hasArg();
      OptionBuilder.withDescription("connect to the VM on the same machine");
      Option jmx_localVM = OptionBuilder.create("localVM");
  
      opts.addOption(jmx_server);
      opts.addOption(jmx_help);
      opts.addOption(jmx_service);
      opts.addOption(jmx_port);
      opts.addOption(jmx_localVM);
  
      CommandLine commandLine=null;
      CommandLineParser parser = new GnuParser();
      try {
        commandLine = parser.parse(opts, args, true);
      } catch(ParseException e) {
        printUsage(opts);
        throw new IllegalArgumentException("invalid args: " + e.getMessage()); 
      }
      return commandLine;
    }
docs: parse args