masatoru / Sharprompt

Interactive command-line based application framework for C#

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sharprompt

Build Downloads NuGet License

Interactive command-line based application framework for C#

sharprompt

Install

Install-Package Sharprompt
dotnet add package Sharprompt

Features

  • Multi-platform support
  • Supports the popular prompts (Input / Password / Select / etc)
  • Supports model-based prompts (In preview)
  • Validation of input value
  • Automatic generation of data source using Enum value
  • Customizable symbols and color schema
  • Unicode support (Multi-byte characters and EmojiπŸ˜€πŸŽ‰)

Usage

// Simple input prompt
var name = Prompt.Input<string>("What's your name?");
Console.WriteLine($"Hello, {name}!");

// Password prompt
var secret = Prompt.Password("Type new password", new[] { Validators.Required(), Validators.MinLength(8) });
Console.WriteLine("Password OK");

// Confirmation prompt
var answer = Prompt.Confirm("Are you ready?", defaultValue: true);
Console.WriteLine($"Your answer is {answer}");

APIs

Input

var name = Prompt.Input<string>("What's your name?");
Console.WriteLine($"Hello, {name}!");

input

Confirm

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

confirm

Password

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

password

Select

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

select

Enum support

var value = Prompt.Select<MyEnum>("Select enum value");
Console.WriteLine($"You selected {value}");

MultiSelect

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(", ", options)}");

multiselect

List

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

list

AutoForms (Preview)

// Model definition
public class MyFormModel
{
    [Display(Prompt = "What's your name?")]
    [Required]
    public string Name { get; set; }

    [Display(Prompt = "Type new password")]
    [DataType(DataType.Password)]
    [Required]
    [MinLength(8)]
    public string Password { get; set; }

    [Display(Prompt = "Are you ready?")]
    public bool Ready { get; set; }
}

var result = Prompt.AutoForms<MyFormModel>();

Configuration

Symbols

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

Prompt.ColorSchema.Answer = ConsoleColor.DarkRed;
Prompt.ColorSchema.Select = ConsoleColor.DarkCyan;

var name = Prompt.Input<string>("What's your name?");
Console.WriteLine($"Hello, {name}!");

Unicode support

// Prefer UTF-8 as the output encoding
Console.OutputEncoding = Encoding.UTF8;

var name = Prompt.Input<string>("What's your name?");
Console.WriteLine($"Hello, {name}!");

unicode

Cancellation support

// 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 ex)
{
    Console.WriteLine("Prompt canceled");
}

Supported platforms

  • Windows
    • Command Prompt / PowerShell / Windows Terminal
  • Ubuntu
    • Bash
  • macOS
    • Bash

License

This project is licensed under the MIT License

About

Interactive command-line based application framework for C#

License:MIT License


Languages

Language:C# 100.0%