📄 getopt.java
字号:
nextchar = ""; if (pfound.flag != null) { pfound.flag.setLength(0); pfound.flag.append(pfound.val); return(0); } return(pfound.val); } // if (pfound != null) longopt_handled = false; return(0);}/**************************************************************************//** * This method returns a char that is the current option that has been * parsed from the command line. If the option takes an argument, then * the internal variable 'optarg' is set which is a String representing * the the value of the argument. This value can be retrieved by the * caller using the getOptarg() method. If an invalid option is found, * an error message is printed and a '?' is returned. The name of the * invalid option character can be retrieved by calling the getOptopt() * method. When there are no more options to be scanned, this method * returns -1. The index of first non-option element in argv can be * retrieved with the getOptind() method. * * @return Various things as described above */public intgetopt(){ optarg = null; if ((nextchar == null) || (nextchar.equals(""))) { // If we have just processed some options following some non-options, // exchange them so that the options come first. if (last_nonopt > optind) last_nonopt = optind; if (first_nonopt > optind) first_nonopt = optind; if (ordering == PERMUTE) { // If we have just processed some options following some non-options, // exchange them so that the options come first. if ((first_nonopt != last_nonopt) && (last_nonopt != optind)) exchange(argv); else if (last_nonopt != optind) first_nonopt = optind; // Skip any additional non-options // and extend the range of non-options previously skipped. while ((optind < argv.length) && (argv[optind].equals("") || (argv[optind].charAt(0) != '-') || argv[optind].equals("-"))) { optind++; } last_nonopt = optind; } // The special ARGV-element `--' means premature end of options. // Skip it like a null option, // then exchange with previous non-options as if it were an option, // then skip everything else like a non-option. if ((optind != argv.length) && argv[optind].equals("--")) { optind++; if ((first_nonopt != last_nonopt) && (last_nonopt != optind)) exchange (argv); else if (first_nonopt == last_nonopt) first_nonopt = optind; last_nonopt = argv.length; optind = argv.length; } // If we have done all the ARGV-elements, stop the scan // and back over any non-options that we skipped and permuted. if (optind == argv.length) { // Set the next-arg-index to point at the non-options // that we previously skipped, so the caller will digest them. if (first_nonopt != last_nonopt) optind = first_nonopt; return(-1); } // If we have come to a non-option and did not permute it, // either stop the scan or describe it to the caller and pass it by. if (argv[optind].equals("") || (argv[optind].charAt(0) != '-') || argv[optind].equals("-")) { if (ordering == REQUIRE_ORDER) return(-1); optarg = argv[optind++]; return(1); } // We have found another option-ARGV-element. // Skip the initial punctuation. if (argv[optind].startsWith("--")) nextchar = argv[optind].substring(2); else nextchar = argv[optind].substring(1); } // Decode the current option-ARGV-element. /* Check whether the ARGV-element is a long option. If long_only and the ARGV-element has the form "-f", where f is a valid short option, don't consider it an abbreviated form of a long option that starts with f. Otherwise there would be no way to give the -f short option. On the other hand, if there's a long option "fubar" and the ARGV-element is "-fu", do consider that an abbreviation of the long option, just like "--fu", and not "-f" with arg "u". This distinction seems to be the most useful approach. */ if ((long_options != null) && (argv[optind].startsWith("--") || (long_only && ((argv[optind].length() > 2) || (optstring.indexOf(argv[optind].charAt(1)) == -1))))) { int c = checkLongOption(); if (longopt_handled) return(c); // Can't find it as a long option. If this is not getopt_long_only, // or the option starts with '--' or is not a valid short // option, then it's an error. // Otherwise interpret it as a short option. if (!long_only || argv[optind].startsWith("--") || (optstring.indexOf(nextchar.charAt(0)) == -1)) { if (opterr) { if (argv[optind].startsWith("--")) { Object[] msgArgs = { progname, nextchar }; System.err.println(MessageFormat.format( _messages.getString("getopt.unrecognized"), msgArgs)); } else { Object[] msgArgs = { progname, new Character(argv[optind].charAt(0)).toString(), nextchar }; System.err.println(MessageFormat.format( _messages.getString("getopt.unrecognized2"), msgArgs)); } } nextchar = ""; ++optind; optopt = 0; return('?'); } } // if (longopts) // Look at and handle the next short option-character */ int c = nextchar.charAt(0); //**** Do we need to check for empty str? if (nextchar.length() > 1) nextchar = nextchar.substring(1); else nextchar = ""; String temp = null; if (optstring.indexOf(c) != -1) temp = optstring.substring(optstring.indexOf(c)); if (nextchar.equals("")) ++optind; if ((temp == null) || (c == ':')) { if (opterr) { if (posixly_correct) { // 1003.2 specifies the format of this message Object[] msgArgs = { progname, new Character((char)c).toString() }; System.err.println(MessageFormat.format( _messages.getString("getopt.illegal"), msgArgs)); } else { Object[] msgArgs = { progname, new Character((char)c).toString() }; System.err.println(MessageFormat.format( _messages.getString("getopt.invalid"), msgArgs)); } } optopt = c; return('?'); } // Convenience. Treat POSIX -W foo same as long option --foo if ((temp.charAt(0) == 'W') && (temp.length() > 1) && (temp.charAt(1) == ';')) { if (!nextchar.equals("")) { optarg = nextchar; } // No further cars in this argv element and no more argv elements else if (optind == argv.length) { if (opterr) { // 1003.2 specifies the format of this message. Object[] msgArgs = { progname, new Character((char)c).toString() }; System.err.println(MessageFormat.format( _messages.getString("getopt.requires2"), msgArgs)); } optopt = c; if (optstring.charAt(0) == ':') return(':'); else return('?'); } else { // We already incremented `optind' once; // increment it again when taking next ARGV-elt as argument. nextchar = argv[optind]; optarg = argv[optind]; } c = checkLongOption(); if (longopt_handled) return(c); else // Let the application handle it { nextchar = null; ++optind; return('W'); } } if ((temp.length() > 1) && (temp.charAt(1) == ':')) { if ((temp.length() > 2) && (temp.charAt(2) == ':')) // This is an option that accepts and argument optionally { if (!nextchar.equals("")) { optarg = nextchar; ++optind; } else { optarg = null; } nextchar = null; } else { if (!nextchar.equals("")) { optarg = nextchar; ++optind; } else if (optind == argv.length) { if (opterr) { // 1003.2 specifies the format of this message Object[] msgArgs = { progname, new Character((char)c).toString() }; System.err.println(MessageFormat.format( _messages.getString("getopt.requires2"), msgArgs)); } optopt = c; if (optstring.charAt(0) == ':') return(':'); else return('?'); } else { optarg = argv[optind]; ++optind; } nextchar = null; } } return(c);}} // Class Getopt
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -