in gshell-support/gshell-clp/src/main/java/org/apache/geronimo/gshell/clp/CommandLineProcessor.java [218:318]
public void process(final String... args) throws ProcessingException {
ParametersImpl params = new ParametersImpl(args);
Set<Handler> present = new HashSet<Handler>();
int argIndex = 0;
boolean processOptions = true;
boolean requireOverride = false;
//
// TODO: Need to rewrite some of this to allow more posix-style argument processing, like --foo=bar and --foo bar, and -vvvv
//
while (params.hasMore()) {
String arg = params.current();
Handler handler;
if (processOptions && arg.startsWith("-")) {
boolean isKeyValuePair = arg.indexOf('=') != -1;
// parse this as an option.
handler = isKeyValuePair ? findOptionHandler(arg) : findOptionByName(arg);
if (handler == null) {
if (stopAtNonOption) {
// Slurp up the remaining bits as arguments (including the option we just looked at)
processOptions = false;
continue;
}
else {
// Unknown option, complain
throw new ProcessingException(Messages.UNDEFINED_OPTION.format(arg));
}
}
else if (isKeyValuePair){
// known option, but further processing is required in the handler.
handler.isKeyValuePair = isKeyValuePair;
}
else {
// known option; skip its name
params.skip(1);
}
}
else {
// Complain if we have more arguments than we have handlers configured
if (argIndex >= argumentHandlers.size()) {
Messages msg = argumentHandlers.size() == 0 ? Messages.NO_ARGUMENT_ALLOWED : Messages.TOO_MANY_ARGUMENTS;
throw new ProcessingException(msg.format(arg));
}
// known argument
handler = argumentHandlers.get(argIndex);
if (!handler.descriptor.isMultiValued()) {
argIndex++;
}
}
try {
//Hook up the current handler to the params for error message rendering
params.handler = handler;
// If this is an option which overrides requirements track it
if (!requireOverride && handler.descriptor instanceof OptionDescriptor) {
OptionDescriptor d = (OptionDescriptor) handler.descriptor;
requireOverride = d.isRequireOverride();
}
// Invoker the handler and then skip arguments which it has eatten up
int consumed = handler.handle(params);
params.skip(consumed);
}
catch (StopProcessingOptionsNotification n) {
processOptions = false;
}
// Keep a list of the handlers which have been processed (for required validation below)
present.add(handler);
}
// Ensure that all required option handlers are present, unless a processed option has overridden requirments
if (!requireOverride) {
for (Handler handler : optionHandlers) {
if (handler.descriptor.isRequired() && !present.contains(handler)) {
//
// FIXME: This needs to be i18n aware
//
throw new ProcessingException(Messages.REQUIRED_OPTION_MISSING.format(handler.descriptor.toString()));
}
}
// Ensure that all required argument handlers are present
for (Handler handler : argumentHandlers) {
if (handler.descriptor.isRequired() && !present.contains(handler)) {
//
// FIXME: This needs to be i18n aware
//
throw new ProcessingException(Messages.REQUIRED_ARGUMENT_MISSING.format(handler.descriptor.toString()));
}
}
}
}