⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 getopt.java

📁 it is a basic java browser
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/**  * In GNU getopt, it is possible to change the string containg valid options  * on the fly because it is passed as an argument to getopt() each time.  In  * this version we do not pass the string on every call.  In order to allow  * dynamic option string changing, this method is provided.  *  * @param optstring The new option string to use  */public voidsetOptstring(String optstring){  if (optstring.length() == 0)    optstring = " ";  this.optstring = optstring;}/**************************************************************************//**  * optind it the index in ARGV of the next element to be scanned.  * This is used for communication to and from the caller  * and for communication between successive calls to `getopt'.  *  * When `getopt' returns -1, this is the index of the first of the  * non-option elements that the caller should itself scan.  *  * Otherwise, `optind' communicates from one call to the next  * how much of ARGV has been scanned so far.    */public intgetOptind(){  return(optind);}/**************************************************************************//**  * This method allows the optind index to be set manually.  Normally this  * is not necessary (and incorrect usage of this method can lead to serious  * lossage), but optind is a public symbol in GNU getopt, so this method   * was added to allow it to be modified by the caller if desired.  *  * @param optind The new value of optind  */public voidsetOptind(int optind){  this.optind = optind;}/**************************************************************************//**  * Since in GNU getopt() the argument vector is passed back in to the  * function every time, the caller can swap out argv on the fly.  Since  * passing argv is not required in the Java version, this method allows  * the user to override argv.  Note that incorrect use of this method can  * lead to serious lossage.  *  * @param argv New argument list  */public voidsetArgv(String[] argv){  this.argv = argv;}/**************************************************************************//**   * For communication from `getopt' to the caller.  * When `getopt' finds an option that takes an argument,  * the argument value is returned here.  * Also, when `ordering' is RETURN_IN_ORDER,  * each non-option ARGV-element is returned here.  * No set method is provided because setting this variable has no effect.  */public StringgetOptarg(){  return(optarg);}/**************************************************************************//**  * Normally Getopt will print a message to the standard error when an  * invalid option is encountered.  This can be suppressed (or re-enabled)  * by calling this method.  There is no get method for this variable   * because if you can't remember the state you set this to, why should I?  */public voidsetOpterr(boolean opterr){  this.opterr = opterr;}/**************************************************************************//**  * When getopt() encounters an invalid option, it stores the value of that  * option in optopt which can be retrieved with this method.  There is  * no corresponding set method because setting this variable has no effect.  */public intgetOptopt(){  return(optopt);}/**************************************************************************//**  * Returns the index into the array of long options (NOT argv) representing  * the long option that was found.  */public intgetLongind(){  return(longind);}/**************************************************************************//**  * Exchange the shorter segment with the far end of the longer segment.  * That puts the shorter segment into the right place.  * It leaves the longer segment in the right place overall,  * but it consists of two parts that need to be swapped next.  * This method is used by getopt() for argument permutation.  */protected voidexchange(String[] argv){  int bottom = first_nonopt;  int middle = last_nonopt;  int top = optind;  String tem;  while (top > middle && middle > bottom)    {      if (top - middle > middle - bottom)        {          // Bottom segment is the short one.           int len = middle - bottom;          int i;          // Swap it with the top part of the top segment.           for (i = 0; i < len; i++)            {              tem = argv[bottom + i];              argv[bottom + i] = argv[top - (middle - bottom) + i];              argv[top - (middle - bottom) + i] = tem;            }          // Exclude the moved bottom segment from further swapping.           top -= len;        }      else        {          // Top segment is the short one.          int len = top - middle;          int i;          // Swap it with the bottom part of the bottom segment.           for (i = 0; i < len; i++)            {              tem = argv[bottom + i];              argv[bottom + i] = argv[middle + i];              argv[middle + i] = tem;            }          // Exclude the moved top segment from further swapping.           bottom += len;        }    }  // Update records for the slots the non-options now occupy.   first_nonopt += (optind - last_nonopt);  last_nonopt = optind;}/**************************************************************************//**  * Check to see if an option is a valid long option.  Called by getopt().  * Put in a separate method because this needs to be done twice.  (The  * C getopt authors just copy-pasted the code!).  *  * @param longind A buffer in which to store the 'val' field of found LongOpt  *  * @return Various things depending on circumstances  */protected intcheckLongOption(){  LongOpt pfound = null;  int nameend;  boolean ambig;  boolean exact;    longopt_handled = true;  ambig = false;  exact = false;  longind = -1;  nameend = nextchar.indexOf("=");  if (nameend == -1)    nameend = nextchar.length();    // Test all lnog options for either exact match or abbreviated matches  for (int i = 0; i < long_options.length; i++)    {      if (long_options[i].getName().startsWith(nextchar.substring(0, nameend)))        {          if (long_options[i].getName().equals(nextchar.substring(0, nameend)))            {              // Exact match found              pfound = long_options[i];              longind = i;              exact = true;              break;            }          else if (pfound == null)            {              // First nonexact match found              pfound = long_options[i];              longind = i;            }          else            {              // Second or later nonexact match found              ambig = true;            }        }    } // for    // Print out an error if the option specified was ambiguous  if (ambig && !exact)    {      if (opterr)        {          Object[] msgArgs = { progname, argv[optind] };          System.err.println(MessageFormat.format(                             _messages.getString("getopt.ambigious"),                              msgArgs));        }       nextchar = "";       optopt = 0;       ++optind;        return('?');    }   if (pfound != null)    {      ++optind;       if (nameend != nextchar.length())        {          if (pfound.has_arg != LongOpt.NO_ARGUMENT)            {              if (nextchar.substring(nameend).length() > 1)                optarg = nextchar.substring(nameend+1);              else                optarg = "";            }          else            {              if (opterr)                {                  // -- option                  if (argv[optind - 1].startsWith("--"))                    {                      Object[] msgArgs = { progname, pfound.name };                      System.err.println(MessageFormat.format(                                  _messages.getString("getopt.arguments1"),                                   msgArgs));                    }                  // +option or -option                  else                    {                      Object[] msgArgs = { progname, new                                Character(argv[optind-1].charAt(0)).toString(),                               pfound.name };                      System.err.println(MessageFormat.format(                               _messages.getString("getopt.arguments2"),                                msgArgs));                    }                 }                 nextchar = "";              optopt = pfound.val;                 return('?');            }        } // if (nameend)      else if (pfound.has_arg == LongOpt.REQUIRED_ARGUMENT)        {          if (optind < argv.length)            {               optarg = argv[optind];               ++optind;            }          else            {              if (opterr)                {                  Object[] msgArgs = { progname, argv[optind-1] };                  System.err.println(MessageFormat.format(                                     _messages.getString("getopt.requires"),                                      msgArgs));                }                 nextchar = "";              optopt = pfound.val;              if (optstring.charAt(0) == ':')                return(':');              else                return('?');            }        } // else if (pfound)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -