1 32 33 package com.jeantessier.commandline; 34 35 import java.util.*; 36 37 40 public class CommandLineUsage implements Visitor { 41 private final static String EOL = System.getProperty("line.separator", "\n"); 42 43 private String command; 44 private StringBuffer usage = new StringBuffer (); 45 private String switchName; 46 47 public CommandLineUsage(String 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 ) 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 toString() { 135 return usage.toString(); 136 } 137 } 138 | Popular Tags |