CShell 1.0.8

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

// Install CShell as a Cake Tool
#tool nuget:?package=CShell&version=1.0.8                

CShell

CShell creates a runtime environment to make it easy to create C# based shell style scripts. CShell is built on top of MedallionShell and runs great as a CSX on .NET Core giving you a great cross platform C# alternative to powershell and bash scripts.

There are 2 functionalities which CShell provides:

  • The concept of a current folder with relative commands for navigating and manipulating files and folders
  • The ability to smoothly invoke processes and pipe
  • Helpers to make it easy to work with the output of processes

By maintaining the concept of a current folder then all file and folder commands can be expressed using relative paths just like a normal shell.

You can just create a CShell directly and start working with it You can just create a CShell directly and start working with it

var shell = new CShell();

shell.CreateFolder("test");
shell.ChangeFolder("test");
foreach(var folder in shell.CurrentFolder.EnumerateDirectories()) 
{
	Console.WriteLine(folder.FullName);
}

Or you can derive a class and not need the shell. prefix at all

class MyScript: CShell()
{
	async Task Go()
	{
		CreateFolder("test");
		ChangeFolder("test");
		foreach(var folder in shell.CurrentFolder.EnumerateDirectories()) 
		{
			Console.WriteLine(folder.FullName);
		}
	}
}

CShell Process Commands

CShell is built using MedallionShell, which provides a great set of functionality for easily invoking processes and piping data between them. CShell adds on location awareness and helper methods to make it even easier to work with the output of processes.

To invoke a process you simply call .Run(). You can chain processes together using .RedirectTo(), pipe '|' or greater than '>'.

class MyScript: CShell()
{
	async Task Go()
	{
		// Invoke multiple commands using fluent style
		CommandResult result3 = await Run("cmd1", "args1")
			.RedirectTo("cmd2", "args2", "args3")
			.RedirectTo("cmd3", "args4")
			.AsResult();

		// we can even chain commands together with the pipe operator
		CommandResult result = await Run("cmd1", "args1") 
			| Run("cmd2", "args2", "args3") 
			| Run("cmd3", "args4")
			.AsResult();

		// we can even chain commands together with the > operator
		CommandResult result2 = await Run("cmd1", "args1") 
 			> Run("cmd2", "args2", "args3")
			> Run("cmd3", "args4")
			.AsResult();
	}
}

The CommandResult object has StandardOutput, StandardError information for further processing.

Working with typed results

Medallion makes it wasy to work with processes, but CShell adds on helper methods to make it even easier to work with the result of a command chain.

You can easily change the previous example directly into a typed object in one command:

class MyScript: CShell()
{
	async Task Go()
	{
		// get the result as dynamic object
		dynamic jsonResult  = await Run("cmd1", "args1")
			.RedirectTo("cmd2", "args2", "args3")
			.RedirectTo("cmd3", "args4")
			.ToJson();

		// get the json result as a typed object
		MyObject myObject  = await Run("cmd1", "args1")
			.RedirectTo("cmd2", "args2", "args3")
			.RedirectTo("cmd3", "args4")
			.ToJson<MyObject>();
	}
}
Working with files

CShell has ReadFile() and AsFile() extension methods which allow you to "start" the process chain from a file, or to end with it in a file.

class MyScript: CShell()
{
	async Task Go()
	{
		// start with text file and pass into cmd2, then write the final stdout and stderr and return a CommandResult
		var result  = await ReadFile("myfile.txt")
			.RedirectTo("cmd2", "args2", "args3")
			.RedirectTo("cmd3", "args4")
			.AsFile("stdout.txt", "stderr.txt");
	}
}
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 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.4.0 335 8/17/2024
1.3.0 554 3/24/2023
1.2.4 7,778 9/4/2020
1.2.3 405 9/4/2020
1.2.2 422 9/4/2020
1.1.0 1,190 9/28/2018
1.0.11 738 9/28/2018
1.0.10 728 9/27/2018
1.0.9 737 9/27/2018
1.0.8 717 9/27/2018
1.0.7 727 9/27/2018