KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the docs/licenses/apache-1.1.txt file.
7  */

8 // This file is pulled from package org.apache.avalon.excalibur.cli Excalibur
9
// version 4.1 (Jan 30, 2002). Only the package name has been changed.
10
package org.jboss.axis.utils;
11
12
13 /**
14  * CLUtil offers basic utility operations for use both internal and external to package.
15  *
16  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
17  * @since 4.0
18  */

19 public final class CLUtil
20 {
21    private static final int MAX_DESCRIPTION_COLUMN_LENGTH = 60;
22
23    /**
24     * Format options into StringBuffer and return. This is typically used to
25     * print "Usage" text in response to a "--help" or invalid option.
26     *
27     * @param options the option descriptors
28     * @return the formatted description/help for options
29     */

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

103    private CLUtil()
104    {
105    }
106 }
107
Popular Tags