TJC.ConsoleApplication 0.7.4

There is a newer version of this package available.
See the version list below for details.
dotnet add package TJC.ConsoleApplication --version 0.7.4                
NuGet\Install-Package TJC.ConsoleApplication -Version 0.7.4                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TJC.ConsoleApplication" Version="0.7.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TJC.ConsoleApplication --version 0.7.4                
#r "nuget: TJC.ConsoleApplication, 0.7.4"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install TJC.ConsoleApplication as a Cake Addin
#addin nuget:?package=TJC.ConsoleApplication&version=0.7.4

// Install TJC.ConsoleApplication as a Cake Tool
#tool nuget:?package=TJC.ConsoleApplication&version=0.7.4                

GitHub Tag GitHub Release NuGet Version

NuGet Downloads Size License

Table of Contents


Console Setup

  • Used to setup the console application with exceptions handling, logging, etc.

ConsoleSetup.Setup(ConsoleSettings? consoleSettings = null, ProcessExitOptions? processExitSettings = null)

  • Allows setting up the console application with a variety of options.
ConsoleSetup.Setup();

ConsoleSetup.SetupSilent()

  • Sets up the console application to be silent (no output to console) unless an exception occurs.
ConsoleSetup.SetupSilent();

ConsoleHeaderExtensions.WriteConsoleHeader();

  • Will write a header to the console with the application title, version, copyright, & description.
  • Note: This is built into ConsoleSetup.Setup() by default.

Arguments

Arguments Parsing

  • Use ConsoleArguments to configure the arguments that the program will accept.
  • If any required arguments are missing or invalid, an error & help message will be displayed.
Example

In the main program, call the Arguments.Parse(args); to parse the arguments.

internal class Arguments
{
    internal static void Parse(string[] args) =>
        ConsoleArguments.ParseAndValidate(args, Assembly.GetCallingAssembly().GetName().Name);

    internal static DryRunArgument DryRun => DryRunArgument.Default;
    internal static bool Item1 { get; private set; }
    internal static string? Item2 { get; private set; }

    internal static readonly ConsoleArguments ConsoleArguments = new(flagRequired: true, logParsedOptions: true)
    {
        DryRun,
        { "item1", v => Item1 = !string.IsNullOrEmpty(v), "Example Item 1" },
        { "item2", v => Item2 = v, "Example Item 2" }
    };
}

Prompts

Yes/No

bool ConsolePrompt.GetYesNo(string message)
  • Prompts the user with the message and waits for a 'Y' or 'N' input (case-insensitive)
  • Outputs error message and waits for input again if input is invalid
  • Returns true if 'Y' and false if 'N'
var result = ConsolePrompt.GetYesNo("Do you want to continue?");
// Outputs: Do you want to continue? (Y/N): 

Text

string ConsolePrompt.GetString(string message)
  • Prompts the user with the message and waits for a string input
var result = ConsolePrompt.GetString("Enter your name:");
// Outputs: Enter your name:
string ConsolePrompt.GetStringChange(string message, string original)
  • Prompts the user to choose whether to change from original value or not
var original = "John"
var result = ConsolePrompt.GetStringChange("MyName", original);
// Outputs: Do you want to change MyName from [John]? (Y/N):
// Waits for user input of 'Y' or 'N' (case-insensitive)
// Returns the original value if 'N' otherwise prompts for a new value like so:
// Outputs: MyName:
ConsolePrompt.GetStringChange(string message, ref string value)
  • Prompts the user to choose whether to change from original value or not
var original = "John"
ConsolePrompt.GetStringChange("MyName", ref original);
// Outputs: Do you want to change MyName from [John]? (Y/N):
// Waits for user input of 'Y' or 'N' (case-insensitive)
// Returns the original value if 'N' otherwise prompts for a new value like so
// Outputs: MyName:

Numbers

int ConsolePrompt.GetInt(string message)
  • Prompts the user to enter an integer
  • Outputs error message and waits for input again if input is invalid
ConsolePrompt.GetInt("Enter Value");
int ConsolePrompt.GetIntRange(string message, int max, int min, bool inclusive)
  • Prompts the user to enter an integer within a specified range (inclusive or exclusive)
  • Outputs error message and waits for input again if input is invalid
ConsolePrompt.GetInt("Enter Value", 10, 1);

Choices

int ConsolePrompt.GetChoiceIndex(string message, IEnumerable<string> choices, int offset = 1)
  • Prompts the user to select an option from a list of choices
  • Returns the index of the selected choice
var options = new List<string> { "Option 1", "Option 2", "Option 3" };
var index = ConsolePrompt.GetChoiceIndex("Choose", options);
// Outputs:
// Choose:
// 1. Option 1
// 2. Option 2
// 3. Option 3
string ConsolePrompt.GetChoice(string message, IEnumerable<string> choices, int offset = 1)
  • Prompts the user to select an option from a list of choices
  • Returns the string of the selected choice
var options = new List<string> { "Option 1", "Option 2", "Option 3" };
var choice = ConsolePrompt.GetChoice("Choose", options);
// Outputs:
// Choose:
// 1. Option 1
// 2. Option 2
// 3. Option 3
T ConsolePrompt.GetChoice<T>(string message) where T: Enum
  • Prompts the user to select an option from an enum
var choice = ConsolePrompt.GetChoice<MyEnum>("Choose");
// Outputs:
// Choose:
// 1. EnumItem1
// 2. EnumItem2
// 3. EnumItem3
T? ConsolePrompt.GetChoiceDone<T>(string message) where T: Enum
  • Prompts the user to select an option from an enum
  • If the first option is selected, it returns null
var choice = ConsolePrompt.GetChoiceDone<MyEnum>("Choose");
// Outputs:
// Choose:
// 0. Done
// 1. EnumItem1
// 2. EnumItem2
// 3. EnumItem3
T ConsolePrompt.GetChoiceChange<T>(string message, T current, T? defaultChoice) where T: Enum
  • Prompts the user if they want to change from the currently selected enum
var choice = MyEnum.EnumItem1
choice = ConsolePrompt.GetChoiceChange<MyEnum>("choice", choice);
// Outputs:
// Change choice from [EnumItem1]? (Y/N): Y
// 1. EnumItem1
// 2. EnumItem2
// 3. EnumItem3

Collections

ICollection<string> ConsolePrompt.GetCollection(string message, string messageIndividual = "")
  • Prompts the user to enter a collection of strings
  • If messageIndividual is provided, it will be used as the prompt for each item in the collection
  • If no value is provided, the collection ends, and the response(s) are returned
var collection = ConsolePrompt.GetCollection("Enter a collection of strings", "str");
// Outputs: Enter a collection of strings (press enter after each item | press enter on an empty line to complete the list):
// Outputs: str:
ICollection<int> ConsolePrompt.GetCollectionInt(string message, string messageIndividual = "")
  • Prompts the user to enter a collection of integers
var collection = ConsolePrompt.GetCollection("Enter a collection of ints");
ICollection<double> ConsolePrompt.GetCollectionDouble(string message, string messageIndividual = "")
  • Prompts the user to enter a collection of doubles
var collection = ConsolePrompt.GetCollection("Enter a collection of doubles");
ICollection<T> ConsolePrompt.GetCollectionEnum<T>(string message, string messageIndividual = "")
  • Prompts the user to enter a collection of enums
var collection = ConsolePrompt.GetCollectionEnum<MyEnum>("Enter a collection of enums");

Exit

ProcessExitExtensions.ConfigureProcessExitEvent(ProcessExitSettings settings)

  • Configures the process to display run time & error messages on exit
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on TJC.ConsoleApplication:

Package Downloads
TJC.Collection.Console

Collection of NuGet Packages for Console Applications

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.14.3 167 11/15/2024
0.14.2 160 11/10/2024
0.14.1 95 11/5/2024
0.14.0 161 11/5/2024
0.13.1 292 10/27/2024
0.13.0 112 10/26/2024
0.12.1 77 10/26/2024
0.12.0 116 10/26/2024
0.11.0 70 10/26/2024
0.10.0 69 10/25/2024
0.8.0 113 10/20/2024
0.7.5 144 10/20/2024
0.7.4 82 10/20/2024
0.7.3 82 10/20/2024
0.7.2 148 10/16/2024
0.7.1 112 10/13/2024
0.7.0 115 10/11/2024
0.6.0 81 10/11/2024
0.5.0 82 10/11/2024
0.4.0 139 10/10/2024
0.3.0 139 10/9/2024
0.2.0 85 10/8/2024
0.1.27 145 10/6/2024
0.1.26 146 10/2/2024
0.1.25 144 9/29/2024
0.1.24 173 9/26/2024
0.1.23 179 9/9/2024
0.1.22 103 9/9/2024
0.1.21 129 9/6/2024
0.1.20 192 9/5/2024
0.1.19 101 9/5/2024
0.1.18 224 9/5/2024
0.1.17 100 9/5/2024
0.1.16 104 9/5/2024
0.1.15 150 9/4/2024
0.1.14 102 9/4/2024
0.1.13 94 9/3/2024
0.1.12 247 8/22/2024
0.1.11 130 8/22/2024
0.1.10 162 7/25/2024
0.1.9 92 7/24/2024
0.1.8 115 7/22/2024
0.1.7 108 7/22/2024
0.1.6 113 7/21/2024
0.1.5 103 7/21/2024
0.1.4 113 7/21/2024
0.1.3 105 7/21/2024
0.1.2 119 7/21/2024
0.1.1 112 7/21/2024
0.1.0 113 7/21/2024