[jacli commit] r21 - in trunk: samples/src/main/java/com/google/jacli/samples/opt src/main/java/com/google/jacli...

2 views
Skip to first unread message

codesite...@google.com

unread,
Jul 2, 2008, 3:12:55 AM7/2/08
to fant...@googlegroups.com
Author: maomaode
Date: Wed Jul 2 00:11:45 2008
New Revision: 21

Modified:
trunk/samples/src/main/java/com/google/jacli/samples/opt/Sample.java
trunk/src/main/java/com/google/jacli/CommandLine.java
trunk/src/main/java/com/google/jacli/CommandLineParser.java
trunk/src/main/java/com/google/jacli/DefaultUsage.java
trunk/src/main/java/com/google/jacli/Pattern.java
trunk/src/main/java/com/google/jacli/compilers/AbstractCompiler.java

Log:
* Use the prefix/shortPrefix to replace the hard coded "-"
* Update the sample, and we prefer the unix style, i.e "-" for the
shortName, and "--" for the fullName

Modified: trunk/samples/src/main/java/com/google/jacli/samples/opt/Sample.java
==============================================================================
---
trunk/samples/src/main/java/com/google/jacli/samples/opt/Sample.java (original)
+++
trunk/samples/src/main/java/com/google/jacli/samples/opt/Sample.java
Wed Jul 2 00:11:45 2008
@@ -12,7 +12,7 @@
public static void main(String[] args) throws Exception {
SampleBean bean = new SampleBean();

- CommandLine commandLine = CommandLineParser.parse(bean, args);
+ CommandLine commandLine = CommandLineParser.parse(bean, args, "--", "-");
commandLine.printUsage();

Method[] fields = SampleBean.class.getDeclaredMethods();

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 Jul 2
00:11:45 2008
@@ -9,6 +9,8 @@
private List<Field> fields = new ArrayList<Field>();
private Object bean;
private Pattern pattern = Pattern.SEQ;
+ private String prefix = CommandLineParser.DEFAULT_PREFIX;
+ private String shortPrefix = CommandLineParser.DEFAULT_SHORT_PREFIX;

public final Object getBean() {
return bean;
@@ -36,9 +38,26 @@

public void setPattern(Pattern s) {
this.pattern = s;
+ Pattern.command = this;
}

public Pattern getPattern() {
return pattern;
+ }
+
+ public String getPrefix() {
+ return this.prefix;
+ }
+
+ public void setPrefix(String p) {
+ this.prefix = p;
+ }
+
+ public String getShortPrefix() {
+ return this.shortPrefix;
+ }
+
+ public void setShortPrefix(String p) {
+ this.shortPrefix = p;
}
}

Modified: trunk/src/main/java/com/google/jacli/CommandLineParser.java
==============================================================================
--- trunk/src/main/java/com/google/jacli/CommandLineParser.java (original)
+++ trunk/src/main/java/com/google/jacli/CommandLineParser.java Wed Jul
2 00:11:45 2008
@@ -6,6 +6,9 @@
import java.util.List;

public final class CommandLineParser {
+ public static final String DEFAULT_PREFIX = "--";
+ public static final String DEFAULT_SHORT_PREFIX = "-";
+
private CommandLineParser() {
// empty
}
@@ -44,7 +47,18 @@
}

public static CommandLine parse(Object obj, String[] args) {
+ return parse(obj, args, DEFAULT_SHORT_PREFIX, DEFAULT_SHORT_PREFIX);
+ }
+
+ public static CommandLine parse(Object obj, String[] args, String
prefix) {
+ return parse(obj, args, prefix, prefix);
+ }
+
+ public static CommandLine parse(Object obj, String[] args, String
prefix, String shortPrefix) {
CommandLine command = getCommandLine(obj);
+ command.setPrefix(prefix);
+ command.setShortPrefix(shortPrefix);
+
if (args == null || args.length == 0) {
return command;
}
@@ -54,7 +68,7 @@
if (fieldList == null) {
System.err.println("No arguments found");
}
-
+
command.getPattern().compile(fieldList, obj, args);

return command;

Modified: trunk/src/main/java/com/google/jacli/DefaultUsage.java
==============================================================================
--- trunk/src/main/java/com/google/jacli/DefaultUsage.java (original)
+++ trunk/src/main/java/com/google/jacli/DefaultUsage.java Wed Jul 2
00:11:45 2008
@@ -40,11 +40,11 @@
}

if (argument.shortName().length() > 0) {
- buffer.append("-");
+ buffer.append(commandLine.getShortPrefix());
buffer.append(argument.shortName());
buffer.append(", ");
}
- buffer.append("-" + argument.option());
+ buffer.append(commandLine.getPrefix() + argument.option());
buffer.append(getDots(argument.option().length()));

if (argument.documentation().length() > 0) {

Modified: trunk/src/main/java/com/google/jacli/Pattern.java
==============================================================================
--- trunk/src/main/java/com/google/jacli/Pattern.java (original)
+++ trunk/src/main/java/com/google/jacli/Pattern.java Wed Jul 2
00:11:45 2008
@@ -3,6 +3,7 @@
import java.lang.reflect.Field;
import java.util.List;

+import com.google.jacli.compiler.AbstractCompiler;
import com.google.jacli.compiler.OptionCompiler;
import com.google.jacli.compiler.SequenceCompiler;

@@ -10,14 +11,38 @@

SEQ {
void compile(final List<Field> fieldList, final Object target,
final String[] args) {
- new SequenceCompiler().compile(fieldList, target, args);
+ AbstractCompiler compiler = new SequenceCompiler();
+ compiler.setPrefix(getPrefix());
+ compiler.setShortPrefix(getShortPrefix());
+ compiler.compile(fieldList, target, args);
}
},
OPT {
void compile(final List<Field> fieldList, final Object target,
final String[] args) {
- new OptionCompiler().compile(fieldList, target, args);
+ AbstractCompiler compiler = new OptionCompiler();
+ compiler.setPrefix(getPrefix());
+ compiler.setShortPrefix(getShortPrefix());
+ compiler.compile(fieldList, target, args);
}
};

abstract void compile(final List<Field> fieldList, final Object
target, final String[] args);
+
+ public static CommandLine command;
+
+ public String getPrefix() {
+ if (command != null) {
+ return command.getPrefix();
+ } else {
+ return CommandLineParser.DEFAULT_PREFIX;
+ }
+ }
+
+ public String getShortPrefix() {
+ if (command != null) {
+ return command.getShortPrefix();
+ } else {
+ return CommandLineParser.DEFAULT_SHORT_PREFIX;
+ }
+ }
}

Modified: trunk/src/main/java/com/google/jacli/compilers/AbstractCompiler.java
==============================================================================
---
trunk/src/main/java/com/google/jacli/compilers/AbstractCompiler.java (original)
+++
trunk/src/main/java/com/google/jacli/compilers/AbstractCompiler.java
Wed Jul 2 00:11:45 2008
@@ -17,6 +17,9 @@
import com.google.jacli.Argument;

public abstract class AbstractCompiler {
+ private String prefix;
+ private String shortPrefix;
+
public abstract void compile(final List<Field> fieldList, final
Object target, final String[] args);

protected Argument getArgument(final Field field) {
@@ -132,7 +135,7 @@
}

protected boolean hasOption(final Argument argument, final String
arg) {
- return ("-" + argument.option()).equals(arg) || ("-" + argument.shortName()).equals(arg);
+ return (getPrefix() + argument.option()).equals(arg) ||
(getShortPrefix() + argument.shortName()).equals(arg);
}

protected boolean hasOption(final Argument argument, final
String[] args) {
@@ -160,5 +163,21 @@
}
}
return values;
+ }
+
+ public void setPrefix(String p) {
+ this.prefix = p;
+ }
+
+ public String getPrefix() {
+ return this.prefix;
+ }
+
+ public String getShortPrefix() {
+ return this.shortPrefix;
+ }
+
+ public void setShortPrefix(String p) {
+ this.shortPrefix = p;
}
}

Reply all
Reply to author
Forward
0 new messages