TJC.ConsoleApplication 0.13.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package TJC.ConsoleApplication --version 0.13.1                
NuGet\Install-Package TJC.ConsoleApplication -Version 0.13.1                
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.13.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TJC.ConsoleApplication --version 0.13.1                
#r "nuget: TJC.ConsoleApplication, 0.13.1"                
#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.13.1

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

GitHub Tag GitHub Release NuGet Version

NuGet Downloads Size License

codecov

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 147 11/15/2024
0.14.2 152 11/10/2024
0.14.1 90 11/5/2024
0.14.0 155 11/5/2024
0.13.1 286 10/27/2024
0.13.0 110 10/26/2024
0.12.1 75 10/26/2024
0.12.0 114 10/26/2024
0.11.0 68 10/26/2024
0.10.0 67 10/25/2024
0.8.0 111 10/20/2024
0.7.5 142 10/20/2024
0.7.4 80 10/20/2024
0.7.3 80 10/20/2024
0.7.2 146 10/16/2024
0.7.1 110 10/13/2024
0.7.0 113 10/11/2024
0.6.0 79 10/11/2024
0.5.0 80 10/11/2024
0.4.0 137 10/10/2024
0.3.0 137 10/9/2024
0.2.0 83 10/8/2024
0.1.27 143 10/6/2024
0.1.26 144 10/2/2024
0.1.25 142 9/29/2024
0.1.24 171 9/26/2024
0.1.23 177 9/9/2024
0.1.22 101 9/9/2024
0.1.21 127 9/6/2024
0.1.20 190 9/5/2024
0.1.19 99 9/5/2024
0.1.18 222 9/5/2024
0.1.17 98 9/5/2024
0.1.16 102 9/5/2024
0.1.15 148 9/4/2024
0.1.14 100 9/4/2024
0.1.13 92 9/3/2024
0.1.12 245 8/22/2024
0.1.11 128 8/22/2024
0.1.10 160 7/25/2024
0.1.9 90 7/24/2024
0.1.8 113 7/22/2024
0.1.7 106 7/22/2024
0.1.6 111 7/21/2024
0.1.5 101 7/21/2024
0.1.4 111 7/21/2024
0.1.3 103 7/21/2024
0.1.2 117 7/21/2024
0.1.1 110 7/21/2024
0.1.0 111 7/21/2024

# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [0.13.1] - 2024-10-27

## [0.13.0] - 2024-10-26

### Added

- Release notes to the NuGet Package

## [0.12.1] - 2024-10-26

### Fixed

- Prototype formatting in help menu

## [0.12.0] - 2024-10-26

### Added

- Console argument with enum command class

## [0.11.0] - 2024-10-26

### Changed

- Refactored the `Argument` class
- Decoupled several classes

## [0.10.0] - 2024-10-25

### Added

- Enum argument

## [0.9.1] - 2024-10-24

## [0.9.0] - 2024-10-24

### Added

- Help argument singleton

## [0.8.0] - 2024-10-20

### Added

- Codecov reporting

## [0.7.5] - 2024-10-20

## [0.7.4] - 2024-10-20

## [0.7.3] - 2024-10-20

## [0.7.2] - 2024-10-16

## [0.7.1] - 2024-10-13

## [0.7.0] - 2024-10-11

## [0.6.0] - 2024-10-11

## [0.5.0] - 2024-10-11

## [0.4.0] - 2024-10-10

### Added

- Changelog Automation

## [0.3.0] - 2024-10-04

### Changed

- Changed setting classes into [singletons](https://github.com/TJC-Tools/TJC.Singleton)

## [0.2.0] - 2024-10-08

### Added

- Create a setting for the number of digits in the version display

[Unreleased]: https://github.com/TJC-Tools/TJC.ConsoleApplication/compare/v0.13.1...HEAD

[0.13.1]: https://github.com/TJC-Tools/TJC.ConsoleApplication/compare/v0.13.0...v0.13.1

[0.13.0]: https://github.com/TJC-Tools/TJC.ConsoleApplication/compare/v0.12.1...v0.13.0

[0.12.1]: https://github.com/TJC-Tools/TJC.ConsoleApplication/compare/v0.12.0...v0.12.1

[0.12.0]: https://github.com/TJC-Tools/TJC.ConsoleApplication/compare/v0.11.0...v0.12.0

[0.11.0]: https://github.com/TJC-Tools/TJC.ConsoleApplication/compare/v0.10.0...v0.11.0

[0.10.0]: https://github.com/TJC-Tools/TJC.ConsoleApplication/compare/v0.9.1...v0.10.0

[0.9.1]: https://github.com/TJC-Tools/TJC.ConsoleApplication/compare/v0.9.0...v0.9.1

[0.9.0]: https://github.com/TJC-Tools/TJC.ConsoleApplication/compare/v0.8.0...v0.9.0

[0.8.0]: https://github.com/TJC-Tools/TJC.ConsoleApplication/compare/v0.7.5...v0.8.0

[0.7.5]: https://github.com/TJC-Tools/TJC.ConsoleApplication/compare/v0.7.4...v0.7.5

[0.7.4]: https://github.com/TJC-Tools/TJC.ConsoleApplication/compare/v0.7.3...v0.7.4

[0.7.3]: https://github.com/TJC-Tools/TJC.ConsoleApplication/compare/v0.7.2...v0.7.3

[0.7.2]: https://github.com/TJC-Tools/TJC.ConsoleApplication/compare/v0.7.1...v0.7.2

[0.7.1]: https://github.com/TJC-Tools/TJC.ConsoleApplication/compare/v0.7.0...v0.7.1

[0.7.0]: https://github.com/TJC-Tools/TJC.ConsoleApplication/compare/v0.6.0...v0.7.0

[0.6.0]: https://github.com/TJC-Tools/TJC.ConsoleApplication/compare/v0.5.0...v0.6.0

[0.5.0]: https://github.com/TJC-Tools/TJC.ConsoleApplication/compare/v0.4.0...v0.5.0

[0.4.0]: https://github.com/TJC-Tools/TJC.ConsoleApplication/compare/v0.3.0...v0.4.0

[0.3.0]: https://github.com/TJC-Tools/TJC.ConsoleApplication/compare/v0.2.0...v0.3.0

[0.2.0]: https://github.com/TJC-Tools/TJC.ConsoleApplication/releases/tag/v0.2.0