Add Help component for CLI usage and input validation support
- Introduced `Help` class for generating CLI help/usage blocks with options, descriptions, and customizable formatting. - Enhanced `Prompt` and `PromptBuilder` with validation capabilities using predicates and retry messages. - Added terminal control methods `clearScreen` and `cursorTo` with ANSI support. - Updated examples and documentation (`Examples.md`, `Components.md`) to showcase the new features. - Extended `Examples` class with a `helpBlock` method for demonstrating the `Help` usage.
This commit is contained in:
@@ -108,6 +108,13 @@ String name = Terminal.prompt("Name: ").ask();
|
||||
String secret = Terminal.prompt("Password: ").masked().ask();
|
||||
// With shared scanner (e.g. in a loop):
|
||||
String line = Terminal.prompt("Input: ").ask(sharedScanner);
|
||||
|
||||
// With validation (asks again until input matches):
|
||||
String age = Terminal.prompt("Age: ").validate(s -> s.matches("\\d+")).ask();
|
||||
String email = Terminal.prompt("Email: ")
|
||||
.validate(s -> s.contains("@"))
|
||||
.retryMessage("Invalid email, try again: ")
|
||||
.ask();
|
||||
```
|
||||
|
||||
## Interactive: Confirm
|
||||
@@ -259,6 +266,26 @@ Terminal.code("java")
|
||||
.print(System.out);
|
||||
```
|
||||
|
||||
## Help (CLI usage)
|
||||
|
||||
```java
|
||||
Terminal.help()
|
||||
.title("Options")
|
||||
.option("-v, --verbose", "Enable verbose output")
|
||||
.option("-h, --help", "Show this help")
|
||||
.option("--file <path>", "Input file path")
|
||||
.print(System.out);
|
||||
```
|
||||
|
||||
## Clear screen & cursor
|
||||
|
||||
```java
|
||||
Terminal.clearScreen(); // Clear terminal (ANSI)
|
||||
Terminal.cursorTo(1, 1); // Move cursor to row 1, col 1 (1-based)
|
||||
```
|
||||
|
||||
No-op when ANSI is disabled.
|
||||
|
||||
## SysInfo
|
||||
|
||||
```java
|
||||
|
||||
Reference in New Issue
Block a user