Sharprompt Documentation

repository·master·Indexed 21 days ago

https://github.com/shibayan/sharprompt

An interactive command-line application framework for C# that provides rich, model-based, and customizable prompts for building CLI tools. It features a static Prompt class for common tasks like string inputs, password entry, and boolean confirmations, as well as a Bind method that automatically generates prompt sequences from model classes using C# Data Annotations. Supports fluent configuration, custom color schemas, Unicode/emoji rendering, and various prompt types including Input, Confirm, Select, MultiSelect, and List.

Tokens
9.8K
Snippets
56
Records
61
Agent score
73%

What's inside Sharprompt

  1. How Bind determines prompt types

    master

    Sharprompt maps C# property types and specific Data Annotations to specialized prompt types. This allows you to control the user interface (e.g., showing a password mask or a selection list) directly from your data model.

    Property Type / AttributeResulting Prompt Type
    stringInput
    string with [DataType(DataType.Password)]Password
    bool?Confirm
    string with [InlineItems(...)]Select
    Enum typeSelect
  2. Apply validation to Input prompts

    master

    You can enforce rules on user input by providing a list of validators. Validators are functions that return a ValidationResult?. If the result is not null, the input is considered invalid.

    Commonly used validators include Validators.Required() and Validators.MinLength(int).

    You can pass validators via the InputOptions<T> class, the Fluent API, or as a direct parameter in the Prompt.Input method.

    var name = Prompt.Input<string>("What's your name?",
        defaultValue: "John Smith",
        placeholder: "At least 3 characters",
        validators: new[] { Validators.Required(), Validators.MinLength(3) });
  3. Use the Input prompt for basic type conversion

    master

    The Input prompt allows you to capture user input and automatically convert it to a specified generic type T. This is useful for capturing strings, integers, or other types that support conversion.

    Use Prompt.Input<T>(message) to prompt the user.

    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}");
  4. Enable Unicode and Emoji support

    master

    Sharprompt supports multi-byte characters and emojis. To ensure they render correctly in your terminal, set the Console.OutputEncoding to Encoding.UTF8 at the start of your application.

    Console.OutputEncoding = Encoding.UTF8;
    
    var name = Prompt.Input<string>("What's your name?");
  5. Use the MultiSelect prompt

    master

    The MultiSelect prompt allows users to select multiple items from a list using checkboxes. You can use it by passing a message, a collection of items, and optional constraints like pageSize, minimum, or maximum selections.

    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)}");
  6. Use built-in validators in Sharprompt prompts

    master

    Sharprompt allows you to enforce input constraints using the validators parameter. This parameter accepts an array of validator functions and is compatible with Input, Password, and List prompts. If a validator fails, the prompt will typically re-prompt the user with the error message provided by the validator.

    var secret = Prompt.Password("Type new password",
        validators: new[] { Validators.Required(), Validators.MinLength(8) });
  7. Use Enum types with Select and MultiSelect

    master

    When using an enum type with Select or MultiSelect, Sharprompt automatically generates the list items from the enum values. To customize the text displayed to the user instead of the raw enum member name, apply the [Display(Name = "...")] attribute from System.ComponentModel.DataAnnotations to the enum members.

    using System.ComponentModel.DataAnnotations;
    
    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}");
    
    // For MultiSelect:
    var values = Prompt.MultiSelect<MyEnum>("Select enum values");
  8. Use the Select prompt to choose a single item

    master

    The Select prompt allows a user to pick exactly one item from a provided list. You can use it by passing a message and an array of items, or by using the SelectOptions<T> class for more granular control.

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

    master

    Sharprompt provides a static Prompt class for common interactive terminal tasks. You can perform simple string inputs, secure password entry with validation, and boolean confirmations.

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