Changeset 10209 in josm for trunk/src/gnu/getopt


Ignore:
Timestamp:
2016-05-14T14:41:18+02:00 (8 years ago)
Author:
Don-vip
Message:

see #11924 - Java 9 - fixes more deprecation warnings

Location:
trunk/src/gnu/getopt
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/gnu/getopt/Getopt.java

    r5337 r10209  
    44/*
    55/* This program is free software; you can redistribute it and/or modify
    6 /* it under the terms of the GNU Library General Public License as published 
     6/* it under the terms of the GNU Library General Public License as published
    77/* by  the Free Software Foundation; either version 2 of the License or
    88/* (at your option) any later version.
     
    1414/*
    1515/* You should have received a copy of the GNU Library General Public License
    16 /* along with this program; see the file COPYING.LIB.  If not, write to 
    17 /* the Free Software Foundation Inc., 59 Temple Place - Suite 330, 
     16/* along with this program; see the file COPYING.LIB.  If not, write to
     17/* the Free Software Foundation Inc., 59 Temple Place - Suite 330,
    1818/* Boston, MA  02111-1307 USA
    1919/**************************************************************************/
     
    5555  * which can be retrieved using the getOptopt() method.  To suppress
    5656  * the printing of error messages for this or any other error, set
    57   * the value of the opterr instance variable to false using the 
     57  * the value of the opterr instance variable to false using the
    5858  * setOpterr() method.
    5959  * <p>
     
    6565  * <p>
    6666  * Note that this object expects command line options to be passed in the
    67   * traditional Unix manner.  That is, proceeded by a '-' character. 
     67  * traditional Unix manner.  That is, proceeded by a '-' character.
    6868  * Multiple options can follow the '-'.  For example "-abc" is equivalent
    6969  * to "-a -b -c".  If an option takes a required argument, the value
     
    8080  * <p>
    8181  * The user can stop getopt() from scanning any further into a command line
    82   * by using the special argument "--" by itself.  For example: 
     82  * by using the special argument "--" by itself.  For example:
    8383  * "-a -- -d" would return an option character of 'a', then return -1
    8484  * The "--" is discarded and "-d" is pointed to by optind as the first
     
    104104  *          case 'c':
    105105  *            arg = g.getOptarg();
    106   *            System.out.print("You picked " + (char)c + 
     106  *            System.out.print("You picked " + (char)c +
    107107  *                             " with an argument of " +
    108108  *                             ((arg != null) ? arg : "null") + "\n");
     
    145145  * options were at the beginning of the command line, and all non-options
    146146  * were at the end.  For example, calling getopt() with command line args
    147   * of "-a foo bar -d" returns options 'a' and 'd', then sets optind to 
     147  * of "-a foo bar -d" returns options 'a' and 'd', then sets optind to
    148148  * point to "foo".  The program would read the last two argv elements as
    149   * "foo" and "bar", just as if the user had typed "-a -d foo bar". 
    150   * <p> 
     149  * "foo" and "bar", just as if the user had typed "-a -d foo bar".
     150  * <p>
    151151  * The user can force getopt() to stop scanning the command line with
    152152  * the special argument "--" by itself.  Any elements occuring before the
    153153  * "--" are scanned and permuted as normal.  Any elements after the "--"
    154   * are returned as is as non-option argv elements.  For example, 
    155   * "foo -a -- bar -d" would return  option 'a' then -1.  optind would point 
     154  * are returned as is as non-option argv elements.  For example,
     155  * "foo -a -- bar -d" would return  option 'a' then -1.  optind would point
    156156  * to "foo", "bar" and "-d" as the non-option argv elements.  The "--"
    157157  * is discarded by getopt().
     
    175175  * is discarded.
    176176  * <p>
    177   * The POSIX/traditional behavior is enabled by either setting the 
     177  * The POSIX/traditional behavior is enabled by either setting the
    178178  * property "gnu.posixly_correct" or by putting a '+' sign as the first
    179   * character of the option string.  The difference between the two 
     179  * character of the option string.  The difference between the two
    180180  * methods is that setting the gnu.posixly_correct property also forces
    181181  * certain error messages to be displayed in POSIX format.  To enable
    182182  * the "return in order" functionality, put a '-' as the first character
    183   * of the option string.  Note that after determining the proper 
     183  * of the option string.  Note that after determining the proper
    184184  * behavior, Getopt strips this leading '+' or '-', meaning that a ':'
    185185  * placed as the second character after one of those two will still cause
     
    191191  * can be as long as desired.  Long options provide a more user-friendly
    192192  * way of entering command line options.  For example, in addition to a
    193   * "-h" for help, a program could support also "--help". 
    194   * <p>
    195   * Like short options, long options can also take a required or non-required 
     193  * "-h" for help, a program could support also "--help".
     194  * <p>
     195  * Like short options, long options can also take a required or non-required
    196196  * argument.  Required arguments can either be specified by placing an
    197197  * equals sign after the option name, then the argument, or by putting the
     
    205205  * with no argument and a first non-option argv element of "foo".
    206206  * <p>
    207   * Long options can also be specified using a special POSIX argument 
    208   * format (one that I highly discourage).  This form of entry is 
     207  * Long options can also be specified using a special POSIX argument
     208  * format (one that I highly discourage).  This form of entry is
    209209  * enabled by placing a "W;" (yes, 'W' then a semi-colon) in the valid
    210210  * option string.  This causes getopt to treat the name following the
     
    212212  * would be equivalent to "--outputdir=foo".  The name can immediately
    213213  * follow the "-W" like so: "-Woutputdir=foo".  Option arguments are
    214   * handled identically to normal long options.  If a string follows the 
     214  * handled identically to normal long options.  If a string follows the
    215215  * "-W" that does not represent a valid long option, then getopt() returns
    216216  * 'W' and the caller must decide what to do.  Otherwise getopt() returns
     
    221221  * few characters as required to uniquely identify it.  If the name can
    222222  * represent multiple long options, then an error message is printed and
    223   * getopt() returns a '?'. 
    224   * <p>
    225   * If an invalid option is specified or a required option argument is 
     223  * getopt() returns a '?'.
     224  * <p>
     225  * If an invalid option is specified or a required option argument is
    226226  * missing, getopt() prints an error and returns a '?' or ':' exactly
    227227  * as for short options.  Note that when an invalid long option is
     
    242242  * <p>
    243243  * When getopt() is called and a long option is encountered, one of two
    244   * things can be returned.  If the flag field in the LongOpt object 
     244  * things can be returned.  If the flag field in the LongOpt object
    245245  * representing the long option is non-null, then the integer value field
    246246  * is stored there and an integer 0 is returned to the caller.  The val
     
    250250  * If the flag field in the LongOpt object is null, then the value field
    251251  * of the LongOpt is returned.  This can be the character of a short option.
    252   * This allows an app to have both a long and short option sequence 
     252  * This allows an app to have both a long and short option sequence
    253253  * (say, "-h" and "--help") that do the exact same thing.
    254254  * <p>
    255   * With long options, there is an alternative method of determining 
     255  * With long options, there is an alternative method of determining
    256256  * which option was selected.  The method getLongind() will return the
    257257  * the index in the long option array (NOT argv) of the long option found.
    258258  * So if multiple long options are configured to return the same value,
    259   * the application can use getLongind() to distinguish between them. 
     259  * the application can use getLongind() to distinguish between them.
    260260  * <p>
    261261  * Here is an expanded Getopt example using long options and various
     
    266266  * String arg;
    267267  * LongOpt[] longopts = new LongOpt[3];
    268   * // 
     268  * //
    269269  * StringBuffer sb = new StringBuffer();
    270270  * longopts[0] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h');
    271   * longopts[1] = new LongOpt("outputdir", LongOpt.REQUIRED_ARGUMENT, sb, 'o'); 
     271  * longopts[1] = new LongOpt("outputdir", LongOpt.REQUIRED_ARGUMENT, sb, 'o');
    272272  * longopts[2] = new LongOpt("maximum", LongOpt.OPTIONAL_ARGUMENT, null, 2);
    273   * // 
     273  * //
    274274  * Getopt g = new Getopt("testprog", argv, "-:bc::d:hW;", longopts);
    275275  * g.setOpterr(false); // We'll do our own error handling
     
    297297  *          System.out.println("We picked option " +
    298298  *                             longopts[g.getLongind()].getName() +
    299   *                           " with value " + 
     299  *                           " with value " +
    300300  *                           ((arg != null) ? arg : "null"));
    301301  *          break;
     
    308308  *        case 'd':
    309309  *          arg = g.getOptarg();
    310   *          System.out.println("You picked option '" + (char)c + 
     310  *          System.out.println("You picked option '" + (char)c +
    311311  *                             "' with argument " +
    312312  *                             ((arg != null) ? arg : "null"));
     
    328328  *          //
    329329  *        case '?':
    330   *          System.out.println("The option '" + (char)g.getOptopt() + 
     330  *          System.out.println("The option '" + (char)g.getOptopt() +
    331331  *                           "' is not valid");
    332332  *          break;
     
    351351  * It does not cause only long options to be parsed but instead enables
    352352  * the behavior described above.
    353   * <p> 
    354   * Note that the functionality and variable names used are driven from 
    355   * the C lib version as this object is a port of the C code, not a 
     353  * <p>
     354  * Note that the functionality and variable names used are driven from
     355  * the C lib version as this object is a port of the C code, not a
    356356  * new implementation.  This should aid in porting existing C/C++ code,
    357357  * as well as helping programmers familiar with the glibc version to
     
    383383 */
    384384
    385 /** 
     385/**
    386386  * Describe how to deal with options that follow non-option ARGV-elements.
    387387  *
    388388  * If the caller did not specify anything,
    389   * the default is REQUIRE_ORDER if the property 
     389  * the default is REQUIRE_ORDER if the property
    390390  * gnu.posixly_correct is defined, PERMUTE otherwise.
    391391  *
     
    426426 * Instance Variables
    427427 */
    428  
     428
    429429/**
    430430  * For communication from `getopt' to the caller.
     
    447447  *
    448448  *  Otherwise, `optind' communicates from one call to the next
    449   *  how much of ARGV has been scanned so far. 
     449  *  how much of ARGV has been scanned so far.
    450450  */
    451451protected int optind = 0;
    452452
    453 /** 
     453/**
    454454  * Callers store false here to inhibit the error message
    455   * for unrecognized options. 
     455  * for unrecognized options.
    456456  */
    457457protected boolean opterr = true;
    458458
    459 /** 
     459/**
    460460  * When an unrecognized option is encountered, getopt will return a '?'
    461461  * and store the value of the invalid option here.
     
    463463protected int optopt = '?';
    464464
    465 /** 
     465/**
    466466  * The next char to be scanned in the option-element
    467467  * in which the last option character we returned was found.
     
    469469  *
    470470  * If this is zero, or a null string, it means resume the scan
    471   * by advancing to the next ARGV-element. 
     471  * by advancing to the next ARGV-element.
    472472  */
    473473protected String nextchar;
     
    479479
    480480/**
    481   * This is an array of LongOpt objects which describ the valid long 
     481  * This is an array of LongOpt objects which describ the valid long
    482482  * options.
    483483  */
     
    574574  */
    575575public
    576 Getopt(String progname, String[] argv, String optstring, 
     576Getopt(String progname, String[] argv, String optstring,
    577577       LongOpt[] long_options)
    578578{
     
    612612  * Construct a Getopt instance with given input data that is capable of
    613613  * parsing long options and short options.  Contrary to what you might
    614   * think, the flag 'long_only' does not determine whether or not we 
     614  * think, the flag 'long_only' does not determine whether or not we
    615615  * scan for only long arguments.  Instead, a value of true here allows
    616616  * long arguments to start with a '-' instead of '--' unless there is a
     
    624624  */
    625625public
    626 Getopt(String progname, String[] argv, String optstring, 
     626Getopt(String progname, String[] argv, String optstring,
    627627       LongOpt[] long_options, boolean long_only)
    628628{
     
    673673
    674674/**************************************************************************/
    675  
     675
    676676/*
    677677 * Instance Methods
     
    706706  *
    707707  * Otherwise, `optind' communicates from one call to the next
    708   * how much of ARGV has been scanned so far. 
     708  * how much of ARGV has been scanned so far.
    709709  */
    710710public int
     
    719719  * This method allows the optind index to be set manually.  Normally this
    720720  * is not necessary (and incorrect usage of this method can lead to serious
    721   * lossage), but optind is a public symbol in GNU getopt, so this method 
     721  * lossage), but optind is a public symbol in GNU getopt, so this method
    722722  * was added to allow it to be modified by the caller if desired.
    723723  *
     
    749749/**************************************************************************/
    750750
    751 /** 
     751/**
    752752  * For communication from `getopt' to the caller.
    753753  * When `getopt' finds an option that takes an argument,
     
    768768  * Normally Getopt will print a message to the standard error when an
    769769  * invalid option is encountered.  This can be suppressed (or re-enabled)
    770   * by calling this method.  There is no get method for this variable 
     770  * by calling this method.  There is no get method for this variable
    771771  * because if you can't remember the state you set this to, why should I?
    772772  */
     
    823823      if (top - middle > middle - bottom)
    824824        {
    825           // Bottom segment is the short one. 
     825          // Bottom segment is the short one.
    826826          int len = middle - bottom;
    827827          int i;
    828828
    829           // Swap it with the top part of the top segment. 
     829          // Swap it with the top part of the top segment.
    830830          for (i = 0; i < len; i++)
    831831            {
     
    834834              argv[top - (middle - bottom) + i] = tem;
    835835            }
    836           // Exclude the moved bottom segment from further swapping. 
     836          // Exclude the moved bottom segment from further swapping.
    837837          top -= len;
    838838        }
     
    843843          int i;
    844844
    845           // Swap it with the bottom part of the bottom segment. 
     845          // Swap it with the bottom part of the bottom segment.
    846846          for (i = 0; i < len; i++)
    847847            {
     
    850850              argv[middle + i] = tem;
    851851            }
    852           // Exclude the moved top segment from further swapping. 
     852          // Exclude the moved top segment from further swapping.
    853853          bottom += len;
    854854        }
    855855    }
    856856
    857   // Update records for the slots the non-options now occupy. 
     857  // Update records for the slots the non-options now occupy.
    858858
    859859  first_nonopt += (optind - last_nonopt);
     
    879879  boolean ambig;
    880880  boolean exact;
    881  
     881
    882882  longopt_handled = true;
    883883  ambig = false;
     
    888888  if (nameend == -1)
    889889    nameend = nextchar.length();
    890  
     890
    891891  // Test all lnog options for either exact match or abbreviated matches
    892892  for (int i = 0; i < long_options.length; i++)
     
    915915        }
    916916    } // for
    917  
     917
    918918  // Print out an error if the option specified was ambiguous
    919919  if (ambig && !exact)
     
    923923          Object[] msgArgs = { progname, argv[optind] };
    924924          System.err.println(MessageFormat.format(
    925                              _messages.getString("getopt.ambigious"), 
     925                             _messages.getString("getopt.ambigious"),
    926926                             msgArgs));
    927927        }
     
    930930       optopt = 0;
    931931       ++optind;
    932  
     932
    933933       return('?');
    934934    }
    935  
     935
    936936  if (pfound != null)
    937937    {
    938938      ++optind;
    939  
     939
    940940      if (nameend != nextchar.length())
    941941        {
     
    956956                      Object[] msgArgs = { progname, pfound.name };
    957957                      System.err.println(MessageFormat.format(
    958                                   _messages.getString("getopt.arguments1"), 
     958                                  _messages.getString("getopt.arguments1"),
    959959                                  msgArgs));
    960960                    }
     
    962962                  else
    963963                    {
    964                       Object[] msgArgs = { progname, new
    965                                Character(argv[optind-1].charAt(0)).toString(),
     964                      Object[] msgArgs = { progname,
     965                               Character.toString(argv[optind-1].charAt(0)),
    966966                               pfound.name };
    967967                      System.err.println(MessageFormat.format(
    968                                _messages.getString("getopt.arguments2"), 
     968                               _messages.getString("getopt.arguments2"),
    969969                               msgArgs));
    970970                    }
    971971                 }
    972    
     972
    973973              nextchar = "";
    974974              optopt = pfound.val;
    975    
     975
    976976              return('?');
    977977            }
     
    990990                  Object[] msgArgs = { progname, argv[optind-1] };
    991991                  System.err.println(MessageFormat.format(
    992                                      _messages.getString("getopt.requires"), 
     992                                     _messages.getString("getopt.requires"),
    993993                                     msgArgs));
    994994                }
    995    
     995
    996996              nextchar = "";
    997997              optopt = pfound.val;
     
    10021002            }
    10031003        } // else if (pfound)
    1004    
     1004
    10051005      nextchar = "";
    10061006
     
    10091009          pfound.flag.setLength(0);
    10101010          pfound.flag.append(pfound.val);
    1011    
     1011
    10121012          return(0);
    10131013        }
     
    10151015      return(pfound.val);
    10161016   } // if (pfound != null)
    1017  
     1017
    10181018  longopt_handled = false;
    10191019
     
    10701070              optind++;
    10711071            }
    1072          
     1072
    10731073          last_nonopt = optind;
    10741074        }
     
    11061106      // If we have come to a non-option and did not permute it,
    11071107      // either stop the scan or describe it to the caller and pass it by.
    1108       if (argv[optind].equals("") || (argv[optind].charAt(0) != '-') || 
     1108      if (argv[optind].equals("") || (argv[optind].charAt(0) != '-') ||
    11091109          argv[optind].equals("-"))
    11101110        {
     
    11151115            return(1);
    11161116        }
    1117      
     1117
    11181118      // We have found another option-ARGV-element.
    11191119      // Skip the initial punctuation.
     
    11391139     This distinction seems to be the most useful approach.  */
    11401140  if ((long_options != null) && (argv[optind].startsWith("--")
    1141       || (long_only && ((argv[optind].length()  > 2) || 
     1141      || (long_only && ((argv[optind].length()  > 2) ||
    11421142      (optstring.indexOf(argv[optind].charAt(1)) == -1)))))
    11431143    {
     
    11461146       if (longopt_handled)
    11471147         return(c);
    1148          
     1148
    11491149      // Can't find it as a long option.  If this is not getopt_long_only,
    11501150      // or the option starts with '--' or is not a valid short
     
    11601160                  Object[] msgArgs = { progname, nextchar };
    11611161                  System.err.println(MessageFormat.format(
    1162                                    _messages.getString("getopt.unrecognized"), 
     1162                                   _messages.getString("getopt.unrecognized"),
    11631163                                   msgArgs));
    11641164                }
    11651165              else
    11661166                {
    1167                   Object[] msgArgs = { progname, new
    1168                                  Character(argv[optind].charAt(0)).toString(),
     1167                  Object[] msgArgs = { progname,
     1168                                 Character.toString(argv[optind].charAt(0)),
    11691169                                 nextchar };
    11701170                  System.err.println(MessageFormat.format(
    1171                                  _messages.getString("getopt.unrecognized2"), 
     1171                                 _messages.getString("getopt.unrecognized2"),
    11721172                                 msgArgs));
    11731173                }
     
    11771177          ++optind;
    11781178          optopt = 0;
    1179    
     1179
    11801180          return('?');
    11811181        }
     
    11881188  else
    11891189    nextchar = "";
    1190  
     1190
    11911191  String temp = null;
    11921192  if (optstring.indexOf(c) != -1)
     
    12031203            {
    12041204              // 1003.2 specifies the format of this message
    1205               Object[] msgArgs = { progname, new
    1206                                    Character((char)c).toString() };
     1205              Object[] msgArgs = { progname,
     1206                                   Character.toString((char)c) };
    12071207              System.err.println(MessageFormat.format(
    12081208                            _messages.getString("getopt.illegal"), msgArgs));
     
    12101210          else
    12111211            {
    1212               Object[] msgArgs = { progname, new
    1213                                    Character((char)c).toString() };
     1212              Object[] msgArgs = { progname,
     1213                                   Character.toString((char)c) };
    12141214              System.err.println(MessageFormat.format(
    12151215                            _messages.getString("getopt.invalid"), msgArgs));
     
    12341234          if (opterr)
    12351235            {
    1236               // 1003.2 specifies the format of this message. 
    1237               Object[] msgArgs = { progname, new
    1238                                    Character((char)c).toString() };
     1236              // 1003.2 specifies the format of this message.
     1237              Object[] msgArgs = { progname,
     1238                                   Character.toString((char)c) };
    12391239              System.err.println(MessageFormat.format(
    12401240                            _messages.getString("getopt.requires2"), msgArgs));
     
    12501250        {
    12511251          // We already incremented `optind' once;
    1252           // increment it again when taking next ARGV-elt as argument. 
     1252          // increment it again when taking next ARGV-elt as argument.
    12531253          nextchar = argv[optind];
    12541254          optarg  = argv[optind];
     
    12971297                {
    12981298                  // 1003.2 specifies the format of this message
    1299                   Object[] msgArgs = { progname, new
    1300                                        Character((char)c).toString() };
     1299                  Object[] msgArgs = { progname,
     1300                                       Character.toString((char)c) };
    13011301                  System.err.println(MessageFormat.format(
    13021302                            _messages.getString("getopt.requires2"), msgArgs));
     
    13041304
    13051305              optopt = c;
    1306  
     1306
    13071307              if (optstring.charAt(0) == ':')
    13081308                return(':');
     
    13271327                        {
    13281328                          // 1003.2 specifies the format of this message
    1329                           Object[] msgArgs = { progname, new
    1330                                                Character((char)c).toString() };
     1329                          Object[] msgArgs = { progname,
     1330                                               Character.toString((char)c) };
    13311331                          System.err.println(MessageFormat.format(
    13321332                             _messages.getString("getopt.requires2"), msgArgs));
     
    13341334
    13351335                      optopt = c;
    1336  
     1336
    13371337                      if (optstring.charAt(0) == ':')
    13381338                        return(':');
  • trunk/src/gnu/getopt/LongOpt.java

    r5279 r10209  
    55/*
    66/* This program is free software; you can redistribute it and/or modify
    7 /* it under the terms of the GNU Library General Public License as published 
     7/* it under the terms of the GNU Library General Public License as published
    88/* by  the Free Software Foundation; either version 2 of the License or
    99/* (at your option) any later version.
     
    1515/*
    1616/* You should have received a copy of the GNU Library General Public License
    17 /* along with this program; see the file COPYING.LIB.  If not, write to 
    18 /* the Free Software Foundation Inc., 59 Temple Place - Suite 330, 
     17/* along with this program; see the file COPYING.LIB.  If not, write to
     18/* the Free Software Foundation Inc., 59 Temple Place - Suite 330,
    1919/* Boston, MA  02111-1307 USA
    2020/**************************************************************************/
     
    2222package gnu.getopt;
    2323
    24 import java.util.Locale;
    25 import java.util.ResourceBundle;
    2624import java.text.MessageFormat;
    2725
     
    3432  * session.  Refer to the getopt documentation for details on the
    3533  * format of long options.
    36   * 
     34  *
    3735  * @version 1.0.5
    3836  * @author Aaron M. Renn (arenn@urbanophile.com)
     
    5553public static final int NO_ARGUMENT = 0;
    5654
    57 /** 
     55/**
    5856  * Constant value used for the "has_arg" constructor argument.  This
    5957  * value indicates that the option takes an argument that is required.
     
    116114  * @param flag If non-null, this is a location to store the value of "val" when this option is encountered, otherwise "val" is treated as the equivalent short option character.
    117115  * @param val The value to return for this long option, or the equivalent single letter option to emulate if flag is null.
    118   * 
     116  *
    119117  * @exception IllegalArgumentException If the has_arg param is not one of NO_ARGUMENT, REQUIRED_ARGUMENT or OPTIONAL_ARGUMENT.
    120118  */
    121119public
    122 LongOpt(String name, int has_arg, 
     120LongOpt(String name, int has_arg,
    123121        StringBuffer flag, int val) throws IllegalArgumentException
    124122{
    125123  // Validate has_arg
    126   if ((has_arg != NO_ARGUMENT) && (has_arg != REQUIRED_ARGUMENT) 
     124  if ((has_arg != NO_ARGUMENT) && (has_arg != REQUIRED_ARGUMENT)
    127125     && (has_arg != OPTIONAL_ARGUMENT))
    128126    {
    129       Object[] msgArgs = { new Integer(has_arg).toString() };
     127      Object[] msgArgs = { Integer.toString(has_arg) };
    130128      throw new IllegalArgumentException(MessageFormat.format(
    131129                    _messages.getString("getopt.invalidValue"), msgArgs));
Note: See TracChangeset for help on using the changeset viewer.