Source Code Cross Referenced for Getopts.java in  » Report » datavision-1.1.0 » jimm » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Report » datavision 1.1.0 » jimm.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package jimm.util;
002:
003:        import java.util.HashMap;
004:
005:        /**
006:         * Getopts is similar to the UN*X getopt() system call. It parses an array of
007:         * Strings (usually the command line), looking for specified option flags and
008:         * values.
009:         * <p>
010:         * An instance of Getopts parses the whole args list at once, and stores the
011:         * option flags and values that it finds.
012:         *
013:         * @author Jim Menard,
014:         * <a href="mailto:jimm@io.com">jimm@io.com</a>
015:         */
016:        public class Getopts {
017:            String[] argv;
018:            HashMap options = new HashMap();
019:            boolean errorFlag = false;
020:
021:            /**
022:             * This constructor takes a list of legal options and a list of (usually
023:             * command line) arguments. Each option in optionListString may be followed
024:             * by a ':' to signify that option takes an argument.
025:             *
026:             * @param optionListString option chars with optional ':' specifying arg.
027:             * For example, "ab:c" specifies three options, a, b, and c. Option b takes
028:             * a (required) argument.
029:             * @param args array of command line arguments
030:             */
031:            public Getopts(String optionListString, String[] args) {
032:                String optChoices = optionListString;
033:
034:                for (int index = 0; index < args.length; ++index) {
035:                    String arg = args[index];
036:                    if (arg.startsWith("-")) {
037:                        char optionChar = arg.charAt(1);
038:                        int optionLoc = optChoices.indexOf(optionChar);
039:                        if (optionLoc == -1)
040:                            errorFlag = true;
041:                        else {
042:                            // Look for argument, if any
043:                            boolean hasArgument = optChoices.length() > optionLoc + 1
044:                                    && optChoices.charAt(optionLoc + 1) == ':';
045:                            if (hasArgument) {
046:                                String optarg = arg.substring(2);
047:                                if (optarg.equals("")) {
048:                                    ++index;
049:                                    try {
050:                                        optarg = args[index];
051:                                    } catch (Exception e) { // Catch ArrayOutOfBounds
052:                                        optarg = "";
053:                                        errorFlag = true;
054:                                    }
055:                                }
056:                                options.put(new Character(optionChar), optarg);
057:                            } else {
058:                                // No arg, store empty string
059:                                options.put(new Character(optionChar), "");
060:                            }
061:                        }
062:                    } else { // End of options. Store rest of args
063:                        argv = new String[args.length - index];
064:                        int offset = index;
065:                        while (index < args.length) {
066:                            argv[index - offset] = args[index];
067:                            ++index;
068:                        }
069:                        break;
070:                    }
071:                }
072:            }
073:
074:            /**
075:             * 
076:             * Return true if there was an error while parsing the command line.
077:             */
078:            public boolean error() {
079:                return errorFlag;
080:            }
081:
082:            /**
083:             * Returns existence of an option.
084:             *
085:             * @return true of option 'c' exists, else return false.
086:             * @param c any character
087:             */
088:            public boolean hasOption(char c) {
089:                if (options == null)
090:                    return false;
091:                return options.containsKey(new Character(c));
092:            }
093:
094:            /**
095:             * Return an option or, if missing, the empty string.
096:             *
097:             * @return option string, or "" if error or option has no argument
098:             * @param c the option whose value is returned
099:             */
100:            public String option(char c) {
101:                return option(c, "");
102:            }
103:
104:            /**
105:             * Return an option or, if missing, a default value.
106:             *
107:             * @return option string, or defaultValue if error or option has no argument
108:             * @param c the option whose value is returned
109:             * @param defaultValue the value to return if there is no such option
110:             */
111:            public String option(char c, String defaultValue) {
112:                if (options == null)
113:                    return defaultValue;
114:
115:                String s;
116:                try {
117:                    Object o = options.get(new Character(c));
118:                    if (o == null || !(o instanceof  String))
119:                        s = defaultValue;
120:                    else
121:                        s = (String) o;
122:                } catch (Exception e) {
123:                    s = defaultValue;
124:                }
125:                return s;
126:            }
127:
128:            /**
129:             * Return the remaining command-line arguments.
130:             *
131:             * @return an array of Strings
132:             * @see #argc
133:             * @see #argv
134:             */
135:            public String[] args() {
136:                return argv;
137:            }
138:
139:            /**
140:             * Return the number of non-option args.
141:             */
142:            public int argc() {
143:                if (argv == null)
144:                    return 0;
145:                return argv.length;
146:            }
147:
148:            /**
149:             * Return a command line argument or "" if <var>argv</var> is
150:             * <code>null</code>. Index starts at 0.
151:             *
152:             * @param index which argument to return
153:             * @return the index'th arg or "" if <var>argv</var> is <code>null</code>
154:             */
155:            public String argv(int index) {
156:                if (argv == null)
157:                    return "";
158:
159:                return argv[index];
160:            }
161:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.