org.apache.commons.cli.avalon

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 » Testing » jakarta jmeter » org.apache.commons.cli.avalon 
org.apache.commons.cli.avalon
Package Documentation for org.apache.commons.cli.avalon Package Utility code for parsing command-line options.

These classes were originally in the Avalon project in the package org.apache.avalon.excalibur.cli

Introduction

The utilities in org.apache.commons.cli.avalon assist you in parsing command line options during startup time. It allows you to associate a short option and a long option to the same command, and then test for it in a switch statement.

Usage Example

import java.util.List;

import org.apache.commons.cli.avalon.CLArgsParser;
import org.apache.commons.cli.avalon.CLOption;
import org.apache.commons.cli.avalon.CLOptionDescriptor;
import org.apache.commons.cli.avalon.CLUtil;

/**
* Demonstrates the excalibur command-line parsing utility.
*
*/
public class CLDemo {
    // Define our short one-letter option identifiers.
    protected static final int HELP_OPT = 'h';
    protected static final int VERSION_OPT = 'v';
    protected static final int MSG_OPT = 'm';

    /**
     *  Define the understood options. Each CLOptionDescriptor contains:
     * - The "long" version of the option. Eg, "help" means that "--help" will
     * be recognised.
     * - The option flags, governing the option's argument(s).
     * - The "short" version of the option. Eg, 'h' means that "-h" will be
     * recognised.
     * - A description of the option.
     */
    protected static final CLOptionDescriptor [] options = new CLOptionDescriptor [] {
        new CLOptionDescriptor("help",
                CLOptionDescriptor.ARGUMENT_DISALLOWED,
                HELP_OPT,
                "print this message and exit"),
        new CLOptionDescriptor("version",
                CLOptionDescriptor.ARGUMENT_DISALLOWED,
                VERSION_OPT,
                "print the version information and exit"),
        new CLOptionDescriptor("msg",
                CLOptionDescriptor.ARGUMENT_REQUIRED,
                MSG_OPT,
                "the message to print"),
    };

    public static void main(String args[]) {
        // Parse the arguments
        CLArgsParser parser = new CLArgsParser(args, options);

        if( null != parser.getErrorString() ) {
           System.err.println( "Error: " + parser.getErrorString() );
           return;
        }

        // Get a list of parsed options
        List clOptions = parser.getArguments();
        int size = clOptions.size();

        for (int i = 0; i < size; i++) {
            CLOption option = (CLOption) clOptions.get(i);

            switch (option.getId()) {
                case CLOption.TEXT_ARGUMENT:
                    System.out.println("Unknown arg: "+option.getArgument());
                    break;

                case HELP_OPT:
                    printUsage();
                    break;

                case VERSION_OPT:
                    printVersion();
                    break;


                case MSG_OPT:
                    System.out.println(option.getArgument());
                    break;
            }
        }
    }

    private static void printVersion() {
        System.out.println("1.0");
        System.exit(0);
    }

    private static void printUsage() {
        String lSep = System.getProperty("line.separator");
        StringBuffer msg = new StringBuffer();
        msg.append("------------------------------------------------------------------------ ").append(lSep);
        msg.append("Excalibur command-line arg parser demo").append(lSep);
        msg.append("Usage: java "+CLDemo.class.getName()+" [options]").append(lSep).append(lSep);
        msg.append("Options: ").append(lSep);
        msg.append(CLUtil.describeOptions(CLDemo.options).toString());
        System.out.println(msg.toString());
        System.exit(0);
    }
}

Parsing Rules

The command line is parsed according to the following rules. There are two forms of options in this package, the Long form and the Short form. The long form of an option is preceded by the '--' characters while the short form is preceded by a single '-'. Some example options would be; "--an-option", "-a", "--day", "-s -f -a".

In the tradition of UNIX programs, the short form of an option can occur immediately after another short form option. So if 'a', 'b' and 'c' are short forms of options that take no parameters then the following command lines are equivalent: "-abc", "-a -bc", "-a -b -c", "-ab -c", etc.

Options can also accept arguments if specified. You can specify that an option requires an argument in which the text immediately following the option will be considered to be an argument to the option. So if 'a' was an option that required an argument then the following would be equivalent; "-abc", "-a bc" (namely the option 'a' with argument 'bc').

Options can also specify optional arguments. In this case if there is any text immediately following the option character then it is considered an argument. Otherwise, the option has no arguments. For example if 'a' was an option that required an optional argument then "-abc" is an option 'a' with argument "bc" while "-a bc" is an option 'a' with no argument, followed by the text "bc".

It is also possible to place an '=' sign between the option and its argument. So if we assume that a is an option that requires an argument then the following are equivalent; "-a=bc" and "-abc".

In the case of a long option with an optional argument, the '=' sign is required. For example. --optarg=1, not --optarg 1.

In some cases it is also necessary to disable command line parsing so that you can pass a text argument to the program that starts with a '-' character. To do this insert the sequence '--' onto the command line with no text immediately following it. This will disable processing for the rest of the command line. The '--' characters will not be passed to the user program. For instance the line "-- -b" would result in the program being passed the text "-b" (ie. not as an option).

Java Source File NameTypeComment
AbstractParserControl.javaClass Class to inherit from so when in future when new controls are added clients will no have to implement them.
CLArgsParser.javaClass Parser for command line arguments.
CLOption.javaClass Basic class describing an instance of option.
CLOptionDescriptor.javaClass Basic class describing an type of option.
CLUtil.javaClass CLUtil offers basic utility operations for use both internal and external to package.
ClutilTestCase.javaClass
ParserControl.javaInterface ParserControl is used to control particular behaviour of the parser.
Token.javaClass
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.