
org.unix4j.unix.echo.EchoArguments Maven / Gradle / Ivy
package org.unix4j.unix.echo;
import java.util.List;
import java.util.Map;
import java.util.Arrays;
import org.unix4j.command.Arguments;
import org.unix4j.context.ExecutionContext;
import org.unix4j.convert.ValueConverter;
import org.unix4j.option.DefaultOptionSet;
import org.unix4j.util.ArgsUtil;
import org.unix4j.util.ArrayUtil;
import org.unix4j.variable.Arg;
import org.unix4j.variable.VariableContext;
import org.unix4j.unix.Echo;
/**
* Arguments and options for the {@link Echo echo} command.
*/
public final class EchoArguments implements Arguments {
private final EchoOptions options;
// operand:
private String string;
private boolean stringIsSet = false;
// operand:
private String[] strings;
private boolean stringsIsSet = false;
// operand:
private String[] args;
private boolean argsIsSet = false;
/**
* Constructor to use if no options are specified.
*/
public EchoArguments() {
this.options = EchoOptions.EMPTY;
}
/**
* Constructor with option set containing the selected command options.
*
* @param options the selected options
* @throws NullPointerException if the argument is null
*/
public EchoArguments(EchoOptions 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 EchoOptions getOptions() {
return options;
}
/**
* 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 EchoArguments(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: echo " + 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 (EchoOptions.class.equals(operandType)) {
convertedValue = operandType.cast(EchoOptions.CONVERTER.convert(value));
} else {
convertedValue = null;
}
}
if (convertedValue != null) {
return convertedValue;
}
throw new IllegalArgumentException("cannot convert --" + operandName +
" value '" + value + "' into the type " + operandType.getName() +
" for echo command");
}
private V convertList(ExecutionContext context, String operandName, Class operandType, List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy