KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jeantessier > commandline > CommandLineUsage


1 /*
2  * Copyright (c) 2001-2005, Jean Tessier
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Jean Tessier nor the names of his contributors
17  * may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32
33 package com.jeantessier.commandline;
34
35 import java.util.*;
36
37 /**
38  * Generates summary information about the command-line specification.
39  */

40 public class CommandLineUsage implements Visitor {
41     private final static String JavaDoc EOL = System.getProperty("line.separator", "\n");
42     
43     private String JavaDoc command;
44     private StringBuffer JavaDoc usage = new StringBuffer JavaDoc();
45     private String JavaDoc switchName;
46
47     public CommandLineUsage(String JavaDoc command) {
48         this.command = command;
49     }
50
51     public void visitCommandLine(CommandLine commandLine) {
52         usage.append("USAGE: ").append(command).append(EOL);
53
54         Iterator i = commandLine.getKnownSwitches().iterator();
55         while (i.hasNext()) {
56             switchName = (String JavaDoc) i.next();
57
58             commandLine.getSwitch(switchName).accept(this);
59         }
60
61         commandLine.getParameterStrategy().accept(this);
62     }
63
64     public void visitToggleSwitch(ToggleSwitch cls) {
65         if (cls.isMandatory()) {
66             usage.append(" -").append(switchName).append(" (defaults to ").append(cls.getDefaultValue()).append(")").append(EOL);
67         } else {
68             usage.append(" [-").append(switchName).append("] (defaults to ").append(cls.getDefaultValue()).append(")").append(EOL);
69         }
70     }
71
72     public void visitSingleValueSwitch(SingleValueSwitch cls) {
73         if (cls.isMandatory()) {
74             usage.append(" -").append(switchName).append(" value (defaults to ").append(cls.getDefaultValue()).append(")").append(EOL);
75         } else {
76             usage.append(" [-").append(switchName).append(" value] (defaults to ").append(cls.getDefaultValue()).append(")").append(EOL);
77         }
78     }
79
80     public void visitOptionalValueSwitch(OptionalValueSwitch cls) {
81         if (cls.isMandatory()) {
82             usage.append(" -").append(switchName).append(" [value] (defaults to ").append(cls.getDefaultValue()).append(")").append(EOL);
83         } else {
84             usage.append(" [-").append(switchName).append(" [value]] (defaults to ").append(cls.getDefaultValue()).append(")").append(EOL);
85         }
86     }
87
88     public void visitMultipleValuesSwitch(MultipleValuesSwitch cls) {
89         if (cls.isMandatory()) {
90             usage.append(" (-").append(switchName).append(" value)+ (defaults to ").append(cls.getDefaultValue()).append(")").append(EOL);
91         } else {
92             usage.append(" (-").append(switchName).append(" value)* (defaults to ").append(cls.getDefaultValue()).append(")").append(EOL);
93         }
94     }
95
96     public void visitNullParameterStrategy(NullParameterStrategy strategy) {
97     }
98
99     public void visitAnyParameterStrategy(AnyParameterStrategy strategy) {
100         usage.append(" [param ...]").append(EOL);
101     }
102
103     public void visitAtLeastParameterStrategy(AtLeastParameterStrategy strategy) {
104         for (int i=1; i<=strategy.getNbParameters(); i++) {
105             usage.append(" ").append("param").append(i).append(EOL);
106         }
107
108         usage.append(" ...").append(EOL);
109     }
110
111     public void visitExactlyParameterStrategy(ExactlyParameterStrategy strategy) {
112         for (int i=1; i<=strategy.getNbParameters(); i++) {
113             usage.append(" ").append("param").append(i).append(EOL);
114         }
115     }
116
117     public void visitAtMostParameterStrategy(AtMostParameterStrategy strategy) {
118         usage.append(" ");
119
120         for (int i=1; i<=strategy.getNbParameters(); i++) {
121             usage.append("[param").append(i);
122             if (i < strategy.getNbParameters()) {
123                 usage.append(" ");
124             }
125         }
126
127         for (int i=1; i<=strategy.getNbParameters(); i++) {
128             usage.append("]");
129         }
130
131         usage.append(EOL);
132     }
133
134     public String JavaDoc toString() {
135         return usage.toString();
136     }
137 }
138
Popular Tags