@@ -54,6 +54,13 @@ public interface CommandContext {
|
||||
*/
|
||||
CommandParserResults getParserResults();
|
||||
|
||||
/**
|
||||
* Gets a command registration.
|
||||
*
|
||||
* @return the command registration
|
||||
*/
|
||||
CommandRegistration getCommandRegistration();
|
||||
|
||||
/**
|
||||
* Gets an mapped option value.
|
||||
*
|
||||
@@ -76,10 +83,12 @@ public interface CommandContext {
|
||||
* @param args the arguments
|
||||
* @param results the results
|
||||
* @param terminal the terminal
|
||||
* @param commandRegistration the command registration
|
||||
* @return a command context
|
||||
*/
|
||||
static CommandContext of(String[] args, CommandParserResults results, Terminal terminal) {
|
||||
return new DefaultCommandContext(args, results, terminal);
|
||||
static CommandContext of(String[] args, CommandParserResults results, Terminal terminal,
|
||||
CommandRegistration commandRegistration) {
|
||||
return new DefaultCommandContext(args, results, terminal, commandRegistration);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -87,14 +96,17 @@ public interface CommandContext {
|
||||
*/
|
||||
static class DefaultCommandContext implements CommandContext {
|
||||
|
||||
private String[] args;
|
||||
private CommandParserResults results;
|
||||
private Terminal terminal;
|
||||
private final String[] args;
|
||||
private final CommandParserResults results;
|
||||
private final Terminal terminal;
|
||||
private final CommandRegistration commandRegistration;
|
||||
|
||||
DefaultCommandContext(String[] args, CommandParserResults results, Terminal terminal) {
|
||||
DefaultCommandContext(String[] args, CommandParserResults results, Terminal terminal,
|
||||
CommandRegistration commandRegistration) {
|
||||
this.args = args;
|
||||
this.results = results;
|
||||
this.terminal = terminal;
|
||||
this.commandRegistration = commandRegistration;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -112,6 +124,11 @@ public interface CommandContext {
|
||||
return results;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandRegistration getCommandRegistration() {
|
||||
return commandRegistration;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T getOptionValue(String name) {
|
||||
|
||||
@@ -104,7 +104,7 @@ public interface CommandExecution {
|
||||
throw new CommandParserExceptionsException("Command parser resulted errors", results.errors());
|
||||
}
|
||||
|
||||
CommandContext ctx = CommandContext.of(args, results, terminal);
|
||||
CommandContext ctx = CommandContext.of(args, results, terminal, registration);
|
||||
|
||||
Object res = null;
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ import org.springframework.shell.command.CommandContext;
|
||||
@SuppressWarnings("unused")
|
||||
public class CommandContextSnippets {
|
||||
|
||||
CommandContext ctx = CommandContext.of(null, null, null);
|
||||
CommandContext ctx = CommandContext.of(null, null, null, null);
|
||||
|
||||
void dump1() {
|
||||
// tag::snippet1[]
|
||||
|
||||
@@ -87,4 +87,22 @@ public class FunctionCommands {
|
||||
.and()
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CommandRegistration commandRegistration4() {
|
||||
return CommandRegistration.builder()
|
||||
.command("function", "command4")
|
||||
.help("function sample")
|
||||
.group("Function Commands")
|
||||
.withTarget()
|
||||
.consumer(ctx -> {
|
||||
ctx.getTerminal().writer()
|
||||
.println(String.format("hi, command is '%s'", ctx.getCommandRegistration().getCommand()));
|
||||
})
|
||||
.and()
|
||||
.withOption()
|
||||
.longNames("arg1")
|
||||
.and()
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user