001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.commandline;
034:
035: import java.util.*;
036:
037: import com.jeantessier.text.*;
038:
039: /**
040: * Generates summary information about the command-line specification.
041: */
042: public class CommandLineUsage extends Printer {
043: private String commandName;
044: private PrinterBuffer extraText = new PrinterBuffer();
045:
046: public CommandLineUsage(String commandName) {
047: this .commandName = commandName;
048: raiseIndent();
049: }
050:
051: public String toString() {
052: PrinterBuffer buffer = new PrinterBuffer();
053:
054: buffer.indent().append("USAGE:").eol();
055: buffer.raiseIndent();
056: buffer.indent().append(commandName).eol();
057: buffer.lowerIndent();
058:
059: buffer.append(super .toString());
060:
061: if (extraText.length() > 0) {
062: buffer.append(extraText);
063: }
064:
065: return buffer.toString();
066: }
067:
068: protected Set<String> getSwitchNames(CommandLine commandLine) {
069: return commandLine.getKnownSwitches();
070: }
071:
072: public void visitToggleSwitch(ToggleSwitch cls) {
073: if (cls.isMandatory()) {
074: indent().append("-").append(cls.getName()).append(
075: " (defaults to ").append(cls.getDefaultValue())
076: .append(")").eol();
077: } else {
078: indent().append("[-").append(cls.getName()).append(
079: "] (defaults to ").append(cls.getDefaultValue())
080: .append(")").eol();
081: }
082: }
083:
084: public void visitSingleValueSwitch(SingleValueSwitch cls) {
085: if (cls.isMandatory()) {
086: indent().append("-").append(cls.getName()).append(
087: " value (defaults to ").append(
088: cls.getDefaultValue()).append(")").eol();
089: } else {
090: indent().append("[-").append(cls.getName()).append(
091: " value] (defaults to ").append(
092: cls.getDefaultValue()).append(")").eol();
093: }
094: }
095:
096: public void visitOptionalValueSwitch(OptionalValueSwitch cls) {
097: if (cls.isMandatory()) {
098: indent().append("-").append(cls.getName()).append(
099: " [value] (defaults to ").append(
100: cls.getDefaultValue()).append(")").eol();
101: } else {
102: indent().append("[-").append(cls.getName()).append(
103: " [value]] (defaults to ").append(
104: cls.getDefaultValue()).append(")").eol();
105: }
106: }
107:
108: public void visitMultipleValuesSwitch(MultipleValuesSwitch cls) {
109: if (cls.isMandatory()) {
110: indent().append("(-").append(cls.getName()).append(
111: " value)+ (defaults to ").append(
112: cls.getDefaultValue()).append(")").eol();
113: } else {
114: indent().append("(-").append(cls.getName()).append(
115: " value)* (defaults to ").append(
116: cls.getDefaultValue()).append(")").eol();
117: }
118: }
119:
120: public void visitAliasSwitch(AliasSwitch cls) {
121: indent().append("[-").append(cls.getName()).append("]").eol();
122:
123: extraText.eol();
124: extraText.indent().append("-").append(cls.getName()).append(
125: " is shorthand for the combination:").eol();
126:
127: extraText.raiseIndent();
128: for (CommandLineSwitch commandLineSwitch : cls.getSwitches()) {
129: extraText.indent().append("-").append(
130: commandLineSwitch.getName()).eol();
131: }
132: extraText.lowerIndent();
133: }
134:
135: public void visitNullParameterStrategy(
136: NullParameterStrategy strategy) {
137: // Do nothing
138: }
139:
140: public void visitCollectingParameterStrategy(
141: CollectingParameterStrategy strategy) {
142: indent().append("[param ...]").eol();
143: }
144:
145: public void visitAtLeastParameterStrategy(
146: AtLeastParameterStrategy strategy) {
147: for (int i = 1; i <= strategy.getLimit(); i++) {
148: indent().append("param").append(i).eol();
149: }
150:
151: indent().append("...").eol();
152: }
153:
154: public void visitExactlyParameterStrategy(
155: ExactlyParameterStrategy strategy) {
156: for (int i = 1; i <= strategy.getLimit(); i++) {
157: indent().append("param").append(i).eol();
158: }
159: }
160:
161: public void visitAtMostParameterStrategy(
162: AtMostParameterStrategy strategy) {
163: indent();
164:
165: for (int i = 1; i <= strategy.getLimit(); i++) {
166: append("[param").append(i);
167: if (i < strategy.getLimit()) {
168: append(" ");
169: }
170: }
171:
172: for (int i = 1; i <= strategy.getLimit(); i++) {
173: append("]");
174: }
175:
176: eol();
177: }
178: }
|