< prev index next >

src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/SAGetopt.java

Print this page




  38 
  39     public SAGetopt(String[] args) {
  40         _argv  = args.clone();
  41         _optind   = 0;
  42         _optopt   = 1;
  43         _optarg   = null;
  44         _optreset = true;
  45     }
  46 
  47     public String getOptarg() {
  48         return _optarg;
  49     }
  50 
  51     public int getOptind() {
  52         return _optind;
  53     }
  54 
  55     private void extractOptarg(String opt) {
  56         // Argument expected
  57         if (_optind > _argv.length) {
  58             throw new RuntimeException("Not enough arguments for '" + opt + "'");
  59         }
  60 
  61         if (! _argv[_optind].isEmpty() && _argv[_optind].charAt(0) == '-') {
  62             throw new RuntimeException("Argument is expected for '" + opt + "'");
  63         }
  64 
  65         _optarg = _argv[_optind];
  66         _optind += 1;
  67     }
  68 
  69     private String processLongOptions(String carg, String[] longOptStr) {
  70         List<String> los = Arrays.asList(longOptStr);
  71         String[] ca = carg.split("=", 2);
  72 
  73         if (los.contains(ca[0])) {
  74             if (ca.length > 1) {
  75                 throw new RuntimeException("Argument is not expected for '" + ca[0] + "'");
  76             }
  77             return carg;
  78         }
  79 
  80         if (los.contains(ca[0] + "=")) {
  81             if (ca.length > 1) {
  82                 // GNU style options --file=name
  83                 _optarg = ca[1];
  84             }
  85             else {
  86                 // Mixed style options --file name
  87                 try {
  88                     extractOptarg(ca[0]);
  89                 } catch (ArrayIndexOutOfBoundsException e) {
  90                     throw new RuntimeException("Argument is expected for '" + ca[0] + "'");
  91                 }
  92             }
  93 
  94             return ca[0];
  95         }
  96 
  97         throw new RuntimeException("Invalid option '" + ca[0] + "'");
  98     }
  99 
 100     public String next(String optStr, String[] longOptStr) {
 101 
 102         if (_optind >= _argv.length || _argv[_optind] == null) {
 103             // All arguments processed
 104             return null;
 105         }
 106 
 107         String carg = _argv[_optind];
 108         _optarg = null;
 109 
 110         if (_optreset) {
 111             // End of option batch like '-abc' reached, expect option to start from '-'
 112 
 113             if (carg.isEmpty() || carg.charAt(0) != '-' || carg.equals("--")) {
 114                 // Stop processing on -- or first non-option argument;
 115                 return null;
 116             }
 117 


 131             if (optStr == null || optStr.length() == 0) {
 132                 // No short options
 133                 return null;
 134             }
 135 
 136             // At this point carg[0] contains '-'
 137             _optreset = false;
 138             _optopt = 1;
 139         }
 140 
 141         char ch = carg.charAt(_optopt);
 142 
 143         // adjust pointer to next character
 144         _optopt += 1;
 145 
 146         // Okay, ready to process options like
 147         // -abc -d bla -ef
 148 
 149         int chIndex = optStr.indexOf(ch);
 150         if (chIndex == -1) {
 151             throw new RuntimeException("Invalid option '" + ch + "'");
 152         }
 153 
 154         if (_optopt >= carg.length()) {
 155             _optind += 1;
 156             _optreset = true;
 157         }
 158 
 159         if (chIndex < optStr.length()-1 && optStr.charAt(chIndex+1) == ':') {
 160             // Argument expected
 161             extractOptarg(String.valueOf(ch));
 162         }
 163 
 164         return String.valueOf(ch);
 165     }
 166 }


  38 
  39     public SAGetopt(String[] args) {
  40         _argv  = args.clone();
  41         _optind   = 0;
  42         _optopt   = 1;
  43         _optarg   = null;
  44         _optreset = true;
  45     }
  46 
  47     public String getOptarg() {
  48         return _optarg;
  49     }
  50 
  51     public int getOptind() {
  52         return _optind;
  53     }
  54 
  55     private void extractOptarg(String opt) {
  56         // Argument expected
  57         if (_optind > _argv.length) {
  58             throw new SAGetoptException("Not enough arguments for '" + opt + "'");
  59         }
  60 
  61         if (! _argv[_optind].isEmpty() && _argv[_optind].charAt(0) == '-') {
  62             throw new SAGetoptException("Argument is expected for '" + opt + "'");
  63         }
  64 
  65         _optarg = _argv[_optind];
  66         _optind += 1;
  67     }
  68 
  69     private String processLongOptions(String carg, String[] longOptStr) {
  70         List<String> los = Arrays.asList(longOptStr);
  71         String[] ca = carg.split("=", 2);
  72 
  73         if (los.contains(ca[0])) {
  74             if (ca.length > 1) {
  75                 throw new SAGetoptException("Argument is not expected for '" + ca[0] + "'");
  76             }
  77             return carg;
  78         }
  79 
  80         if (los.contains(ca[0] + "=")) {
  81             if (ca.length > 1) {
  82                 // GNU style options --file=name
  83                 _optarg = ca[1];
  84             }
  85             else {
  86                 // Mixed style options --file name
  87                 try {
  88                     extractOptarg(ca[0]);
  89                 } catch (ArrayIndexOutOfBoundsException e) {
  90                     throw new SAGetoptException("Argument is expected for '" + ca[0] + "'");
  91                 }
  92             }
  93 
  94             return ca[0];
  95         }
  96 
  97         throw new SAGetoptException("Invalid option '" + ca[0] + "'");
  98     }
  99 
 100     public String next(String optStr, String[] longOptStr) {
 101 
 102         if (_optind >= _argv.length || _argv[_optind] == null) {
 103             // All arguments processed
 104             return null;
 105         }
 106 
 107         String carg = _argv[_optind];
 108         _optarg = null;
 109 
 110         if (_optreset) {
 111             // End of option batch like '-abc' reached, expect option to start from '-'
 112 
 113             if (carg.isEmpty() || carg.charAt(0) != '-' || carg.equals("--")) {
 114                 // Stop processing on -- or first non-option argument;
 115                 return null;
 116             }
 117 


 131             if (optStr == null || optStr.length() == 0) {
 132                 // No short options
 133                 return null;
 134             }
 135 
 136             // At this point carg[0] contains '-'
 137             _optreset = false;
 138             _optopt = 1;
 139         }
 140 
 141         char ch = carg.charAt(_optopt);
 142 
 143         // adjust pointer to next character
 144         _optopt += 1;
 145 
 146         // Okay, ready to process options like
 147         // -abc -d bla -ef
 148 
 149         int chIndex = optStr.indexOf(ch);
 150         if (chIndex == -1) {
 151             throw new SAGetoptException("Invalid option '" + ch + "'");
 152         }
 153 
 154         if (_optopt >= carg.length()) {
 155             _optind += 1;
 156             _optreset = true;
 157         }
 158 
 159         if (chIndex < optStr.length()-1 && optStr.charAt(chIndex+1) == ':') {
 160             // Argument expected
 161             extractOptarg(String.valueOf(ch));
 162         }
 163 
 164         return String.valueOf(ch);
 165     }
 166 }
< prev index next >