Skip to content

Repository files navigation

Sharprompt

BuildDownloadsNuGetLicense

Interactive command-line based application framework for C#

sharprompt

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

// Simple inputvarname=Prompt.Input<string>("What's your name?");Console.WriteLine($"Hello, {name}!");// Password inputvarsecret=Prompt.Password("Type new password",validators:new[]{Validators.Required(),Validators.MinLength(8)});Console.WriteLine("Password OK");// Confirmationvaranswer=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

Prompt types

Input

Takes a generic type parameter and performs type conversion as appropriate.

varname=Prompt.Input<string>("What's your name?");Console.WriteLine($"Hello, {name}!");varnumber=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).

input

Confirm

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

confirm

Password

varsecret=Prompt.Password("Type new password");Console.WriteLine("Password OK");

password

Select

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

select

MultiSelect (Checkbox)

varcities=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)}");

multiselect

List

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

list

Bind (Model-based prompts)

// Input model definitionpublicclassMyFormModel{[Display(Name="What's your name?")][Required]publicstringName{get;set;}[Display(Name="Type new password")][DataType(DataType.Password)][Required][MinLength(8)]publicstringPassword{get;set;}[Display(Name="Select your city")][Required][InlineItems("Seattle","London","Tokyo")]publicstringCity{get;set;}[Display(Name="Are you ready?")]publicbool?Ready{get;set;}}varresult=Prompt.Bind<MyFormModel>();

Configuration

Symbols

Prompt.Symbols.Prompt=newSymbol("🤔","?");Prompt.Symbols.Done=newSymbol("😎","V");Prompt.Symbols.Error=newSymbol("😱",">>");varname=Prompt.Input<string>("What's your name?");Console.WriteLine($"Hello, {name}!");

Color schema

Prompt.ColorSchema.Answer=ConsoleColor.DarkRed;Prompt.ColorSchema.Select=ConsoleColor.DarkCyan;varname=Prompt.Input<string>("What's your name?");Console.WriteLine($"Hello, {name}!");

Cancellation support

// Throw an exception when canceling with Ctrl-CPrompt.ThrowExceptionOnCancel=true;try{varname=Prompt.Input<string>("What's your name?");Console.WriteLine($"Hello, {name}!");}catch(PromptCanceledExceptionex){Console.WriteLine("Prompt canceled");}

Validators

Sharprompt provides built-in validators that can be used with the validators parameter.

varsecret=Prompt.Password("Type new password",validators:new[]{Validators.Required(),Validators.MinLength(8)});
ValidatorDescription
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

publicenumMyEnum{[Display(Name="First value")]First,[Display(Name="Second value")]Second,[Display(Name="Third value")]Third}varvalue=Prompt.Select<MyEnum>("Select enum value");Console.WriteLine($"You selected {value}");

Unicode support

// Prefer UTF-8 as the output encodingConsole.OutputEncoding=Encoding.UTF8;varname=Prompt.Input<string>("What's your name?");Console.WriteLine($"Hello, {name}!");

unicode support

Fluent interface support

usingSharprompt.Fluent;// Use fluent interfacevarcity=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

About

Interactive command-line based application framework for C#

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

847 stars

Watchers

10 watching

Forks

Releases

Sponsor this project

Used by

Contributors

Languages