[jacli commit] r11 - trunk/src/main/java/com/google/jacli

1 view
Skip to first unread message

codesite...@google.com

unread,
May 28, 2008, 11:13:11 PM5/28/08
to fant...@googlegroups.com
Author: maomaode
Date: Wed May 28 20:13:02 2008
New Revision: 11

Added:
trunk/src/main/java/com/google/jacli/DefaultUsage.java
trunk/src/main/java/com/google/jacli/Usage.java
Modified:
trunk/src/main/java/com/google/jacli/CommandLine.java

Log:
* Abstract the Usage interface, so user can add his own Usage class to
print the usages

Modified: trunk/src/main/java/com/google/jacli/CommandLine.java
==============================================================================
--- trunk/src/main/java/com/google/jacli/CommandLine.java (original)
+++ trunk/src/main/java/com/google/jacli/CommandLine.java Wed May 28
20:13:02 2008
@@ -3,10 +3,6 @@
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
-import java.util.MissingResourceException;
-import java.util.ResourceBundle;
-
-import com.google.jacli.l10n.BundleUtil;

public class CommandLine {

@@ -26,68 +22,12 @@
return this.fields;
}

- private String getDots(int length) {
- StringBuffer sb = new StringBuffer();
- for (int i = 0; i < 20 - length; i++) {
- sb.append(" ");
- }
- return sb.toString();
- }
-
public void printSimpleUsage() {
- StringBuffer buffer = new StringBuffer();
- ResourceBundle bundle = null;
-
- try {
- bundle = BundleUtil.getBundle(bean.getClass());
- } catch (MissingResourceException e) {
- // ignore the exception
- }
-
- if (pattern == Pattern.OPT) {
- for (Field field : getFields()) {
- Argument argument = CommandLineParser.getArgument(field);
-
- if (!argument.required()) {
- buffer.append("[");
- }
-
- if (argument.optionType() == String.class &&
argument.option().length() == 0) {
- // the last argument
- continue;
- }
-
- if (argument.simpleName().length() > 0) {
- buffer.append("-");
- buffer.append(argument.simpleName());
- buffer.append(", ");
- }
- buffer.append("-" + argument.option());
- buffer.append(getDots(argument.option().length()));
-
- if (argument.optionType() == String.class) {
- if (argument.documentation().length() > 0) {
- if (bundle != null) {
- try {
- buffer.append(bundle.getString(argument.documentation()));
- } catch (MissingResourceException e) {
- buffer.append(argument.documentation());
- }
- } else {
- buffer.append(argument.documentation());
- }
+ printUsage(new DefaultUsage());
+ }

- } else {
- buffer.append("N/A");
- }
- }
- if (!argument.required()) {
- buffer.append("]");
- }
- buffer.append("\n");
- }
- }
- System.out.println(buffer);
+ public void printUsage(Usage usage) {
+ usage.print(this);
}

public void setPattern(Pattern s) {

Added: trunk/src/main/java/com/google/jacli/DefaultUsage.java
==============================================================================
--- (empty file)
+++ trunk/src/main/java/com/google/jacli/DefaultUsage.java Wed May 28
20:13:02 2008
@@ -0,0 +1,74 @@
+package com.google.jacli;
+
+import java.lang.reflect.Field;
+import java.util.MissingResourceException;
+import java.util.ResourceBundle;
+
+import com.google.jacli.l10n.BundleUtil;
+
+public class DefaultUsage implements Usage {
+
+ public void print(CommandLine commandLine) {
+ StringBuffer buffer = new StringBuffer();
+ ResourceBundle bundle = null;
+
+ try {
+ bundle = BundleUtil.getBundle(commandLine.getBean().getClass());
+ } catch (MissingResourceException e) {
+ // ignore the exception
+ }
+
+ if (commandLine.getPattern() == Pattern.OPT) {
+ for (Field field : commandLine.getFields()) {
+ Argument argument = CommandLineParser.getArgument(field);
+
+ if (!argument.required()) {
+ buffer.append("[");
+ }
+
+ if (argument.optionType() == String.class &&
argument.option().length() == 0) {
+ // the last argument
+ continue;
+ }
+
+ if (argument.simpleName().length() > 0) {
+ buffer.append("-");
+ buffer.append(argument.simpleName());
+ buffer.append(", ");
+ }
+ buffer.append("-" + argument.option());
+ buffer.append(getDots(argument.option().length()));
+
+ if (argument.optionType() == String.class) {
+ if (argument.documentation().length() > 0) {
+ if (bundle != null) {
+ try {
+ buffer.append(bundle.getString(argument.documentation()));
+ } catch (MissingResourceException e) {
+ buffer.append(argument.documentation());
+ }
+ } else {
+ buffer.append(argument.documentation());
+ }
+
+ } else {
+ buffer.append("N/A");
+ }
+ }
+ if (!argument.required()) {
+ buffer.append("]");
+ }
+ buffer.append("\n");
+ }
+ }
+ System.out.println(buffer);
+ }
+
+ private String getDots(int length) {
+ StringBuffer sb = new StringBuffer();
+ for (int i = 0; i < 20 - length; i++) {
+ sb.append(" ");
+ }
+ return sb.toString();
+ }
+}
\ No newline at end of file

Added: trunk/src/main/java/com/google/jacli/Usage.java
==============================================================================
--- (empty file)
+++ trunk/src/main/java/com/google/jacli/Usage.java Wed May 28 20:13:02 2008
@@ -0,0 +1,5 @@
+package com.google.jacli;
+
+public interface Usage {
+ void print(CommandLine commandline);
+}
\ No newline at end of file

Reply all
Reply to author
Forward
0 new messages