Giter VIP home page Giter VIP logo

sharprompt's Introduction

Sharprompt

Build Downloads NuGet License

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
// 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 Sharprompt.Example contains all the samples. Please check it.

dotnet run --project Sharprompt.Example

Prompt types

Input

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

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

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

MultiSelect (Checkbox)

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

multiselect

List

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

list

Bind (Model-based prompts)

// Input model definition
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 = "Are you ready?")]
    public bool? Ready { get; set; }
}

var result = Prompt.Bind<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}!");

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

Features

Enum type support

public enum MyEnum
{
    [Display(Name = "First value")]
    First,
    [Display(Name = "Second value")]
    Second,
    [Display(Name = "Third value")]
    Third
}

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

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 support

Fluent interface support

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

License

This project is licensed under the MIT License

sharprompt's People

Contributors

shibayan avatar m-nab avatar kimsey0 avatar ryfu-msft avatar 0xflotus avatar ibexpeak avatar pitysoft avatar rodrigopiccelli avatar masatoru avatar volkanpaksoy avatar rhotav avatar wischi-chr avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.