private CommandLine doParse(ParameterInt param, List<String> lines,
boolean ignoreMissing) throws CommandLineParserException {
param.clean();
CommandLine commandLine = new CommandLine();
OptionInt active = null;
boolean addedArgument = false;
//skip first entry since that's the name of the command
for(int i=1; i < lines.size(); i++) {
String parseLine = lines.get(i);
//longName
if(parseLine.startsWith("--")) {
//make sure that we dont have any "active" options lying around
if(active != null)
throw new OptionParserException("Option: "+active.getDisplayName()+" must be given a value");
active = findLongOption(param, parseLine.substring(2));
if(active != null && active.isProperty()) {
if(parseLine.length() <= (2+active.getLongName().length()) ||
!parseLine.contains(EQUALS))
throw new OptionParserException(
"Option "+active.getDisplayName()+", must be part of a property");
String name =
parseLine.substring(2+active.getLongName().length(),
parseLine.indexOf(EQUALS));
String value = parseLine.substring( parseLine.indexOf(EQUALS)+1);
commandLine.addOption(new
ParsedOption(active.getName(), active.getLongName(),
new OptionProperty(name, value), active.getType()));
active = null;
if(addedArgument)
throw new ArgumentParserException("An argument was given to an option that do not support it.");
}
else if(active != null && (!active.hasValue() || active.getValue() != null)) {
commandLine.addOption(new ParsedOption(active.getName(), active.getLongName(),
active.getValue(), active.getType()));
active = null;
if(addedArgument)
throw new ArgumentParserException("An argument was given to an option that do not support it.");
}
else if(active == null)
throw new OptionParserException("Option: "+parseLine+" is not a valid option for this command");
}
//name
else if(parseLine.startsWith("-")) {
//make sure that we dont have any "active" options lying around
if(active != null)
throw new OptionParserException("Option: "+active.getDisplayName()+" must be given a value");
if(parseLine.length() != 2 && !parseLine.contains("="))
throw new OptionParserException("Option: - must be followed by a valid operator");
active = findOption(param, parseLine.substring(1));
if(active != null && active.isProperty()) {
if(parseLine.length() <= 2 ||
!parseLine.contains(EQUALS))
throw new OptionParserException(
"Option "+active.getDisplayName()+", must be part of a property");
String name =
parseLine.substring(2, // 2+char.length
parseLine.indexOf(EQUALS));
String value = parseLine.substring( parseLine.indexOf(EQUALS)+1);
commandLine.addOption(new
ParsedOption(active.getName(), active.getLongName(),
new OptionProperty(name, value), active.getType()));
active = null;
if(addedArgument)
throw new OptionParserException("An argument was given to an option that do not support it.");
}
else if(active != null && (!active.hasValue() || active.getValue() != null)) {
commandLine.addOption(new ParsedOption(String.valueOf(active.getName()),
active.getLongName(), active.getValue(), active.getType()));
active = null;
if(addedArgument)
throw new OptionParserException("An argument was given to an option that do not support it.");
}
else if(active == null)
throw new OptionParserException("Option: "+parseLine+" is not a valid option for this command");
}
else if(active != null) {
if(active.hasMultipleValues()) {
if(parseLine.contains(String.valueOf(active.getValueSeparator()))) {
for(String value : parseLine.split(String.valueOf(active.getValueSeparator()))) {
active.addValue(value.trim());
}
}
}
else
active.addValue(parseLine);
commandLine.addOption(new ParsedOption(active.getName(),
active.getLongName(), active.getValues(), active.getType()));
active = null;
if(addedArgument)
throw new OptionParserException("An argument was given to an option that do not support it.");
}
//if no param is "active", we add it as an argument
else {
commandLine.addArgument(parseLine);
addedArgument = true;
}
}
if(active != null && ignoreMissing) {
commandLine.addOption(new ParsedOption(active.getName(),
active.getLongName(), active.getValues(), active.getType()));
}
//this will throw and CommandLineParserException if needed
if(!ignoreMissing)
checkForMissingRequiredOptions(param, commandLine);