//If some is recognized as valid option. First extra arg is InvalidArgument
if (!extraArgsValid || optMap.containsKey(option)) {
msg = getErrorMessage("InvalidArgument", extraArgs.get(0).get(0));
int exitCode = getCustomExitCode("InvalidArgument", extraArgs.get(0).get(0));
extraArgs = null;
throw new JGDIException(msg, exitCode);
}
}
//We now store the extraArgs to this command.
this.extraArgs = extraArgs;
return null; //null says now you have the extra args so save them
}
//We take out first arg list
tempArg = args.remove(0);
//And remove the recognized option
tempArg.remove(0);
//And we put it back if not empty
if (tempArg.size()>0) {
args.add(0, tempArg);
}
OptionDescriptor od = optMap.get(option);
List<List<String>> argList = new ArrayList<List<String>>();
List<String> tempList = new ArrayList<String>();
String arg;
if (!od.isWithoutArgs()) {
int i = 0;
//Try to get all mandatory args
while (i < od.getMandatoryArgCount() && args.size() > 0) {
tempArg = args.remove(0);
tempList.clear();
while (tempArg.size() > 0 && i < od.getMandatoryArgCount()) {
arg = tempArg.remove(0);
tempList.add(arg);
i++;
}
if (tempList.size() > 0) {
argList.add(new ArrayList<String>(tempList));
}
}
//We check we completed the subList (comma separated list) otherwise we mark it for continue
boolean appendToLastList = false;
if (tempArg.size() > 0) {
appendToLastList = true;
args.add(0, tempArg);
}
//Check we have all mandatory args
if (i != od.getMandatoryArgCount()) {
final String msgType = (i == 0) ? "NoArgument" : "LessArguments";
msg = getErrorMessage(msgType, option, argList);
int exitCode = getCustomExitCode(msgType, option);
throw new JGDIException(msg, exitCode);
}
//Try to get as many optional args as possible
String argVal;
boolean exit = false;
i = 0;
while (!exit && (i < od.getOptionalArgCount() && args.size() > 0)) {
tempArg = args.remove(0);
tempList.clear();
if (appendToLastList) {
appendToLastList = false;
tempList = argList.remove(argList.size() - 1);
}
for (int j = 0; j<tempArg.size(); j++) {
//We have comma separated list with more elems than required - Error
if (i >= od.getOptionalArgCount()) {
final String msgType = "MoreArguments";
msg = getErrorMessage(msgType, option, argList);
int exitCode = getCustomExitCode(msgType, option);
throw new JGDIException(msg, exitCode);
}
argVal = tempArg.get(j);
//Not enough args?
if (optMap.containsKey(argVal)) {
if (j == 0) {
//Next recognized option must be the first in the sub list
exit = true;
args.add(0, tempArg);
break;
} else {
//This is an error since option is found in the list
//qconf -sq a,b c,d,-sh <= -sh is known option so whole command is wrong
//qconf -sq a,g -sh would be accepted
msg = getErrorMessage("InvalidArgument", option, argVal);
int exitCode = getCustomExitCode("InvalidArgument", option);
throw new JGDIException(msg, exitCode);
}
}
tempList.add(argVal);
i++;
}