![JAR search and dependency download from the Maven repository](/logo.png)
codegen.templates.Arguments Maven / Gradle / Ivy
<#include "/include/macros.fmpp">
<@pp.dropOutputFile />
<#list commandDefs as def>
<#global cmd=def.command>
<#global commandName=def.commandName>
<#global optionName=cmd.simpleName+"Option">
<#global optionsName=cmd.simpleName+"Options">
<#global argumentsName=cmd.simpleName+"Arguments">
<#global hasTrueOperand=def.operands?size != 0 && (def.operands?size != 1 || !isOptions(def.operands?values[0]))>
<@pp.changeOutputFile name=pp.pathTo("/"+def.pkg.path+"/"+argumentsName+".java")/>
package ${def.pkg.name};
<#if hasArgsOperand(def)>
import java.util.List;
import java.util.Map;
import java.util.Arrays;
#if>
import org.unix4j.command.Arguments;
import org.unix4j.context.ExecutionContext;
<#if hasArgsOperand(def)>
import org.unix4j.convert.ValueConverter;
#if>
<#if def.options?size != 0>
import org.unix4j.option.DefaultOptionSet;
#if>
<#if hasArgsOperand(def)>
import org.unix4j.util.ArgsUtil;
#if>
<#if hasTrueOperand>
import org.unix4j.util.ArrayUtil;
#if>
<#if hasArgsOperand(def)>
import org.unix4j.variable.Arg;
import org.unix4j.variable.VariableContext;
#if>
import ${cmd.pkg.name}.${cmd.simpleName};
<#function getter operand>
<#return "get" + operand.name?cap_first>
#function>
<#function setter operand>
<#return "set" + operand.name?cap_first>
#function>
<#function isset operand>
<#return "is" + operand.name?cap_first + "Set">
#function>
<#function isOptionSet option>
<#return "is" + option.name?cap_first>
#function>
<#function isOptions operand>
<#return operand.type == cmd.simpleName + "Options">
#function>
/**
* Arguments and options for the {@link ${cmd.simpleName} ${commandName}} command.
*/
public final class ${argumentsName} implements Arguments<${argumentsName}> {
<#if def.options?size != 0>
private final ${optionsName} options;
#if>
<#if def.options?size != 0 || def.operands?size != 0>
#if>
<#foreach operand in def.operands?values>
<#if !isOptions(operand) && operand.redirection?length == 0>
// operand: <${operand.name}>
private ${normalizeVarArgType(operand.type, false)} ${operand.name};
private boolean ${operand.name}IsSet = false;
#if>
#foreach>
/**
* Constructor to use if no options are specified.
*/
public ${argumentsName}() {
<#if def.options?size != 0>
this.options = ${optionsName}.EMPTY;
<#else>
super();
#if>
}
<#if def.options?size != 0>
/**
* Constructor with option set containing the selected command options.
*
* @param options the selected options
* @throws NullPointerException if the argument is null
*/
public ${argumentsName}(${optionsName} options) {
if (options == null) {
throw new NullPointerException("options argument cannot be null");
}
this.options = options;
}
/**
* Returns the options set containing the selected command options. Returns
* an empty options set if no option has been selected.
*
* @return set with the selected options
*/
public ${optionsName} getOptions() {
return options;
}
#if>
<#if hasArgsOperand(def)>
/**
* Constructor string arguments encoding options and arguments, possibly
* also containing variable expressions.
*
* @param args string arguments for the command
* @throws NullPointerException if args is null
*/
public ${argumentsName}(String... args) {
this();
this.args = args;
this.argsIsSet = true;
}
private Object resolveVariable(VariableContext context, String variable) {
final Object value = context.getValue(variable);
if (value != null) {
return value;
}
throw new IllegalArgumentException("cannot resolve variable " + variable +
" in command: ${commandName} " + this);
}
private Object[] resolveVariables(VariableContext context, String... unresolved) {
final Object[] resolved = new Object[unresolved.length];
for (int i = 0; i < resolved.length; i++) {
final String expression = unresolved[i];
if (Arg.isVariable(expression)) {
resolved[i] = resolveVariable(context, expression);
} else {
resolved[i] = expression;
}
}
return resolved;
}
private V convert(ExecutionContext context, String operandName, Class operandType, Object value) {
final ValueConverter converter = context.getValueConverterFor(operandType);
final V convertedValue;
if (converter != null) {
convertedValue = converter.convert(value);
} else {
<#if def.options?size != 0>
if (${optionsName}.class.equals(operandType)) {
convertedValue = operandType.cast(${optionsName}.CONVERTER.convert(value));
} else {
convertedValue = null;
}
<#else>
convertedValue = null;
#if>
}
if (convertedValue != null) {
return convertedValue;
}
throw new IllegalArgumentException("cannot convert --" + operandName +
" value '" + value + "' into the type " + operandType.getName() +
" for ${commandName} command");
}
private V convertList(ExecutionContext context, String operandName, Class operandType, List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy