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

📄 getopt.java

📁 垃圾邮件过滤器源代码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
  *          System.out.println("getopt() returned " + c);  *          break;  *     }  * //  * for (int i = g.getOptind(); i < argv.length ; i++)  *   System.out.println("Non option argv element: " + argv[i] + "\n");  * </pre>  * <p>  * There is an alternative form of the constructor used for long options  * above.  This takes a trailing boolean flag.  If set to false, Getopt  * performs identically to the example, but if the boolean flag is true  * then long options are allowed to start with a single '-' instead of  * "--".  If the first character of the option is a valid short option  * character, then the option is treated as if it were the short option.  * Otherwise it behaves as if the option is a long option.  Note that  * the name given to this option - long_only - is very counter-intuitive.  * It does not cause only long options to be parsed but instead enables  * the behavior described above.  * <p>   * Note that the functionality and variable names used are driven from   * the C lib version as this object is a port of the C code, not a   * new implementation.  This should aid in porting existing C/C++ code,  * as well as helping programmers familiar with the glibc version to  * adapt to the Java version even if it seems very non-Java at times.  * <p>  * In this release I made all instance variables protected due to  * overwhelming public demand.  Any code which relied on optarg,  * opterr, optind, or optopt being public will need to be modified to  * use the appropriate access methods.  * <p>  * Please send all bug reports, requests, and comments to  * <a href="mailto:arenn@urbanophile.com">arenn@urbanophile.com</a>.  *  * @version 1.0.7  *  * @author Roland McGrath (roland@gnu.ai.mit.edu)  * @author Ulrich Drepper (drepper@cygnus.com)  * @author Aaron M. Renn (arenn@urbanophile.com)  *  * @see LongOpt  */public class Getopt extends Object{/**************************************************************************//* * Class Variables *//**   * Describe how to deal with options that follow non-option ARGV-elements.  *  * If the caller did not specify anything,  * the default is REQUIRE_ORDER if the property   * gnu.posixly_correct is defined, PERMUTE otherwise.  *  * The special argument `--' forces an end of option-scanning regardless  * of the value of `ordering'.  In the case of RETURN_IN_ORDER, only  * `--' can cause `getopt' to return -1 with `optind' != ARGC.  *  * REQUIRE_ORDER means don't recognize them as options;  * stop option processing when the first non-option is seen.  * This is what Unix does.  * This mode of operation is selected by either setting the property  * gnu.posixly_correct, or using `+' as the first character  * of the list of option characters.  */protected static final int REQUIRE_ORDER = 1;/**  * PERMUTE is the default.  We permute the contents of ARGV as we scan,  * so that eventually all the non-options are at the end.  This allows options  * to be given in any order, even with programs that were not written to  * expect this.  */protected static final int PERMUTE = 2;/**  * RETURN_IN_ORDER is an option available to programs that were written  * to expect options and other ARGV-elements in any order and that care about  * the ordering of the two.  We describe each non-option ARGV-element  * as if it were the argument of an option with character code 1.  * Using `-' as the first character of the list of option characters  * selects this mode of operation.  */protected static final int RETURN_IN_ORDER = 3;/**************************************************************************//* * Instance Variables */ /**  * 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.  */protected String optarg;/**  *  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'.  *  *  On entry to `getopt', zero means this is the first call; initialize.  *  *  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.    */protected int optind = 0;/**   * Callers store false here to inhibit the error message  * for unrecognized options.    */protected boolean opterr = true;/**   * When an unrecognized option is encountered, getopt will return a '?'  * and store the value of the invalid option here.  */protected int optopt = '?';/**   * The next char to be scanned in the option-element  * in which the last option character we returned was found.  * This allows us to pick up the scan where we left off.  *  * If this is zero, or a null string, it means resume the scan  * by advancing to the next ARGV-element.    */protected String nextchar;/**  * This is the string describing the valid short options.  */protected String optstring;/**  * This is an array of LongOpt objects which describ the valid long   * options.  */protected LongOpt[] long_options;/**  * This flag determines whether or not we are parsing only long args  */protected boolean long_only;/**  * Stores the index into the long_options array of the long option found  */protected int longind;/**  * The flag determines whether or not we operate in strict POSIX compliance  */protected boolean posixly_correct;/**  * A flag which communicates whether or not checkLongOption() did all  * necessary processing for the current option  */protected boolean longopt_handled;/**  * The index of the first non-option in argv[]  */protected int first_nonopt = 1;/**  * The index of the last non-option in argv[]  */protected int last_nonopt = 1;/**  * Flag to tell getopt to immediately return -1 the next time it is  * called.  */private boolean endparse = false;/**  * Saved argument list passed to the program  */protected String[] argv;/**  * Determines whether we permute arguments or not  */protected int ordering;/**  * Name to print as the program name in error messages.  This is necessary  * since Java does not place the program name in argv[0]  */protected String progname;/**  * The localized strings are kept in a separate file  */private ResourceBundle _messages = PropertyResourceBundle.getBundle(                           "gnu/getopt/MessagesBundle", Locale.getDefault());/**************************************************************************//* * Constructors *//**  * Construct a basic Getopt instance with the given input data.  Note that  * this handles "short" options only.  *  * @param progname The name to display as the program name when printing errors  * @param argv The String array passed as the command line to the program.  * @param optstring A String containing a description of the valid args for this program  */publicGetopt(String progname, String[] argv, String optstring){  this(progname, argv, optstring, null, false);}/**************************************************************************//**  * Construct a Getopt instance with given input data that is capable of  * parsing long options as well as short.  *  * @param progname The name to display as the program name when printing errors  * @param argv The String array passed as the command ilne to the program  * @param optstring A String containing a description of the valid short args for this program  * @param long_options An array of LongOpt objects that describes the valid long args for this program  */publicGetopt(String progname, String[] argv, String optstring,        LongOpt[] long_options){  this(progname, argv, optstring, long_options, false);}/**************************************************************************//**  * Construct a Getopt instance with given input data that is capable of  * parsing long options and short options.  Contrary to what you might  * think, the flag 'long_only' does not determine whether or not we   * scan for only long arguments.  Instead, a value of true here allows  * long arguments to start with a '-' instead of '--' unless there is a  * conflict with a short option name.  *  * @param progname The name to display as the program name when printing errors  * @param argv The String array passed as the command ilne to the program  * @param optstring A String containing a description of the valid short args for this program  * @param long_options An array of LongOpt objects that describes the valid long args for this program  * @param long_only true if long options that do not conflict with short options can start with a '-' as well as '--'  */publicGetopt(String progname, String[] argv, String optstring,        LongOpt[] long_options, boolean long_only){  if (optstring.length() == 0)    optstring = " ";  // This function is essentially _getopt_initialize from GNU getopt  this.progname = progname;  this.argv = argv;  this.optstring = optstring;  this.long_options = long_options;  this.long_only = long_only;  // Check for property "gnu.posixly_correct" to determine whether to  // strictly follow the POSIX standard.  This replaces the "POSIXLY_CORRECT"  // environment variable in the C version  if (System.getProperty("gnu.posixly_correct", null) == null)    posixly_correct = false;  else    {      posixly_correct = true;      _messages = PropertyResourceBundle.getBundle("gnu/getopt/MessagesBundle",                                                   Locale.US);    }  // Determine how to handle the ordering of options and non-options  if (optstring.charAt(0) == '-')    {      ordering = RETURN_IN_ORDER;      if (optstring.length() > 1)        this.optstring = optstring.substring(1);    }  else if (optstring.charAt(0) == '+')    {      ordering = REQUIRE_ORDER;      if (optstring.length() > 1)        this.optstring = optstring.substring(1);    }  else if (posixly_correct)    {      ordering = REQUIRE_ORDER;    }  else    {      ordering = PERMUTE; // The normal default case    }}/**************************************************************************/ /* * Instance Methods *//**  * 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;}

⌨️ 快捷键说明

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