Use CommandContext as method parameter

- With annotation(@Command) model it's now possible to just
  add CommandContext and it will get resolved and doesn't
  cause it to appear as an option.
- Fixes #779
This commit is contained in:
Janne Valkealahti
2024-01-24 15:38:22 +00:00
parent ef3b6e1f3d
commit 9830fafe1a
6 changed files with 137 additions and 8 deletions

View File

@@ -1,7 +1,7 @@
[[writing]]
= Writing
ifndef::snippets[:snippets: ../../test/java/org/springframework/shell/docs]
ifndef::snippets[:snippets: ../../../../src/test/java/org/springframework/shell/docs]
When something needs to get written into your console you can always
use JDK's `System.out` which then goes directly into JDK's own streams.
@@ -17,9 +17,17 @@ to return anything given `CommandContext` contains reference to
include::{snippets}/WritingSnippets.java[tag=reg-terminal-writer]
----
It's possible to autowire `Terminal` to get access to its writer.
If using `@Command` you can get access to `CommandContext` and get
`Terminal` from there.
[source, java, indent=0]
----
include::{snippets}/WritingSnippets.java[tag=anno-terminal-writer]
----
It's possible to autowire `Terminal` to get access to its writer.
[source, java, indent=0]
----
include::{snippets}/WritingSnippets.java[tag=legacyanno-terminal-writer]
----

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 the original author or authors.
* Copyright 2022-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,14 +18,16 @@ package org.springframework.shell.docs;
import org.jline.terminal.Terminal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.shell.command.CommandContext;
import org.springframework.shell.command.CommandRegistration;
import org.springframework.shell.command.annotation.Command;
import org.springframework.shell.standard.ShellMethod;
class WritingSnippets {
class Dump1 {
// tag::anno-terminal-writer[]
// tag::legacyanno-terminal-writer[]
@Autowired
Terminal terminal;
@@ -34,6 +36,17 @@ class WritingSnippets {
terminal.writer().println("hi");
terminal.writer().flush();
}
// end::legacyanno-terminal-writer[]
}
class Dump2 {
// tag::anno-terminal-writer[]
@Command
public void example(CommandContext ctx) {
ctx.getTerminal().writer().println("hi");
ctx.getTerminal().writer().flush();
}
// end::anno-terminal-writer[]
}