Latest bnd code
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1350613 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/bundleplugin/src/main/java/aQute/lib/getopt/CommandLine.java b/bundleplugin/src/main/java/aQute/lib/getopt/CommandLine.java
index f95b6c4..a5185e4 100644
--- a/bundleplugin/src/main/java/aQute/lib/getopt/CommandLine.java
+++ b/bundleplugin/src/main/java/aQute/lib/getopt/CommandLine.java
@@ -17,16 +17,18 @@
* parameter of Options type. Usually this is an interface that extends Options.
* The methods on this interface are options or flags (when they return
* boolean).
- *
*/
-@SuppressWarnings("unchecked") public class CommandLine {
- static int LINELENGTH = 60;
- static Pattern ASSIGNMENT = Pattern.compile("(\\w[\\w\\d]*+)\\s*=\\s*([^\\s]+)\\s*");
- Reporter reporter;
- Justif justif = new Justif(60);
-
+@SuppressWarnings("unchecked")
+public class CommandLine {
+ static int LINELENGTH = 60;
+ static Pattern ASSIGNMENT = Pattern.compile("(\\w[\\w\\d]*+)\\s*=\\s*([^\\s]+)\\s*");
+ Reporter reporter;
+ Justif justif = new Justif(60);
+ CommandLineMessages msg;
+
public CommandLine(Reporter reporter) {
this.reporter = reporter;
+ msg = ReporterMessages.base(reporter, CommandLineMessages.class);
}
/**
@@ -56,11 +58,11 @@
//
List<String> arguments = new ArrayList<String>(input);
- Map<String, Method> commands = getCommands(target);
+ Map<String,Method> commands = getCommands(target);
Method m = commands.get(cmd);
if (m == null) {
- reporter.error("No such command %s\n", cmd);
+ msg.NoSuchCommand_(cmd);
return help(target, null, null);
}
@@ -68,7 +70,7 @@
// Parse the options
//
- Class<? extends Options> optionClass = (Class<? extends Options>) m.getParameterTypes()[0];
+ Class< ? extends Options> optionClass = (Class< ? extends Options>) m.getParameterTypes()[0];
Options options = getOptions(optionClass, arguments);
if (options == null) {
// had some error, already reported
@@ -85,7 +87,7 @@
// Check for commands without any arguments
if (patterns.length == 0 && arguments.size() > 0) {
- reporter.error("This command takes no arguments but found %s\n", arguments);
+ msg.TooManyArguments_(arguments);
return help(target, cmd, null);
}
@@ -108,7 +110,7 @@
if (i > arguments.size()) {
if (!optional)
- reporter.error("Missing argument %s\n", patterns[i]);
+ msg.MissingArgument_(patterns[i]);
return help(target, cmd, optionClass);
}
}
@@ -116,8 +118,7 @@
// Check if we have unconsumed arguments left
if (i < arguments.size()) {
- reporter.error("Too many arguments specified %s, expecting %s\n", arguments,
- Arrays.asList(patterns));
+ msg.TooManyArguments_(arguments);
return help(target, cmd, optionClass);
}
}
@@ -129,7 +130,7 @@
return help(target, cmd, optionClass);
}
- private String help(Object target, String cmd, Class<? extends Options> type) throws Exception {
+ private String help(Object target, String cmd, Class< ? extends Options> type) throws Exception {
StringBuilder sb = new StringBuilder();
Formatter f = new Formatter(sb);
if (cmd == null)
@@ -148,13 +149,11 @@
* Parse the options in a command line and return an interface that provides
* the options from this command line. This will parse up to (and including)
* -- or an argument that does not start with -
- *
*/
- public <T extends Options> T getOptions(Class<T> specification, List<String> arguments)
- throws Exception {
- Map<String, String> properties = Create.map();
- Map<String, Object> values = new HashMap<String, Object>();
- Map<String, Method> options = getOptions(specification);
+ public <T extends Options> T getOptions(Class<T> specification, List<String> arguments) throws Exception {
+ Map<String,String> properties = Create.map();
+ Map<String,Object> values = new HashMap<String,Object>();
+ Map<String,Method> options = getOptions(specification);
argloop: while (arguments.size() > 0) {
@@ -173,7 +172,7 @@
String name = option.substring(2);
Method m = options.get(name);
if (m == null)
- reporter.error("Unrecognized option %s\n", name);
+ msg.UnrecognizedOption_(name);
else
assignOptionValue(values, m, arguments, true);
@@ -185,15 +184,14 @@
char optionChar = option.charAt(j);
- for (Entry<String, Method> entry : options.entrySet()) {
+ for (Entry<String,Method> entry : options.entrySet()) {
if (entry.getKey().charAt(0) == optionChar) {
boolean last = (j + 1) >= option.length();
- assignOptionValue(values, entry.getValue(),
- arguments, last);
+ assignOptionValue(values, entry.getValue(), arguments, last);
continue charloop;
}
}
- reporter.error("No such option -%s\n", optionChar);
+ msg.UnrecognizedOption_(optionChar + "");
}
}
} else {
@@ -207,11 +205,11 @@
// check if all required elements are set
- for (Entry<String, Method> entry : options.entrySet()) {
+ for (Entry<String,Method> entry : options.entrySet()) {
Method m = entry.getValue();
String name = entry.getKey();
if (!values.containsKey(name) && isMandatory(m))
- reporter.error("Required option --%s not set", name);
+ msg.OptionNotSet_(name);
}
values.put(".", arguments);
@@ -223,8 +221,8 @@
/**
* Answer a list of the options specified in an options interface
*/
- private Map<String, Method> getOptions(Class<? extends Options> interf) {
- Map<String, Method> map = new TreeMap<String, Method>();
+ private Map<String,Method> getOptions(Class< ? extends Options> interf) {
+ Map<String,Method> map = new TreeMap<String,Method>();
for (Method m : interf.getMethods()) {
if (m.getName().startsWith("_"))
@@ -259,8 +257,7 @@
* if this is the last in a multi single character option
* @return
*/
- public void assignOptionValue(Map<String, Object> options, Method m, List<String> args,
- boolean last) {
+ public void assignOptionValue(Map<String,Object> options, Method m, List<String> args, boolean last) {
String name = m.getName();
Type type = m.getGenericReturnType();
@@ -274,15 +271,12 @@
// The option is followed by an argument
if (!last) {
- reporter.error(
- "Option --%s not last in a set of 1-letter options (%s) but it requires an argument of type ",
- name, name.charAt(0), getTypeDescriptor(type));
+ msg.Option__WithArgumentNotLastInAvvreviation_(name, name.charAt(0), getTypeDescriptor(type));
return;
}
if (args.isEmpty()) {
- reporter.error("Missing argument %s for option --%s, -%s ",
- getTypeDescriptor(type), name, name.charAt(0));
+ msg.MissingArgument__(name, name.charAt(0));
return;
}
@@ -301,7 +295,7 @@
} else {
if (options.containsKey(name)) {
- reporter.error("The option %s can only occur once", name);
+ msg.OptionCanOnlyOccurOnce_(name);
return;
}
@@ -314,10 +308,10 @@
* Provide a help text.
*/
- public void help(Formatter f, Object target, String cmd, Class<? extends Options> specification) {
+ public void help(Formatter f, Object target, String cmd, Class< ? extends Options> specification) {
Description descr = specification.getAnnotation(Description.class);
Arguments patterns = specification.getAnnotation(Arguments.class);
- Map<String, Method> options = getOptions(specification);
+ Map<String,Method> options = getOptions(specification);
String description = descr == null ? "" : descr.value();
@@ -339,7 +333,7 @@
}
f.format("OPTIONS\n");
- for (Entry<String, Method> entry : options.entrySet()) {
+ for (Entry<String,Method> entry : options.entrySet()) {
String optionName = entry.getKey();
Method m = entry.getValue();
@@ -347,8 +341,7 @@
Description d = m.getAnnotation(Description.class);
boolean required = isMandatory(m);
- String methodDescription = cfg != null ? cfg.description() : (d == null ? "" : d
- .value());
+ String methodDescription = cfg != null ? cfg.description() : (d == null ? "" : d.value());
f.format(" %s -%s, --%s %s%s - %s\n", required ? " " : "[", //
optionName.charAt(0), //
@@ -398,7 +391,7 @@
if (m == null)
f.format("No such command: %s\n", cmd);
else {
- Class<? extends Options> options = (Class<? extends Options>) m.getParameterTypes()[0];
+ Class< ? extends Options> options = (Class< ? extends Options>) m.getParameterTypes()[0];
help(f, target, cmd, options);
}
}
@@ -409,13 +402,13 @@
* @param target
* @return
*/
- public Map<String, Method> getCommands(Object target) {
- Map<String, Method> map = new TreeMap<String, Method>();
+ public Map<String,Method> getCommands(Object target) {
+ Map<String,Method> map = new TreeMap<String,Method>();
for (Method m : target.getClass().getMethods()) {
if (m.getParameterTypes().length == 1 && m.getName().startsWith("_")) {
- Class<?> clazz = m.getParameterTypes()[0];
+ Class< ? > clazz = m.getParameterTypes()[0];
if (Options.class.isAssignableFrom(clazz)) {
String name = m.getName().substring(1);
map.put(name, m);
@@ -453,7 +446,7 @@
ParameterizedType pt = (ParameterizedType) type;
Type c = pt.getRawType();
if (c instanceof Class) {
- if (Collection.class.isAssignableFrom((Class<?>) c)) {
+ if (Collection.class.isAssignableFrom((Class< ? >) c)) {
return getTypeDescriptor(pt.getActualTypeArguments()[0]) + "*";
}
}
@@ -461,7 +454,7 @@
if (!(type instanceof Class))
return "<>";
- Class<?> clazz = (Class<?>) type;
+ Class< ? > clazz = (Class< ? >) type;
if (clazz == Boolean.class || clazz == boolean.class)
return ""; // Is a flag
diff --git a/bundleplugin/src/main/java/aQute/lib/getopt/CommandLineMessages.java b/bundleplugin/src/main/java/aQute/lib/getopt/CommandLineMessages.java
new file mode 100644
index 0000000..afeb0b9
--- /dev/null
+++ b/bundleplugin/src/main/java/aQute/lib/getopt/CommandLineMessages.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) OSGi Alliance (2012). All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package aQute.lib.getopt;
+
+import java.util.*;
+
+import aQute.libg.reporter.*;
+
+public interface CommandLineMessages extends Messages {
+
+ ERROR Option__WithArgumentNotLastInAvvreviation_(String name, char charAt, String typeDescriptor);
+
+ ERROR MissingArgument__(String name, char charAt);
+
+ ERROR OptionCanOnlyOccurOnce_(String name);
+
+ ERROR NoSuchCommand_(String cmd);
+
+ ERROR TooManyArguments_(List<String> arguments);
+
+ ERROR MissingArgument_(String string);
+
+ ERROR UnrecognizedOption_(String name);
+
+ ERROR OptionNotSet_(String name);
+
+}
diff --git a/bundleplugin/src/main/java/aQute/lib/getopt/Options.java b/bundleplugin/src/main/java/aQute/lib/getopt/Options.java
index aab2b7f..ff2b168 100644
--- a/bundleplugin/src/main/java/aQute/lib/getopt/Options.java
+++ b/bundleplugin/src/main/java/aQute/lib/getopt/Options.java
@@ -4,8 +4,12 @@
public interface Options {
List<String> _();
+
CommandLine _command();
+
Map<String,String> _properties();
+
boolean _ok();
+
boolean _help();
}