-
Notifications
You must be signed in to change notification settings - Fork 0
Defining an enum argument
Martin "Marrow" Rowlinson edited this page Sep 14, 2017
·
4 revisions
Say, for example, you want an argument that accepts a proxy (java.net.Proxy.Type
).
First, you need to create the argument:
import com.adeptions.clarguments.*;
import com.adeptions.clarguments.arguments.*;
import com.adeptions.clarguments.definitions.*;
import java.net.Proxy;
public class ProxyTypeArgument extends AbstractArgument<Proxy.Type> implements Argument<Proxy.Type> {
public ProxyTypeArgument(Arguments parentArguments, ArgumentDefinition<Proxy.Type> definition) {
super(parentArguments, definition);
}
@Override
public void setRawValue(int tokenPosition, String rawValue, ArgName specifiedArgName) throws BadArgException {
values.add(definition.convertRawValue(tokenPosition, rawValue, this, specifiedArgName));
specified = true;
}
}
Then create the argument definition:
import com.adeptions.clarguments.*;
import com.adeptions.clarguments.definitions.*;
import com.adeptions.clarguments.arguments.*;
import java.net.Proxy;
public class ProxyTypeArgumentDefinition extends AbstractArgumentDefinition<Proxy.Type> implements ArgumentDefinition<Proxy.Type> {
public ProxyTypeArgumentDefinition(String name, String description) {
super(ArgumentDefinitionType.VALUED, name, description);
}
public ProxyTypeArgumentDefinition(String[] names, String description) {
super(ArgumentDefinitionType.VALUED, names, description);
}
@Override
public Proxy.Type convertRawValue(int tokenPosition, String rawValue, Argument<Proxy.Type> argument, ArgName specifiedArgName) throws BadArgException {
for (Proxy.Type type: Proxy.Type.values()) {
if (type.name().equals(rawValue)) {
return type;
}
}
throw new BadArgException(PredefinedBadArgReasons.INVALID_VALUE, tokenPosition, "Value '" + rawValue + "' is not a valid proxy type", argument, specifiedArgName);
}
@Override
public Argument<Proxy.Type> createArgumentInstance(Arguments parentArguments) {
return new ProxyTypeArgument(parentArguments, this);
}
}
You can now use this argument definition, e.g.
import com.adeptions.clarguments.*;
import com.adeptions.clarguments.definitions.*;
public class Application {
public static void main(String[] args) throws BadArgException {
// define arguments...
ArgumentDefinitions argumentDefinitions = new ArgumentDefinitions(
new StringArgumentDefinition("proxyAddress", "The address of the proxy"),
new ProxyTypeArgumentDefinition("proxyType", "The type of the proxy (DIRECT|HTTP|SOCKS)")
);
// parse args...
Arguments arguments = argumentDefinitions.parseArgs(args);
if (arguments.hasParsingExceptions()) {
// display parsing exceptions...
for (BadArgException badArgException: arguments.getParsingExceptions()) {
System.err.println(badArgException.getMessage());
}
} else {
// display arguments...
System.out.println("proxyAddress = " + arguments.get("proxyAddress").getValue());
System.out.println("proxyType = " + arguments.get("proxyType").getValue());
}
}
}
Alternatively, to avoid having to write classes for your typed argument, you can use the GenericArgumentDefinition to define the enum typed argument and just supply the raw value converter, e.g.
import com.adeptions.clarguments.*;
import com.adeptions.clarguments.definitions.*;
import com.adeptions.clarguments.arguments.*;
import com.adeptions.clarguments.converters.*;
import java.net.Proxy;
public class Application {
public static void main(String[] args) throws BadArgException {
// define arguments...
ArgumentDefinitions argumentDefinitions = new ArgumentDefinitions(
new StringArgumentDefinition("proxyAddress", "The address of the proxy"),
new GenericArgumentDefinition<Proxy.Type>("proxyType2", "The type of the proxy (DIRECT|HTTP|SOCKS)", new ArgumentValueConverter<Proxy.Type>() {
@Override
public Proxy.Type convert(int tokenPosition, String rawValue, Argument argument, ArgName specifiedArgName) throws BadArgException {
for (Proxy.Type type: Proxy.Type.values()) {
if (type.name().equals(rawValue)) {
return type;
}
}
throw new BadArgException(PredefinedBadArgReasons.INVALID_VALUE, tokenPosition, "Value '" + rawValue + "' is not a valid proxy type", argument, specifiedArgName);
}
})
);
// parse args...
Arguments arguments = argumentDefinitions.parseArgs(args);
if (arguments.hasParsingExceptions()) {
// display parsing exceptions...
for (BadArgException badArgException: arguments.getParsingExceptions()) {
System.err.println(badArgException.getMessage());
}
} else {
// display arguments...
System.out.println("proxyAddress = " + arguments.get("proxyAddress").getValue());
System.out.println("proxyType = " + arguments.get("proxyType").getValue());
}
}
}