CmdLineNet 0.5.0

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

// Install CmdLineNet as a Cake Tool
#tool nuget:?package=CmdLineNet&version=0.5.0                

CmdLine

A helper library used to parse command line arguments, at a very basic level. Aiming to add a source generator as well.

Usage

Note that because this package is currently not at 1.0 yet, this may change. Below is some example code.

public sealed record class MoveArgs(string Source, string Dest, bool Overwrite) : ICmdParseable<MoveArgs.Id, MoveArgs>
{
	public enum Id { Source, Dest, Overwrite }
	private static readonly ArgsReader<Id> reader = new ArgsReaderBuilder<Id>()
		.Option(Id.Source, 's', "source", 1, 1, "The source file to move")
		.Option(Id.Dest, 'd', "dest", 1, 1, "The destination to move the file to")
		.Switch(Id.Overwrite, 'o', "overwrite", 1, 1, "Whether or not to allow overwriting the destination file")
		.Build();
	public static ArgsReader<Id> GetReader() { return reader; }
	public static ParseResult<MoveArgs> Parse(IEnumerable<RawArg<Id>> args)
	{
		string? source = null, dest = null;
		bool overwrite = false;
		foreach (RawArg<Id> a in args)
		{
			if (!a.Ok) return a.Content;
			switch (a.Id)
			{
				case Id.Source:
					source = a.Content;
					break;
				case Id.Dest:
					dest = a.Content;
					break;
				case Id.Overwrite:
					overwrite = true;
					break;
			}
		}
		if (source == null) return "Missing required option: -s|--source";
		if (dest == null) return "Missing required option: -d|--dest";
		return new MoveArgs(source, dest, overwrite);
	}
}
public sealed record class NewArgs(string Name) : ICmdParseable<NewArgs.Id, NewArgs>
{
	public enum Id { Name }
	private static readonly ArgsReader<Id> reader = new ArgsReaderBuilder<Id>()
		.Value(Id.Name, "Name", 1, 1, "The name of the new file")
		.Build();
	public static ArgsReader<Id> GetReader() { return reader; }
	public static ParseResult<NewArgs> Parse(IEnumerable<RawArg<Id>> args)
	{
		string? name = null;
		foreach (RawArg<Id> a in args)
		{
			if (!a.Ok) return a.Content;
			switch (a.Id)
			{
				case Id.Name:
					name = a.Content;
					break;
			}
		}
		if (name == null) return "Missing required value: Name";
		return new NewArgs(name);
	}
}
public static class Program
{
	const int errorBadVerb = 1;
	const int errorBadArgs = 2;
	const int errorOnNew = 3;
	const int errorOnMove = 4;
	public static int Main(string[] args)
	{
		DictionaryVerbHandler<int> verbHandler = new
		(
			allVerbs: [],
			unknownVerbHandler: DefaultDelegate.UnknownVerbHandler(returnValue: errorBadVerb)
		);

		verbHandler.AddVerb
		(
			name: "new",
			description: "Creates a new file",
			execute: DefaultDelegate.ExecuteOrErrorMessage<int, NewArgs.Id, NewArgs>(New, errorBadArgs),
			writeDetailedHelp: DefaultDelegate.WriteVerbDetailedHelp<NewArgs.Id, NewArgs>()
		);
		verbHandler.AddVerb
		(
			name: "move",
			description: "Moves a file",
			execute: DefaultDelegate.ExecuteOrErrorMessage<int, MoveArgs.Id, MoveArgs>(Move, errorBadArgs),
			writeDetailedHelp: DefaultDelegate.WriteVerbDetailedHelp<MoveArgs.Id, MoveArgs>()
		);
		verbHandler.AddHelpVerb
		(
			name: "help",
			description: "Displays general help or specific help for other verbs. Use \"help help\" for help on this verb",
			helpText: "To show general help, just type help. To show detailed help on a verb, type help <verb>.",
			returnValue: 0,
			writeGeneralHelp: DefaultDelegate.WriteVerbGeneralHelp<int>(),
			unknownVerbHelp: DefaultDelegate.UnknownVerbHelp
		);

		string? verb = args.FirstOrDefault();
		if (verb != null)
		{
			return verbHandler.HandleVerb(verb, args.Skip(1));
		}
		else
		{
			Console.WriteLine("Use \"help\" to get some help");
			return 0;
		}
	}
	private static int New(NewArgs args)
	{
		try
		{
			File.Create(args.Name);
			return 0;
		}
		catch (Exception ex)
		{
			Console.WriteLine(ex.ToString());
			return errorOnNew;
		}
	}
	private static int Move(MoveArgs args)
	{
		try
		{
			File.Move(args.Source, args.Dest, args.Overwrite);
			return 0;
		}
		catch (Exception ex)
		{
			Console.WriteLine(ex.ToString());
			return errorOnMove;
		}
	}
}

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  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 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.
  • net7.0

    • No dependencies.
  • net8.0

    • 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
0.5.1 105 7/8/2024
0.5.0 102 6/29/2024
0.4.0 215 2/27/2024
0.3.0 240 2/14/2024
0.2.0 422 5/8/2023
0.1.0 456 5/7/2023

- Verbs added
- DuplicatingValuesEnumerator now just returns the IDs intead of the entire struct containing the ID and Max properties
- HelpWriterSettings has more flexible alignment settings, and the default is assignable
- Writing help is improved; has better alignment, less allocation of strings, and can write to any TextWriter
- Default delegates added, and many delegates are explicitly defined instead of using Func/Action for clarity
- More documentation added