KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > cli > avalon > CLUtil


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.commons.cli.avalon;
18 //Renamed from org.apache.avalon.excalibur.cli
19

20 /**
21  * CLUtil offers basic utility operations for use both internal and external to package.
22  *
23  * @version $Revision: 1.1.2.2 $ $Date: 2005/02/20 15:56:12 $
24  * @see CLOptionDescriptor
25  */

26 public final class CLUtil
27 {
28     private static final int MAX_DESCRIPTION_COLUMN_LENGTH = 60;
29
30     /**
31      * Format options into StringBuffer and return. This is typically used to
32      * print "Usage" text in response to a "--help" or invalid option.
33      *
34      * @param options the option descriptors
35      * @return the formatted description/help for options
36      */

37     public static final StringBuffer JavaDoc describeOptions( final CLOptionDescriptor[] options )
38     {
39         final String JavaDoc lSep = System.getProperty( "line.separator" );
40         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
41
42         for( int i = 0; i < options.length; i++ )
43         {
44             final char ch = (char)options[i].getId();
45             final String JavaDoc name = options[i].getName();
46             String JavaDoc description = options[i].getDescription();
47             int flags = options[i].getFlags();
48             boolean argumentRequired =
49                     ((flags & CLOptionDescriptor.ARGUMENT_REQUIRED) ==
50                     CLOptionDescriptor.ARGUMENT_REQUIRED);
51             boolean twoArgumentsRequired =
52                     ((flags & CLOptionDescriptor.ARGUMENTS_REQUIRED_2) ==
53                     CLOptionDescriptor.ARGUMENTS_REQUIRED_2);
54             boolean needComma = false;
55             if( twoArgumentsRequired )
56             {
57                 argumentRequired = true;
58             }
59
60             sb.append( '\t' );
61
62             if( Character.isLetter( ch ) )
63             {
64                 sb.append( "-" );
65                 sb.append( ch );
66                 needComma = true;
67             }
68
69             if( null != name )
70             {
71                 if( needComma )
72                 {
73                     sb.append( ", " );
74                 }
75
76                 sb.append( "--" );
77                 sb.append( name );
78             }
79
80             if( argumentRequired )
81             {
82                 sb.append( " <argument>" );
83             }
84             if( twoArgumentsRequired )
85             {
86                 sb.append( "=<value>" );
87             }
88             sb.append( lSep );
89
90             if( null != description )
91             {
92                 while( description.length() > MAX_DESCRIPTION_COLUMN_LENGTH )
93                 {
94                     final String JavaDoc descriptionPart =
95                             description.substring( 0, MAX_DESCRIPTION_COLUMN_LENGTH );
96                     description =
97                             description.substring( MAX_DESCRIPTION_COLUMN_LENGTH );
98                     sb.append( "\t\t" );
99                     sb.append( descriptionPart );
100                     sb.append( lSep );
101                 }
102
103                 sb.append( "\t\t" );
104                 sb.append( description );
105                 sb.append( lSep );
106             }
107         }
108         return sb;
109     }
110
111     /**
112      * Private Constructor so that no instance can ever be created.
113      *
114      */

115     private CLUtil()
116     {
117     }
118 }
119
Popular Tags