Changeset 10209 in josm for trunk/src/gnu/getopt
- Timestamp:
- 2016-05-14T14:41:18+02:00 (8 years ago)
- Location:
- trunk/src/gnu/getopt
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/gnu/getopt/Getopt.java
r5337 r10209 4 4 /* 5 5 /* 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 7 7 /* by the Free Software Foundation; either version 2 of the License or 8 8 /* (at your option) any later version. … … 14 14 /* 15 15 /* 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, 18 18 /* Boston, MA 02111-1307 USA 19 19 /**************************************************************************/ … … 55 55 * which can be retrieved using the getOptopt() method. To suppress 56 56 * 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 58 58 * setOpterr() method. 59 59 * <p> … … 65 65 * <p> 66 66 * 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. 68 68 * Multiple options can follow the '-'. For example "-abc" is equivalent 69 69 * to "-a -b -c". If an option takes a required argument, the value … … 80 80 * <p> 81 81 * 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: 83 83 * "-a -- -d" would return an option character of 'a', then return -1 84 84 * The "--" is discarded and "-d" is pointed to by optind as the first … … 104 104 * case 'c': 105 105 * arg = g.getOptarg(); 106 * System.out.print("You picked " + (char)c + 106 * System.out.print("You picked " + (char)c + 107 107 * " with an argument of " + 108 108 * ((arg != null) ? arg : "null") + "\n"); … … 145 145 * options were at the beginning of the command line, and all non-options 146 146 * 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 148 148 * 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> 151 151 * The user can force getopt() to stop scanning the command line with 152 152 * the special argument "--" by itself. Any elements occuring before the 153 153 * "--" 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 156 156 * to "foo", "bar" and "-d" as the non-option argv elements. The "--" 157 157 * is discarded by getopt(). … … 175 175 * is discarded. 176 176 * <p> 177 * The POSIX/traditional behavior is enabled by either setting the 177 * The POSIX/traditional behavior is enabled by either setting the 178 178 * 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 180 180 * methods is that setting the gnu.posixly_correct property also forces 181 181 * certain error messages to be displayed in POSIX format. To enable 182 182 * 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 184 184 * behavior, Getopt strips this leading '+' or '-', meaning that a ':' 185 185 * placed as the second character after one of those two will still cause … … 191 191 * can be as long as desired. Long options provide a more user-friendly 192 192 * 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 196 196 * argument. Required arguments can either be specified by placing an 197 197 * equals sign after the option name, then the argument, or by putting the … … 205 205 * with no argument and a first non-option argv element of "foo". 206 206 * <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 209 209 * enabled by placing a "W;" (yes, 'W' then a semi-colon) in the valid 210 210 * option string. This causes getopt to treat the name following the … … 212 212 * would be equivalent to "--outputdir=foo". The name can immediately 213 213 * 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 215 215 * "-W" that does not represent a valid long option, then getopt() returns 216 216 * 'W' and the caller must decide what to do. Otherwise getopt() returns … … 221 221 * few characters as required to uniquely identify it. If the name can 222 222 * 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 226 226 * missing, getopt() prints an error and returns a '?' or ':' exactly 227 227 * as for short options. Note that when an invalid long option is … … 242 242 * <p> 243 243 * 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 245 245 * representing the long option is non-null, then the integer value field 246 246 * is stored there and an integer 0 is returned to the caller. The val … … 250 250 * If the flag field in the LongOpt object is null, then the value field 251 251 * 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 253 253 * (say, "-h" and "--help") that do the exact same thing. 254 254 * <p> 255 * With long options, there is an alternative method of determining 255 * With long options, there is an alternative method of determining 256 256 * which option was selected. The method getLongind() will return the 257 257 * the index in the long option array (NOT argv) of the long option found. 258 258 * 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. 260 260 * <p> 261 261 * Here is an expanded Getopt example using long options and various … … 266 266 * String arg; 267 267 * LongOpt[] longopts = new LongOpt[3]; 268 * // 268 * // 269 269 * StringBuffer sb = new StringBuffer(); 270 270 * 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'); 272 272 * longopts[2] = new LongOpt("maximum", LongOpt.OPTIONAL_ARGUMENT, null, 2); 273 * // 273 * // 274 274 * Getopt g = new Getopt("testprog", argv, "-:bc::d:hW;", longopts); 275 275 * g.setOpterr(false); // We'll do our own error handling … … 297 297 * System.out.println("We picked option " + 298 298 * longopts[g.getLongind()].getName() + 299 * " with value " + 299 * " with value " + 300 300 * ((arg != null) ? arg : "null")); 301 301 * break; … … 308 308 * case 'd': 309 309 * arg = g.getOptarg(); 310 * System.out.println("You picked option '" + (char)c + 310 * System.out.println("You picked option '" + (char)c + 311 311 * "' with argument " + 312 312 * ((arg != null) ? arg : "null")); … … 328 328 * // 329 329 * case '?': 330 * System.out.println("The option '" + (char)g.getOptopt() + 330 * System.out.println("The option '" + (char)g.getOptopt() + 331 331 * "' is not valid"); 332 332 * break; … … 351 351 * It does not cause only long options to be parsed but instead enables 352 352 * 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 356 356 * new implementation. This should aid in porting existing C/C++ code, 357 357 * as well as helping programmers familiar with the glibc version to … … 383 383 */ 384 384 385 /** 385 /** 386 386 * Describe how to deal with options that follow non-option ARGV-elements. 387 387 * 388 388 * 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 390 390 * gnu.posixly_correct is defined, PERMUTE otherwise. 391 391 * … … 426 426 * Instance Variables 427 427 */ 428 428 429 429 /** 430 430 * For communication from `getopt' to the caller. … … 447 447 * 448 448 * 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. 450 450 */ 451 451 protected int optind = 0; 452 452 453 /** 453 /** 454 454 * Callers store false here to inhibit the error message 455 * for unrecognized options. 455 * for unrecognized options. 456 456 */ 457 457 protected boolean opterr = true; 458 458 459 /** 459 /** 460 460 * When an unrecognized option is encountered, getopt will return a '?' 461 461 * and store the value of the invalid option here. … … 463 463 protected int optopt = '?'; 464 464 465 /** 465 /** 466 466 * The next char to be scanned in the option-element 467 467 * in which the last option character we returned was found. … … 469 469 * 470 470 * 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. 472 472 */ 473 473 protected String nextchar; … … 479 479 480 480 /** 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 482 482 * options. 483 483 */ … … 574 574 */ 575 575 public 576 Getopt(String progname, String[] argv, String optstring, 576 Getopt(String progname, String[] argv, String optstring, 577 577 LongOpt[] long_options) 578 578 { … … 612 612 * Construct a Getopt instance with given input data that is capable of 613 613 * 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 615 615 * scan for only long arguments. Instead, a value of true here allows 616 616 * long arguments to start with a '-' instead of '--' unless there is a … … 624 624 */ 625 625 public 626 Getopt(String progname, String[] argv, String optstring, 626 Getopt(String progname, String[] argv, String optstring, 627 627 LongOpt[] long_options, boolean long_only) 628 628 { … … 673 673 674 674 /**************************************************************************/ 675 675 676 676 /* 677 677 * Instance Methods … … 706 706 * 707 707 * 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. 709 709 */ 710 710 public int … … 719 719 * This method allows the optind index to be set manually. Normally this 720 720 * 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 722 722 * was added to allow it to be modified by the caller if desired. 723 723 * … … 749 749 /**************************************************************************/ 750 750 751 /** 751 /** 752 752 * For communication from `getopt' to the caller. 753 753 * When `getopt' finds an option that takes an argument, … … 768 768 * Normally Getopt will print a message to the standard error when an 769 769 * 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 771 771 * because if you can't remember the state you set this to, why should I? 772 772 */ … … 823 823 if (top - middle > middle - bottom) 824 824 { 825 // Bottom segment is the short one. 825 // Bottom segment is the short one. 826 826 int len = middle - bottom; 827 827 int i; 828 828 829 // Swap it with the top part of the top segment. 829 // Swap it with the top part of the top segment. 830 830 for (i = 0; i < len; i++) 831 831 { … … 834 834 argv[top - (middle - bottom) + i] = tem; 835 835 } 836 // Exclude the moved bottom segment from further swapping. 836 // Exclude the moved bottom segment from further swapping. 837 837 top -= len; 838 838 } … … 843 843 int i; 844 844 845 // Swap it with the bottom part of the bottom segment. 845 // Swap it with the bottom part of the bottom segment. 846 846 for (i = 0; i < len; i++) 847 847 { … … 850 850 argv[middle + i] = tem; 851 851 } 852 // Exclude the moved top segment from further swapping. 852 // Exclude the moved top segment from further swapping. 853 853 bottom += len; 854 854 } 855 855 } 856 856 857 // Update records for the slots the non-options now occupy. 857 // Update records for the slots the non-options now occupy. 858 858 859 859 first_nonopt += (optind - last_nonopt); … … 879 879 boolean ambig; 880 880 boolean exact; 881 881 882 882 longopt_handled = true; 883 883 ambig = false; … … 888 888 if (nameend == -1) 889 889 nameend = nextchar.length(); 890 890 891 891 // Test all lnog options for either exact match or abbreviated matches 892 892 for (int i = 0; i < long_options.length; i++) … … 915 915 } 916 916 } // for 917 917 918 918 // Print out an error if the option specified was ambiguous 919 919 if (ambig && !exact) … … 923 923 Object[] msgArgs = { progname, argv[optind] }; 924 924 System.err.println(MessageFormat.format( 925 _messages.getString("getopt.ambigious"), 925 _messages.getString("getopt.ambigious"), 926 926 msgArgs)); 927 927 } … … 930 930 optopt = 0; 931 931 ++optind; 932 932 933 933 return('?'); 934 934 } 935 935 936 936 if (pfound != null) 937 937 { 938 938 ++optind; 939 939 940 940 if (nameend != nextchar.length()) 941 941 { … … 956 956 Object[] msgArgs = { progname, pfound.name }; 957 957 System.err.println(MessageFormat.format( 958 _messages.getString("getopt.arguments1"), 958 _messages.getString("getopt.arguments1"), 959 959 msgArgs)); 960 960 } … … 962 962 else 963 963 { 964 Object[] msgArgs = { progname, new965 Character (argv[optind-1].charAt(0)).toString(),964 Object[] msgArgs = { progname, 965 Character.toString(argv[optind-1].charAt(0)), 966 966 pfound.name }; 967 967 System.err.println(MessageFormat.format( 968 _messages.getString("getopt.arguments2"), 968 _messages.getString("getopt.arguments2"), 969 969 msgArgs)); 970 970 } 971 971 } 972 972 973 973 nextchar = ""; 974 974 optopt = pfound.val; 975 975 976 976 return('?'); 977 977 } … … 990 990 Object[] msgArgs = { progname, argv[optind-1] }; 991 991 System.err.println(MessageFormat.format( 992 _messages.getString("getopt.requires"), 992 _messages.getString("getopt.requires"), 993 993 msgArgs)); 994 994 } 995 995 996 996 nextchar = ""; 997 997 optopt = pfound.val; … … 1002 1002 } 1003 1003 } // else if (pfound) 1004 1004 1005 1005 nextchar = ""; 1006 1006 … … 1009 1009 pfound.flag.setLength(0); 1010 1010 pfound.flag.append(pfound.val); 1011 1011 1012 1012 return(0); 1013 1013 } … … 1015 1015 return(pfound.val); 1016 1016 } // if (pfound != null) 1017 1017 1018 1018 longopt_handled = false; 1019 1019 … … 1070 1070 optind++; 1071 1071 } 1072 1072 1073 1073 last_nonopt = optind; 1074 1074 } … … 1106 1106 // If we have come to a non-option and did not permute it, 1107 1107 // 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) != '-') || 1109 1109 argv[optind].equals("-")) 1110 1110 { … … 1115 1115 return(1); 1116 1116 } 1117 1117 1118 1118 // We have found another option-ARGV-element. 1119 1119 // Skip the initial punctuation. … … 1139 1139 This distinction seems to be the most useful approach. */ 1140 1140 if ((long_options != null) && (argv[optind].startsWith("--") 1141 || (long_only && ((argv[optind].length() > 2) || 1141 || (long_only && ((argv[optind].length() > 2) || 1142 1142 (optstring.indexOf(argv[optind].charAt(1)) == -1))))) 1143 1143 { … … 1146 1146 if (longopt_handled) 1147 1147 return(c); 1148 1148 1149 1149 // Can't find it as a long option. If this is not getopt_long_only, 1150 1150 // or the option starts with '--' or is not a valid short … … 1160 1160 Object[] msgArgs = { progname, nextchar }; 1161 1161 System.err.println(MessageFormat.format( 1162 _messages.getString("getopt.unrecognized"), 1162 _messages.getString("getopt.unrecognized"), 1163 1163 msgArgs)); 1164 1164 } 1165 1165 else 1166 1166 { 1167 Object[] msgArgs = { progname, new1168 Character (argv[optind].charAt(0)).toString(),1167 Object[] msgArgs = { progname, 1168 Character.toString(argv[optind].charAt(0)), 1169 1169 nextchar }; 1170 1170 System.err.println(MessageFormat.format( 1171 _messages.getString("getopt.unrecognized2"), 1171 _messages.getString("getopt.unrecognized2"), 1172 1172 msgArgs)); 1173 1173 } … … 1177 1177 ++optind; 1178 1178 optopt = 0; 1179 1179 1180 1180 return('?'); 1181 1181 } … … 1188 1188 else 1189 1189 nextchar = ""; 1190 1190 1191 1191 String temp = null; 1192 1192 if (optstring.indexOf(c) != -1) … … 1203 1203 { 1204 1204 // 1003.2 specifies the format of this message 1205 Object[] msgArgs = { progname, new1206 Character ((char)c).toString() };1205 Object[] msgArgs = { progname, 1206 Character.toString((char)c) }; 1207 1207 System.err.println(MessageFormat.format( 1208 1208 _messages.getString("getopt.illegal"), msgArgs)); … … 1210 1210 else 1211 1211 { 1212 Object[] msgArgs = { progname, new1213 Character ((char)c).toString() };1212 Object[] msgArgs = { progname, 1213 Character.toString((char)c) }; 1214 1214 System.err.println(MessageFormat.format( 1215 1215 _messages.getString("getopt.invalid"), msgArgs)); … … 1234 1234 if (opterr) 1235 1235 { 1236 // 1003.2 specifies the format of this message. 1237 Object[] msgArgs = { progname, new1238 Character ((char)c).toString() };1236 // 1003.2 specifies the format of this message. 1237 Object[] msgArgs = { progname, 1238 Character.toString((char)c) }; 1239 1239 System.err.println(MessageFormat.format( 1240 1240 _messages.getString("getopt.requires2"), msgArgs)); … … 1250 1250 { 1251 1251 // 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. 1253 1253 nextchar = argv[optind]; 1254 1254 optarg = argv[optind]; … … 1297 1297 { 1298 1298 // 1003.2 specifies the format of this message 1299 Object[] msgArgs = { progname, new1300 Character ((char)c).toString() };1299 Object[] msgArgs = { progname, 1300 Character.toString((char)c) }; 1301 1301 System.err.println(MessageFormat.format( 1302 1302 _messages.getString("getopt.requires2"), msgArgs)); … … 1304 1304 1305 1305 optopt = c; 1306 1306 1307 1307 if (optstring.charAt(0) == ':') 1308 1308 return(':'); … … 1327 1327 { 1328 1328 // 1003.2 specifies the format of this message 1329 Object[] msgArgs = { progname, new1330 Character ((char)c).toString() };1329 Object[] msgArgs = { progname, 1330 Character.toString((char)c) }; 1331 1331 System.err.println(MessageFormat.format( 1332 1332 _messages.getString("getopt.requires2"), msgArgs)); … … 1334 1334 1335 1335 optopt = c; 1336 1336 1337 1337 if (optstring.charAt(0) == ':') 1338 1338 return(':'); -
trunk/src/gnu/getopt/LongOpt.java
r5279 r10209 5 5 /* 6 6 /* 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 8 8 /* by the Free Software Foundation; either version 2 of the License or 9 9 /* (at your option) any later version. … … 15 15 /* 16 16 /* 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, 19 19 /* Boston, MA 02111-1307 USA 20 20 /**************************************************************************/ … … 22 22 package gnu.getopt; 23 23 24 import java.util.Locale;25 import java.util.ResourceBundle;26 24 import java.text.MessageFormat; 27 25 … … 34 32 * session. Refer to the getopt documentation for details on the 35 33 * format of long options. 36 * 34 * 37 35 * @version 1.0.5 38 36 * @author Aaron M. Renn (arenn@urbanophile.com) … … 55 53 public static final int NO_ARGUMENT = 0; 56 54 57 /** 55 /** 58 56 * Constant value used for the "has_arg" constructor argument. This 59 57 * value indicates that the option takes an argument that is required. … … 116 114 * @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. 117 115 * @param val The value to return for this long option, or the equivalent single letter option to emulate if flag is null. 118 * 116 * 119 117 * @exception IllegalArgumentException If the has_arg param is not one of NO_ARGUMENT, REQUIRED_ARGUMENT or OPTIONAL_ARGUMENT. 120 118 */ 121 119 public 122 LongOpt(String name, int has_arg, 120 LongOpt(String name, int has_arg, 123 121 StringBuffer flag, int val) throws IllegalArgumentException 124 122 { 125 123 // Validate has_arg 126 if ((has_arg != NO_ARGUMENT) && (has_arg != REQUIRED_ARGUMENT) 124 if ((has_arg != NO_ARGUMENT) && (has_arg != REQUIRED_ARGUMENT) 127 125 && (has_arg != OPTIONAL_ARGUMENT)) 128 126 { 129 Object[] msgArgs = { new Integer(has_arg).toString() };127 Object[] msgArgs = { Integer.toString(has_arg) }; 130 128 throw new IllegalArgumentException(MessageFormat.format( 131 129 _messages.getString("getopt.invalidValue"), msgArgs));
Note:
See TracChangeset
for help on using the changeset viewer.