Source Code Cross Referenced for CommandLine.java in  » Library » Apache-command-line » org » apache » commons » cli » 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 » Library » Apache command line » org.apache.commons.cli 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */package org.apache.commons.cli;
017:
018:        import java.util.Collection;
019:        import java.util.Iterator;
020:        import java.util.LinkedList;
021:        import java.util.List;
022:        import java.util.Set;
023:        import java.util.HashSet;
024:
025:        /** 
026:         * <p>Represents list of arguments parsed against
027:         * a {@link Options} descriptor.<p>
028:         *
029:         * <p>It allows querying of a boolean {@link #hasOption(String opt)},
030:         * in addition to retrieving the {@link #getOptionValue(String opt)}
031:         * for options requiring arguments.</p>
032:         *
033:         * <p>Additionally, any left-over or unrecognized arguments,
034:         * are available for further processing.</p>
035:         *
036:         * @author bob mcwhirter (bob @ werken.com)
037:         * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
038:         * @author John Keyes (john at integralsource.com)
039:         */
040:        public class CommandLine {
041:
042:            /** the unrecognised options/arguments */
043:            private List args = new LinkedList();
044:
045:            /** the processed options */
046:            private Set options = new HashSet();
047:
048:            /** Map of unique options for ease to get complete list of options */
049:            //    private Set allOptions = new HashSet();
050:            /**
051:             * Creates a command line.
052:             */
053:            CommandLine() {
054:                // nothing to do
055:            }
056:
057:            /** 
058:             * Query to see if an option has been set.
059:             *
060:             * @param opt Short name of the option
061:             * @return true if set, false if not
062:             */
063:            public boolean hasOption(String opt) {
064:                return options.contains(resolveOption(opt));
065:            }
066:
067:            /** 
068:             * Query to see if an option has been set.
069:             *
070:             * @param opt character name of the option
071:             * @return true if set, false if not
072:             */
073:            public boolean hasOption(char opt) {
074:                return hasOption(String.valueOf(opt));
075:            }
076:
077:            /**
078:             * Return the <code>Object</code> type of this <code>Option</code>.
079:             *
080:             * @param opt the name of the option
081:             * @return the type of this <code>Option</code>
082:             */
083:            public Object getOptionObject(String opt) {
084:                String res = getOptionValue(opt);
085:
086:                Option option = resolveOption(opt);
087:                if (option == null) {
088:                    return null;
089:                }
090:
091:                Object type = option.getType();
092:
093:                return (res == null) ? null : TypeHandler
094:                        .createValue(res, type);
095:            }
096:
097:            /**
098:             * Return the <code>Object</code> type of this <code>Option</code>.
099:             *
100:             * @param opt the name of the option
101:             * @return the type of opt
102:             */
103:            public Object getOptionObject(char opt) {
104:                return getOptionObject(String.valueOf(opt));
105:            }
106:
107:            /** 
108:             * Retrieve the argument, if any, of this option.
109:             *
110:             * @param opt the name of the option
111:             * @return Value of the argument if option is set, and has an argument,
112:             * otherwise null.
113:             */
114:            public String getOptionValue(String opt) {
115:                String[] values = getOptionValues(opt);
116:
117:                return (values == null) ? null : values[0];
118:            }
119:
120:            /** 
121:             * Retrieve the argument, if any, of this option.
122:             *
123:             * @param opt the character name of the option
124:             * @return Value of the argument if option is set, and has an argument,
125:             * otherwise null.
126:             */
127:            public String getOptionValue(char opt) {
128:                return getOptionValue(String.valueOf(opt));
129:            }
130:
131:            /** 
132:             * Retrieves the array of values, if any, of an option.
133:             *
134:             * @param opt string name of the option
135:             * @return Values of the argument if option is set, and has an argument,
136:             * otherwise null.
137:             */
138:            public String[] getOptionValues(String opt) {
139:                Option key = resolveOption(opt);
140:
141:                if (options.contains(key)) {
142:                    return key.getValues();
143:                }
144:
145:                return null;
146:            }
147:
148:            /**
149:             * <p>Retrieves the option object given the long or short option as a String</p>
150:             * @param opt short or long name of the option
151:             * @return Canonicalized option
152:             */
153:            private Option resolveOption(String opt) {
154:                opt = Util.stripLeadingHyphens(opt);
155:                for (Iterator it = options.iterator(); it.hasNext();) {
156:                    Option option = (Option) it.next();
157:                    if (opt.equals(option.getOpt())) {
158:                        return option;
159:                    }
160:                    if (opt.equals(option.getLongOpt())) {
161:                        return option;
162:                    }
163:
164:                }
165:                return null;
166:            }
167:
168:            /** 
169:             * Retrieves the array of values, if any, of an option.
170:             *
171:             * @param opt character name of the option
172:             * @return Values of the argument if option is set, and has an argument,
173:             * otherwise null.
174:             */
175:            public String[] getOptionValues(char opt) {
176:                return getOptionValues(String.valueOf(opt));
177:            }
178:
179:            /** 
180:             * Retrieve the argument, if any, of an option.
181:             *
182:             * @param opt name of the option
183:             * @param defaultValue is the default value to be returned if the option 
184:             * is not specified
185:             * @return Value of the argument if option is set, and has an argument,
186:             * otherwise <code>defaultValue</code>.
187:             */
188:            public String getOptionValue(String opt, String defaultValue) {
189:                String answer = getOptionValue(opt);
190:
191:                return (answer != null) ? answer : defaultValue;
192:            }
193:
194:            /** 
195:             * Retrieve the argument, if any, of an option.
196:             *
197:             * @param opt character name of the option
198:             * @param defaultValue is the default value to be returned if the option 
199:             * is not specified
200:             * @return Value of the argument if option is set, and has an argument,
201:             * otherwise <code>defaultValue</code>.
202:             */
203:            public String getOptionValue(char opt, String defaultValue) {
204:                return getOptionValue(String.valueOf(opt), defaultValue);
205:            }
206:
207:            /** 
208:             * Retrieve any left-over non-recognized options and arguments
209:             *
210:             * @return remaining items passed in but not parsed as an array
211:             */
212:            public String[] getArgs() {
213:                String[] answer = new String[args.size()];
214:
215:                args.toArray(answer);
216:
217:                return answer;
218:            }
219:
220:            /** 
221:             * Retrieve any left-over non-recognized options and arguments
222:             *
223:             * @return remaining items passed in but not parsed as a <code>List</code>.
224:             */
225:            public List getArgList() {
226:                return args;
227:            }
228:
229:            /** 
230:             * jkeyes
231:             * - commented out until it is implemented properly
232:             * <p>Dump state, suitable for debugging.</p>
233:             *
234:             * @return Stringified form of this object
235:             */
236:
237:            /*
238:            public String toString() {
239:                StringBuffer buf = new StringBuffer();
240:                    
241:                buf.append("[ CommandLine: [ options: ");
242:                buf.append(options.toString());
243:                buf.append(" ] [ args: ");
244:                buf.append(args.toString());
245:                buf.append(" ] ]");
246:                    
247:                return buf.toString();
248:            }
249:             */
250:
251:            /**
252:             * Add left-over unrecognized option/argument.
253:             *
254:             * @param arg the unrecognised option/argument.
255:             */
256:            void addArg(String arg) {
257:                args.add(arg);
258:            }
259:
260:            /**
261:             * Add an option to the command line.  The values of 
262:             * the option are stored.
263:             *
264:             * @param opt the processed option
265:             */
266:            void addOption(Option opt) {
267:                options.add(opt);
268:            }
269:
270:            /**
271:             * Returns an iterator over the Option members of CommandLine.
272:             *
273:             * @return an <code>Iterator</code> over the processed {@link Option} 
274:             * members of this {@link CommandLine}
275:             */
276:            public Iterator iterator() {
277:                return options.iterator();
278:            }
279:
280:            /**
281:             * Returns an array of the processed {@link Option}s.
282:             *
283:             * @return an array of the processed {@link Option}s.
284:             */
285:            public Option[] getOptions() {
286:                Collection processed = options;
287:
288:                // reinitialise array
289:                Option[] optionsArray = new Option[processed.size()];
290:
291:                // return the array
292:                return (Option[]) processed.toArray(optionsArray);
293:            }
294:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.