KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > utils > CLUtil


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

16 // This file is pulled from package org.apache.avalon.excalibur.cli Excalibur
17
// version 4.1 (Jan 30, 2002). Only the package name has been changed.
18
package org.apache.axis.utils;
19
20
21 /**
22  * CLUtil offers basic utility operations for use both internal and external to package.
23  *
24  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
25  * @since 4.0
26  */

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

38     public static final StringBuffer JavaDoc describeOptions( final CLOptionDescriptor[] options )
39     {
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                 argumentRequired = true;
57
58             sb.append('\t');
59
60             if( Character.isLetter(ch) )
61             {
62                 sb.append( "-" );
63                 sb.append( ch );
64                 needComma = true;
65             }
66
67             if( null != name )
68             {
69                 if( needComma )
70                 {
71                     sb.append( ", " );
72                 }
73
74                 sb.append( "--" );
75                 sb.append( name );
76                 if (argumentRequired)
77                 {
78                     sb.append(" <argument>");
79                 }
80                 if (twoArgumentsRequired)
81                 {
82                     sb.append("=<value>");
83                 }
84                 sb.append( JavaUtils.LS );
85             }
86
87             if( null != description )
88             {
89                 while( description.length() > MAX_DESCRIPTION_COLUMN_LENGTH )
90                 {
91                     final String JavaDoc descriptionPart =
92                         description.substring( 0, MAX_DESCRIPTION_COLUMN_LENGTH );
93                     description =
94                         description.substring( MAX_DESCRIPTION_COLUMN_LENGTH );
95                     sb.append( "\t\t" );
96                     sb.append( descriptionPart );
97                     sb.append( JavaUtils.LS );
98                 }
99
100                 sb.append( "\t\t" );
101                 sb.append( description );
102                 sb.append( JavaUtils.LS );
103             }
104         }
105         return sb;
106     }
107
108     /**
109      * Private Constructor so that no instance can ever be created.
110      *
111      */

112     private CLUtil()
113     {
114     }
115 }
116
Popular Tags