Add argument parsing

This commit is contained in:
manzerbredes 2016-03-31 10:47:37 +02:00
parent f9764bba46
commit 58a70ad52d
5 changed files with 90 additions and 14 deletions

View file

@ -7,6 +7,9 @@ import org.manzerbredes.open_klm.drivers.Driver;
import org.manzerbredes.open_klm.drivers.DriverTypeA;
import org.manzerbredes.open_klm.drivers.DriverTypeA.*;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
/**
* Parse args for DriverTypeA
*
@ -48,12 +51,63 @@ public class ArgsTypeA implements Args{
this.mode=Mode.NORMAL;
}
@Override
public void applyAndExit(Driver aDriver, String[] args){
if(aDriver.getType().equals(DriverTypeA.class)){
// Build arguments parser
OptionParser argsParser = new OptionParser();
//TODO Parse and apply args with args4j
// Define left color, middle color, right color
argsParser.accepts("lc").withRequiredArg();
argsParser.accepts("mc").withRequiredArg();
argsParser.accepts("rc").withRequiredArg();
// Define left secondary color, middle secondary color, right secondary color
argsParser.accepts("lsc").withRequiredArg();
argsParser.accepts("msc").withRequiredArg();
argsParser.accepts("rsc").withRequiredArg();
// Define mode
argsParser.accepts("mode").withRequiredArg();
// Parse the options
OptionSet parsedOptions = argsParser.parse(args);
// Try to apply options
try {
if(parsedOptions.hasArgument("lc")){
this.primaryColorsState.put(Region.LEFT, new Pair<DriverTypeA.Color, DriverTypeA.Intensity>(Color.valueOf((String) parsedOptions.valueOf("lc")),Intensity.HIGH));
}
if(parsedOptions.hasArgument("mc")){
this.primaryColorsState.put(Region.MIDDLE, new Pair<DriverTypeA.Color, DriverTypeA.Intensity>(Color.valueOf((String) parsedOptions.valueOf("mc")),Intensity.HIGH));
}
if(parsedOptions.hasArgument("rc")){
this.primaryColorsState.put(Region.RIGHT, new Pair<DriverTypeA.Color, DriverTypeA.Intensity>(Color.valueOf((String) parsedOptions.valueOf("rc")),Intensity.HIGH));
}
if(parsedOptions.hasArgument("lsc")){
this.secondaryColorsState.put(Region.LEFT, new Pair<DriverTypeA.Color, DriverTypeA.Intensity>(Color.valueOf((String) parsedOptions.valueOf("lsc")),Intensity.HIGH));
}
if(parsedOptions.hasArgument("msc")){
this.secondaryColorsState.put(Region.MIDDLE, new Pair<DriverTypeA.Color, DriverTypeA.Intensity>(Color.valueOf((String) parsedOptions.valueOf("msc")),Intensity.HIGH));
}
if(parsedOptions.hasArgument("rsc")){
this.secondaryColorsState.put(Region.RIGHT, new Pair<DriverTypeA.Color, DriverTypeA.Intensity>(Color.valueOf((String) parsedOptions.valueOf("rsc")),Intensity.HIGH));
}
if(parsedOptions.hasArgument("mode")){
this.mode=Mode.valueOf(parsedOptions.argumentOf("mode"));
}
}
catch(Exception e){
System.err.println(e.getMessage());
System.exit(1);
}
// Apply argument
this.applyArguments((DriverTypeA) aDriver);
// Exit after apply
System.exit(0);
}
@ -61,4 +115,19 @@ public class ArgsTypeA implements Args{
System.exit(1);
}
/**
* Apply to device
*/
private void applyArguments(DriverTypeA driver){
for(Region region:Region.values()){
driver.setRegionColor(region, this.primaryColorsState.get(region).getValue0(), this.primaryColorsState.get(region).getValue1());
}
for(Region region:Region.values()){
driver.setSecondaryRegionColor(region, this.secondaryColorsState.get(region).getValue0(), this.primaryColorsState.get(region).getValue1());
}
driver.setMode(this.mode);
}
}