ConsoleEssentials 1.2.3

dotnet add package ConsoleEssentials --version 1.2.3
NuGet\Install-Package ConsoleEssentials -Version 1.2.3
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="ConsoleEssentials" Version="1.2.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ConsoleEssentials --version 1.2.3
#r "nuget: ConsoleEssentials, 1.2.3"
#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 ConsoleEssentials as a Cake Addin
#addin nuget:?package=ConsoleEssentials&version=1.2.3

// Install ConsoleEssentials as a Cake Tool
#tool nuget:?package=ConsoleEssentials&version=1.2.3

ConsoleEssentials

Synopsis

This .NET library seeks to streamline the process of writing console applications. It's lightweight and facilitates handling of the most tedious tasks when creating a console application.

Features

This is the list of current features

  • Logging to console and/or file.
  • Customizable log location and file format.
  • Parse arguments.
  • Verify arguments.
  • Easy extraction of parameter values.
  • Available for all major versions of .NET.
  • Available for some versions of .NET Core.
    • 2.0, 2.1 and 2.2.

More features will be added along the way as they are needed. Please use the issues part of GitHub to submit ideas.

Motivation

Through the course of a day at my work, I often come across some small task that requires automation or to create a simple POC of something new that can be isolated completely and tested separately. I had several "GenericConsole" projects lying around where stuff would be bunched together, or I would have 100 small projects that had 30% redundant code in it. This usually was all of the parameter parsing and logging in it, and every project would be a little bit different than the one before. So I finally decided to create a NuGet package for it, so that when ever I would update it, ALL of my previous projects would almost instantly benefit from that update.

Installation

The easiest way to install it is to find it on the NuGet market place, but here's some alternatives.

Package Manager

PM> Install-Package ConsoleEssentials -Version 1.2.3

.NET CLI

dotnet add package ConsoleEssentials --version 1.2.3

Paket CLI

paket add ConsoleEssentials --version 1.2.3

Example Code

The example below showcases the parsing of parameters (arguments/args) and some simple loggin to the default location.

using System.Collections;
using ConsoleEssentials;

// Required parameters
private static string REQ_PARAM1 = "RequiredParam1";
private static string REQ_PARAM2 = "RequiredParam2";

// Optional parameters
private static string OPTIONAL_PARAM = "OptionalParam";
private static string OPTIONAL_SWITCH = "Switch";

private static readonly string[] RequiredParameters = { REQ_PARAM1, REQ_PARAM2 };
private static Hashtable m_Parameters;

public static void Main(string[] args)
{
	// Parse the args to a Hashtable
	m_Parameters = Arguments.GetOptions(args);
	string[] missingParameters = m_Parameters.CheckOptions(RequiredParameters);
	if (missingParameters != null && missingParameters.Length > 0)
	{
		// Linq aggregate used for demo, won't work on all versions on .Net
		Log.Error($"Missing these required options: {missingParameters.Aggregate((i, j) => $"{i}, {j}")}");
		return;
	}
	
	// Use "GetOptionString" if you know it's there.
	string RequiredParam1Value = m_Parameters.GetOptionString(REQ_PARAM1);
	string RequiredParam2Value = m_Parameters.GetOptionString(REQ_PARAM2);

	// Use "GetOptionStringIfNotNull" if its an optional string parameter. The first parameter of this method is the default value.
	string OptionalParamValue = m_Parameters.GetOptionStringIfNotNull(null, OPTIONAL_PARAM);

	// Use "GetOptionSwitch" to set a bool to if the switch is set or not.
	bool OptionalSwitchValue = m_Parameters.GetOptionSwitch(OPTIONAL_SWITCH);

	// Use "GetMainOption" to get the option which has no name. This option can't follow a switch type.
	string mainParameterValue = m_Parameters.GetMainOption();
	if (!string.IsNullOrEmpty(mainParameterValue))
		Log.Error(mainParameterValue);

	Log.Information(RequiredParam1Value);
	Log.Warning(RequiredParam2Value);

	if (OptionalSwitchValue)
	{
		if (!string.IsNullOrEmpty(OptionalParamValue))
			Log.Critical(OptionalParamValue);
		else
			Log.Information("No optional value supllied!");
	}
	
	// Your code that utilizes the values
}

To run the console application we need to parse the arguments like these examples:

Input

Program.exe "This is the main param" -RequiredParam1 "This is the first param value" -RequiredParam2 "C:\Temp files\lala" -OptionalParam "Here's the optional param" -Switch

Output

25-Apr-18 15:34:35 - ERROR - This is the main param
25-Apr-18 15:34:35 - INFO  - This is the first param value
25-Apr-18 15:34:35 - WARN  - C:\Temp files\lala
25-Apr-18 15:34:35 - CRIT  - Here's the optional param

Input

Program.exe -RequiredParam1 "This is the first param value" -RequiredParam2 "C:\Temp files\lala" -Switch

Output

25-Apr-18 15:33:15 - INFO  - This is the first param value
25-Apr-18 15:33:15 - WARN  - C:\Temp files\lala
25-Apr-18 15:33:15 - INFO  - No optional value supllied!

Input

Program.exe -RequiredParam1 "This is the first param value" -RequiredParam2 "C:\Temp files\lala" -OptionalParam "Here's the optional param" -Switch

Output

25-Apr-18 15:34:35 - INFO  - This is the first param value
25-Apr-18 15:34:35 - WARN  - C:\Temp files\lala
25-Apr-18 15:34:35 - CRIT  - Here's the optional param

Contribute

The easiest way to contribut is to make pull requests to the repository along with a description of what the commit contains. Alternatively write an issue ticket here.

License

The MIT License (MIT) @ Christian Schubert

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 is compatible.  netcoreapp2.1 is compatible.  netcoreapp2.2 is compatible.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Framework net20 is compatible.  net30 is compatible.  net35 is compatible.  net40 is compatible.  net403 was computed.  net45 is compatible.  net451 is compatible.  net452 is compatible.  net46 is compatible.  net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 is compatible.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 2.0

    • No dependencies.
  • .NETCoreApp 2.1

    • No dependencies.
  • .NETCoreApp 2.2

    • No dependencies.
  • .NETFramework 2.0

    • No dependencies.
  • .NETFramework 3.0

    • No dependencies.
  • .NETFramework 3.5

    • No dependencies.
  • .NETFramework 4.0

    • No dependencies.
  • .NETFramework 4.5

    • No dependencies.
  • .NETFramework 4.5.1

    • No dependencies.
  • .NETFramework 4.5.2

    • No dependencies.
  • .NETFramework 4.6

    • No dependencies.
  • .NETFramework 4.6.1

    • No dependencies.
  • .NETFramework 4.7.1

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.3 961 1/16/2020
1.1.0 1,139 4/19/2018
1.0.6 1,039 4/19/2018

Added support for .NET 4.7.1 and .Net Core 2.0, 2.1 and 2.2. Added support for first no-name argument. Use GetMainOption.