001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
014: * implied.
015: *
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019: package org.apache.commons.cli.avalon;
020:
021: // Renamed from org.apache.avalon.excalibur.cli
022:
023: /**
024: * CLUtil offers basic utility operations for use both internal and external to
025: * package.
026: *
027: * @see CLOptionDescriptor
028: */
029: public final class CLUtil {
030: private static final int MAX_DESCRIPTION_COLUMN_LENGTH = 60;
031:
032: /**
033: * Private Constructor so that no instance can ever be created.
034: *
035: */
036: private CLUtil() {
037: }
038:
039: /**
040: * Format options into StringBuffer and return. This is typically used to
041: * print "Usage" text in response to a "--help" or invalid option.
042: *
043: * @param options
044: * the option descriptors
045: * @return the formatted description/help for options
046: */
047: public static final StringBuffer describeOptions(
048: final CLOptionDescriptor[] options) {
049: final String lSep = System.getProperty("line.separator");
050: final StringBuffer sb = new StringBuffer();
051:
052: for (int i = 0; i < options.length; i++) {
053: final char ch = (char) options[i].getId();
054: final String name = options[i].getName();
055: String description = options[i].getDescription();
056: int flags = options[i].getFlags();
057: boolean argumentOptional = ((flags & CLOptionDescriptor.ARGUMENT_OPTIONAL) == CLOptionDescriptor.ARGUMENT_OPTIONAL);
058: boolean argumentRequired = ((flags & CLOptionDescriptor.ARGUMENT_REQUIRED) == CLOptionDescriptor.ARGUMENT_REQUIRED);
059: boolean twoArgumentsRequired = ((flags & CLOptionDescriptor.ARGUMENTS_REQUIRED_2) == CLOptionDescriptor.ARGUMENTS_REQUIRED_2);
060: boolean needComma = false;
061: if (twoArgumentsRequired) {
062: argumentRequired = true;
063: }
064:
065: sb.append('\t');
066:
067: if (Character.isLetter(ch)) {
068: sb.append("-");
069: sb.append(ch);
070: needComma = true;
071: }
072:
073: if (null != name) {
074: if (needComma) {
075: sb.append(", ");
076: }
077:
078: sb.append("--");
079: sb.append(name);
080: }
081:
082: if (argumentOptional) {
083: sb.append(" [<argument>]");
084: }
085: if (argumentRequired) {
086: sb.append(" <argument>");
087: }
088: if (twoArgumentsRequired) {
089: sb.append("=<value>");
090: }
091: sb.append(lSep);
092:
093: if (null != description) {
094: while (description.length() > MAX_DESCRIPTION_COLUMN_LENGTH) {
095: final String descriptionPart = description
096: .substring(0, MAX_DESCRIPTION_COLUMN_LENGTH);
097: description = description
098: .substring(MAX_DESCRIPTION_COLUMN_LENGTH);
099: sb.append("\t\t");
100: sb.append(descriptionPart);
101: sb.append(lSep);
102: }
103:
104: sb.append("\t\t");
105: sb.append(description);
106: sb.append(lSep);
107: }
108: }
109: return sb;
110: }
111: }
|