Java Doc for OptionParser.java in  » Development » JOpt-Simple » joptsimple » 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 » Development » JOpt Simple » joptsimple 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   joptsimple.OptionParser

OptionParser
public class OptionParser (Code)

Parses command line arguments according to GNU's conventions.

This parser supports both short options and long options.

  • Short options begin with a single hyphen ("-") followed by a single letter or digit.
  • Short options can accept single arguments. The argument can be made required or optional. The option can occur in:
    • the argument after the option, as in -d /tmp
    • right up against it, as in -d/tmp
    • right up against it separated by an equals sign ("="), as in -d=/tmp
  • Short options can be clustered, so that -abc is treated as -a -b -c, if none of those options can accept arguments.
  • An argument consisting only of two hyphens ("--") signals that the remaining arguments are to be treated as non-options.
  • An argument consisting only of a single hyphen is considered a non-option argument (though it can be an argument of an option). Many Unix programs treat single hyphens as stand-ins for the standard input or standard output streams.
  • Long options generally begin with two hyphens ("--"), followed by multiple letters and/or digits.
  • You can abbreviate long options, so long as the abbreviation is unique.
  • Long options can accept single arguments. The argument can be made required or optional. The option can occur in:
    • the argument after the option, as in --directory /tmp
    • right up against it separated by an equals sign ("="), as in --directory=/tmp
  • You can use a single hyphen ("-") instead of a double hyphen ("--") for a long option.
  • The option -W is reserved. If you tell the parser to , then it will treat, for example, -W foo=bar as the long option foo with argument bar, as though you had written --foo=bar.
  • You can specify -W as a valid short option, or use it as an abbreviation for a long option, but will always supersede this behavior.
  • You can specify a given short or long option multiple times on a single command line. The parser collects any arguments specified for those options as a list.
  • If the parser detects an option whose argument is optional, and the next argument "looks like" an option, that argument is not treated as the argument to the option, but as a potentially valid option. If, on the other hand, the optional argument is typed as a derivative of Number , then that argument is treated as the negative number argument of the option, even if the parser recognizes the corresponding numeric option. For example:
     parser.accepts( "a" ).withOptionalArg().ofType( Integer.class );
     parser.accepts( "1" );
     OptionSet options = parser.parse( new String[] { "-a", "-1" } );
     
    In this case, the option set contains "a" with argument -1, not both "a" and "1". Swapping the elements in the args array gives the latter.

There are two ways to tell the parser what options to recognize:

  1. A "domain-specific language"-style API for specifying options, available since version 2. Sentences in this domain-specific language begin with a call to OptionParser.accepts(String) accepts ; methods on the ensuing chain of objects describe whether the option passed to OptionParser.accepts(String) accepts can take an argument, whether the argument is required or optional, and to what type arguments of the option should be converted.
  2. Since version 1, a more concise way of specifying short options to recognize has been to use the special . Arguments of options specified in this manner will be of type String . Here are the rules for the format of the specification strings this constructor accepts:
    • Any letter or digit is treated as an option character.
    • If an option character is followed by a single colon (":"), then the option requires an argument.
    • If an option character is followed by two colons ("::"), then the option accepts an optional argument.
    • Otherwise, the option character accepts no argument.
    • If the option specification string begins with a plus sign ("+"), the parser will behave "POSIX-ly correct".
    • If the option specification string contains the sequence "W;" (capital W followed by a semicolon), the parser will recognize the alternative form of long options.

By default, as with GNU getopt(), the parser allows intermixing of options and non-options. If, however, the parser has been created to be "POSIX-ly correct", then the first argument that does not look lexically like an option, and is not a required argument of a preceding option, signals the end of options. You can still bind optional arguments to their options using the abutting (for short options) or = syntax.

Unlike GNU getopt(), this parser does not honor the environment variable POSIXLY_CORRECT. "POSIX-ly correct" parsers are configured by either:

  1. using the method OptionParser.setPosixlyCorrect(boolean) , or
  2. using the with an argument whose first character is a plus sign ("+")

since:
   1.0
author:
   Paul Holser
version:
   $Id: OptionParser.java,v 1.42 2007/04/21 22:24:58 pholser Exp $
See Also:    The GNU C Library



Constructor Summary
public  OptionParser()
     Creates an option parser that initially recognizes no options, and does not exhibit "POSIX-ly correct" behavior.
public  OptionParser(String optionSpecification)
     Creates an option parser and configures it to recognize the short options specified in the given string.

Method Summary
public  OptionSpecBuilderaccepts(String option)
     Tells the parser to recognize the given option.
public  OptionSpecBuilderaccepts(String option, String description)
     Tells the parser to recognize the given option.
since:
   2.1
See Also:   OptionParser.accepts(String)
Parameters:
  option - the option to recognize
Parameters:
  description - a string that describes the purpose of the option.
 voidhandleLongOptionToken(String candidate, ArgumentList arguments, OptionSet detected)
    
 voidhandleShortOptionToken(String candidate, ArgumentList arguments, OptionSet detected)
    
 booleanisPosixlyCorrect()
    
 booleanlooksLikeAnOption(String argument)
    
 voidnoMoreOptions()
    
public  OptionSetparse(String[] arguments)
     Parses the given command line arguments according to the option specifications given to the parser.
public  voidprintHelpOn(OutputStream sink)
     Writes information about the options this parser recognizes to the given output sink.
public  voidprintHelpOn(Writer sink)
     Writes information about the options this parser recognizes to the given output sink.
 voidrecognize(OptionSpec spec)
    
public  voidrecognizeAlternativeLongOptions(boolean recognize)
     Tells the parser either to recognize or ignore "-W"-style long options.
public  voidsetPosixlyCorrect(boolean posixlyCorrect)
     Tells the parser whether or not to behave "POSIX-ly correct"-ly.


Constructor Detail
OptionParser
public OptionParser()(Code)
Creates an option parser that initially recognizes no options, and does not exhibit "POSIX-ly correct" behavior.
since:
   1.0



OptionParser
public OptionParser(String optionSpecification)(Code)
Creates an option parser and configures it to recognize the short options specified in the given string.

Arguments of options specified this way will be of type String .
since:
   1.0
Parameters:
  optionSpecification - an option specification
throws:
  NullPointerException - if optionSpecification isnull





Method Detail
accepts
public OptionSpecBuilder accepts(String option)(Code)
Tells the parser to recognize the given option.

This method returns an instance of OptionSpecBuilder to allow the formation of parser directives as sentences in a domain-specific language. For example:

 OptionParser parser = new OptionParser();
 parser.accepts( "c" ).withRequiredArg().ofType( Integer.class );
 
If no methods are invoked on the returned OptionSpecBuilder , then the parser treats the option as accepting no argument.
since:
   2.0
Parameters:
  option - the option to recognize an object that can be used to flesh out more detail about the option
throws:
  OptionException - if the option contains illegal characters
throws:
  NullPointerException - if the option is null



accepts
public OptionSpecBuilder accepts(String option, String description)(Code)
Tells the parser to recognize the given option.
since:
   2.1
See Also:   OptionParser.accepts(String)
Parameters:
  option - the option to recognize
Parameters:
  description - a string that describes the purpose of the option. This isused when generating help information about the parser. an object that can be used to flesh out more detail about the option
throws:
  OptionException - if the option contains illegal characters
throws:
  NullPointerException - if the option is null



handleLongOptionToken
void handleLongOptionToken(String candidate, ArgumentList arguments, OptionSet detected)(Code)



handleShortOptionToken
void handleShortOptionToken(String candidate, ArgumentList arguments, OptionSet detected)(Code)



isPosixlyCorrect
boolean isPosixlyCorrect()(Code)



looksLikeAnOption
boolean looksLikeAnOption(String argument)(Code)



noMoreOptions
void noMoreOptions()(Code)



parse
public OptionSet parse(String[] arguments)(Code)
Parses the given command line arguments according to the option specifications given to the parser.
since:
   1.0
Parameters:
  arguments - arguments to parse an OptionSet describing the parsed options, their arguments, andany non-option arguments found
throws:
  OptionException - if problems are detected while parsing
throws:
  NullPointerException - if the argument list is null



printHelpOn
public void printHelpOn(OutputStream sink) throws IOException(Code)
Writes information about the options this parser recognizes to the given output sink.
since:
   2.1
Parameters:
  sink - the sink to write information to
throws:
  IOException - if there is a problem writing to the sink
throws:
  NullPointerException - if sink is null



printHelpOn
public void printHelpOn(Writer sink) throws IOException(Code)
Writes information about the options this parser recognizes to the given output sink.
since:
   2.1
Parameters:
  sink - the sink to write information to
throws:
  IOException - if there is a problem writing to the sink
throws:
  NullPointerException - if sink is null



recognize
void recognize(OptionSpec spec)(Code)



recognizeAlternativeLongOptions
public void recognizeAlternativeLongOptions(boolean recognize)(Code)
Tells the parser either to recognize or ignore "-W"-style long options.
since:
   1.0
Parameters:
  recognize - true if the parser is to recognize the special styleof long options



setPosixlyCorrect
public void setPosixlyCorrect(boolean posixlyCorrect)(Code)
Tells the parser whether or not to behave "POSIX-ly correct"-ly.
since:
   1.0
Parameters:
  posixlyCorrect - true if the parser should behave "POSIX-lycorrect"-ly



Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.