mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-16 00:42:51 +08:00
Update documentation: add example for the Text Prompt usage (#1636)
This commit is contained in:
parent
a55b80220d
commit
78f3f80b17
@ -27,6 +27,32 @@ you can use the `Prompt<TResult>`.
|
|||||||
Run prompt example? [y/n] (y): _
|
Run prompt example? [y/n] (y): _
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Usage
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
// Ask the user to confirm
|
||||||
|
var confirmation = AnsiConsole.Prompt(
|
||||||
|
new TextPrompt<bool>("Run prompt example?")
|
||||||
|
.AddChoice(true)
|
||||||
|
.AddChoice(false)
|
||||||
|
.DefaultValue(true)
|
||||||
|
.WithConverter(choice => choice ? "y" : "n"));
|
||||||
|
|
||||||
|
// Echo the confirmation back to the terminal
|
||||||
|
Console.WriteLine(confirmation ? "Confirmed" : "Declined");
|
||||||
|
```
|
||||||
|
|
||||||
|
Otherwise it is possible to use the `ConfirmationPrompt`
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
// Ask the user to confirm
|
||||||
|
var confirmation = AnsiConsole.Prompt(
|
||||||
|
new ConfirmationPrompt("Run prompt example?"));
|
||||||
|
|
||||||
|
// Echo the confirmation back to the terminal
|
||||||
|
Console.WriteLine(confirmation ? "Confirmed" : "Declined");
|
||||||
|
```
|
||||||
|
|
||||||
## Simple
|
## Simple
|
||||||
|
|
||||||
<?# Example symbol="M:Prompt.Program.AskName" project="Prompt" /?>
|
<?# Example symbol="M:Prompt.Program.AskName" project="Prompt" /?>
|
||||||
@ -36,6 +62,30 @@ What's your name? Patrik
|
|||||||
What's your age? 37
|
What's your age? 37
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Usage
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
// Ask the user a couple of simple questions
|
||||||
|
var name = AnsiConsole.Prompt(
|
||||||
|
new TextPrompt<string>("What's your name?"));
|
||||||
|
var age = AnsiConsole.Prompt(
|
||||||
|
new TextPrompt<int>("What's your age?"));
|
||||||
|
|
||||||
|
// Echo the name and age back to the terminal
|
||||||
|
AnsiConsole.WriteLine($"So you're {name} and you're {age} years old");
|
||||||
|
```
|
||||||
|
|
||||||
|
Otherwise it is possible to use the `Ask` method
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
// Ask the user a couple of simple questions
|
||||||
|
var name = AnsiConsole.Ask<string>("What's your name?");
|
||||||
|
var age = AnsiConsole.Ask<int>("What's your age?");
|
||||||
|
|
||||||
|
// Echo the name and age back to the terminal
|
||||||
|
AnsiConsole.WriteLine($"So you're {name} and you're {age} years old");
|
||||||
|
```
|
||||||
|
|
||||||
## Choices
|
## Choices
|
||||||
|
|
||||||
<?# Example symbol="M:Prompt.Program.AskFruit" project="Prompt" /?>
|
<?# Example symbol="M:Prompt.Program.AskFruit" project="Prompt" /?>
|
||||||
@ -44,6 +94,19 @@ What's your age? 37
|
|||||||
What's your favorite fruit? [Apple/Banana/Orange] (Orange): _
|
What's your favorite fruit? [Apple/Banana/Orange] (Orange): _
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Usage
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
// Ask for the user's favorite fruit
|
||||||
|
var fruit = AnsiConsole.Prompt(
|
||||||
|
new TextPrompt<string>("What's your favorite fruit?")
|
||||||
|
.AddChoices(["Apple", "Banana", "Orange"])
|
||||||
|
.DefaultValue("Orange"));
|
||||||
|
|
||||||
|
// Echo the fruit back to the terminal
|
||||||
|
Console.WriteLine($"I agree. {fruit} is tasty!");
|
||||||
|
```
|
||||||
|
|
||||||
## Validation
|
## Validation
|
||||||
|
|
||||||
<?# Example symbol="M:Prompt.Program.AskAge" project="Prompt" /?>
|
<?# Example symbol="M:Prompt.Program.AskAge" project="Prompt" /?>
|
||||||
@ -56,6 +119,23 @@ Too high
|
|||||||
What's the secret number? _
|
What's the secret number? _
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Usage
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
// Ask the user to guess the secret number
|
||||||
|
var number = AnsiConsole.Prompt(
|
||||||
|
new TextPrompt<int>("What's the secret number?")
|
||||||
|
.Validate((n) => n switch
|
||||||
|
{
|
||||||
|
< 50 => ValidationResult.Error("Too low"),
|
||||||
|
50 => ValidationResult.Success(),
|
||||||
|
> 50 => ValidationResult.Error("Too high"),
|
||||||
|
}));
|
||||||
|
|
||||||
|
// Echo the user's success back to the terminal
|
||||||
|
Console.WriteLine($"Correct! The secret number is {number}.");
|
||||||
|
```
|
||||||
|
|
||||||
## Secrets
|
## Secrets
|
||||||
|
|
||||||
<?# Example symbol="M:Prompt.Program.AskPassword" project="Prompt" /?>
|
<?# Example symbol="M:Prompt.Program.AskPassword" project="Prompt" /?>
|
||||||
@ -65,6 +145,18 @@ What's the secret number? _
|
|||||||
Enter password: ************_
|
Enter password: ************_
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Usage
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
// Ask the user to enter the password
|
||||||
|
var password = AnsiConsole.Prompt(
|
||||||
|
new TextPrompt<string>("Enter password:")
|
||||||
|
.Secret());
|
||||||
|
|
||||||
|
// Echo the password back to the terminal
|
||||||
|
Console.WriteLine($"Joking is not a secret that your password is {password}");
|
||||||
|
```
|
||||||
|
|
||||||
## Masks
|
## Masks
|
||||||
|
|
||||||
<?# Example symbol="M:Prompt.Program.AskPasswordWithCustomMask" project="Prompt" /?>
|
<?# Example symbol="M:Prompt.Program.AskPasswordWithCustomMask" project="Prompt" /?>
|
||||||
@ -82,6 +174,18 @@ You can utilize a null character to completely hide input.
|
|||||||
Enter password: _
|
Enter password: _
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Usage
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
// Ask the user to enter the password
|
||||||
|
var password = AnsiConsole.Prompt(
|
||||||
|
new TextPrompt<string>("Enter password:")
|
||||||
|
.Secret('-'));
|
||||||
|
|
||||||
|
// Echo the password back to the terminal
|
||||||
|
Console.WriteLine($"Joking is not a secret that your password is {password}");
|
||||||
|
```
|
||||||
|
|
||||||
## Optional
|
## Optional
|
||||||
|
|
||||||
<?# Example symbol="M:Prompt.Program.AskColor" project="Prompt" /?>
|
<?# Example symbol="M:Prompt.Program.AskColor" project="Prompt" /?>
|
||||||
@ -89,3 +193,17 @@ Enter password: _
|
|||||||
```text
|
```text
|
||||||
[Optional] Favorite color? _
|
[Optional] Favorite color? _
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Usage
|
||||||
|
|
||||||
|
```csharp
|
||||||
|
// Ask the user to enter the password
|
||||||
|
var color = AnsiConsole.Prompt(
|
||||||
|
new TextPrompt<string>("[[Optional]] Favorite color?")
|
||||||
|
.AllowEmpty());
|
||||||
|
|
||||||
|
// Echo the color back to the terminal
|
||||||
|
Console.WriteLine(string.IsNullOrWhiteSpace(color)
|
||||||
|
? "You're right, all colors are beautiful"
|
||||||
|
: $"I agree. {color} is a very beautiful color");
|
||||||
|
```
|
Loading…
x
Reference in New Issue
Block a user