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

Binary file not shown.

Binary file not shown.

View file

@ -10,9 +10,7 @@ import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;
import org.manzerbredes.open_klm.args.ArgsTypeA;
import org.manzerbredes.open_klm.client.MainWindow;
import org.manzerbredes.open_klm.drivers.*;
import org.manzerbredes.open_klm.drivers.DriverTypeA.*;
@ -22,7 +20,7 @@ import org.manzerbredes.open_klm.drivers.DriverTypeA.*;
*
*/
public class App
{
{/*
@Option(name="-leftColor",usage="Set the left color")
public String leftColor=null;
@Option(name="-middleColor",usage="Set the middle color")
@ -52,22 +50,26 @@ public class App
System.exit(0);
}
}
}*/
public static void main( String[] args ) throws InstantiationException, CmdLineException, LineUnavailableException, IOException
public static void main( String[] args )
{
App app =new App();
CmdLineParser parser = new CmdLineParser(app);
parser.parseArgument(args);
//app.parseArguments();
// new MainWindow();
DriverTypeA device=new Driver_1770_ff00();
/*app.parseArguments();
new MainWindow();*/
/*DriverTypeA device=new Driver_1770_ff00();
Driver a=(Driver) device;
a.initDriver();
device.setRegionColor(Region.LEFT, Color.RED, Intensity.HIGH);
device.setRegionColor(Region.MIDDLE, Color.ORANGE, Intensity.HIGH);
device.setRegionColor(Region.RIGHT, Color.PURPLE, Intensity.HIGH);
device.setMode(Mode.NORMAL);
device.setMode(Mode.WAVE);*/
System.out.println("Parsing... "+ args.length);
ArgsTypeA a=new ArgsTypeA();
Driver aa=new Driver_1770_ff00();
aa.initDriver();
a.applyAndExit(aa, args);
}
}

View file

@ -0,0 +1,5 @@
package org.manzerbredes.open_klm.args;
public interface ArgsManager{
}

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);
}
}