1 22 23 package gnu.getopt; 24 25 import java.util.Locale ; 26 import java.util.ResourceBundle ; 27 import java.util.PropertyResourceBundle ; 28 import java.text.MessageFormat ; 29 30 31 32 377 public class Getopt extends Object 378 { 379 380 381 382 385 386 404 protected static final int REQUIRE_ORDER = 1; 405 406 412 protected static final int PERMUTE = 2; 413 414 422 protected static final int RETURN_IN_ORDER = 3; 423 424 425 426 429 430 437 protected String optarg; 438 439 452 protected int optind = 0; 453 454 458 protected boolean opterr = true; 459 460 464 protected int optopt = '?'; 465 466 474 protected String nextchar; 475 476 479 protected String optstring; 480 481 485 protected LongOpt[] long_options; 486 487 490 protected boolean long_only; 491 492 495 protected int longind; 496 497 500 protected boolean posixly_correct; 501 502 506 protected boolean longopt_handled; 507 508 511 protected int first_nonopt = 1; 512 513 516 protected int last_nonopt = 1; 517 518 521 protected String [] argv; 522 523 526 protected int ordering; 527 528 532 protected String progname; 533 534 537 private ResourceBundle _messages = PropertyResourceBundle.getBundle( 538 "gnu/getopt/MessagesBundle", Locale.getDefault()); 539 540 541 542 545 546 554 public 555 Getopt(String progname, String [] argv, String optstring) 556 { 557 this(progname, argv, optstring, null, false); 558 } 559 560 561 562 571 public 572 Getopt(String progname, String [] argv, String optstring, 573 LongOpt[] long_options) 574 { 575 this(progname, argv, optstring, long_options, false); 576 } 577 578 579 580 594 public 595 Getopt(String progname, String [] argv, String optstring, 596 LongOpt[] long_options, boolean long_only) 597 { 598 if (optstring.length() == 0) 599 optstring = " "; 600 601 this.progname = progname; 603 this.argv = argv; 604 this.optstring = optstring; 605 this.long_options = long_options; 606 this.long_only = long_only; 607 608 if (System.getProperty("gnu.posixly_correct", null) == null) 612 posixly_correct = false; 613 else 614 { 615 posixly_correct = true; 616 _messages = PropertyResourceBundle.getBundle("gnu/getopt/MessagesBundle", 617 Locale.US); 618 } 619 620 if (optstring.charAt(0) == '-') 622 { 623 ordering = RETURN_IN_ORDER; 624 if (optstring.length() > 1) 625 this.optstring = optstring.substring(1); 626 } 627 else if (optstring.charAt(0) == '+') 628 { 629 ordering = REQUIRE_ORDER; 630 if (optstring.length() > 1) 631 this.optstring = optstring.substring(1); 632 } 633 else if (posixly_correct) 634 { 635 ordering = REQUIRE_ORDER; 636 } 637 else 638 { 639 ordering = PERMUTE; } 641 } 642 643 644 645 648 649 657 public void 658 setOptstring(String optstring) 659 { 660 if (optstring.length() == 0) 661 optstring = " "; 662 663 this.optstring = optstring; 664 } 665 666 667 668 679 public int 680 getOptind() 681 { 682 return(optind); 683 } 684 685 686 687 695 public void 696 setOptind(int optind) 697 { 698 this.optind = optind; 699 } 700 701 702 703 712 public void 713 setArgv(String [] argv) 714 { 715 this.argv = argv; 716 } 717 718 719 720 728 public String 729 getOptarg() 730 { 731 return(optarg); 732 } 733 734 735 736 742 public void 743 setOpterr(boolean opterr) 744 { 745 this.opterr = opterr; 746 } 747 748 749 750 755 public int 756 getOptopt() 757 { 758 return(optopt); 759 } 760 761 762 763 767 public int 768 getLongind() 769 { 770 return(longind); 771 } 772 773 774 775 782 protected void 783 exchange(String [] argv) 784 { 785 int bottom = first_nonopt; 786 int middle = last_nonopt; 787 int top = optind; 788 String tem; 789 790 while (top > middle && middle > bottom) 791 { 792 if (top - middle > middle - bottom) 793 { 794 int len = middle - bottom; 796 int i; 797 798 for (i = 0; i < len; i++) 800 { 801 tem = argv[bottom + i]; 802 argv[bottom + i] = argv[top - (middle - bottom) + i]; 803 argv[top - (middle - bottom) + i] = tem; 804 } 805 top -= len; 807 } 808 else 809 { 810 int len = top - middle; 812 int i; 813 814 for (i = 0; i < len; i++) 816 { 817 tem = argv[bottom + i]; 818 argv[bottom + i] = argv[middle + i]; 819 argv[middle + i] = tem; 820 } 821 bottom += len; 823 } 824 } 825 826 828 first_nonopt += (optind - last_nonopt); 829 last_nonopt = optind; 830 } 831 832 833 834 843 protected int 844 checkLongOption() 845 { 846 LongOpt pfound = null; 847 int nameend; 848 boolean ambig; 849 boolean exact; 850 851 longopt_handled = true; 852 ambig = false; 853 exact = false; 854 longind = -1; 855 856 nameend = nextchar.indexOf("="); 857 if (nameend == -1) 858 nameend = nextchar.length(); 859 860 for (int i = 0; i < long_options.length; i++) 862 { 863 if (long_options[i].getName().startsWith(nextchar.substring(0, nameend))) 864 { 865 if (long_options[i].getName().equals(nextchar.substring(0, nameend))) 866 { 867 pfound = long_options[i]; 869 longind = i; 870 exact = true; 871 break; 872 } 873 else if (pfound == null) 874 { 875 pfound = long_options[i]; 877 longind = i; 878 } 879 else 880 { 881 ambig = true; 883 } 884 } 885 } 887 if (ambig && !exact) 889 { 890 if (opterr) 891 { 892 Object [] msgArgs = { progname, argv[optind] }; 893 System.err.println(MessageFormat.format( 894 _messages.getString("getopt.ambigious"), 895 msgArgs)); 896 } 897 898 nextchar = ""; 899 optopt = 0; 900 ++optind; 901 902 return('?'); 903 } 904 905 if (pfound != null) 906 { 907 ++optind; 908 909 if (nameend != nextchar.length()) 910 { 911 if (pfound.has_arg != LongOpt.NO_ARGUMENT) 912 { 913 if (nextchar.substring(nameend).length() > 1) 914 optarg = nextchar.substring(nameend+1); 915 else 916 optarg = ""; 917 } 918 else 919 { 920 if (opterr) 921 { 922 if (argv[optind - 1].startsWith("--")) 924 { 925 Object [] msgArgs = { progname, pfound.name }; 926 System.err.println(MessageFormat.format( 927 _messages.getString("getopt.arguments1"), 928 msgArgs)); 929 } 930 else 932 { 933 Object [] msgArgs = { progname, new 934 Character (argv[optind-1].charAt(0)).toString(), 935 pfound.name }; 936 System.err.println(MessageFormat.format( 937 _messages.getString("getopt.arguments2"), 938 msgArgs)); 939 } 940 } 941 942 nextchar = ""; 943 optopt = pfound.val; 944 945 return('?'); 946 } 947 } else if (pfound.has_arg == LongOpt.REQUIRED_ARGUMENT) 949 { 950 if (optind < argv.length) 951 { 952 optarg = argv[optind]; 953 ++optind; 954 } 955 else 956 { 957 if (opterr) 958 { 959 Object [] msgArgs = { progname, argv[optind-1] }; 960 System.err.println(MessageFormat.format( 961 _messages.getString("getopt.requires"), 962 msgArgs)); 963 } 964 965 nextchar = ""; 966 optopt = pfound.val; 967 if (optstring.charAt(0) == ':') 968 return(':'); 969 else 970 return('?'); 971 } 972 } 974 nextchar = ""; 975 976 if (pfound.flag != null) 977 { 978 pfound.flag.setLength(0); 979 pfound.flag.append(pfound.val); 980 981 return(0); 982 } 983 984 return(pfound.val); 985 } 987 longopt_handled = false; 988 989 return(0); 990 } 991 992 993 994 1008public int 1009getopt() 1010{ 1011 optarg = null; 1012 1013 if ((nextchar == null) || (nextchar.equals(""))) 1014 { 1015 if (last_nonopt > optind) 1018 last_nonopt = optind; 1019 if (first_nonopt > optind) 1020 first_nonopt = optind; 1021 1022 if (ordering == PERMUTE) 1023 { 1024 if ((first_nonopt != last_nonopt) && (last_nonopt != optind)) 1027 exchange(argv); 1028 else if (last_nonopt != optind) 1029 first_nonopt = optind; 1030 1031 while ((optind < argv.length) && (argv[optind].equals("") || 1034 (argv[optind].charAt(0) != '-') || argv[optind].equals("-"))) 1035 { 1036 optind++; 1037 } 1038 1039 last_nonopt = optind; 1040 } 1041 1042 if ((optind != argv.length) && argv[optind].equals("--")) 1047 { 1048 optind++; 1049 1050 if ((first_nonopt != last_nonopt) && (last_nonopt != optind)) 1051 exchange (argv); 1052 else if (first_nonopt == last_nonopt) 1053 first_nonopt = optind; 1054 1055 last_nonopt = argv.length; 1056 1057 optind = argv.length; 1058 } 1059 1060 if (optind == argv.length) 1063 { 1064 if (first_nonopt != last_nonopt) 1067 optind = first_nonopt; 1068 1069 return(-1); 1070 } 1071 1072 if (argv[optind].equals("") || (argv[optind].charAt(0) != '-') || 1075 argv[optind].equals("-")) 1076 { 1077 if (ordering == REQUIRE_ORDER) 1078 return(-1); 1079 1080 optarg = argv[optind++]; 1081 return(1); 1082 } 1083 1084 if (argv[optind].startsWith("--")) 1087 nextchar = argv[optind].substring(2); 1088 else 1089 nextchar = argv[optind].substring(1); 1090 } 1091 1092 1094 1106 if ((long_options != null) && (argv[optind].startsWith("--") 1107 || (long_only && ((argv[optind].length() > 2) || 1108 (optstring.indexOf(argv[optind].charAt(1)) == -1))))) 1109 { 1110 int c = checkLongOption(); 1111 1112 if (longopt_handled) 1113 return(c); 1114 1115 if (!long_only || argv[optind].startsWith("--") 1120 || (optstring.indexOf(nextchar.charAt(0)) == -1)) 1121 { 1122 if (opterr) 1123 { 1124 if (argv[optind].startsWith("--")) 1125 { 1126 Object [] msgArgs = { progname, nextchar }; 1127 System.err.println(MessageFormat.format( 1128 _messages.getString("getopt.unrecognized"), 1129 msgArgs)); 1130 } 1131 else 1132 { 1133 Object [] msgArgs = { progname, new 1134 Character (argv[optind].charAt(0)).toString(), 1135 nextchar }; 1136 System.err.println(MessageFormat.format( 1137 _messages.getString("getopt.unrecognized2"), 1138 msgArgs)); 1139 } 1140 } 1141 1142 nextchar = ""; 1143 ++optind; 1144 optopt = 0; 1145 1146 return('?'); 1147 } 1148 } 1150 int c = nextchar.charAt(0); if (nextchar.length() > 1) 1153 nextchar = nextchar.substring(1); 1154 else 1155 nextchar = ""; 1156 1157 String temp = null; 1158 if (optstring.indexOf(c) != -1) 1159 temp = optstring.substring(optstring.indexOf(c)); 1160 1161 if (nextchar.equals("")) 1162 ++optind; 1163 1164 if ((temp == null) || (c == ':')) 1165 { 1166 if (opterr) 1167 { 1168 if (posixly_correct) 1169 { 1170 Object [] msgArgs = { progname, new 1172 Character ((char)c).toString() }; 1173 System.err.println(MessageFormat.format( 1174 _messages.getString("getopt.illegal"), msgArgs)); 1175 } 1176 else 1177 { 1178 Object [] msgArgs = { progname, new 1179 Character ((char)c).toString() }; 1180 System.err.println(MessageFormat.format( 1181 _messages.getString("getopt.invalid"), msgArgs)); 1182 } 1183 } 1184 1185 optopt = c; 1186 1187 return('?'); 1188 } 1189 1190 if ((temp.charAt(0) == 'W') && (temp.length() > 1) && (temp.charAt(1) == ';')) 1192 { 1193 if (!nextchar.equals("")) 1194 { 1195 optarg = nextchar; 1196 } 1197 else if (optind == argv.length) 1199 { 1200 if (opterr) 1201 { 1202 Object [] msgArgs = { progname, new 1204 Character ((char)c).toString() }; 1205 System.err.println(MessageFormat.format( 1206 _messages.getString("getopt.requires2"), msgArgs)); 1207 } 1208 1209 optopt = c; 1210 if (optstring.charAt(0) == ':') 1211 return(':'); 1212 else 1213 return('?'); 1214 } 1215 else 1216 { 1217 nextchar = argv[optind]; 1220 optarg = argv[optind]; 1221 } 1222 1223 c = checkLongOption(); 1224 1225 if (longopt_handled) 1226 return(c); 1227 else 1228 { 1230 nextchar = null; 1231 ++optind; 1232 return('W'); 1233 } 1234 } 1235 1236 if ((temp.length() > 1) && (temp.charAt(1) == ':')) 1237 { 1238 if ((temp.length() > 2) && (temp.charAt(2) == ':')) 1239 { 1241 if (!nextchar.equals("")) 1242 { 1243 optarg = nextchar; 1244 ++optind; 1245 } 1246 else 1247 { 1248 optarg = null; 1249 } 1250 1251 nextchar = null; 1252 } 1253 else 1254 { 1255 if (!nextchar.equals("")) 1256 { 1257 optarg = nextchar; 1258 ++optind; 1259 } 1260 else if (optind == argv.length) 1261 { 1262 if (opterr) 1263 { 1264 Object [] msgArgs = { progname, new 1266 Character ((char)c).toString() }; 1267 System.err.println(MessageFormat.format( 1268 _messages.getString("getopt.requires2"), msgArgs)); 1269 } 1270 1271 optopt = c; 1272 1273 if (optstring.charAt(0) == ':') 1274 return(':'); 1275 else 1276 return('?'); 1277 } 1278 else 1279 { 1280 optarg = argv[optind]; 1281 ++optind; 1282 } 1283 1284 nextchar = null; 1285 } 1286 } 1287 1288 return(c); 1289} 1290 1291} 1293 1294 | Popular Tags |