Arguments
The @Argument annotation defines positional command-line arguments.
Properties
| Property | Type | Default | Description |
|---|---|---|---|
description | String | "" | Help description |
required | boolean | false | Is argument required? |
defaultValue | String[] | {} | Default values |
askIfNotSet | boolean | false | Prompt user if not set |
overrideRequired | boolean | false | Override required validation |
converter | Class<? extends Converter> | NullConverter.class | Custom value converter |
completer | Class<? extends OptionCompleter> | NullOptionCompleter.class | Custom completer |
validator | Class<? extends OptionValidator> | NullValidator.class | Custom validator |
activator | Class<? extends OptionActivator> | NullActivator.class | Custom activator |
renderer | Class<? extends OptionRenderer> | NullOptionRenderer.class | Custom renderer |
parser | Class<? extends OptionParser> | AeshOptionParser.class | Custom parser |
Basic Example
@CommandDefinition(name = "echo")
public class EchoCommand implements Command<CommandInvocation> {
@Argument(description = "Text to echo")
private String text;
@Override
public CommandResult execute(CommandInvocation invocation) {
invocation.println(text);
return CommandResult.SUCCESS;
}
}Usage: echo Hello World
Multiple Arguments with @Arguments
For multiple values, use @Arguments (plural) with a Collection:
@CommandDefinition(name = "sum")
public class SumCommand implements Command<CommandInvocation> {
@Arguments(description = "Numbers to sum")
private List<Integer> numbers;
@Override
public CommandResult execute(CommandInvocation invocation) {
int sum = numbers.stream().mapToInt(Integer::intValue).sum();
invocation.println("Sum: " + sum);
return CommandResult.SUCCESS;
}
}Usage: sum 1 2 3 4 5
Required Argument
@Argument(required = true, description = "Required file")
private String file;Default Value
@Argument(defaultValue = "output.txt", description = "Output file")
private String outputFile;Combined with Options
Arguments and options can be used together:
@CommandDefinition(name = "copy")
public class CopyCommand implements Command<CommandInvocation> {
@Argument(required = true, description = "Source file")
private String source;
@Option(shortName = 'd', description = "Destination directory")
private String destination = ".";
@Option(shortName = 'v', hasValue = false, description = "Verbose")
private boolean verbose;
@Override
public CommandResult execute(CommandInvocation invocation) {
// implementation
return CommandResult.SUCCESS;
}
}Usage: copy myfile.txt -d /tmp -v