Last active
August 29, 2015 14:00
-
-
Save mnstrspeed/11018514 to your computer and use it in GitHub Desktop.
Java command line options base
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package nl.tomsanders.util; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.RetentionPolicy; | |
import java.lang.reflect.Field; | |
import java.lang.reflect.Method; | |
import java.util.Arrays; | |
import java.util.List; | |
public abstract class Program | |
{ | |
@Retention(RetentionPolicy.RUNTIME) | |
public @interface Argument | |
{ | |
String[] tags(); | |
} | |
public Program withArguments(String[] args) | |
{ | |
List<String> arguments = Arrays.asList(args); | |
try | |
{ | |
for (Field field : this.getClass().getFields()) | |
{ | |
if (field.isAnnotationPresent(Argument.class)) | |
{ | |
Argument argument = field.getAnnotation(Argument.class); | |
boolean isFlag = field.getType().equals(boolean.class); | |
int offset = matchInArguments(argument, arguments, isFlag ? 0 : 1); | |
if (offset >= 0) | |
field.set(this, isFlag ? true : arguments.get(offset + 1)); | |
} | |
} | |
boolean matched = false; | |
for (Method method : this.getClass().getMethods()) | |
{ | |
if (method.isAnnotationPresent(Argument.class)) | |
{ | |
Argument argument = method.getAnnotation(Argument.class); | |
int parameterCount = method.getParameterTypes().length; | |
int offset = matchInArguments(argument, arguments, parameterCount); | |
if (offset >= 0) | |
{ | |
method.invoke(this, arguments.subList(offset + 1, | |
offset + 1 + parameterCount).toArray()); | |
matched = true; | |
} | |
if (matched) | |
break; | |
} | |
} | |
if (!matched) | |
this.printUsage(); | |
} | |
catch (Exception ex) | |
{ | |
this.printUsage(); | |
} | |
return this; | |
} | |
private static int matchInArguments(Argument argument, List<String> arguments, int parameterCount) | |
{ | |
for (String tag : argument.tags()) | |
{ | |
for (int offset = 0; offset < arguments.size() - parameterCount; offset++) | |
{ | |
if (arguments.get(offset).equals(tag)) | |
return offset; | |
} | |
} | |
return -1; | |
} | |
protected void printUsage() | |
{ | |
System.out.println("Usage:"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import nl.tomsanders.util.Program; | |
public class SampleProgram extends Program { | |
@Argument(tags = { "-formal", "-f"}) | |
public boolean formal; | |
@Argument(tags = "hello") | |
public void sayHello(String firstName, String lastName) { | |
String name = firstName; | |
if (formal) { | |
name += " " + lastName; | |
} | |
System.out.println("Hello, " + name); | |
} | |
public static void main(String[] args) { | |
new SampleProgram().withArguments(args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment