shibayan/Sharprompt
소개
C#용 대화형 명령줄 애플리케이션 프레임워크
웹 및 앱 개발What you can do
- Multi-platform support
- Supports the popular prompts (Input / Password / Select / etc)
- Supports model-based prompts
- Validation of input value
측정된 스타 증가
- in 24h
- +1
- in 7 days
- +1
- in 15 days
- +2
- in 1 month
- +2
서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.
README.md
OriginalSharprompt
Interactive command-line based application framework for C#

Features
- Multi-platform support
- Supports the popular prompts (
Input/Password/Select/ etc) - Supports model-based prompts
- Validation of input value
- Automatic generation of data source using Enum type
- Customizable symbols and color schema
- Unicode support (Multi-byte characters and Emoji😀🎉)
Installation
Install-Package Sharprompt
dotnet add package Sharprompt
Quick start
using Sharprompt;
// Simple input
var name = Prompt.Input<string>("What's your name?");
Console.WriteLine($"Hello, {name}!");
// Password input
var secret = Prompt.Password("Type new password", validators: new[] { Validators.Required(), Validators.MinLength(8) });
Console.WriteLine("Password OK");
// Confirmation
var answer = Prompt.Confirm("Are you ready?", defaultValue: true);
Console.WriteLine($"Your answer is {answer}");
Examples
The project in the folder samples/Sharprompt.Example contains all the samples. Please check it.
dotnet run --project samples/Sharprompt.Example
For API selection, model constraints, cancellation, and common mistakes, see the usage guide. See automated testing for a complete example that runs without a terminal:
dotnet run --project samples/Sharprompt.Automation -f net10.0
The documentation is also available as Markdown, with an llms.txt index. Public API XML documentation is included in the NuGet package.
Prompt types
Input
Takes a generic type parameter and performs type conversion as appropriate.
using Sharprompt;
var name = Prompt.Input<string>("What's your name?");
Console.WriteLine($"Hello, {name}!");
var number = Prompt.Input<int>("Enter any number");
Console.WriteLine($"Input = {number}");
When a default value is provided, pressing Enter with nothing typed accepts the default. Press Tab to insert the default value for editing, or Backspace to dismiss the default — the prompt then behaves as if no default value was provided, so an empty value can be submitted (subject to the usual required check for non-nullable types and any validators).

Confirm
using Sharprompt;
var answer = Prompt.Confirm("Are you ready?");
Console.WriteLine($"Your answer is {answer}");

Password
using Sharprompt;
var secret = Prompt.Password("Type new password");
Console.WriteLine("Password OK");

Select
using Sharprompt;
var city = Prompt.Select("Select your city", new[] { "Seattle", "London", "Tokyo" });
Console.WriteLine($"Hello, {city}!");

MultiSelect (Checkbox)
using Sharprompt;
var cities = Prompt.MultiSelect("Which cities would you like to visit?", new[] { "Seattle", "London", "Tokyo", "New York", "Singapore", "Shanghai" }, pageSize: 3);
Console.WriteLine($"You picked {string.Join(", ", cities)}");

List
using Sharprompt;
var value = Prompt.List<string>("Please add item(s)");
Console.WriteLine($"You picked {string.Join(", ", value)}");

Bind (Model-based prompts)
Bind automatically generates a sequence of prompts from a model annotated with [PromptBindable]. The model can be a class or a record (record class) with a public parameterless constructor and settable properties (init-only properties are skipped with a SHARP002 warning). Properties with [Display(Order)] are prompted first, in ascending order, followed by the rest in declaration order:
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Sharprompt;
var result = Prompt.Bind<MyFormModel>();
// Input model definition
[PromptBindable]
public class MyFormModel
{
[Display(Name = "What's your name?")]
[Required]
public string Name { get; set; } = "";
[Display(Name = "Type new password")]
[DataType(DataType.Password)]
[Required]
[MinLength(8)]
public string Password { get; set; } = "";
[Display(Name = "Select your city")]
[Required]
[InlineItems("Seattle", "London", "Tokyo")]
public string City { get; set; } = "";
[Display(Name = "Select favorite fruit")]
[MemberItems(nameof(Fruits))]
public string Fruit { get; set; } = "";
[Display(Name = "Are you ready?")]
public bool? Ready { get; set; }
public static IEnumerable<string> Fruits => new[] { "Apple", "Banana", "Orange" };
}
Configuration
Symbols
using Sharprompt;
Prompt.Symbols.Prompt = new Symbol("🤔", "?");
Prompt.Symbols.Done = new Symbol("😎", "V");
Prompt.Symbols.Error = new Symbol("😱", ">>");
var name = Prompt.Input<string>("What's your name?");
Console.WriteLine($"Hello, {name}!");
Color schema
using Sharprompt;
Prompt.ColorSchema.Answer = ConsoleColor.DarkRed;
Prompt.ColorSchema.Select = ConsoleColor.DarkCyan;
var name = Prompt.Input<string>("What's your name?");
Console.WriteLine($"Hello, {name}!");
Cancellation support
using Sharprompt;
// Throw an exception when canceling with Ctrl-C
Prompt.ThrowExceptionOnCancel = true;
try
{
var name = Prompt.Input<string>("What's your name?");
Console.WriteLine($"Hello, {name}!");
}
catch (PromptCanceledException)
{
Console.WriteLine("Prompt canceled");
}
// Cancel from code with a CancellationToken (throws OperationCanceledException)
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
var answer = Prompt.Confirm(options => options.Message = "Are you ready?", cts.Token);
Validators
Sharprompt provides built-in validators that can be used with the validators parameter.
using Sharprompt;
var secret = Prompt.Password("Type new password", validators: new[] { Validators.Required(), Validators.MinLength(8) });
| Validator | Description |
|---|---|
Validators.Required() |
Ensures the input is not empty |
Validators.MinLength(length) |
Ensures the input has at least the specified number of characters |
Validators.MaxLength(length) |
Ensures the input does not exceed the specified number of characters |
Validators.RegularExpression(pattern) |
Ensures the input matches the specified regular expression |
Additional features
Enum type support
using Sharprompt;
using System.ComponentModel.DataAnnotations;
var value = Prompt.Select<MyEnum>("Select enum value");
Console.WriteLine($"You selected {value}");
public enum MyEnum
{
[Display(Name = "First value")]
First,
[Display(Name = "Second value")]
Second,
[Display(Name = "Third value")]
Third
}
Unicode support
using Sharprompt;
using System.Text;
// Prefer UTF-8 as the output encoding
Console.OutputEncoding = Encoding.UTF8;
var name = Prompt.Input<string>("What's your name?");
Console.WriteLine($"Hello, {name}!");

Fluent interface support
using Sharprompt;
using Sharprompt.Fluent;
// Use fluent interface
var city = Prompt.Select<string>(o => o.WithMessage("Select your city")
.WithItems(new[] { "Seattle", "London", "Tokyo" })
.WithDefaultValue("Seattle"));
Supported platforms
- Windows
- Command Prompt
- PowerShell
- Windows Terminal
- Linux (Ubuntu, etc)
- Windows Terminal (WSL 2)
- macOS
- Terminal.app
Contributing
Please see CONTRIBUTING.md for local setup, validation commands, and pull request expectations.
Security
Please see SECURITY.md for how to report vulnerabilities privately.
License
This project is licensed under the MIT License
관련 프로젝트 살펴보기
불러오는 중…