├── gnu └── getopt │ ├── ChangeLog │ ├── Getopt.class │ ├── LongOpt.class │ ├── GetoptDemo.class │ ├── MessagesBundle_chs.properties │ ├── MessagesBundle_cs.properties │ ├── MessagesBundle_de.properties │ ├── MessagesBundle_es.properties │ ├── MessagesBundle_fr.properties │ ├── MessagesBundle_hu.properties │ ├── MessagesBundle_it.properties │ ├── MessagesBundle_no.properties │ ├── Makefile │ ├── LANGUAGES │ ├── MessagesBundle_cht.properties │ ├── MessagesBundle_nl.properties │ ├── MessagesBundle.properties │ ├── MessagesBundle_pl.properties │ ├── MessagesBundle_ro.properties │ ├── buildx.xml │ ├── MessagesBundle_ja.properties │ ├── README │ ├── GetoptDemo.java │ ├── LongOpt.java │ ├── gnu.getopt.LongOpt.html │ ├── COPYING.LIB │ ├── gnu.getopt.Getopt.html │ └── Getopt.java ├── java-getopt-1.0.13.jar ├── java-getopt-1.0.14.jar ├── java-getopt-1.0.13.tar.gz └── java-getopt-1.0.14.tar.gz /gnu/getopt/ChangeLog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arenn/java-getopt/HEAD/gnu/getopt/ChangeLog -------------------------------------------------------------------------------- /java-getopt-1.0.13.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arenn/java-getopt/HEAD/java-getopt-1.0.13.jar -------------------------------------------------------------------------------- /java-getopt-1.0.14.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arenn/java-getopt/HEAD/java-getopt-1.0.14.jar -------------------------------------------------------------------------------- /gnu/getopt/Getopt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arenn/java-getopt/HEAD/gnu/getopt/Getopt.class -------------------------------------------------------------------------------- /gnu/getopt/LongOpt.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arenn/java-getopt/HEAD/gnu/getopt/LongOpt.class -------------------------------------------------------------------------------- /java-getopt-1.0.13.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arenn/java-getopt/HEAD/java-getopt-1.0.13.tar.gz -------------------------------------------------------------------------------- /java-getopt-1.0.14.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arenn/java-getopt/HEAD/java-getopt-1.0.14.tar.gz -------------------------------------------------------------------------------- /gnu/getopt/GetoptDemo.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arenn/java-getopt/HEAD/gnu/getopt/GetoptDemo.class -------------------------------------------------------------------------------- /gnu/getopt/MessagesBundle_chs.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arenn/java-getopt/HEAD/gnu/getopt/MessagesBundle_chs.properties -------------------------------------------------------------------------------- /gnu/getopt/MessagesBundle_cs.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arenn/java-getopt/HEAD/gnu/getopt/MessagesBundle_cs.properties -------------------------------------------------------------------------------- /gnu/getopt/MessagesBundle_de.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arenn/java-getopt/HEAD/gnu/getopt/MessagesBundle_de.properties -------------------------------------------------------------------------------- /gnu/getopt/MessagesBundle_es.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arenn/java-getopt/HEAD/gnu/getopt/MessagesBundle_es.properties -------------------------------------------------------------------------------- /gnu/getopt/MessagesBundle_fr.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arenn/java-getopt/HEAD/gnu/getopt/MessagesBundle_fr.properties -------------------------------------------------------------------------------- /gnu/getopt/MessagesBundle_hu.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arenn/java-getopt/HEAD/gnu/getopt/MessagesBundle_hu.properties -------------------------------------------------------------------------------- /gnu/getopt/MessagesBundle_it.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arenn/java-getopt/HEAD/gnu/getopt/MessagesBundle_it.properties -------------------------------------------------------------------------------- /gnu/getopt/MessagesBundle_no.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/arenn/java-getopt/HEAD/gnu/getopt/MessagesBundle_no.properties -------------------------------------------------------------------------------- /gnu/getopt/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for Java port of GNU getopt 2 | 3 | all: 4 | javac LongOpt.java Getopt.java GetoptDemo.java 5 | 6 | docs: 7 | javadoc -author -version -public Getopt.java LongOpt.java 8 | -------------------------------------------------------------------------------- /gnu/getopt/LANGUAGES: -------------------------------------------------------------------------------- 1 | Getopt supports error messages in the following languages: 2 | 3 | English (default) 4 | Chinese (simplified and traditional) 5 | Czech 6 | Dutch 7 | French 8 | German 9 | Hungarian 10 | Italian 11 | Japanese 12 | Norwegian 13 | Polish 14 | Romanian 15 | Spanish 16 | 17 | Anyone can create a translation without knowing how to program Java. 18 | Simply supply me with a MessagesBundle file for your locale and I'm happy 19 | to include it. See MessagesBundle.properties as an example with reference 20 | English messages. 21 | 22 | Aaron (arenn@urbanophile.com) 23 | 24 | -------------------------------------------------------------------------------- /gnu/getopt/MessagesBundle_cht.properties: -------------------------------------------------------------------------------- 1 | /************************************************************************** 2 | /* MessagesBundle.properties - Triditional Chinese language error messages 3 | /* 4 | /* Copyright (c) 2012 by David Zhang (david290@qq.com) 5 | /* 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 8 | /* by the Free Software Foundation; either version 2 of the License or 9 | /* (at your option) any later version. 10 | /* 11 | /* This program is distributed in the hope that it will be useful, but 12 | /* WITHOUT ANY WARRANTY; without even the implied warranty of 13 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | /* GNU Library General Public License for more details. 15 | /* 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, 19 | /* Boston, MA 02111-1307 USA 20 | /**************************************************************************/ 21 | 22 | getopt.ambigious={0}: 選項 ''{1}'' 有歧義 23 | getopt.arguments1={0}:選項''--{1}'' 不能帶參數 24 | getopt.arguments2={0}:選項''{1}{2}''不能帶參數 25 | getopt.requires={0}: 選項 ''{1}'' 要求帶有參數 26 | getopt.unrecognized={0}: 無法識別的選項 ''--{1}'' 27 | getopt.unrecognized2={0}:無法識別的選項''{1}{2}'' 28 | getopt.illegal={0}: 非法選項 -- {1} 29 | getopt.invalid={0}: 無效選項 -- {1} 30 | getopt.requires2={0}:選項需要有參數 -- {1} 31 | getopt.invalidValue=選項 'has_arg'的值 {0} 非法 32 | 33 | 34 | -------------------------------------------------------------------------------- /gnu/getopt/MessagesBundle_nl.properties: -------------------------------------------------------------------------------- 1 | /************************************************************************** 2 | /* MessagesBundle_nl.properties -- Dutch language error messages 3 | /* 4 | /* Copyright (c) 1999 by Ernst de Haan (ernst@jollem.com) 5 | /* 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 8 | /* by the Free Software Foundation; either version 2 of the License or 9 | /* (at your option) any later version. 10 | /* 11 | /* This program is distributed in the hope that it will be useful, but 12 | /* WITHOUT ANY WARRANTY; without even the implied warranty of 13 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | /* GNU Library General Public License for more details. 15 | /* 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, 19 | /* Boston, MA 02111-1307 USA 20 | /**************************************************************************/ 21 | 22 | getopt.ambigious={0}: optie ''{1}'' is ambigue 23 | getopt.arguments1={0}: optie ''--{1}'' staat geen argumenten toe 24 | getopt.arguments2={0}: optie ''{1}{2}'' staat geen argumenten toe 25 | getopt.requires={0}: optie ''{1}'' heeft een argument nodig 26 | getopt.unrecognized={0}: onbekende optie ''--{1}'' 27 | getopt.unrecognized2={0}: onbekende optie ''{1}{2}'' 28 | getopt.illegal={0}: niet-toegestane optie -- {1} 29 | getopt.invalid={0}: onjuiste optie -- {1} 30 | getopt.requires2={0}: optie heeft een argument nodig -- {1} 31 | getopt.invalidValue=Ongeldige waarde {0} voor parameter 'has_arg' 32 | 33 | -------------------------------------------------------------------------------- /gnu/getopt/MessagesBundle.properties: -------------------------------------------------------------------------------- 1 | /************************************************************************** 2 | /* MessagesBundle.properties -- English language error messages 3 | /* 4 | /* Copyright (c) 1998 by William King (wrking@eng.sun.com) and 5 | /* Aaron M. Renn (arenn@urbanophile.com) 6 | /* 7 | /* This program is free software; you can redistribute it and/or modify 8 | /* it under the terms of the GNU Library General Public License as published 9 | /* by the Free Software Foundation; either version 2 of the License or 10 | /* (at your option) any later version. 11 | /* 12 | /* This program is distributed in the hope that it will be useful, but 13 | /* WITHOUT ANY WARRANTY; without even the implied warranty of 14 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | /* GNU Library General Public License for more details. 16 | /* 17 | /* You should have received a copy of the GNU Library General Public License 18 | /* along with this program; see the file COPYING.LIB. If not, write to 19 | /* the Free Software Foundation Inc., 59 Temple Place - Suite 330, 20 | /* Boston, MA 02111-1307 USA 21 | /**************************************************************************/ 22 | 23 | getopt.ambigious={0}: option ''{1}'' is ambiguous 24 | getopt.arguments1={0}: option ''--{1}'' doesn't allow an argument 25 | getopt.arguments2={0}: option ''{1}{2}'' doesn't allow an argument 26 | getopt.requires={0}: option ''{1}'' requires an argument 27 | getopt.unrecognized={0}: unrecognized option ''--{1}'' 28 | getopt.unrecognized2={0}: unrecognized option ''{1}{2}'' 29 | getopt.illegal={0}: illegal option -- {1} 30 | getopt.invalid={0}: invalid option -- {1} 31 | getopt.requires2={0}: option requires an argument -- {1} 32 | getopt.invalidValue=Invalid value {0} for parameter 'has_arg' 33 | 34 | -------------------------------------------------------------------------------- /gnu/getopt/MessagesBundle_pl.properties: -------------------------------------------------------------------------------- 1 | /************************************************************************** 2 | /* MessagesBundle_pl.properties -- Polish language error messages 3 | /* 4 | /* Polish Messages Copyright (c) 2006 by Krzysztof Szyma?ski (sirch.s@gmail.com) 5 | /* These messages are encoded in ISO-8859-2 6 | /* 7 | /* This program is free software; you can redistribute it and/or modify 8 | /* it under the terms of the GNU Library General Public License as published 9 | /* by the Free Software Foundation; either version 2 of the License or 10 | /* (at your option) any later version. 11 | /* 12 | /* This program is distributed in the hope that it will be useful, but 13 | /* WITHOUT ANY WARRANTY; without even the implied warranty of 14 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | /* GNU Library General Public License for more details. 16 | /* 17 | /* You should have received a copy of the GNU Library General Public License 18 | /* along with this program; see the file COPYING.LIB. If not, write to 19 | /* the Free Software Foundation Inc., 59 Temple Place - Suite 330, 20 | /* Boston, MA 02111-1307 USA 21 | /**************************************************************************/ 22 | 23 | 24 | getopt.ambigious={0}: opcja ''{1}''jest wieloznaczna 25 | getopt.arguments1={0}: opcja ''--{1}'' nie akceptuje argumentu 26 | getopt.arguments2={0}: opcja ''{1}{2}'' nie akceptuje argumentu 27 | getopt.requires={0}: opcja ''{1}'' wymaga argumentu 28 | getopt.unrecognized={0}: nierozpoznana opcja ''--{1}'' 29 | getopt.unrecognized2={0}: nierozpoznana opcja ''{1}{2}'' 30 | getopt.illegal={0}: nie dopuszczalna opcja --{1} 31 | getopt.invalid={0}: b??dna opcja --{1} 32 | getopt.requires2={0}: opcja --{1} oczekuje argumentu 33 | getopt.invalidValue=Nie poprawna warto?? {0} argument 'has_arg' 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /gnu/getopt/MessagesBundle_ro.properties: -------------------------------------------------------------------------------- 1 | /************************************************************************** 2 | /* MessagesBundle_fr.properties -- Romanian language error messages 3 | /* 4 | /* Copyright (c) 1999 Free Software Foundation, Inc. 5 | /* Marian-Nicolae Ion , 2004, 6 | /* These messages are encoded in ISO-8859-2 7 | /* 8 | /* This program is free software; you can redistribute it and/or modify 9 | /* it under the terms of the GNU Library General Public License as published 10 | /* by the Free Software Foundation; either version 2 of the License or 11 | /* (at your optiunea) any later version. 12 | /* 13 | /* This program is distributed in the hope that it will be useful, but 14 | /* WITHOUT ANY WARRANTY; without even the implied warranty of 15 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | /* GNU Library General Public License for more details. 17 | /* 18 | /* You should have received a copy of the GNU Library General Public License 19 | /* along with this program; see the file COPYING.LIB. If not, write to 20 | /* the Free Software Foundation Inc., 59 Temple Place - Suite 330, 21 | /* Boston, MA 02111-1307 USA 22 | /**************************************************************************/ 23 | 24 | getopt.ambigious={0}: opţiunea ''{1}'' este ambiguă 25 | getopt.arguments1={0}: opţiunea ''--{1}'' nu acceptă parametru 26 | getopt.arguments2={0}: opţiunea ''{1}{2}'' nu acceptă parametru 27 | getopt.requires={0}: opţiunea ''{1}'' cere un parametru 28 | getopt.unrecognized={0}: opţiune necunoscută ''--{1}'' 29 | getopt.unrecognized2={0}: opţiune necunoscută ''{1}{2}'' 30 | getopt.illegal={0}: opţiune ilegală -- {1} 31 | getopt.invalid={0}: opţiune invalidă -- {1} 32 | getopt.requires2={0}: această opţiune cere un parametru -- {1} 33 | getopt.invalidValue=Valoare invalidă {0} pentru parametrul 'has_arg' 34 | 35 | -------------------------------------------------------------------------------- /gnu/getopt/buildx.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /gnu/getopt/MessagesBundle_ja.properties: -------------------------------------------------------------------------------- 1 | /************************************************************************** 2 | /* MessagesBundle.properties -- Japanese language error messages 3 | /* 4 | /* Copyright (c) 2001 by Yasuoka Masahiko (yasuoka@yasuoka.net) 5 | /* 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 8 | /* by the Free Software Foundation; either version 2 of the License or 9 | /* (at your option) any later version. 10 | /* 11 | /* This program is distributed in the hope that it will be useful, but 12 | /* WITHOUT ANY WARRANTY; without even the implied warranty of 13 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | /* GNU Library General Public License for more details. 15 | /* 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, 19 | /* Boston, MA 02111-1307 USA 20 | /**************************************************************************/ 21 | 22 | getopt.ambigious={0}: ''{1}'' \u30aa\u30d7\u30b7\u30e7\u30f3\u306f\u66d6\u6627\u3067\u3059\u3002 23 | getopt.arguments1={0}: ''--{1}'' \u30aa\u30d7\u30b7\u30e7\u30f3\u306f\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u3082\u3061\u307e\u305b\u3093\u3002 24 | getopt.arguments2={0}: ''{1}{2}'' \u30aa\u30d7\u30b7\u30e7\u30f3\u306f\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u3082\u3061\u307e\u305b\u3093\u3002 25 | getopt.requires={0}: ''{1}'' \u30aa\u30d7\u30b7\u30e7\u30f3\u306b\u306f\u30d1\u30e9\u30e1\u30fc\u30bf\u304c\u5fc5\u8981\u3067\u3059\u3002 26 | getopt.unrecognized={0}: ''--{1}'' \u306f\u7121\u52b9\u306a\u30aa\u30d7\u30b7\u30e7\u30f3\u3067\u3059\u3002 27 | getopt.unrecognized2={0}: ''{1}{2}'' \u306f\u7121\u52b9\u306a\u30aa\u30d7\u30b7\u30e7\u30f3\u3067\u3059\u3002 28 | getopt.illegal={0}: -- {1} \u306f\u4e0d\u6b63\u306a\u30aa\u30d7\u30b7\u30e7\u30f3\u3067\u3059\u3002 29 | getopt.invalid={0}: -- {1} \u30aa\u30d7\u30b7\u30e7\u30f3\u306f\u6b63\u3057\u304f\u3042\u308a\u307e\u305b\u3093\u3002 30 | getopt.requires2={0}: -- {1} \u30aa\u30d7\u30b7\u30e7\u30f3\u306b\u306f\u30d1\u30e9\u30e1\u30fc\u30bf\u304c\u5fc5\u8981\u3067\u3059\u3002 31 | getopt.invalidValue={0} \u306f\u3001'has_arg' \u30d1\u30e9\u30e1\u30fc\u30bf\u3068\u3057\u3066\u4e0d\u6b63\u306a\u5024\u3067\u3059\u3002 32 | 33 | -------------------------------------------------------------------------------- /gnu/getopt/README: -------------------------------------------------------------------------------- 1 | This is a Java port of the GNU getopt functions based on the versions 2 | contained in glibc 2.0.6. I have attempted to keep the functionality 3 | and programmer's interface as faithful to the original as possible. 4 | However, due to differences between Java and C, some minor changes 5 | has to me made. (Given the obtuse interface in the clib version, 6 | perhaps some major changes should have been made). This should not 7 | affect the way options appear to be parsed to the end user of program 8 | that uses this Java getopt. The use of these classes are completely 9 | documented in the javadoc comments, so I will not repeat that info here. 10 | 11 | Note that since these objects are part of a package called "gnu.getopt", 12 | they need to be in a subdirectory called gnu/getopt somewhere in your 13 | CLASSPATH. This includes the "MessagesBundle" files. 14 | 15 | I am not aware of any bugs. If you find one though, please send email 16 | to me at arenn@urbanophile.com. The more detailed a bug report the better. 17 | Bug fixes are also welcome at the same address. Please reference 18 | release number "1.0.13". If you use this code, it would be helpful 19 | if you let me know so that I can let you know if anything changes or 20 | if any major bugs have been found/fixed. 21 | 22 | I have included a Makefile for compiling the code. If you do not have 23 | access to make, then you can simply do a "javac *.java" at the OS 24 | command line (or follow your vendor's instructions for compiling a 25 | Java class). To build the documentation, do a "make docs" 26 | or "javadoc -public *.java". Note that the images needed by the html 27 | generated by javadoc are not included. You will need to get those 28 | from some other Java documentation package. 29 | 30 | Note that the Makefile is not compliant with the GNU makefile 31 | standards as I anticipate that at some point a master makefile will 32 | be created for various GNU Java packages. And it is serious overkill 33 | to create a megabloat makefile (kinda like this megabloat README) for 34 | such a simple package. 35 | 36 | There is sample code showing how to use getopt available in the 37 | GetoptDemo.java file. 38 | 39 | NEW: A support file for the "ant" build process was contributed. Here are 40 | some brief things you can do with it. Note that I have not ever used this 41 | so it is doubly AS IS. 42 | 43 | Get ant from jakarta project (see jakarta.apache.org/ant), and run it with 44 | one of these target (all is default target): 45 | ant prepare: create the needed directories 46 | ant classes: compile the java classes 47 | ant jar: create the jar archive 48 | ant javadoc: create the javadoc 49 | ant all: create jar and javadoc 50 | ant clean: clean everything 51 | 52 | Happy hacking, 53 | 54 | Aaron. 55 | arenn@urbanophile.com 56 | http://www.urbanophile.com/arenn/ 57 | 58 | -------------------------------------------------------------------------------- /gnu/getopt/GetoptDemo.java: -------------------------------------------------------------------------------- 1 | import gnu.getopt.LongOpt; 2 | import gnu.getopt.Getopt; 3 | 4 | /* 5 | * This sample code was written by Aaron M. Renn and is a demonstration 6 | * of how to utilize some of the features of the GNU getopt package. This 7 | * sample code is hereby placed into the public domain by the author and 8 | * may be used without restriction. 9 | */ 10 | 11 | public class GetoptDemo 12 | { 13 | 14 | public static void 15 | main(String[] argv) 16 | { 17 | int c; 18 | String arg; 19 | LongOpt[] longopts = new LongOpt[3]; 20 | // 21 | StringBuffer sb = new StringBuffer(); 22 | longopts[0] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h'); 23 | longopts[1] = new LongOpt("outputdir", LongOpt.REQUIRED_ARGUMENT, sb, 'o'); 24 | longopts[2] = new LongOpt("maximum", LongOpt.OPTIONAL_ARGUMENT, null, 2); 25 | // 26 | Getopt g = new Getopt("testprog", argv, "-:bc::d:hW;", longopts); 27 | g.setOpterr(false); // We'll do our own error handling 28 | // 29 | while ((c = g.getopt()) != -1) 30 | switch (c) 31 | { 32 | case 0: 33 | arg = g.getOptarg(); 34 | System.out.println("Got long option with value '" + 35 | (char)(new Integer(sb.toString())).intValue() 36 | + "' with argument " + 37 | ((arg != null) ? arg : "null")); 38 | break; 39 | // 40 | case 1: 41 | System.out.println("I see you have return in order set and that " + 42 | "a non-option argv element was just found " + 43 | "with the value '" + g.getOptarg() + "'"); 44 | break; 45 | // 46 | case 2: 47 | arg = g.getOptarg(); 48 | System.out.println("I know this, but pretend I didn't"); 49 | System.out.println("We picked option " + 50 | longopts[g.getLongind()].getName() + 51 | " with value " + 52 | ((arg != null) ? arg : "null")); 53 | break; 54 | // 55 | case 'b': 56 | System.out.println("You picked plain old option " + (char)c); 57 | break; 58 | // 59 | case 'c': 60 | case 'd': 61 | arg = g.getOptarg(); 62 | System.out.println("You picked option '" + (char)c + 63 | "' with argument " + 64 | ((arg != null) ? arg : "null")); 65 | break; 66 | // 67 | case 'h': 68 | System.out.println("I see you asked for help"); 69 | break; 70 | // 71 | case 'W': 72 | System.out.println("Hmmm. You tried a -W with an incorrect long " + 73 | "option name"); 74 | break; 75 | // 76 | case ':': 77 | System.out.println("Doh! You need an argument for option " + 78 | (char)g.getOptopt()); 79 | break; 80 | // 81 | case '?': 82 | System.out.println("The option '" + (char)g.getOptopt() + 83 | "' is not valid"); 84 | break; 85 | // 86 | default: 87 | System.out.println("getopt() returned " + c); 88 | break; 89 | } 90 | // 91 | for (int i = g.getOptind(); i < argv.length ; i++) 92 | System.out.println("Non option argv element: " + argv[i] + "\n"); 93 | } 94 | 95 | } // Class GetoptDemo 96 | 97 | 98 | -------------------------------------------------------------------------------- /gnu/getopt/LongOpt.java: -------------------------------------------------------------------------------- 1 | /************************************************************************** 2 | /* LongOpt.java -- Long option object for Getopt 3 | /* 4 | /* Copyright (c) 1998 by Aaron M. Renn (arenn@urbanophile.com) 5 | /* 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 8 | /* by the Free Software Foundation; either version 2 of the License or 9 | /* (at your option) any later version. 10 | /* 11 | /* This program is distributed in the hope that it will be useful, but 12 | /* WITHOUT ANY WARRANTY; without even the implied warranty of 13 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | /* GNU Library General Public License for more details. 15 | /* 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, 19 | /* Boston, MA 02111-1307 USA 20 | /**************************************************************************/ 21 | 22 | package gnu.getopt; 23 | 24 | import java.util.Locale; 25 | import java.util.ResourceBundle; 26 | import java.text.MessageFormat; 27 | 28 | /**************************************************************************/ 29 | 30 | /** 31 | * This object represents the definition of a long option in the Java port 32 | * of GNU getopt. An array of LongOpt objects is passed to the Getopt 33 | * object to define the list of valid long options for a given parsing 34 | * session. Refer to the getopt documentation for details on the 35 | * format of long options. 36 | * 37 | * @version 1.0.5 38 | * @author Aaron M. Renn (arenn@urbanophile.com) 39 | * 40 | * @see Getopt 41 | */ 42 | public class LongOpt extends Object 43 | { 44 | 45 | /**************************************************************************/ 46 | 47 | /* 48 | * Class Variables 49 | */ 50 | 51 | /** 52 | * Constant value used for the "has_arg" constructor argument. This 53 | * value indicates that the option takes no argument. 54 | */ 55 | public static final int NO_ARGUMENT = 0; 56 | 57 | /** 58 | * Constant value used for the "has_arg" constructor argument. This 59 | * value indicates that the option takes an argument that is required. 60 | */ 61 | public static final int REQUIRED_ARGUMENT = 1; 62 | 63 | /** 64 | * Constant value used for the "has_arg" constructor argument. This 65 | * value indicates that the option takes an argument that is optional. 66 | */ 67 | public static final int OPTIONAL_ARGUMENT = 2; 68 | 69 | /**************************************************************************/ 70 | 71 | /* 72 | * Instance Variables 73 | */ 74 | 75 | /** 76 | * The name of the long option 77 | */ 78 | protected String name; 79 | 80 | /** 81 | * Indicates whether the option has no argument, a required argument, or 82 | * an optional argument. 83 | */ 84 | protected int has_arg; 85 | 86 | /** 87 | * If this variable is not null, then the value stored in "val" is stored 88 | * here when this long option is encountered. If this is null, the value 89 | * stored in "val" is treated as the name of an equivalent short option. 90 | */ 91 | protected StringBuffer flag; 92 | 93 | /** 94 | * The value to store in "flag" if flag is not null, otherwise the 95 | * equivalent short option character for this long option. 96 | */ 97 | protected int val; 98 | 99 | /** 100 | * Localized strings for error messages 101 | */ 102 | private ResourceBundle _messages = ResourceBundle.getBundle( 103 | "gnu/getopt/MessagesBundle", Locale.getDefault()); 104 | 105 | /**************************************************************************/ 106 | 107 | /* 108 | * Constructors 109 | */ 110 | 111 | /** 112 | * Create a new LongOpt object with the given parameter values. If the 113 | * value passed as has_arg is not valid, then an exception is thrown. 114 | * 115 | * @param name The long option String. 116 | * @param has_arg Indicates whether the option has no argument (NO_ARGUMENT), a required argument (REQUIRED_ARGUMENT) or an optional argument (OPTIONAL_ARGUMENT). 117 | * @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. 118 | * @param val The value to return for this long option, or the equivalent single letter option to emulate if flag is null. 119 | * 120 | * @exception IllegalArgumentException If the has_arg param is not one of NO_ARGUMENT, REQUIRED_ARGUMENT or OPTIONAL_ARGUMENT. 121 | */ 122 | public 123 | LongOpt(String name, int has_arg, 124 | StringBuffer flag, int val) throws IllegalArgumentException 125 | { 126 | // Validate has_arg 127 | if ((has_arg != NO_ARGUMENT) && (has_arg != REQUIRED_ARGUMENT) 128 | && (has_arg != OPTIONAL_ARGUMENT)) 129 | { 130 | Object[] msgArgs = { new Integer(has_arg).toString() }; 131 | throw new IllegalArgumentException(MessageFormat.format( 132 | _messages.getString("getopt.invalidValue"), msgArgs)); 133 | } 134 | 135 | // Store off values 136 | this.name = name; 137 | this.has_arg = has_arg; 138 | this.flag = flag; 139 | this.val = val; 140 | } 141 | 142 | /**************************************************************************/ 143 | 144 | /** 145 | * Returns the name of this LongOpt as a String 146 | * 147 | * @return Then name of the long option 148 | */ 149 | public String 150 | getName() 151 | { 152 | return(name); 153 | } 154 | 155 | /**************************************************************************/ 156 | 157 | /** 158 | * Returns the value set for the 'has_arg' field for this long option 159 | * 160 | * @return The value of 'has_arg' 161 | */ 162 | public int 163 | getHasArg() 164 | { 165 | return(has_arg); 166 | } 167 | 168 | /**************************************************************************/ 169 | 170 | /** 171 | * Returns the value of the 'flag' field for this long option 172 | * 173 | * @return The value of 'flag' 174 | */ 175 | public StringBuffer 176 | getFlag() 177 | { 178 | return(flag); 179 | } 180 | 181 | /** 182 | * Returns the value of the 'val' field for this long option 183 | * 184 | * @return The value of 'val' 185 | */ 186 | public int 187 | getVal() 188 | { 189 | return(val); 190 | } 191 | 192 | /**************************************************************************/ 193 | 194 | } // Class LongOpt 195 | 196 | -------------------------------------------------------------------------------- /gnu/getopt/gnu.getopt.LongOpt.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Class gnu.getopt.LongOpt 8 | 9 | 10 | 11 | 12 |
 13 | All Packages  Class Hierarchy  This Package  Previous  Next  Index
14 |
15 |

16 | Class gnu.getopt.LongOpt 17 |

18 |
 19 | java.lang.Object
 20 |    |
 21 |    +----gnu.getopt.LongOpt
 22 | 
23 |
24 |
25 |
public class LongOpt 26 |
extends Object 27 |
28 | This object represents the definition of a long option in the Java port 29 | of GNU getopt. An array of LongOpt objects is passed to the Getopt 30 | object to define the list of valid long options for a given parsing 31 | session. Refer to the getopt documentation for details on the 32 | format of long options. 33 |

34 |

35 |
Version: 36 |
1.0.3 37 |
Author: 38 |
Aaron M. Renn (arenn@urbanophile.com) 39 |
See Also: 40 |
Getopt 41 |
42 |
43 | 44 |

45 | Variable Index 46 |

47 |
48 |
 o 49 | NO_ARGUMENT 50 |
Constant value used for the "has_arg" constructor argument. 51 |
 o 52 | OPTIONAL_ARGUMENT 53 |
Constant value used for the "has_arg" constructor argument. 54 |
 o 55 | REQUIRED_ARGUMENT 56 |
57 | Constant value used for the "has_arg" constructor argument. 58 |
59 |

60 | Constructor Index 61 |

62 |
63 |
 o 64 | LongOpt(String, int, StringBuffer, int) 65 |
Create a new LongOpt object with the given parameter values. 66 |
67 |

68 | Method Index 69 |

70 |
71 |
 o 72 | getFlag() 73 |
Returns the value of the 'flag' field for this long option 74 | 75 | 76 |
 o 77 | getHasArg() 78 |
Returns the value set for the 'has_arg' field for this long option 79 | 80 | 81 |
 o 82 | getName() 83 |
Returns the name of this LongOpt as a String 84 | 85 | 86 |
 o 87 | getVal() 88 |
Returns the value of the 'val' field for this long option 89 | 90 | 91 |
92 | 93 |

94 | Variables 95 |

96 |  o 97 | NO_ARGUMENT 98 |
 99 |  public static final int NO_ARGUMENT
100 | 
101 |
102 |
Constant value used for the "has_arg" constructor argument. This 103 | value indicates that the option takes no argument.

104 |

105 |  o 106 | REQUIRED_ARGUMENT 107 |
108 |  public static final int REQUIRED_ARGUMENT
109 | 
110 |
111 |
Constant value used for the "has_arg" constructor argument. This 112 | value indicates that the option takes an argument that is required.

113 |

114 |  o 115 | OPTIONAL_ARGUMENT 116 |
117 |  public static final int OPTIONAL_ARGUMENT
118 | 
119 |
120 |
Constant value used for the "has_arg" constructor argument. This 121 | value indicates that the option takes an argument that is optional.

122 |

123 | 124 |

125 | Constructors 126 |

127 | 128 |  o 129 | LongOpt 130 |
131 |  public LongOpt(String name,
132 |                 int has_arg,
133 |                 StringBuffer flag,
134 |                 int val) throws IllegalArgumentException
135 | 
136 |
137 |
Create a new LongOpt object with the given parameter values. If the 138 | value passed as has_arg is not valid, then an exception is thrown. 139 |

140 |

141 |
Parameters: 142 |
name - The long option String. 143 |
has_arg - Indicates whether the option has no argument (NO_ARGUMENT), a required argument (REQUIRED_ARGUMENT) or an optional argument (OPTIONAL_ARGUMENT). 144 |
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. 145 |
val - The value to return for this long option, or the equivalent single letter option to emulate if flag is null. 146 |
Throws: IllegalArgumentException 147 |
If the has_arg param is not one of NO_ARGUMENT, REQUIRED_ARGUMENT or OPTIONAL_ARGUMENT. 148 |
149 |
150 | 151 |

152 | Methods 153 |

154 |  o 155 | getName 156 |
157 |  public String getName()
158 | 
159 |
160 |
Returns the name of this LongOpt as a String 161 |

162 |

163 |
Returns: 164 |
Then name of the long option 165 |
166 |
167 |  o 168 | getHasArg 169 |
170 |  public int getHasArg()
171 | 
172 |
173 |
Returns the value set for the 'has_arg' field for this long option 174 |

175 |

176 |
Returns: 177 |
The value of 'has_arg' 178 |
179 |
180 |  o 181 | getFlag 182 |
183 |  public StringBuffer getFlag()
184 | 
185 |
186 |
Returns the value of the 'flag' field for this long option 187 |

188 |

189 |
Returns: 190 |
The value of 'flag' 191 |
192 |
193 |  o 194 | getVal 195 |
196 |  public int getVal()
197 | 
198 |
199 |
Returns the value of the 'val' field for this long option 200 |

201 |

202 |
Returns: 203 |
The value of 'val' 204 |
205 |
206 |
207 |
208 | All Packages  Class Hierarchy  This Package  Previous  Next  Index
209 | 210 | 211 | -------------------------------------------------------------------------------- /gnu/getopt/COPYING.LIB: -------------------------------------------------------------------------------- 1 | GNU LIBRARY GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1991 Free Software Foundation, Inc. 5 | 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | [This is the first released version of the library GPL. It is 10 | numbered 2 because it goes with version 2 of the ordinary GPL.] 11 | 12 | Preamble 13 | 14 | The licenses for most software are designed to take away your 15 | freedom to share and change it. By contrast, the GNU General Public 16 | Licenses are intended to guarantee your freedom to share and change 17 | free software--to make sure the software is free for all its users. 18 | 19 | This license, the Library General Public License, applies to some 20 | specially designated Free Software Foundation software, and to any 21 | other libraries whose authors decide to use it. You can use it for 22 | your libraries, too. 23 | 24 | When we speak of free software, we are referring to freedom, not 25 | price. Our General Public Licenses are designed to make sure that you 26 | have the freedom to distribute copies of free software (and charge for 27 | this service if you wish), that you receive source code or can get it 28 | if you want it, that you can change the software or use pieces of it 29 | in new free programs; and that you know you can do these things. 30 | 31 | To protect your rights, we need to make restrictions that forbid 32 | anyone to deny you these rights or to ask you to surrender the rights. 33 | These restrictions translate to certain responsibilities for you if 34 | you distribute copies of the library, or if you modify it. 35 | 36 | For example, if you distribute copies of the library, whether gratis 37 | or for a fee, you must give the recipients all the rights that we gave 38 | you. You must make sure that they, too, receive or can get the source 39 | code. If you link a program with the library, you must provide 40 | complete object files to the recipients so that they can relink them 41 | with the library, after making changes to the library and recompiling 42 | it. And you must show them these terms so they know their rights. 43 | 44 | Our method of protecting your rights has two steps: (1) copyright 45 | the library, and (2) offer you this license which gives you legal 46 | permission to copy, distribute and/or modify the library. 47 | 48 | Also, for each distributor's protection, we want to make certain 49 | that everyone understands that there is no warranty for this free 50 | library. If the library is modified by someone else and passed on, we 51 | want its recipients to know that what they have is not the original 52 | version, so that any problems introduced by others will not reflect on 53 | the original authors' reputations. 54 | 55 | Finally, any free program is threatened constantly by software 56 | patents. We wish to avoid the danger that companies distributing free 57 | software will individually obtain patent licenses, thus in effect 58 | transforming the program into proprietary software. To prevent this, 59 | we have made it clear that any patent must be licensed for everyone's 60 | free use or not licensed at all. 61 | 62 | Most GNU software, including some libraries, is covered by the ordinary 63 | GNU General Public License, which was designed for utility programs. This 64 | license, the GNU Library General Public License, applies to certain 65 | designated libraries. This license is quite different from the ordinary 66 | one; be sure to read it in full, and don't assume that anything in it is 67 | the same as in the ordinary license. 68 | 69 | The reason we have a separate public license for some libraries is that 70 | they blur the distinction we usually make between modifying or adding to a 71 | program and simply using it. Linking a program with a library, without 72 | changing the library, is in some sense simply using the library, and is 73 | analogous to running a utility program or application program. However, in 74 | a textual and legal sense, the linked executable is a combined work, a 75 | derivative of the original library, and the ordinary General Public License 76 | treats it as such. 77 | 78 | Because of this blurred distinction, using the ordinary General 79 | Public License for libraries did not effectively promote software 80 | sharing, because most developers did not use the libraries. We 81 | concluded that weaker conditions might promote sharing better. 82 | 83 | However, unrestricted linking of non-free programs would deprive the 84 | users of those programs of all benefit from the free status of the 85 | libraries themselves. This Library General Public License is intended to 86 | permit developers of non-free programs to use free libraries, while 87 | preserving your freedom as a user of such programs to change the free 88 | libraries that are incorporated in them. (We have not seen how to achieve 89 | this as regards changes in header files, but we have achieved it as regards 90 | changes in the actual functions of the Library.) The hope is that this 91 | will lead to faster development of free libraries. 92 | 93 | The precise terms and conditions for copying, distribution and 94 | modification follow. Pay close attention to the difference between a 95 | "work based on the library" and a "work that uses the library". The 96 | former contains code derived from the library, while the latter only 97 | works together with the library. 98 | 99 | Note that it is possible for a library to be covered by the ordinary 100 | General Public License rather than by this special one. 101 | 102 | GNU LIBRARY GENERAL PUBLIC LICENSE 103 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 104 | 105 | 0. This License Agreement applies to any software library which 106 | contains a notice placed by the copyright holder or other authorized 107 | party saying it may be distributed under the terms of this Library 108 | General Public License (also called "this License"). Each licensee is 109 | addressed as "you". 110 | 111 | A "library" means a collection of software functions and/or data 112 | prepared so as to be conveniently linked with application programs 113 | (which use some of those functions and data) to form executables. 114 | 115 | The "Library", below, refers to any such software library or work 116 | which has been distributed under these terms. A "work based on the 117 | Library" means either the Library or any derivative work under 118 | copyright law: that is to say, a work containing the Library or a 119 | portion of it, either verbatim or with modifications and/or translated 120 | straightforwardly into another language. (Hereinafter, translation is 121 | included without limitation in the term "modification".) 122 | 123 | "Source code" for a work means the preferred form of the work for 124 | making modifications to it. For a library, complete source code means 125 | all the source code for all modules it contains, plus any associated 126 | interface definition files, plus the scripts used to control compilation 127 | and installation of the library. 128 | 129 | Activities other than copying, distribution and modification are not 130 | covered by this License; they are outside its scope. The act of 131 | running a program using the Library is not restricted, and output from 132 | such a program is covered only if its contents constitute a work based 133 | on the Library (independent of the use of the Library in a tool for 134 | writing it). Whether that is true depends on what the Library does 135 | and what the program that uses the Library does. 136 | 137 | 1. You may copy and distribute verbatim copies of the Library's 138 | complete source code as you receive it, in any medium, provided that 139 | you conspicuously and appropriately publish on each copy an 140 | appropriate copyright notice and disclaimer of warranty; keep intact 141 | all the notices that refer to this License and to the absence of any 142 | warranty; and distribute a copy of this License along with the 143 | Library. 144 | 145 | You may charge a fee for the physical act of transferring a copy, 146 | and you may at your option offer warranty protection in exchange for a 147 | fee. 148 | 149 | 2. You may modify your copy or copies of the Library or any portion 150 | of it, thus forming a work based on the Library, and copy and 151 | distribute such modifications or work under the terms of Section 1 152 | above, provided that you also meet all of these conditions: 153 | 154 | a) The modified work must itself be a software library. 155 | 156 | b) You must cause the files modified to carry prominent notices 157 | stating that you changed the files and the date of any change. 158 | 159 | c) You must cause the whole of the work to be licensed at no 160 | charge to all third parties under the terms of this License. 161 | 162 | d) If a facility in the modified Library refers to a function or a 163 | table of data to be supplied by an application program that uses 164 | the facility, other than as an argument passed when the facility 165 | is invoked, then you must make a good faith effort to ensure that, 166 | in the event an application does not supply such function or 167 | table, the facility still operates, and performs whatever part of 168 | its purpose remains meaningful. 169 | 170 | (For example, a function in a library to compute square roots has 171 | a purpose that is entirely well-defined independent of the 172 | application. Therefore, Subsection 2d requires that any 173 | application-supplied function or table used by this function must 174 | be optional: if the application does not supply it, the square 175 | root function must still compute square roots.) 176 | 177 | These requirements apply to the modified work as a whole. If 178 | identifiable sections of that work are not derived from the Library, 179 | and can be reasonably considered independent and separate works in 180 | themselves, then this License, and its terms, do not apply to those 181 | sections when you distribute them as separate works. But when you 182 | distribute the same sections as part of a whole which is a work based 183 | on the Library, the distribution of the whole must be on the terms of 184 | this License, whose permissions for other licensees extend to the 185 | entire whole, and thus to each and every part regardless of who wrote 186 | it. 187 | 188 | Thus, it is not the intent of this section to claim rights or contest 189 | your rights to work written entirely by you; rather, the intent is to 190 | exercise the right to control the distribution of derivative or 191 | collective works based on the Library. 192 | 193 | In addition, mere aggregation of another work not based on the Library 194 | with the Library (or with a work based on the Library) on a volume of 195 | a storage or distribution medium does not bring the other work under 196 | the scope of this License. 197 | 198 | 3. You may opt to apply the terms of the ordinary GNU General Public 199 | License instead of this License to a given copy of the Library. To do 200 | this, you must alter all the notices that refer to this License, so 201 | that they refer to the ordinary GNU General Public License, version 2, 202 | instead of to this License. (If a newer version than version 2 of the 203 | ordinary GNU General Public License has appeared, then you can specify 204 | that version instead if you wish.) Do not make any other change in 205 | these notices. 206 | 207 | Once this change is made in a given copy, it is irreversible for 208 | that copy, so the ordinary GNU General Public License applies to all 209 | subsequent copies and derivative works made from that copy. 210 | 211 | This option is useful when you wish to copy part of the code of 212 | the Library into a program that is not a library. 213 | 214 | 4. You may copy and distribute the Library (or a portion or 215 | derivative of it, under Section 2) in object code or executable form 216 | under the terms of Sections 1 and 2 above provided that you accompany 217 | it with the complete corresponding machine-readable source code, which 218 | must be distributed under the terms of Sections 1 and 2 above on a 219 | medium customarily used for software interchange. 220 | 221 | If distribution of object code is made by offering access to copy 222 | from a designated place, then offering equivalent access to copy the 223 | source code from the same place satisfies the requirement to 224 | distribute the source code, even though third parties are not 225 | compelled to copy the source along with the object code. 226 | 227 | 5. A program that contains no derivative of any portion of the 228 | Library, but is designed to work with the Library by being compiled or 229 | linked with it, is called a "work that uses the Library". Such a 230 | work, in isolation, is not a derivative work of the Library, and 231 | therefore falls outside the scope of this License. 232 | 233 | However, linking a "work that uses the Library" with the Library 234 | creates an executable that is a derivative of the Library (because it 235 | contains portions of the Library), rather than a "work that uses the 236 | library". The executable is therefore covered by this License. 237 | Section 6 states terms for distribution of such executables. 238 | 239 | When a "work that uses the Library" uses material from a header file 240 | that is part of the Library, the object code for the work may be a 241 | derivative work of the Library even though the source code is not. 242 | Whether this is true is especially significant if the work can be 243 | linked without the Library, or if the work is itself a library. The 244 | threshold for this to be true is not precisely defined by law. 245 | 246 | If such an object file uses only numerical parameters, data 247 | structure layouts and accessors, and small macros and small inline 248 | functions (ten lines or less in length), then the use of the object 249 | file is unrestricted, regardless of whether it is legally a derivative 250 | work. (Executables containing this object code plus portions of the 251 | Library will still fall under Section 6.) 252 | 253 | Otherwise, if the work is a derivative of the Library, you may 254 | distribute the object code for the work under the terms of Section 6. 255 | Any executables containing that work also fall under Section 6, 256 | whether or not they are linked directly with the Library itself. 257 | 258 | 6. As an exception to the Sections above, you may also compile or 259 | link a "work that uses the Library" with the Library to produce a 260 | work containing portions of the Library, and distribute that work 261 | under terms of your choice, provided that the terms permit 262 | modification of the work for the customer's own use and reverse 263 | engineering for debugging such modifications. 264 | 265 | You must give prominent notice with each copy of the work that the 266 | Library is used in it and that the Library and its use are covered by 267 | this License. You must supply a copy of this License. If the work 268 | during execution displays copyright notices, you must include the 269 | copyright notice for the Library among them, as well as a reference 270 | directing the user to the copy of this License. Also, you must do one 271 | of these things: 272 | 273 | a) Accompany the work with the complete corresponding 274 | machine-readable source code for the Library including whatever 275 | changes were used in the work (which must be distributed under 276 | Sections 1 and 2 above); and, if the work is an executable linked 277 | with the Library, with the complete machine-readable "work that 278 | uses the Library", as object code and/or source code, so that the 279 | user can modify the Library and then relink to produce a modified 280 | executable containing the modified Library. (It is understood 281 | that the user who changes the contents of definitions files in the 282 | Library will not necessarily be able to recompile the application 283 | to use the modified definitions.) 284 | 285 | b) Accompany the work with a written offer, valid for at 286 | least three years, to give the same user the materials 287 | specified in Subsection 6a, above, for a charge no more 288 | than the cost of performing this distribution. 289 | 290 | c) If distribution of the work is made by offering access to copy 291 | from a designated place, offer equivalent access to copy the above 292 | specified materials from the same place. 293 | 294 | d) Verify that the user has already received a copy of these 295 | materials or that you have already sent this user a copy. 296 | 297 | For an executable, the required form of the "work that uses the 298 | Library" must include any data and utility programs needed for 299 | reproducing the executable from it. However, as a special exception, 300 | the source code distributed need not include anything that is normally 301 | distributed (in either source or binary form) with the major 302 | components (compiler, kernel, and so on) of the operating system on 303 | which the executable runs, unless that component itself accompanies 304 | the executable. 305 | 306 | It may happen that this requirement contradicts the license 307 | restrictions of other proprietary libraries that do not normally 308 | accompany the operating system. Such a contradiction means you cannot 309 | use both them and the Library together in an executable that you 310 | distribute. 311 | 312 | 7. You may place library facilities that are a work based on the 313 | Library side-by-side in a single library together with other library 314 | facilities not covered by this License, and distribute such a combined 315 | library, provided that the separate distribution of the work based on 316 | the Library and of the other library facilities is otherwise 317 | permitted, and provided that you do these two things: 318 | 319 | a) Accompany the combined library with a copy of the same work 320 | based on the Library, uncombined with any other library 321 | facilities. This must be distributed under the terms of the 322 | Sections above. 323 | 324 | b) Give prominent notice with the combined library of the fact 325 | that part of it is a work based on the Library, and explaining 326 | where to find the accompanying uncombined form of the same work. 327 | 328 | 8. You may not copy, modify, sublicense, link with, or distribute 329 | the Library except as expressly provided under this License. Any 330 | attempt otherwise to copy, modify, sublicense, link with, or 331 | distribute the Library is void, and will automatically terminate your 332 | rights under this License. However, parties who have received copies, 333 | or rights, from you under this License will not have their licenses 334 | terminated so long as such parties remain in full compliance. 335 | 336 | 9. You are not required to accept this License, since you have not 337 | signed it. However, nothing else grants you permission to modify or 338 | distribute the Library or its derivative works. These actions are 339 | prohibited by law if you do not accept this License. Therefore, by 340 | modifying or distributing the Library (or any work based on the 341 | Library), you indicate your acceptance of this License to do so, and 342 | all its terms and conditions for copying, distributing or modifying 343 | the Library or works based on it. 344 | 345 | 10. Each time you redistribute the Library (or any work based on the 346 | Library), the recipient automatically receives a license from the 347 | original licensor to copy, distribute, link with or modify the Library 348 | subject to these terms and conditions. You may not impose any further 349 | restrictions on the recipients' exercise of the rights granted herein. 350 | You are not responsible for enforcing compliance by third parties to 351 | this License. 352 | 353 | 11. If, as a consequence of a court judgment or allegation of patent 354 | infringement or for any other reason (not limited to patent issues), 355 | conditions are imposed on you (whether by court order, agreement or 356 | otherwise) that contradict the conditions of this License, they do not 357 | excuse you from the conditions of this License. If you cannot 358 | distribute so as to satisfy simultaneously your obligations under this 359 | License and any other pertinent obligations, then as a consequence you 360 | may not distribute the Library at all. For example, if a patent 361 | license would not permit royalty-free redistribution of the Library by 362 | all those who receive copies directly or indirectly through you, then 363 | the only way you could satisfy both it and this License would be to 364 | refrain entirely from distribution of the Library. 365 | 366 | If any portion of this section is held invalid or unenforceable under any 367 | particular circumstance, the balance of the section is intended to apply, 368 | and the section as a whole is intended to apply in other circumstances. 369 | 370 | It is not the purpose of this section to induce you to infringe any 371 | patents or other property right claims or to contest validity of any 372 | such claims; this section has the sole purpose of protecting the 373 | integrity of the free software distribution system which is 374 | implemented by public license practices. Many people have made 375 | generous contributions to the wide range of software distributed 376 | through that system in reliance on consistent application of that 377 | system; it is up to the author/donor to decide if he or she is willing 378 | to distribute software through any other system and a licensee cannot 379 | impose that choice. 380 | 381 | This section is intended to make thoroughly clear what is believed to 382 | be a consequence of the rest of this License. 383 | 384 | 12. If the distribution and/or use of the Library is restricted in 385 | certain countries either by patents or by copyrighted interfaces, the 386 | original copyright holder who places the Library under this License may add 387 | an explicit geographical distribution limitation excluding those countries, 388 | so that distribution is permitted only in or among countries not thus 389 | excluded. In such case, this License incorporates the limitation as if 390 | written in the body of this License. 391 | 392 | 13. The Free Software Foundation may publish revised and/or new 393 | versions of the Library General Public License from time to time. 394 | Such new versions will be similar in spirit to the present version, 395 | but may differ in detail to address new problems or concerns. 396 | 397 | Each version is given a distinguishing version number. If the Library 398 | specifies a version number of this License which applies to it and 399 | "any later version", you have the option of following the terms and 400 | conditions either of that version or of any later version published by 401 | the Free Software Foundation. If the Library does not specify a 402 | license version number, you may choose any version ever published by 403 | the Free Software Foundation. 404 | 405 | 14. If you wish to incorporate parts of the Library into other free 406 | programs whose distribution conditions are incompatible with these, 407 | write to the author to ask for permission. For software which is 408 | copyrighted by the Free Software Foundation, write to the Free 409 | Software Foundation; we sometimes make exceptions for this. Our 410 | decision will be guided by the two goals of preserving the free status 411 | of all derivatives of our free software and of promoting the sharing 412 | and reuse of software generally. 413 | 414 | NO WARRANTY 415 | 416 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO 417 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 418 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR 419 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY 420 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE 421 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 422 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE 423 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME 424 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 425 | 426 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN 427 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY 428 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU 429 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR 430 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 431 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 432 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 433 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF 434 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 435 | DAMAGES. 436 | 437 | END OF TERMS AND CONDITIONS 438 | 439 | Appendix: How to Apply These Terms to Your New Libraries 440 | 441 | If you develop a new library, and you want it to be of the greatest 442 | possible use to the public, we recommend making it free software that 443 | everyone can redistribute and change. You can do so by permitting 444 | redistribution under these terms (or, alternatively, under the terms of the 445 | ordinary General Public License). 446 | 447 | To apply these terms, attach the following notices to the library. It is 448 | safest to attach them to the start of each source file to most effectively 449 | convey the exclusion of warranty; and each file should have at least the 450 | "copyright" line and a pointer to where the full notice is found. 451 | 452 | 453 | Copyright (C) 454 | 455 | This library is free software; you can redistribute it and/or 456 | modify it under the terms of the GNU Library General Public 457 | License as published by the Free Software Foundation; either 458 | version 2 of the License, or (at your option) any later version. 459 | 460 | This library is distributed in the hope that it will be useful, 461 | but WITHOUT ANY WARRANTY; without even the implied warranty of 462 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 463 | Library General Public License for more details. 464 | 465 | You should have received a copy of the GNU Library General Public 466 | License along with this library; if not, write to the Free 467 | Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, 468 | MA 02111-1307, USA 469 | 470 | Also add information on how to contact you by electronic and paper mail. 471 | 472 | You should also get your employer (if you work as a programmer) or your 473 | school, if any, to sign a "copyright disclaimer" for the library, if 474 | necessary. Here is a sample; alter the names: 475 | 476 | Yoyodyne, Inc., hereby disclaims all copyright interest in the 477 | library `Frob' (a library for tweaking knobs) written by James Random Hacker. 478 | 479 | , 1 April 1990 480 | Ty Coon, President of Vice 481 | 482 | That's all there is to it! 483 | -------------------------------------------------------------------------------- /gnu/getopt/gnu.getopt.Getopt.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Class gnu.getopt.Getopt 8 | 9 | 10 | 11 | 12 |
 13 | All Packages  Class Hierarchy  This Package  Previous  Next  Index
14 |
15 |

16 | Class gnu.getopt.Getopt 17 |

18 |
 19 | java.lang.Object
 20 |    |
 21 |    +----gnu.getopt.Getopt
 22 | 
23 |
24 |
25 |
public class Getopt 26 |
extends Object 27 |
28 | This is a Java port of GNU getopt, a class for parsing command line 29 | arguments passed to programs. It it based on the C getopt() functions 30 | in glibc 2.0.6 and should parse options in a 100% compatible manner. 31 | If it does not, that is a bug. The programmer's interface is also 32 | very compatible. 33 |

34 | To use Getopt, create a Getopt object with a argv array passed to the 35 | main method, then call the getopt() method in a loop. It will return an 36 | int that contains the value of the option character parsed from the 37 | command line. When there are no more options to be parsed, it 38 | returns -1. 39 |

40 | A command line option can be defined to take an argument. If an 41 | option has an argument, the value of that argument is stored in an 42 | instance variable called optarg, which can be accessed using the 43 | getOptarg() method. If an option that requires an argument is 44 | found, but there is no argument present, then an error message is 45 | printed. Normally getopt() returns a '?' in this situation, but 46 | that can be changed as described below. 47 |

48 | If an invalid option is encountered, an error message is printed 49 | to the standard error and getopt() returns a '?'. The value of the 50 | invalid option encountered is stored in the instance variable optopt 51 | which can be retrieved using the getOptopt() method. To suppress 52 | the printing of error messages for this or any other error, set 53 | the value of the opterr instance variable to false using the 54 | setOpterr() method. 55 |

56 | Between calls to getopt(), the instance variable optind is used to 57 | keep track of where the object is in the parsing process. After all 58 | options have been returned, optind is the index in argv of the first 59 | non-option argument. This variable can be accessed with the getOptind() 60 | method. 61 |

62 | Note that this object expects command line options to be passed in the 63 | traditional Unix manner. That is, proceeded by a '-' character. 64 | Multiple options can follow the '-'. For example "-abc" is equivalent 65 | to "-a -b -c". If an option takes a required argument, the value 66 | of the argument can immediately follow the option character or be 67 | present in the next argv element. For example, "-cfoo" and "-c foo" 68 | both represent an option character of 'c' with an argument of "foo" 69 | assuming c takes a required argument. If an option takes an argument 70 | that is not required, then any argument must immediately follow the 71 | option character in the same argv element. For example, if c takes 72 | a non-required argument, then "-cfoo" represents option character 'c' 73 | with an argument of "foo" while "-c foo" represents the option 74 | character 'c' with no argument, and a first non-option argv element 75 | of "foo". 76 |

77 | The user can stop getopt() from scanning any further into a command line 78 | by using the special argument "--" by itself. For example: 79 | "-a -- -d" would return an option character of 'a', then return -1 80 | The "--" is discarded and "-d" is pointed to by optind as the first 81 | non-option argv element. 82 |

83 | Here is a basic example of using Getopt: 84 |

85 |

 86 |  Getopt g = new Getopt("testprog", argv, "ab:c::d");
 87 |  //
 88 |  int c;
 89 |  String arg;
 90 |  while ((c = g.getopt()) != -1)
 91 |    {
 92 |      switch(c)
 93 |        {
 94 |           case 'a':
 95 |           case 'd':
 96 |             System.out.print("You picked " + (char)c + "\n");
 97 |             break;
 98 |             //
 99 |           case 'b':
100 |           case 'c':
101 |             arg = g.getOptarg();
102 |             System.out.print("You picked " + (char)c + 
103 |                              " with an argument of " +
104 |                              ((arg != null) ? arg : "null") + "\n");
105 |             break;
106 |             //
107 |           case '?':
108 |             break; // getopt() already printed an error
109 |             //
110 |           default:
111 |             System.out.print("getopt() returned " + c + "\n");
112 |        }
113 |    }
114 |  
115 |

116 | In this example, a new Getopt object is created with three params. 117 | The first param is the program name. This is for printing error 118 | messages in the form "program: error message". In the C version, this 119 | value is taken from argv[0], but in Java the program name is not passed 120 | in that element, thus the need for this parameter. The second param is 121 | the argument list that was passed to the main() method. The third 122 | param is the list of valid options. Each character represents a valid 123 | option. If the character is followed by a single colon, then that 124 | option has a required argument. If the character is followed by two 125 | colons, then that option has an argument that is not required. 126 |

127 | Note in this example that the value returned from getopt() is cast to 128 | a char prior to printing. This is required in order to make the value 129 | display correctly as a character instead of an integer. 130 |

131 | If the first character in the option string is a colon, for example 132 | ":abc::d", then getopt() will return a ':' instead of a '?' when it 133 | encounters an option with a missing required argument. This allows the 134 | caller to distinguish between invalid options and valid options that 135 | are simply incomplete. 136 |

137 | In the traditional Unix getopt(), -1 is returned when the first non-option 138 | charcter is encountered. In GNU getopt(), the default behavior is to 139 | allow options to appear anywhere on the command line. The getopt() 140 | method permutes the argument to make it appear to the caller that all 141 | options were at the beginning of the command line, and all non-options 142 | were at the end. For example, calling getopt() with command line args 143 | of "-a foo bar -d" returns options 'a' and 'd', then sets optind to 144 | point to "foo". The program would read the last two argv elements as 145 | "foo" and "bar", just as if the user had typed "-a -d foo bar". 146 |

147 | The user can force getopt() to stop scanning the command line with 148 | the special argument "--" by itself. Any elements occuring before the 149 | "--" are scanned and permuted as normal. Any elements after the "--" 150 | are returned as is as non-option argv elements. For example, 151 | "foo -a -- bar -d" would return option 'a' then -1. optind would point 152 | to "foo", "bar" and "-d" as the non-option argv elements. The "--" 153 | is discarded by getopt(). 154 |

155 | There are two ways this default behavior can be modified. The first is 156 | to specify traditional Unix getopt() behavior (which is also POSIX 157 | behavior) in which scanning stops when the first non-option argument 158 | encountered. (Thus "-a foo bar -d" would return 'a' as an option and 159 | have "foo", "bar", and "-d" as non-option elements). The second is to 160 | allow options anywhere, but to return all elements in the order they 161 | occur on the command line. When a non-option element is ecountered, 162 | an integer 1 is returned and the value of the non-option element is 163 | stored in optarg is if it were the argument to that option. For 164 | example, "-a foo -d", returns first 'a', then 1 (with optarg set to 165 | "foo") then 'd' then -1. When this "return in order" functionality 166 | is enabled, the only way to stop getopt() from scanning all command 167 | line elements is to use the special "--" string by itself as described 168 | above. An example is "-a foo -b -- bar", which would return 'a', then 169 | integer 1 with optarg set to "foo", then 'b', then -1. optind would 170 | then point to "bar" as the first non-option argv element. The "--" 171 | is discarded. 172 |

173 | The POSIX/traditional behavior is enabled by either setting the 174 | property "gnu.posixly_correct" or by putting a '+' sign as the first 175 | character of the option string. The difference between the two 176 | methods is that setting the gnu.posixly_correct property also forces 177 | certain error messages to be displayed in POSIX format. To enable 178 | the "return in order" functionality, put a '-' as the first character 179 | of the option string. Note that after determining the proper 180 | behavior, Getopt strips this leading '+' or '-', meaning that a ':' 181 | placed as the second character after one of those two will still cause 182 | getopt() to return a ':' instead of a '?' if a required option 183 | argument is missing. 184 |

185 | In addition to traditional single character options, GNU Getopt also 186 | supports long options. These are preceeded by a "--" sequence and 187 | can be as long as desired. Long options provide a more user-friendly 188 | way of entering command line options. For example, in addition to a 189 | "-h" for help, a program could support also "--help". 190 |

191 | Like short options, long options can also take a required or non-required 192 | argument. Required arguments can either be specified by placing an 193 | equals sign after the option name, then the argument, or by putting the 194 | argument in the next argv element. For example: "--outputdir=foo" and 195 | "--outputdir foo" both represent an option of "outputdir" with an 196 | argument of "foo", assuming that outputdir takes a required argument. 197 | If a long option takes a non-required argument, then the equals sign 198 | form must be used to specify the argument. In this case, 199 | "--outputdir=foo" would represent option outputdir with an argument of 200 | "foo" while "--outputdir foo" would represent the option outputdir 201 | with no argument and a first non-option argv element of "foo". 202 |

203 | Long options can also be specified using a special POSIX argument 204 | format (one that I highly discourage). This form of entry is 205 | enabled by placing a "W;" (yes, 'W' then a semi-colon) in the valid 206 | option string. This causes getopt to treat the name following the 207 | "-W" as the name of the long option. For example, "-W outputdir=foo" 208 | would be equivalent to "--outputdir=foo". The name can immediately 209 | follow the "-W" like so: "-Woutputdir=foo". Option arguments are 210 | handled identically to normal long options. If a string follows the 211 | "-W" that does not represent a valid long option, then getopt() returns 212 | 'W' and the caller must decide what to do. Otherwise getopt() returns 213 | a long option value as described below. 214 |

215 | While long options offer convenience, they can also be tedious to type 216 | in full. So it is permissible to abbreviate the option name to as 217 | few characters as required to uniquely identify it. If the name can 218 | represent multiple long options, then an error message is printed and 219 | getopt() returns a '?'. 220 |

221 | If an invalid option is specified or a required option argument is 222 | missing, getopt() prints an error and returns a '?' or ':' exactly 223 | as for short options. Note that when an invalid long option is 224 | encountered, the optopt variable is set to integer 0 and so cannot 225 | be used to identify the incorrect option the user entered. 226 |

227 | Long options are defined by LongOpt objects. These objects are created 228 | with a contructor that takes four params: a String representing the 229 | object name, a integer specifying what arguments the option takes 230 | (the value is one of LongOpt.NO_ARGUMENT, LongOpt.REQUIRED_ARGUMENT, 231 | or LongOpt.OPTIONAL_ARGUMENT), a StringBuffer flag object (described 232 | below), and an integer value (described below). 233 |

234 | To enable long option parsing, create an array of LongOpt's representing 235 | the legal options and pass it to the Getopt() constructor. WARNING: If 236 | all elements of the array are not populated with LongOpt objects, the 237 | getopt() method will throw a NullPointerException. 238 |

239 | When getopt() is called and a long option is encountered, one of two 240 | things can be returned. If the flag field in the LongOpt object 241 | representing the long option is non-null, then the integer value field 242 | is stored there and an integer 0 is returned to the caller. The val 243 | field can then be retrieved from the flag field. Note that since the 244 | flag field is a StringBuffer, the appropriate String to integer converions 245 | must be performed in order to get the actual int value stored there. 246 | If the flag field in the LongOpt object is null, then the value field 247 | of the LongOpt is returned. This can be the character of a short option. 248 | This allows an app to have both a long and short option sequence 249 | (say, "-h" and "--help") that do the exact same thing. 250 |

251 | With long options, there is an alternative method of determining 252 | which option was selected. The method getLongind() will return the 253 | the index in the long option array (NOT argv) of the long option found. 254 | So if multiple long options are configured to return the same value, 255 | the application can use getLongind() to distinguish between them. 256 |

257 | Here is an expanded Getopt example using long options and various 258 | techniques described above: 259 |

260 |

261 |  int c;
262 |  String arg;
263 |  LongOpt[] longopts = new LongOpt[3];
264 |  // 
265 |  StringBuffer sb = new StringBuffer();
266 |  longopts[0] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h');
267 |  longopts[1] = new LongOpt("outputdir", LongOpt.REQUIRED_ARGUMENT, sb, 'o'); 
268 |  longopts[2] = new LongOpt("maximum", LongOpt.OPTIONAL_ARGUMENT, null, 2);
269 |  // 
270 |  Getopt g = new Getopt("testprog", argv, "-:bc::d:hW;", longopts);
271 |  g.setOpterr(false); // We'll do our own error handling
272 |  //
273 |  while ((c = g.getopt()) != -1)
274 |    switch (c)
275 |      {
276 |         case 0:
277 |           arg = g.getOptarg();
278 |           System.out.println("Got long option with value '" +
279 |                              (char)(new Integer(sb.toString())).intValue()
280 |                              + "' with argument " +
281 |                              ((arg != null) ? arg : "null"));
282 |           break;
283 |           //
284 |         case 1:
285 |           System.out.println("I see you have return in order set and that " +
286 |                              "a non-option argv element was just found " +
287 |                              "with the value '" + g.getOptarg() + "'");
288 |           break;
289 |           //
290 |         case 2:
291 |           arg = g.getOptarg();
292 |           System.out.println("I know this, but pretend I didn't");
293 |           System.out.println("We picked option " +
294 |                              longopts[g.getLongind()].getName() +
295 |                            " with value " + 
296 |                            ((arg != null) ? arg : "null"));
297 |           break;
298 |           //
299 |         case 'b':
300 |           System.out.println("You picked plain old option " + (char)c);
301 |           break;
302 |           //
303 |         case 'c':
304 |         case 'd':
305 |           arg = g.getOptarg();
306 |           System.out.println("You picked option '" + (char)c + 
307 |                              "' with argument " +
308 |                              ((arg != null) ? arg : "null"));
309 |           break;
310 |           //
311 |         case 'h':
312 |           System.out.println("I see you asked for help");
313 |           break;
314 |           //
315 |         case 'W':
316 |           System.out.println("Hmmm. You tried a -W with an incorrect long " +
317 |                              "option name");
318 |           break;
319 |           //
320 |         case ':':
321 |           System.out.println("Doh! You need an argument for option " +
322 |                              (char)g.getOptopt());
323 |           break;
324 |           //
325 |         case '?':
326 |           System.out.println("The option '" + (char)g.getOptopt() + 
327 |                            "' is not valid");
328 |           break;
329 |           //
330 |         default:
331 |           System.out.println("getopt() returned " + c);
332 |           break;
333 |      }
334 |  //
335 |  for (int i = g.getOptind(); i < argv.length ; i++)
336 |    System.out.println("Non option argv element: " + argv[i] + "\n");
337 |  
338 |

339 | There is an alternative form of the constructor used for long options 340 | above. This takes a trailing boolean flag. If set to false, Getopt 341 | performs identically to the example, but if the boolean flag is true 342 | then long options are allowed to start with a single '-' instead of 343 | "--". If the first character of the option is a valid short option 344 | character, then the option is treated as if it were the short option. 345 | Otherwise it behaves as if the option is a long option. Note that 346 | the name given to this option - long_only - is very counter-intuitive. 347 | It does not cause only long options to be parsed but instead enables 348 | the behavior described above. 349 |

350 | Note that the functionality and variable names used are driven from 351 | the C lib version as this object is a port of the C code, not a 352 | new implementation. This should aid in porting existing C/C++ code, 353 | as well as helping programmers familiar with the glibc version to 354 | adapt to the Java version even if it seems very non-Java at times. 355 |

356 | In this release I made all instance variables protected due to 357 | overwhelming public demand. Any code which relied on optarg, 358 | opterr, optind, or optopt being public will need to be modified to 359 | use the appropriate access methods. 360 |

361 | Please send all bug reports, requests, and comments to 362 | arenn@urbanophile.com. 363 |

364 |

365 |
Version: 366 |
1.0.3 367 |
Author: 368 |
Roland McGrath (roland@gnu.ai.mit.edu), Ulrich Drepper (drepper@cygnus.com), Aaron M. Renn (arenn@urbanophile.com) 369 |
See Also: 370 |
LongOpt 371 |
372 |
373 | 374 |

375 | Constructor Index 376 |

377 |
378 |
 o 379 | Getopt(String, String[], String) 380 |
Construct a basic Getopt instance with the given input data. 381 |
 o 382 | Getopt(String, String[], String, LongOpt[]) 383 |
Construct a Getopt instance with given input data that is capable of 384 | parsing long options as well as short. 385 |
 o 386 | Getopt(String, String[], String, LongOpt[], boolean) 387 |
Construct a Getopt instance with given input data that is capable of 388 | parsing long options and short options. 389 |
390 |

391 | Method Index 392 |

393 |
394 |
 o 395 | getLongind() 396 |
Returns the index into the array of long options (NOT argv) representing 397 | the long option that was found. 398 |
 o 399 | getopt() 400 |
This method returns a char that is the current option that has been 401 | parsed from the command line. 402 |
 o 403 | getOptarg() 404 |
405 | For communication from `getopt' to the caller. 406 |
 o 407 | getOptind() 408 |
optind it the index in ARGV of the next element to be scanned. 409 |
 o 410 | getOptopt() 411 |
When getopt() encounters an invalid option, it stores the value of that 412 | option in optopt which can be retrieved with this method. 413 |
 o 414 | setArgv(String[]) 415 |
Since in GNU getopt() the argument vector is passed back in to the 416 | function every time, the caller can swap out argv on the fly. 417 |
 o 418 | setOpterr(boolean) 419 |
Normally Getopt will print a message to the standard error when an 420 | invalid option is encountered. 421 |
 o 422 | setOptind(int) 423 |
This method allows the optind index to be set manually. 424 |
 o 425 | setOptstring(String) 426 |
In GNU getopt, it is possible to change the string containg valid options 427 | on the fly because it is passed as an argument to getopt() each time. 428 |
429 | 430 |

431 | Constructors 432 |

433 | 434 |  o 435 | Getopt 436 |
437 |  public Getopt(String progname,
438 |                String argv[],
439 |                String optstring)
440 | 
441 |
442 |
Construct a basic Getopt instance with the given input data. Note that 443 | this handles "short" options only. 444 |

445 |

446 |
Parameters: 447 |
progname - The name to display as the program name when printing errors 448 |
argv - The String array passed as the command line to the program. 449 |
optstring - A String containing a description of the valid args for this program 450 |
451 |
452 |  o 453 | Getopt 454 |
455 |  public Getopt(String progname,
456 |                String argv[],
457 |                String optstring,
458 |                LongOpt long_options[])
459 | 
460 |
461 |
Construct a Getopt instance with given input data that is capable of 462 | parsing long options as well as short. 463 |

464 |

465 |
Parameters: 466 |
progname - The name to display as the program name when printing errors 467 |
argv - The String array passed as the command ilne to the program 468 |
optstring - A String containing a description of the valid short args for this program 469 |
long_options - An array of LongOpt objects that describes the valid long args for this program 470 |
471 |
472 |  o 473 | Getopt 474 |
475 |  public Getopt(String progname,
476 |                String argv[],
477 |                String optstring,
478 |                LongOpt long_options[],
479 |                boolean long_only)
480 | 
481 |
482 |
Construct a Getopt instance with given input data that is capable of 483 | parsing long options and short options. Contrary to what you might 484 | think, the flag 'long_only' does not determine whether or not we 485 | scan for only long arguments. Instead, a value of true here allows 486 | long arguments to start with a '-' instead of '--' unless there is a 487 | conflict with a short option name. 488 |

489 |

490 |
Parameters: 491 |
progname - The name to display as the program name when printing errors 492 |
argv - The String array passed as the command ilne to the program 493 |
optstring - A String containing a description of the valid short args for this program 494 |
long_options - An array of LongOpt objects that describes the valid long args for this program 495 |
long_only - true if long options that do not conflict with short options can start with a '-' as well as '--' 496 |
497 |
498 | 499 |

500 | Methods 501 |

502 |  o 503 | setOptstring 504 |
505 |  public void setOptstring(String optstring)
506 | 
507 |
508 |
In GNU getopt, it is possible to change the string containg valid options 509 | on the fly because it is passed as an argument to getopt() each time. In 510 | this version we do not pass the string on every call. In order to allow 511 | dynamic option string changing, this method is provided. 512 |

513 |

514 |
Parameters: 515 |
optstring - The new option string to use 516 |
517 |
518 |  o 519 | getOptind 520 |
521 |  public int getOptind()
522 | 
523 |
524 |
optind it the index in ARGV of the next element to be scanned. 525 | This is used for communication to and from the caller 526 | and for communication between successive calls to `getopt'. 527 | When `getopt' returns -1, this is the index of the first of the 528 | non-option elements that the caller should itself scan. 529 | Otherwise, `optind' communicates from one call to the next 530 | how much of ARGV has been scanned so far. 531 |

532 |

533 |  o 534 | setOptind 535 |
536 |  public void setOptind(int optind)
537 | 
538 |
539 |
This method allows the optind index to be set manually. Normally this 540 | is not necessary (and incorrect usage of this method can lead to serious 541 | lossage), but optind is a public symbol in GNU getopt, so this method 542 | was added to allow it to be modified by the caller if desired. 543 |

544 |

545 |
Parameters: 546 |
optind - The new value of optind 547 |
548 |
549 |  o 550 | setArgv 551 |
552 |  public void setArgv(String argv[])
553 | 
554 |
555 |
Since in GNU getopt() the argument vector is passed back in to the 556 | function every time, the caller can swap out argv on the fly. Since 557 | passing argv is not required in the Java version, this method allows 558 | the user to override argv. Note that incorrect use of this method can 559 | lead to serious lossage. 560 |

561 |

562 |
Parameters: 563 |
argv - New argument list 564 |
565 |
566 |  o 567 | getOptarg 568 |
569 |  public String getOptarg()
570 | 
571 |
572 |
For communication from `getopt' to the caller. 573 | When `getopt' finds an option that takes an argument, 574 | the argument value is returned here. 575 | Also, when `ordering' is RETURN_IN_ORDER, 576 | each non-option ARGV-element is returned here. 577 | No set method is provided because setting this variable has no effect. 578 |

579 |

580 |  o 581 | setOpterr 582 |
583 |  public void setOpterr(boolean opterr)
584 | 
585 |
586 |
Normally Getopt will print a message to the standard error when an 587 | invalid option is encountered. This can be suppressed (or re-enabled) 588 | by calling this method. There is no get method for this variable 589 | because if you can't remember the state you set this to, why should I? 590 |

591 |

592 |  o 593 | getOptopt 594 |
595 |  public int getOptopt()
596 | 
597 |
598 |
When getopt() encounters an invalid option, it stores the value of that 599 | option in optopt which can be retrieved with this method. There is 600 | no corresponding set method because setting this variable has no effect. 601 |

602 |

603 |  o 604 | getLongind 605 |
606 |  public int getLongind()
607 | 
608 |
609 |
Returns the index into the array of long options (NOT argv) representing 610 | the long option that was found. 611 |

612 |

613 |  o 614 | getopt 615 |
616 |  public int getopt()
617 | 
618 |
619 |
This method returns a char that is the current option that has been 620 | parsed from the command line. If the option takes an argument, then 621 | the internal variable 'optarg' is set which is a String representing 622 | the the value of the argument. This value can be retrieved by the 623 | caller using the getOptarg() method. If an invalid option is found, 624 | an error message is printed and a '?' is returned. The name of the 625 | invalid option character can be retrieved by calling the getOptopt() 626 | method. When there are no more options to be scanned, this method 627 | returns -1. The index of first non-option element in argv can be 628 | retrieved with the getOptind() method. 629 |

630 |

631 |
Returns: 632 |
Various things as described above 633 |
634 |
635 |
636 |
637 | All Packages  Class Hierarchy  This Package  Previous  Next  Index
638 | 639 | 640 | -------------------------------------------------------------------------------- /gnu/getopt/Getopt.java: -------------------------------------------------------------------------------- 1 | /************************************************************************** 2 | /* Getopt.java -- Java port of GNU getopt from glibc 2.0.6 3 | /* 4 | /* Copyright (c) 1987-1997 Free Software Foundation, Inc. 5 | /* Java Port Copyright (c) 1998 by Aaron M. Renn (arenn@urbanophile.com) 6 | /* 7 | /* This program is free software; you can redistribute it and/or modify 8 | /* it under the terms of the GNU Library General Public License as published 9 | /* by the Free Software Foundation; either version 2 of the License or 10 | /* (at your option) any later version. 11 | /* 12 | /* This program is distributed in the hope that it will be useful, but 13 | /* WITHOUT ANY WARRANTY; without even the implied warranty of 14 | /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | /* GNU Library General Public License for more details. 16 | /* 17 | /* You should have received a copy of the GNU Library General Public License 18 | /* along with this program; see the file COPYING.LIB. If not, write to 19 | /* the Free Software Foundation Inc., 59 Temple Place - Suite 330, 20 | /* Boston, MA 02111-1307 USA 21 | /**************************************************************************/ 22 | 23 | package gnu.getopt; 24 | 25 | import java.util.Locale; 26 | import java.util.ResourceBundle; 27 | import java.text.MessageFormat; 28 | 29 | /**************************************************************************/ 30 | 31 | /** 32 | * This is a Java port of GNU getopt, a class for parsing command line 33 | * arguments passed to programs. It it based on the C getopt() functions 34 | * in glibc 2.0.6 and should parse options in a 100% compatible manner. 35 | * If it does not, that is a bug. The programmer's interface is also 36 | * very compatible. 37 | *

38 | * To use Getopt, create a Getopt object with a argv array passed to the 39 | * main method, then call the getopt() method in a loop. It will return an 40 | * int that contains the value of the option character parsed from the 41 | * command line. When there are no more options to be parsed, it 42 | * returns -1. 43 | *

44 | * A command line option can be defined to take an argument. If an 45 | * option has an argument, the value of that argument is stored in an 46 | * instance variable called optarg, which can be accessed using the 47 | * getOptarg() method. If an option that requires an argument is 48 | * found, but there is no argument present, then an error message is 49 | * printed. Normally getopt() returns a '?' in this situation, but 50 | * that can be changed as described below. 51 | *

52 | * If an invalid option is encountered, an error message is printed 53 | * to the standard error and getopt() returns a '?'. The value of the 54 | * invalid option encountered is stored in the instance variable optopt 55 | * which can be retrieved using the getOptopt() method. To suppress 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 58 | * setOpterr() method. 59 | *

60 | * Between calls to getopt(), the instance variable optind is used to 61 | * keep track of where the object is in the parsing process. After all 62 | * options have been returned, optind is the index in argv of the first 63 | * non-option argument. This variable can be accessed with the getOptind() 64 | * method. 65 | *

66 | * Note that this object expects command line options to be passed in the 67 | * traditional Unix manner. That is, proceeded by a '-' character. 68 | * Multiple options can follow the '-'. For example "-abc" is equivalent 69 | * to "-a -b -c". If an option takes a required argument, the value 70 | * of the argument can immediately follow the option character or be 71 | * present in the next argv element. For example, "-cfoo" and "-c foo" 72 | * both represent an option character of 'c' with an argument of "foo" 73 | * assuming c takes a required argument. If an option takes an argument 74 | * that is not required, then any argument must immediately follow the 75 | * option character in the same argv element. For example, if c takes 76 | * a non-required argument, then "-cfoo" represents option character 'c' 77 | * with an argument of "foo" while "-c foo" represents the option 78 | * character 'c' with no argument, and a first non-option argv element 79 | * of "foo". 80 | *

81 | * The user can stop getopt() from scanning any further into a command line 82 | * by using the special argument "--" by itself. For example: 83 | * "-a -- -d" would return an option character of 'a', then return -1 84 | * The "--" is discarded and "-d" is pointed to by optind as the first 85 | * non-option argv element. 86 | *

87 | * Here is a basic example of using Getopt: 88 | *

89 | *

  90 |   * Getopt g = new Getopt("testprog", argv, "ab:c::d");
  91 |   * //
  92 |   * int c;
  93 |   * String arg;
  94 |   * while ((c = g.getopt()) != -1)
  95 |   *   {
  96 |   *     switch(c)
  97 |   *       {
  98 |   *          case 'a':
  99 |   *          case 'd':
 100 |   *            System.out.print("You picked " + (char)c + "\n");
 101 |   *            break;
 102 |   *            //
 103 |   *          case 'b':
 104 |   *          case 'c':
 105 |   *            arg = g.getOptarg();
 106 |   *            System.out.print("You picked " + (char)c + 
 107 |   *                             " with an argument of " +
 108 |   *                             ((arg != null) ? arg : "null") + "\n");
 109 |   *            break;
 110 |   *            //
 111 |   *          case '?':
 112 |   *            break; // getopt() already printed an error
 113 |   *            //
 114 |   *          default:
 115 |   *            System.out.print("getopt() returned " + c + "\n");
 116 |   *       }
 117 |   *   }
 118 |   * 
119 | *

120 | * In this example, a new Getopt object is created with three params. 121 | * The first param is the program name. This is for printing error 122 | * messages in the form "program: error message". In the C version, this 123 | * value is taken from argv[0], but in Java the program name is not passed 124 | * in that element, thus the need for this parameter. The second param is 125 | * the argument list that was passed to the main() method. The third 126 | * param is the list of valid options. Each character represents a valid 127 | * option. If the character is followed by a single colon, then that 128 | * option has a required argument. If the character is followed by two 129 | * colons, then that option has an argument that is not required. 130 | *

131 | * Note in this example that the value returned from getopt() is cast to 132 | * a char prior to printing. This is required in order to make the value 133 | * display correctly as a character instead of an integer. 134 | *

135 | * If the first character in the option string is a colon, for example 136 | * ":abc::d", then getopt() will return a ':' instead of a '?' when it 137 | * encounters an option with a missing required argument. This allows the 138 | * caller to distinguish between invalid options and valid options that 139 | * are simply incomplete. 140 | *

141 | * In the traditional Unix getopt(), -1 is returned when the first non-option 142 | * charcter is encountered. In GNU getopt(), the default behavior is to 143 | * allow options to appear anywhere on the command line. The getopt() 144 | * method permutes the argument to make it appear to the caller that all 145 | * options were at the beginning of the command line, and all non-options 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 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 | *

151 | * The user can force getopt() to stop scanning the command line with 152 | * the special argument "--" by itself. Any elements occuring before the 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 156 | * to "foo", "bar" and "-d" as the non-option argv elements. The "--" 157 | * is discarded by getopt(). 158 | *

159 | * There are two ways this default behavior can be modified. The first is 160 | * to specify traditional Unix getopt() behavior (which is also POSIX 161 | * behavior) in which scanning stops when the first non-option argument 162 | * encountered. (Thus "-a foo bar -d" would return 'a' as an option and 163 | * have "foo", "bar", and "-d" as non-option elements). The second is to 164 | * allow options anywhere, but to return all elements in the order they 165 | * occur on the command line. When a non-option element is ecountered, 166 | * an integer 1 is returned and the value of the non-option element is 167 | * stored in optarg is if it were the argument to that option. For 168 | * example, "-a foo -d", returns first 'a', then 1 (with optarg set to 169 | * "foo") then 'd' then -1. When this "return in order" functionality 170 | * is enabled, the only way to stop getopt() from scanning all command 171 | * line elements is to use the special "--" string by itself as described 172 | * above. An example is "-a foo -b -- bar", which would return 'a', then 173 | * integer 1 with optarg set to "foo", then 'b', then -1. optind would 174 | * then point to "bar" as the first non-option argv element. The "--" 175 | * is discarded. 176 | *

177 | * The POSIX/traditional behavior is enabled by either setting the 178 | * property "gnu.posixly_correct" or by putting a '+' sign as the first 179 | * character of the option string. The difference between the two 180 | * methods is that setting the gnu.posixly_correct property also forces 181 | * certain error messages to be displayed in POSIX format. To enable 182 | * the "return in order" functionality, put a '-' as the first character 183 | * of the option string. Note that after determining the proper 184 | * behavior, Getopt strips this leading '+' or '-', meaning that a ':' 185 | * placed as the second character after one of those two will still cause 186 | * getopt() to return a ':' instead of a '?' if a required option 187 | * argument is missing. 188 | *

189 | * In addition to traditional single character options, GNU Getopt also 190 | * supports long options. These are preceeded by a "--" sequence and 191 | * can be as long as desired. Long options provide a more user-friendly 192 | * way of entering command line options. For example, in addition to a 193 | * "-h" for help, a program could support also "--help". 194 | *

195 | * Like short options, long options can also take a required or non-required 196 | * argument. Required arguments can either be specified by placing an 197 | * equals sign after the option name, then the argument, or by putting the 198 | * argument in the next argv element. For example: "--outputdir=foo" and 199 | * "--outputdir foo" both represent an option of "outputdir" with an 200 | * argument of "foo", assuming that outputdir takes a required argument. 201 | * If a long option takes a non-required argument, then the equals sign 202 | * form must be used to specify the argument. In this case, 203 | * "--outputdir=foo" would represent option outputdir with an argument of 204 | * "foo" while "--outputdir foo" would represent the option outputdir 205 | * with no argument and a first non-option argv element of "foo". 206 | *

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 | * enabled by placing a "W;" (yes, 'W' then a semi-colon) in the valid 210 | * option string. This causes getopt to treat the name following the 211 | * "-W" as the name of the long option. For example, "-W outputdir=foo" 212 | * would be equivalent to "--outputdir=foo". The name can immediately 213 | * follow the "-W" like so: "-Woutputdir=foo". Option arguments are 214 | * handled identically to normal long options. If a string follows the 215 | * "-W" that does not represent a valid long option, then getopt() returns 216 | * 'W' and the caller must decide what to do. Otherwise getopt() returns 217 | * a long option value as described below. 218 | *

219 | * While long options offer convenience, they can also be tedious to type 220 | * in full. So it is permissible to abbreviate the option name to as 221 | * few characters as required to uniquely identify it. If the name can 222 | * represent multiple long options, then an error message is printed and 223 | * getopt() returns a '?'. 224 | *

225 | * If an invalid option is specified or a required option argument is 226 | * missing, getopt() prints an error and returns a '?' or ':' exactly 227 | * as for short options. Note that when an invalid long option is 228 | * encountered, the optopt variable is set to integer 0 and so cannot 229 | * be used to identify the incorrect option the user entered. 230 | *

231 | * Long options are defined by LongOpt objects. These objects are created 232 | * with a contructor that takes four params: a String representing the 233 | * object name, a integer specifying what arguments the option takes 234 | * (the value is one of LongOpt.NO_ARGUMENT, LongOpt.REQUIRED_ARGUMENT, 235 | * or LongOpt.OPTIONAL_ARGUMENT), a StringBuffer flag object (described 236 | * below), and an integer value (described below). 237 | *

238 | * To enable long option parsing, create an array of LongOpt's representing 239 | * the legal options and pass it to the Getopt() constructor. WARNING: If 240 | * all elements of the array are not populated with LongOpt objects, the 241 | * getopt() method will throw a NullPointerException. 242 | *

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 245 | * representing the long option is non-null, then the integer value field 246 | * is stored there and an integer 0 is returned to the caller. The val 247 | * field can then be retrieved from the flag field. Note that since the 248 | * flag field is a StringBuffer, the appropriate String to integer converions 249 | * must be performed in order to get the actual int value stored there. 250 | * If the flag field in the LongOpt object is null, then the value field 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 253 | * (say, "-h" and "--help") that do the exact same thing. 254 | *

255 | * With long options, there is an alternative method of determining 256 | * which option was selected. The method getLongind() will return the 257 | * the index in the long option array (NOT argv) of the long option found. 258 | * So if multiple long options are configured to return the same value, 259 | * the application can use getLongind() to distinguish between them. 260 | *

261 | * Here is an expanded Getopt example using long options and various 262 | * techniques described above: 263 | *

264 | *

 265 |   * int c;
 266 |   * String arg;
 267 |   * LongOpt[] longopts = new LongOpt[3];
 268 |   * // 
 269 |   * StringBuffer sb = new StringBuffer();
 270 |   * longopts[0] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h');
 271 |   * longopts[1] = new LongOpt("outputdir", LongOpt.REQUIRED_ARGUMENT, sb, 'o'); 
 272 |   * longopts[2] = new LongOpt("maximum", LongOpt.OPTIONAL_ARGUMENT, null, 2);
 273 |   * // 
 274 |   * Getopt g = new Getopt("testprog", argv, "-:bc::d:hW;", longopts);
 275 |   * g.setOpterr(false); // We'll do our own error handling
 276 |   * //
 277 |   * while ((c = g.getopt()) != -1)
 278 |   *   switch (c)
 279 |   *     {
 280 |   *        case 0:
 281 |   *          arg = g.getOptarg();
 282 |   *          System.out.println("Got long option with value '" +
 283 |   *                             (char)(new Integer(sb.toString())).intValue()
 284 |   *                             + "' with argument " +
 285 |   *                             ((arg != null) ? arg : "null"));
 286 |   *          break;
 287 |   *          //
 288 |   *        case 1:
 289 |   *          System.out.println("I see you have return in order set and that " +
 290 |   *                             "a non-option argv element was just found " +
 291 |   *                             "with the value '" + g.getOptarg() + "'");
 292 |   *          break;
 293 |   *          //
 294 |   *        case 2:
 295 |   *          arg = g.getOptarg();
 296 |   *          System.out.println("I know this, but pretend I didn't");
 297 |   *          System.out.println("We picked option " +
 298 |   *                             longopts[g.getLongind()].getName() +
 299 |   *                           " with value " + 
 300 |   *                           ((arg != null) ? arg : "null"));
 301 |   *          break;
 302 |   *          //
 303 |   *        case 'b':
 304 |   *          System.out.println("You picked plain old option " + (char)c);
 305 |   *          break;
 306 |   *          //
 307 |   *        case 'c':
 308 |   *        case 'd':
 309 |   *          arg = g.getOptarg();
 310 |   *          System.out.println("You picked option '" + (char)c + 
 311 |   *                             "' with argument " +
 312 |   *                             ((arg != null) ? arg : "null"));
 313 |   *          break;
 314 |   *          //
 315 |   *        case 'h':
 316 |   *          System.out.println("I see you asked for help");
 317 |   *          break;
 318 |   *          //
 319 |   *        case 'W':
 320 |   *          System.out.println("Hmmm. You tried a -W with an incorrect long " +
 321 |   *                             "option name");
 322 |   *          break;
 323 |   *          //
 324 |   *        case ':':
 325 |   *          System.out.println("Doh! You need an argument for option " +
 326 |   *                             (char)g.getOptopt());
 327 |   *          break;
 328 |   *          //
 329 |   *        case '?':
 330 |   *          System.out.println("The option '" + (char)g.getOptopt() + 
 331 |   *                           "' is not valid");
 332 |   *          break;
 333 |   *          //
 334 |   *        default:
 335 |   *          System.out.println("getopt() returned " + c);
 336 |   *          break;
 337 |   *     }
 338 |   * //
 339 |   * for (int i = g.getOptind(); i < argv.length ; i++)
 340 |   *   System.out.println("Non option argv element: " + argv[i] + "\n");
 341 |   * 
342 | *

343 | * There is an alternative form of the constructor used for long options 344 | * above. This takes a trailing boolean flag. If set to false, Getopt 345 | * performs identically to the example, but if the boolean flag is true 346 | * then long options are allowed to start with a single '-' instead of 347 | * "--". If the first character of the option is a valid short option 348 | * character, then the option is treated as if it were the short option. 349 | * Otherwise it behaves as if the option is a long option. Note that 350 | * the name given to this option - long_only - is very counter-intuitive. 351 | * It does not cause only long options to be parsed but instead enables 352 | * the behavior described above. 353 | *

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 | * new implementation. This should aid in porting existing C/C++ code, 357 | * as well as helping programmers familiar with the glibc version to 358 | * adapt to the Java version even if it seems very non-Java at times. 359 | *

360 | * In this release I made all instance variables protected due to 361 | * overwhelming public demand. Any code which relied on optarg, 362 | * opterr, optind, or optopt being public will need to be modified to 363 | * use the appropriate access methods. 364 | *

365 | * Please send all bug reports, requests, and comments to 366 | * arenn@urbanophile.com. 367 | * 368 | * @version 1.0.7 369 | * 370 | * @author Roland McGrath (roland@gnu.ai.mit.edu) 371 | * @author Ulrich Drepper (drepper@cygnus.com) 372 | * @author Aaron M. Renn (arenn@urbanophile.com) 373 | * 374 | * @see LongOpt 375 | */ 376 | public class Getopt extends Object 377 | { 378 | 379 | /**************************************************************************/ 380 | 381 | /* 382 | * Class Variables 383 | */ 384 | 385 | /** 386 | * Describe how to deal with options that follow non-option ARGV-elements. 387 | * 388 | * If the caller did not specify anything, 389 | * the default is REQUIRE_ORDER if the property 390 | * gnu.posixly_correct is defined, PERMUTE otherwise. 391 | * 392 | * The special argument `--' forces an end of option-scanning regardless 393 | * of the value of `ordering'. In the case of RETURN_IN_ORDER, only 394 | * `--' can cause `getopt' to return -1 with `optind' != ARGC. 395 | * 396 | * REQUIRE_ORDER means don't recognize them as options; 397 | * stop option processing when the first non-option is seen. 398 | * This is what Unix does. 399 | * This mode of operation is selected by either setting the property 400 | * gnu.posixly_correct, or using `+' as the first character 401 | * of the list of option characters. 402 | */ 403 | protected static final int REQUIRE_ORDER = 1; 404 | 405 | /** 406 | * PERMUTE is the default. We permute the contents of ARGV as we scan, 407 | * so that eventually all the non-options are at the end. This allows options 408 | * to be given in any order, even with programs that were not written to 409 | * expect this. 410 | */ 411 | protected static final int PERMUTE = 2; 412 | 413 | /** 414 | * RETURN_IN_ORDER is an option available to programs that were written 415 | * to expect options and other ARGV-elements in any order and that care about 416 | * the ordering of the two. We describe each non-option ARGV-element 417 | * as if it were the argument of an option with character code 1. 418 | * Using `-' as the first character of the list of option characters 419 | * selects this mode of operation. 420 | */ 421 | protected static final int RETURN_IN_ORDER = 3; 422 | 423 | /**************************************************************************/ 424 | 425 | /* 426 | * Instance Variables 427 | */ 428 | 429 | /** 430 | * For communication from `getopt' to the caller. 431 | * When `getopt' finds an option that takes an argument, 432 | * the argument value is returned here. 433 | * Also, when `ordering' is RETURN_IN_ORDER, 434 | * each non-option ARGV-element is returned here. 435 | */ 436 | protected String optarg; 437 | 438 | /** 439 | * Index in ARGV of the next element to be scanned. 440 | * This is used for communication to and from the caller 441 | * and for communication between successive calls to `getopt'. 442 | * 443 | * On entry to `getopt', zero means this is the first call; initialize. 444 | * 445 | * When `getopt' returns -1, this is the index of the first of the 446 | * non-option elements that the caller should itself scan. 447 | * 448 | * Otherwise, `optind' communicates from one call to the next 449 | * how much of ARGV has been scanned so far. 450 | */ 451 | protected int optind = 0; 452 | 453 | /** 454 | * Callers store false here to inhibit the error message 455 | * for unrecognized options. 456 | */ 457 | protected boolean opterr = true; 458 | 459 | /** 460 | * When an unrecognized option is encountered, getopt will return a '?' 461 | * and store the value of the invalid option here. 462 | */ 463 | protected int optopt = '?'; 464 | 465 | /** 466 | * The next char to be scanned in the option-element 467 | * in which the last option character we returned was found. 468 | * This allows us to pick up the scan where we left off. 469 | * 470 | * If this is zero, or a null string, it means resume the scan 471 | * by advancing to the next ARGV-element. 472 | */ 473 | protected String nextchar; 474 | 475 | /** 476 | * This is the string describing the valid short options. 477 | */ 478 | protected String optstring; 479 | 480 | /** 481 | * This is an array of LongOpt objects which describ the valid long 482 | * options. 483 | */ 484 | protected LongOpt[] long_options; 485 | 486 | /** 487 | * This flag determines whether or not we are parsing only long args 488 | */ 489 | protected boolean long_only; 490 | 491 | /** 492 | * Stores the index into the long_options array of the long option found 493 | */ 494 | protected int longind; 495 | 496 | /** 497 | * The flag determines whether or not we operate in strict POSIX compliance 498 | */ 499 | protected boolean posixly_correct; 500 | 501 | /** 502 | * A flag which communicates whether or not checkLongOption() did all 503 | * necessary processing for the current option 504 | */ 505 | protected boolean longopt_handled; 506 | 507 | /** 508 | * The index of the first non-option in argv[] 509 | */ 510 | protected int first_nonopt = 1; 511 | 512 | /** 513 | * The index of the last non-option in argv[] 514 | */ 515 | protected int last_nonopt = 1; 516 | 517 | /** 518 | * Flag to tell getopt to immediately return -1 the next time it is 519 | * called. 520 | */ 521 | private boolean endparse = false; 522 | 523 | /** 524 | * Saved argument list passed to the program 525 | */ 526 | protected String[] argv; 527 | 528 | /** 529 | * Determines whether we permute arguments or not 530 | */ 531 | protected int ordering; 532 | 533 | /** 534 | * Name to print as the program name in error messages. This is necessary 535 | * since Java does not place the program name in argv[0] 536 | */ 537 | protected String progname; 538 | 539 | /** 540 | * The localized strings are kept in a separate file 541 | */ 542 | private ResourceBundle _messages = ResourceBundle.getBundle( 543 | "gnu/getopt/MessagesBundle", Locale.getDefault()); 544 | 545 | /**************************************************************************/ 546 | 547 | /* 548 | * Constructors 549 | */ 550 | 551 | /** 552 | * Construct a basic Getopt instance with the given input data. Note that 553 | * this handles "short" options only. 554 | * 555 | * @param progname The name to display as the program name when printing errors 556 | * @param argv The String array passed as the command line to the program. 557 | * @param optstring A String containing a description of the valid args for this program 558 | */ 559 | public 560 | Getopt(String progname, String[] argv, String optstring) 561 | { 562 | this(progname, argv, optstring, null, false); 563 | } 564 | 565 | /**************************************************************************/ 566 | 567 | /** 568 | * Construct a Getopt instance with given input data that is capable of 569 | * parsing long options as well as short. 570 | * 571 | * @param progname The name to display as the program name when printing errors 572 | * @param argv The String array passed as the command ilne to the program 573 | * @param optstring A String containing a description of the valid short args for this program 574 | * @param long_options An array of LongOpt objects that describes the valid long args for this program 575 | */ 576 | public 577 | Getopt(String progname, String[] argv, String optstring, 578 | LongOpt[] long_options) 579 | { 580 | this(progname, argv, optstring, long_options, false); 581 | } 582 | 583 | /**************************************************************************/ 584 | 585 | /** 586 | * Construct a Getopt instance with given input data that is capable of 587 | * parsing long options and short options. Contrary to what you might 588 | * think, the flag 'long_only' does not determine whether or not we 589 | * scan for only long arguments. Instead, a value of true here allows 590 | * long arguments to start with a '-' instead of '--' unless there is a 591 | * conflict with a short option name. 592 | * 593 | * @param progname The name to display as the program name when printing errors 594 | * @param argv The String array passed as the command ilne to the program 595 | * @param optstring A String containing a description of the valid short args for this program 596 | * @param long_options An array of LongOpt objects that describes the valid long args for this program 597 | * @param long_only true if long options that do not conflict with short options can start with a '-' as well as '--' 598 | */ 599 | public 600 | Getopt(String progname, String[] argv, String optstring, 601 | LongOpt[] long_options, boolean long_only) 602 | { 603 | if (optstring.length() == 0) 604 | optstring = " "; 605 | 606 | // This function is essentially _getopt_initialize from GNU getopt 607 | this.progname = progname; 608 | this.argv = argv; 609 | this.optstring = optstring; 610 | this.long_options = long_options; 611 | this.long_only = long_only; 612 | 613 | // Check for property "gnu.posixly_correct" to determine whether to 614 | // strictly follow the POSIX standard. This replaces the "POSIXLY_CORRECT" 615 | // environment variable in the C version 616 | if (System.getProperty("gnu.posixly_correct", null) == null) 617 | posixly_correct = false; 618 | else 619 | { 620 | posixly_correct = true; 621 | _messages = ResourceBundle.getBundle("gnu/getopt/MessagesBundle", 622 | Locale.US); 623 | } 624 | 625 | // Determine how to handle the ordering of options and non-options 626 | if (optstring.charAt(0) == '-') 627 | { 628 | ordering = RETURN_IN_ORDER; 629 | if (optstring.length() > 1) 630 | this.optstring = optstring.substring(1); 631 | } 632 | else if (optstring.charAt(0) == '+') 633 | { 634 | ordering = REQUIRE_ORDER; 635 | if (optstring.length() > 1) 636 | this.optstring = optstring.substring(1); 637 | } 638 | else if (posixly_correct) 639 | { 640 | ordering = REQUIRE_ORDER; 641 | } 642 | else 643 | { 644 | ordering = PERMUTE; // The normal default case 645 | } 646 | } 647 | 648 | /**************************************************************************/ 649 | 650 | /* 651 | * Instance Methods 652 | */ 653 | 654 | /** 655 | * In GNU getopt, it is possible to change the string containg valid options 656 | * on the fly because it is passed as an argument to getopt() each time. In 657 | * this version we do not pass the string on every call. In order to allow 658 | * dynamic option string changing, this method is provided. 659 | * 660 | * @param optstring The new option string to use 661 | */ 662 | public void 663 | setOptstring(String optstring) 664 | { 665 | if (optstring.length() == 0) 666 | optstring = " "; 667 | 668 | this.optstring = optstring; 669 | } 670 | 671 | /**************************************************************************/ 672 | 673 | /** 674 | * optind it the index in ARGV of the next element to be scanned. 675 | * This is used for communication to and from the caller 676 | * and for communication between successive calls to `getopt'. 677 | * 678 | * When `getopt' returns -1, this is the index of the first of the 679 | * non-option elements that the caller should itself scan. 680 | * 681 | * Otherwise, `optind' communicates from one call to the next 682 | * how much of ARGV has been scanned so far. 683 | */ 684 | public int 685 | getOptind() 686 | { 687 | return(optind); 688 | } 689 | 690 | /**************************************************************************/ 691 | 692 | /** 693 | * This method allows the optind index to be set manually. Normally this 694 | * is not necessary (and incorrect usage of this method can lead to serious 695 | * lossage), but optind is a public symbol in GNU getopt, so this method 696 | * was added to allow it to be modified by the caller if desired. 697 | * 698 | * @param optind The new value of optind 699 | */ 700 | public void 701 | setOptind(int optind) 702 | { 703 | this.optind = optind; 704 | } 705 | 706 | /**************************************************************************/ 707 | 708 | /** 709 | * Since in GNU getopt() the argument vector is passed back in to the 710 | * function every time, the caller can swap out argv on the fly. Since 711 | * passing argv is not required in the Java version, this method allows 712 | * the user to override argv. Note that incorrect use of this method can 713 | * lead to serious lossage. 714 | * 715 | * @param argv New argument list 716 | */ 717 | public void 718 | setArgv(String[] argv) 719 | { 720 | this.argv = argv; 721 | } 722 | 723 | /**************************************************************************/ 724 | 725 | /** 726 | * For communication from `getopt' to the caller. 727 | * When `getopt' finds an option that takes an argument, 728 | * the argument value is returned here. 729 | * Also, when `ordering' is RETURN_IN_ORDER, 730 | * each non-option ARGV-element is returned here. 731 | * No set method is provided because setting this variable has no effect. 732 | */ 733 | public String 734 | getOptarg() 735 | { 736 | return(optarg); 737 | } 738 | 739 | /**************************************************************************/ 740 | 741 | /** 742 | * Normally Getopt will print a message to the standard error when an 743 | * invalid option is encountered. This can be suppressed (or re-enabled) 744 | * by calling this method. There is no get method for this variable 745 | * because if you can't remember the state you set this to, why should I? 746 | */ 747 | public void 748 | setOpterr(boolean opterr) 749 | { 750 | this.opterr = opterr; 751 | } 752 | 753 | /**************************************************************************/ 754 | 755 | /** 756 | * When getopt() encounters an invalid option, it stores the value of that 757 | * option in optopt which can be retrieved with this method. There is 758 | * no corresponding set method because setting this variable has no effect. 759 | */ 760 | public int 761 | getOptopt() 762 | { 763 | return(optopt); 764 | } 765 | 766 | /**************************************************************************/ 767 | 768 | /** 769 | * Returns the index into the array of long options (NOT argv) representing 770 | * the long option that was found. 771 | */ 772 | public int 773 | getLongind() 774 | { 775 | return(longind); 776 | } 777 | 778 | /**************************************************************************/ 779 | 780 | /** 781 | * Exchange the shorter segment with the far end of the longer segment. 782 | * That puts the shorter segment into the right place. 783 | * It leaves the longer segment in the right place overall, 784 | * but it consists of two parts that need to be swapped next. 785 | * This method is used by getopt() for argument permutation. 786 | */ 787 | protected void 788 | exchange(String[] argv) 789 | { 790 | int bottom = first_nonopt; 791 | int middle = last_nonopt; 792 | int top = optind; 793 | String tem; 794 | 795 | while (top > middle && middle > bottom) 796 | { 797 | if (top - middle > middle - bottom) 798 | { 799 | // Bottom segment is the short one. 800 | int len = middle - bottom; 801 | int i; 802 | 803 | // Swap it with the top part of the top segment. 804 | for (i = 0; i < len; i++) 805 | { 806 | tem = argv[bottom + i]; 807 | argv[bottom + i] = argv[top - (middle - bottom) + i]; 808 | argv[top - (middle - bottom) + i] = tem; 809 | } 810 | // Exclude the moved bottom segment from further swapping. 811 | top -= len; 812 | } 813 | else 814 | { 815 | // Top segment is the short one. 816 | int len = top - middle; 817 | int i; 818 | 819 | // Swap it with the bottom part of the bottom segment. 820 | for (i = 0; i < len; i++) 821 | { 822 | tem = argv[bottom + i]; 823 | argv[bottom + i] = argv[middle + i]; 824 | argv[middle + i] = tem; 825 | } 826 | // Exclude the moved top segment from further swapping. 827 | bottom += len; 828 | } 829 | } 830 | 831 | // Update records for the slots the non-options now occupy. 832 | 833 | first_nonopt += (optind - last_nonopt); 834 | last_nonopt = optind; 835 | } 836 | 837 | /**************************************************************************/ 838 | 839 | /** 840 | * Check to see if an option is a valid long option. Called by getopt(). 841 | * Put in a separate method because this needs to be done twice. (The 842 | * C getopt authors just copy-pasted the code!). 843 | * 844 | * @param longind A buffer in which to store the 'val' field of found LongOpt 845 | * 846 | * @return Various things depending on circumstances 847 | */ 848 | protected int 849 | checkLongOption() 850 | { 851 | LongOpt pfound = null; 852 | int nameend; 853 | boolean ambig; 854 | boolean exact; 855 | 856 | longopt_handled = true; 857 | ambig = false; 858 | exact = false; 859 | longind = -1; 860 | 861 | nameend = nextchar.indexOf("="); 862 | if (nameend == -1) 863 | nameend = nextchar.length(); 864 | 865 | // Test all lnog options for either exact match or abbreviated matches 866 | for (int i = 0; i < long_options.length; i++) 867 | { 868 | if (long_options[i].getName().startsWith(nextchar.substring(0, nameend))) 869 | { 870 | if (long_options[i].getName().equals(nextchar.substring(0, nameend))) 871 | { 872 | // Exact match found 873 | pfound = long_options[i]; 874 | longind = i; 875 | exact = true; 876 | break; 877 | } 878 | else if (pfound == null) 879 | { 880 | // First nonexact match found 881 | pfound = long_options[i]; 882 | longind = i; 883 | } 884 | else 885 | { 886 | // Second or later nonexact match found 887 | ambig = true; 888 | } 889 | } 890 | } // for 891 | 892 | // Print out an error if the option specified was ambiguous 893 | if (ambig && !exact) 894 | { 895 | if (opterr) 896 | { 897 | Object[] msgArgs = { progname, argv[optind] }; 898 | System.err.println(MessageFormat.format( 899 | _messages.getString("getopt.ambigious"), 900 | msgArgs)); 901 | } 902 | 903 | nextchar = ""; 904 | optopt = 0; 905 | ++optind; 906 | 907 | return('?'); 908 | } 909 | 910 | if (pfound != null) 911 | { 912 | ++optind; 913 | 914 | if (nameend != nextchar.length()) 915 | { 916 | if (pfound.has_arg != LongOpt.NO_ARGUMENT) 917 | { 918 | if (nextchar.substring(nameend).length() > 1) 919 | optarg = nextchar.substring(nameend+1); 920 | else 921 | optarg = ""; 922 | } 923 | else 924 | { 925 | if (opterr) 926 | { 927 | // -- option 928 | if (argv[optind - 1].startsWith("--")) 929 | { 930 | Object[] msgArgs = { progname, pfound.name }; 931 | System.err.println(MessageFormat.format( 932 | _messages.getString("getopt.arguments1"), 933 | msgArgs)); 934 | } 935 | // +option or -option 936 | else 937 | { 938 | Object[] msgArgs = { progname, new 939 | Character(argv[optind-1].charAt(0)).toString(), 940 | pfound.name }; 941 | System.err.println(MessageFormat.format( 942 | _messages.getString("getopt.arguments2"), 943 | msgArgs)); 944 | } 945 | } 946 | 947 | nextchar = ""; 948 | optopt = pfound.val; 949 | 950 | return('?'); 951 | } 952 | } // if (nameend) 953 | else if (pfound.has_arg == LongOpt.REQUIRED_ARGUMENT) 954 | { 955 | if (optind < argv.length) 956 | { 957 | optarg = argv[optind]; 958 | ++optind; 959 | } 960 | else 961 | { 962 | if (opterr) 963 | { 964 | Object[] msgArgs = { progname, argv[optind-1] }; 965 | System.err.println(MessageFormat.format( 966 | _messages.getString("getopt.requires"), 967 | msgArgs)); 968 | } 969 | 970 | nextchar = ""; 971 | optopt = pfound.val; 972 | if (optstring.charAt(0) == ':') 973 | return(':'); 974 | else 975 | return('?'); 976 | } 977 | } // else if (pfound) 978 | 979 | nextchar = ""; 980 | 981 | if (pfound.flag != null) 982 | { 983 | pfound.flag.setLength(0); 984 | pfound.flag.append(pfound.val); 985 | 986 | return(0); 987 | } 988 | 989 | return(pfound.val); 990 | } // if (pfound != null) 991 | 992 | longopt_handled = false; 993 | 994 | return(0); 995 | } 996 | 997 | /**************************************************************************/ 998 | 999 | /** 1000 | * This method returns a char that is the current option that has been 1001 | * parsed from the command line. If the option takes an argument, then 1002 | * the internal variable 'optarg' is set which is a String representing 1003 | * the the value of the argument. This value can be retrieved by the 1004 | * caller using the getOptarg() method. If an invalid option is found, 1005 | * an error message is printed and a '?' is returned. The name of the 1006 | * invalid option character can be retrieved by calling the getOptopt() 1007 | * method. When there are no more options to be scanned, this method 1008 | * returns -1. The index of first non-option element in argv can be 1009 | * retrieved with the getOptind() method. 1010 | * 1011 | * @return Various things as described above 1012 | */ 1013 | public int 1014 | getopt() 1015 | { 1016 | optarg = null; 1017 | 1018 | if (endparse == true) 1019 | return(-1); 1020 | 1021 | if ((nextchar == null) || (nextchar.equals(""))) 1022 | { 1023 | // If we have just processed some options following some non-options, 1024 | // exchange them so that the options come first. 1025 | if (last_nonopt > optind) 1026 | last_nonopt = optind; 1027 | if (first_nonopt > optind) 1028 | first_nonopt = optind; 1029 | 1030 | if (ordering == PERMUTE) 1031 | { 1032 | // If we have just processed some options following some non-options, 1033 | // exchange them so that the options come first. 1034 | if ((first_nonopt != last_nonopt) && (last_nonopt != optind)) 1035 | exchange(argv); 1036 | else if (last_nonopt != optind) 1037 | first_nonopt = optind; 1038 | 1039 | // Skip any additional non-options 1040 | // and extend the range of non-options previously skipped. 1041 | while ((optind < argv.length) && (argv[optind].equals("") || 1042 | (argv[optind].charAt(0) != '-') || argv[optind].equals("-"))) 1043 | { 1044 | optind++; 1045 | } 1046 | 1047 | last_nonopt = optind; 1048 | } 1049 | 1050 | // The special ARGV-element `--' means premature end of options. 1051 | // Skip it like a null option, 1052 | // then exchange with previous non-options as if it were an option, 1053 | // then skip everything else like a non-option. 1054 | if ((optind != argv.length) && argv[optind].equals("--")) 1055 | { 1056 | optind++; 1057 | 1058 | if ((first_nonopt != last_nonopt) && (last_nonopt != optind)) 1059 | exchange (argv); 1060 | else if (first_nonopt == last_nonopt) 1061 | first_nonopt = optind; 1062 | 1063 | last_nonopt = argv.length; 1064 | 1065 | optind = argv.length; 1066 | } 1067 | 1068 | // If we have done all the ARGV-elements, stop the scan 1069 | // and back over any non-options that we skipped and permuted. 1070 | if (optind == argv.length) 1071 | { 1072 | // Set the next-arg-index to point at the non-options 1073 | // that we previously skipped, so the caller will digest them. 1074 | if (first_nonopt != last_nonopt) 1075 | optind = first_nonopt; 1076 | 1077 | return(-1); 1078 | } 1079 | 1080 | // If we have come to a non-option and did not permute it, 1081 | // either stop the scan or describe it to the caller and pass it by. 1082 | if (argv[optind].equals("") || (argv[optind].charAt(0) != '-') || 1083 | argv[optind].equals("-")) 1084 | { 1085 | if (ordering == REQUIRE_ORDER) 1086 | return(-1); 1087 | 1088 | optarg = argv[optind++]; 1089 | return(1); 1090 | } 1091 | 1092 | // We have found another option-ARGV-element. 1093 | // Skip the initial punctuation. 1094 | if (argv[optind].startsWith("--")) 1095 | nextchar = argv[optind].substring(2); 1096 | else 1097 | nextchar = argv[optind].substring(1); 1098 | } 1099 | 1100 | // Decode the current option-ARGV-element. 1101 | 1102 | /* Check whether the ARGV-element is a long option. 1103 | 1104 | If long_only and the ARGV-element has the form "-f", where f is 1105 | a valid short option, don't consider it an abbreviated form of 1106 | a long option that starts with f. Otherwise there would be no 1107 | way to give the -f short option. 1108 | 1109 | On the other hand, if there's a long option "fubar" and 1110 | the ARGV-element is "-fu", do consider that an abbreviation of 1111 | the long option, just like "--fu", and not "-f" with arg "u". 1112 | 1113 | This distinction seems to be the most useful approach. */ 1114 | if ((long_options != null) && (argv[optind].startsWith("--") 1115 | || (long_only && ((argv[optind].length() > 2) || 1116 | (optstring.indexOf(argv[optind].charAt(1)) == -1))))) 1117 | { 1118 | int c = checkLongOption(); 1119 | 1120 | if (longopt_handled) 1121 | return(c); 1122 | 1123 | // Can't find it as a long option. If this is not getopt_long_only, 1124 | // or the option starts with '--' or is not a valid short 1125 | // option, then it's an error. 1126 | // Otherwise interpret it as a short option. 1127 | if (!long_only || argv[optind].startsWith("--") 1128 | || (optstring.indexOf(nextchar.charAt(0)) == -1)) 1129 | { 1130 | if (opterr) 1131 | { 1132 | if (argv[optind].startsWith("--")) 1133 | { 1134 | Object[] msgArgs = { progname, nextchar }; 1135 | System.err.println(MessageFormat.format( 1136 | _messages.getString("getopt.unrecognized"), 1137 | msgArgs)); 1138 | } 1139 | else 1140 | { 1141 | Object[] msgArgs = { progname, new 1142 | Character(argv[optind].charAt(0)).toString(), 1143 | nextchar }; 1144 | System.err.println(MessageFormat.format( 1145 | _messages.getString("getopt.unrecognized2"), 1146 | msgArgs)); 1147 | } 1148 | } 1149 | 1150 | nextchar = ""; 1151 | ++optind; 1152 | optopt = 0; 1153 | 1154 | return('?'); 1155 | } 1156 | } // if (longopts) 1157 | 1158 | // Look at and handle the next short option-character */ 1159 | int c = nextchar.charAt(0); //**** Do we need to check for empty str? 1160 | if (nextchar.length() > 1) 1161 | nextchar = nextchar.substring(1); 1162 | else 1163 | nextchar = ""; 1164 | 1165 | String temp = null; 1166 | if (optstring.indexOf(c) != -1) 1167 | temp = optstring.substring(optstring.indexOf(c)); 1168 | 1169 | if (nextchar.equals("")) 1170 | ++optind; 1171 | 1172 | if ((temp == null) || (c == ':')) 1173 | { 1174 | if (opterr) 1175 | { 1176 | if (posixly_correct) 1177 | { 1178 | // 1003.2 specifies the format of this message 1179 | Object[] msgArgs = { progname, new 1180 | Character((char)c).toString() }; 1181 | System.err.println(MessageFormat.format( 1182 | _messages.getString("getopt.illegal"), msgArgs)); 1183 | } 1184 | else 1185 | { 1186 | Object[] msgArgs = { progname, new 1187 | Character((char)c).toString() }; 1188 | System.err.println(MessageFormat.format( 1189 | _messages.getString("getopt.invalid"), msgArgs)); 1190 | } 1191 | } 1192 | 1193 | optopt = c; 1194 | 1195 | return('?'); 1196 | } 1197 | 1198 | // Convenience. Treat POSIX -W foo same as long option --foo 1199 | if ((temp.charAt(0) == 'W') && (temp.length() > 1) && (temp.charAt(1) == ';')) 1200 | { 1201 | if (!nextchar.equals("")) 1202 | { 1203 | optarg = nextchar; 1204 | } 1205 | // No further cars in this argv element and no more argv elements 1206 | else if (optind == argv.length) 1207 | { 1208 | if (opterr) 1209 | { 1210 | // 1003.2 specifies the format of this message. 1211 | Object[] msgArgs = { progname, new 1212 | Character((char)c).toString() }; 1213 | System.err.println(MessageFormat.format( 1214 | _messages.getString("getopt.requires2"), msgArgs)); 1215 | } 1216 | 1217 | optopt = c; 1218 | if (optstring.charAt(0) == ':') 1219 | return(':'); 1220 | else 1221 | return('?'); 1222 | } 1223 | else 1224 | { 1225 | // We already incremented `optind' once; 1226 | // increment it again when taking next ARGV-elt as argument. 1227 | nextchar = argv[optind]; 1228 | optarg = argv[optind]; 1229 | } 1230 | 1231 | c = checkLongOption(); 1232 | 1233 | if (longopt_handled) 1234 | return(c); 1235 | else 1236 | // Let the application handle it 1237 | { 1238 | nextchar = null; 1239 | ++optind; 1240 | return('W'); 1241 | } 1242 | } 1243 | 1244 | if ((temp.length() > 1) && (temp.charAt(1) == ':')) 1245 | { 1246 | if ((temp.length() > 2) && (temp.charAt(2) == ':')) 1247 | // This is an option that accepts and argument optionally 1248 | { 1249 | if (!nextchar.equals("")) 1250 | { 1251 | optarg = nextchar; 1252 | ++optind; 1253 | } 1254 | else 1255 | { 1256 | optarg = null; 1257 | } 1258 | 1259 | nextchar = null; 1260 | } 1261 | else 1262 | { 1263 | if (!nextchar.equals("")) 1264 | { 1265 | optarg = nextchar; 1266 | ++optind; 1267 | } 1268 | else if (optind == argv.length) 1269 | { 1270 | if (opterr) 1271 | { 1272 | // 1003.2 specifies the format of this message 1273 | Object[] msgArgs = { progname, new 1274 | Character((char)c).toString() }; 1275 | System.err.println(MessageFormat.format( 1276 | _messages.getString("getopt.requires2"), msgArgs)); 1277 | } 1278 | 1279 | optopt = c; 1280 | 1281 | if (optstring.charAt(0) == ':') 1282 | return(':'); 1283 | else 1284 | return('?'); 1285 | } 1286 | else 1287 | { 1288 | optarg = argv[optind]; 1289 | ++optind; 1290 | 1291 | // Ok, here's an obscure Posix case. If we have o:, and 1292 | // we get -o -- foo, then we're supposed to skip the --, 1293 | // end parsing of options, and make foo an operand to -o. 1294 | // Only do this in Posix mode. 1295 | if ((posixly_correct) && optarg.equals("--")) 1296 | { 1297 | // If end of argv, error out 1298 | if (optind == argv.length) 1299 | { 1300 | if (opterr) 1301 | { 1302 | // 1003.2 specifies the format of this message 1303 | Object[] msgArgs = { progname, new 1304 | Character((char)c).toString() }; 1305 | System.err.println(MessageFormat.format( 1306 | _messages.getString("getopt.requires2"), msgArgs)); 1307 | } 1308 | 1309 | optopt = c; 1310 | 1311 | if (optstring.charAt(0) == ':') 1312 | return(':'); 1313 | else 1314 | return('?'); 1315 | } 1316 | 1317 | // Set new optarg and set to end 1318 | // Don't permute as we do on -- up above since we 1319 | // know we aren't in permute mode because of Posix. 1320 | optarg = argv[optind]; 1321 | ++optind; 1322 | first_nonopt = optind; 1323 | last_nonopt = argv.length; 1324 | endparse = true; 1325 | } 1326 | } 1327 | 1328 | nextchar = null; 1329 | } 1330 | } 1331 | 1332 | return(c); 1333 | } 1334 | 1335 | } // Class Getopt 1336 | 1337 | 1338 | --------------------------------------------------------------------------------