TJC.ConsoleApplication
0.13.0
See the version list below for details.
dotnet add package TJC.ConsoleApplication --version 0.13.0
NuGet\Install-Package TJC.ConsoleApplication -Version 0.13.0
<PackageReference Include="TJC.ConsoleApplication" Version="0.13.0" />
paket add TJC.ConsoleApplication --version 0.13.0
#r "nuget: TJC.ConsoleApplication, 0.13.0"
// Install TJC.ConsoleApplication as a Cake Addin #addin nuget:?package=TJC.ConsoleApplication&version=0.13.0 // Install TJC.ConsoleApplication as a Cake Tool #tool nuget:?package=TJC.ConsoleApplication&version=0.13.0
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();
Header
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 | Versions 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. |
-
net8.0
- Humanizer (>= 2.14.1)
- NDesk.Options.Core (>= 1.2.8)
- TJC.AssemblyExtensions (>= 0.7.0)
- TJC.Singleton (>= 0.4.0)
- TJC.StringExtensions (>= 0.5.1)
- TJC.TimeExtensions (>= 0.3.0)
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 |
# 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.0] - 2024-10-26
### Added
- Release notes to the NuGet Package
## [0.12.2] - 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.0...HEAD
[0.13.0]: https://github.com/TJC-Tools/TJC.ConsoleApplication/compare/v0.12.2...v0.13.0
[0.12.2]: https://github.com/TJC-Tools/TJC.ConsoleApplication/compare/v0.12.0...v0.12.2
[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