ConsoleApplicationBase
No description available
Install / Use
/learn @TypecastException/ConsoleApplicationBaseREADME
A Useful, Interactive, and Extensible .NET Console Application Template for Development and Testing
This application can serve as a template of framework into which you can easily plug in commands related tothe code you want to demo, excercise, or which forms the business layer of an actual Console application. The "Interactive" part of this Console is already in place - all you need to do is plug command classes and methods into the Commands namespace and you're ready to go.
The goal here was not to emulate a fully-functioning Shell or terminal. I wanted to be able to:
- Run the program, be greeted with a prompt (customizable, in this case), and then enter commands corresponding to various methods defined in a specific area of the application.
- Receive feedback (where appropriate), error messages, and such
- Easily add/remove commands
- Generally be able to quickly put together a functioning console application, with predictable and familiar interactive behavior, without re-inventing the wheel every time.
For more detailed information about how this works, see the Blog post at: C#: Building a Useful, Extensible .NET Console Application Template for Development and Testing
Assumptions
As it is currently configured, this application makes a few assumptions about architecture. You can easily adapt things to suit your purposes, but out-of-the-box, the following "rules" are assumed:
- Methods representing Console commands will be defined as
public staticmethods which always return astring, and are defined onpublic staticclasses. - Classes containing methods representing Console commands will be located in the
Commandsnamespace, and in the Commands folder. - There will always be a static class named
DefaultCommandsfrom which methods may be invoked from the Console directly by name. For many console projects, this will likely be sufficient. - Commands defined on classes other than DefaultCommands will be invoked from the console using the familiar dot syntax: ClassName.CommandName.
Defining Commands
If you were to define the following (trival example-style) commands in your DefaultCommands class, you will be able to execute these from the Console when you run the application. The DefaultCommands class must be present in the project, and must be within the Commands namespace (note that the methods must be static in order to be available to the console as commands, and the project assumes a string return type),
public static string DoSomething(int id, string data)
{
return string.Format(ConsoleFormatting.Indent(2) +
"I did something to the record Id {0} and saved the data '{1}'", id, data);
}
public static string DoSomethingElse(DateTime date)
{
return string.Format(ConsoleFormatting.Indent(2) + "I did something else on {0}", date);
}
public static string DoSomethingOptional(int id, string data = "No Data Provided")
{
var result = string.Format(ConsoleFormatting.Indent(2) +
"I did something to the record Id {0} and saved the data {1}", id, data);
if(data == "No Data Provided")
{
result = string.Format(ConsoleFormatting.Indent(2) +
"I did something to the record Id {0} but the optinal parameter "
+ "was not provided, so I saved the value '{1}'", id, data);
}
return result;
}
Executing Commands
The commands above can be executed when you run the application with the following syntax:
Execute the DoSomething command:
console> DoSomething 55 "My Data"
Execute the DoSomethingElse command:
console> DoSomethingElse 7/4/2014
The console recognizes and deals with optional method parameters.
Execute the DoSomethingOptional command inluding optional parameters:
console> DoSomethingOptional 212 "This is my optional data"
OR, you could omit the last argument, and the default value defined on the method will be used:
console> DoSomethingOptional 212
I'm happy to take pull requests, suggestions, and ideas.
Related Skills
node-connect
345.4kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
104.6kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
345.4kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
345.4kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
