ManyConsole
An extension to Mono.Options for writing console applications with multiple commands
Install / Use
/learn @fschwiet/ManyConsoleREADME
ManyConsole
Available on Nuget: http://nuget.org/List/Packages/ManyConsole
Thanks to Daniel González for providing some additional documentation: http://dgondotnet.blogspot.dk/2013/08/my-last-console-application-manyconsole.html
Mono.Options (formerly NDesk.Options) is a great library for processing command-line parameters. ManyConsole extends Mono.Options to allow building console applications that support separate commands.
If you are not familiar with Mono.Options, you should start by using that: https://components.xamarin.com/gettingstarted/mono.options?version=5.3.0. Add ManyConsole when you feel the urge to differentiate commands (you'll still need the Mono.Options usage).
ManyConsole provides a console interface for the user to list available commands, call and get help for each.
To use ManyConsole:
- Create a command line app referencing the ManyConsole nuget.
- Have Program.Main call ConsoleCommandDispatcher (see https://github.com/fschwiet/ManyConsole/blob/master/SampleConsole/Program.cs)
- You can use ConsoleCommandDispatcher to find commands in an assembly, or not.
- To add a command to your console application, inherit from ConsoleCommand.
- See the sample comands at https://github.com/fschwiet/ManyConsole/tree/master/SampleConsole
- Commands can be forced to show the user the help text by throwing an exception of type: ConsoleHelpAsException
- There are a handful of methods you can call from the derived class's constructor to add metadata to the command. Use autocompete to find them.
Quick Start Guide
Run this from NuGet Package Management Console:
Install-Package ManyConsole
Drop this in to automatically load all of the commands that we'll create next:
public class Program
{
public static int Main(string[] args)
{
var commands = GetCommands();
return ConsoleCommandDispatcher.DispatchCommand(commands, args, Console.Out);
}
public static IEnumerable<ConsoleCommand> GetCommands()
{
return ConsoleCommandDispatcher.FindCommandsInSameAssemblyAs(typeof(Program));
}
}
Create a command with one optional, one required and a short and long description:
public class PrintFileCommand : ConsoleCommand
{
private const int Success = 0;
private const int Failure = 2;
public string FileLocation { get; set; }
public bool StripCommaCharacter { get; set; }
public PrintFileCommand()
{
// Register the actual command with a simple (optional) description.
IsCommand("PrintFile", "Quick print utility.");
// Add a longer description for the help on that specific command.
HasLongDescription("This can be used to quickly read a file's contents " +
"while optionally stripping out the ',' character.");
// Required options/flags, append '=' to obtain the required value.
HasRequiredOption("f|file=", "The full path of the file.", p => FileLocation = p);
// Optional options/flags, append ':' to obtain an optional value, or null if not specified.
HasOption("s|strip:", "Strips ',' from the file before writing to output.",
t => StripCommaCharacter = t == null ? true : Convert.ToBoolean(t));
}
public override int Run(string[] remainingArguments)
{
try
{
var fileContents = File.ReadAllText(FileLocation);
if (StripCommaCharacter)
fileContents = fileContents.Replace(",", string.Empty);
Console.Out.WriteLine(fileContents);
return Success;
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
Console.Error.WriteLine(ex.StackTrace);
return Failure;
}
}
}
Ok, now when you run it, it should work:
>ManyConsoleDocumentation PrintFile -f "C:\HelloWorld.txt"
Extra parameters specified: C:\HelloWorld.txt
'PrintFile' - Quick print utility.
This can be used to quickly read a file's contents while optionally stripping out the ',' character.
Expected usage: ManyConsoleDocumentation.exe PrintFile <options>
<options> available:
-f, --file The full path of the file.
-s, --strip Strips ',' from the file before writing to
output.
It doesn't work and thinks we specified an invalid parameter. This is because options that are followed by a parameter must have an '=' symbol, so update the two commands with f|file= and s|strip=, it should now work:
>ManyConsoleDocumentation PrintFile -f "C:\HelloWorld.txt"
Executing PrintFile (Quick print utility.):
FileLocation : C:\HelloWorld.txt
StripCommaCharacter : False
Hello, world!
>ManyConsoleDocumentation PrintFile -f "C:\HelloWorld.txt" -s true
Executing PrintFile (Quick print utility.):
FileLocation : C:\HelloWorld.txt
StripCommaCharacter : True
Hello world!
Now you can easily supply multiple commands with their own set of unique arguments:
public class EchoCommand : ConsoleCommand
{
public string ToEcho { get; set; }
public EchoCommand()
{
IsCommand("Echo", "Echo's text");
HasRequiredOption("t|text=", "The text to echo back.", t => ToEcho = t);
}
public override int Run(string[] remainingArguments)
{
Console.Out.WriteLine(ToEcho);
return 0;
}
}
Here's how the help looks, plus help for our two commands:
>ManyConsoleDocumentation help
Available commands are:
Echo - Echo's text
PrintFile - Quick print utility.
help <name> - For help with one of the above commands
>ManyConsoleDocumentation help PrintFile
'PrintFile' - Quick print utility.
This can be used to quickly read a file's contents while optionally stripping out the ',' character.
Expected usage: ManyConsoleDocumentation.exe PrintFile <options>
<options> available:
-f, --file=VALUE The full path of the file.
-s, --strip=VALUE Strips ',' from the file before writing to
output.
>ManyConsoleDocumentation help Echo
'Echo' - Echo's text
Expected usage: ManyConsoleDocumentation.exe Echo <options>
<options> available:
-t, --text=VALUE The text to echo back.
Building the solution, running tests, and packaging the nuget file
- You will probably need to download the old copy of .NET this was built against: https://dotnet.microsoft.com/en-us/download/dotnet/2.2 (the last I built was with SDK 2.2.207)
- From a powershell window, run psake.ps1
- This will run the tasks defined in default.ps1, building the project, running the tests and putting together a nupkg file.
- The version number in default.ps1 needs to be updated when building a new nupkg file for release.
Related Skills
qqbot-channel
347.9kQQ 频道管理技能。查询频道列表、子频道、成员、发帖、公告、日程等操作。使用 qqbot_channel_api 工具代理 QQ 开放平台 HTTP 接口,自动处理 Token 鉴权。当用户需要查看频道、管理子频道、查询成员、发布帖子/公告/日程时使用。
docs-writer
100.2k`docs-writer` skill instructions As an expert technical writer and editor for the Gemini CLI project, you produce accurate, clear, and consistent documentation. When asked to write, edit, or revie
model-usage
347.9kUse CodexBar CLI local cost usage to summarize per-model usage for Codex or Claude, including the current (most recent) model or a full model breakdown. Trigger when asked for model-level usage/cost data from codexbar, or when you need a scriptable per-model summary from codexbar cost JSON.
arscontexta
2.9kClaude Code plugin that generates individualized knowledge systems from conversation. You describe how you think and work, have a conversation and get a complete second brain as markdown files you own.
