ConsoleAuto
1.0.0
自动化控制台参数解析和脚本自动化的库。
命令行解析通常以这种方式接近:您定义参数语法,使用命令行解析器库对其进行解析,然后将参数使用到applciation中。这种方法需要多个步骤,很难搭建。该库仅在一个步骤中崩溃:描述您的方法。
您只需要在某个地方声明一种方法并在其上添加注释即可自动进行。另一个有趣的方案是通过config yaml文件执行多个命令(类似于docker文件机制)。
请参阅Exampe了解如何简化您的控制台应用程序。
Install-Package ConsoleAuto
class Program
{
static void Main ( string [ ] args )
{
ConsoleAuto . Config ( args )
. LoadCommands ( )
. Run ( ) ;
}
[ ConsoleCommand ]
public void MyMethod ( string inputOne , int inputTwo )
{
//do stuff here
}
}该程序将是可用的运行:
Myprogram MyMethod --inputOne xxxx --inputTwo 23
Consolecommand注释的每种方法可用于调用。方法可以在班级中插入程序文件,无论是否静态。非静态类是使用DI创建的,并且可以在Coductunor中接收依赖项。默认命令是信息一个,它显示了用法信息(从命令定义自动生成)
public class IOCommands
{
public ConsoleService consoleService ;
public IOCommands ( ConsoleService consoleService )
{
this . consoleService = consoleService ;
}
[ ConsoleCommand ( isPublic : false , info : "provide this description" ) ]
public void WriteText ( string text , bool newline )
{
//do stuff here
}
} ConsoleAuto . Config ( args , servicecollection ) // pass servicecollection to use the same container of main application
. LoadCommands ( ) //Load all commands from entry assembly + base commands
. LoadCommands ( assembly ) //Load from a custom command
. LoadFromType ( typeof ( MyCommand ) ) //load a single command
. Register < MyService > ( ) // add a service di di container used in my commands
. Register < IMyService2 > ( new Service2 ( ) ) // add a service di di container used in my commands, with a custom implementation
. Default ( "MyDefaultCommand" ) //specify the default action if app starts without any command
. Configure ( config => {
//hack the config here
} )
. Run ( ) ; | 方法 | 笔记 |
|---|---|
| config | INF服务收集为无效,为DI创建了一个内部容器 |
| loadCommand | 加载来自入口汇编 +基本命令的所有命令,基于注释 |
| 登记 | 注册命令的依赖项。如果您在主应用程序上使用DI,则依赖项也将添加到主DI容器中。如果您使用的是DI,并且已经添加了依赖项,则无需注册。 |
| 配置 | 这揭示了结果配置。出于您的风险。 |
| 跑步 | 执行程序 |
| 默认 | 如果未指定命令,则执行命令。默认值是info命令。 |
| 范围 | 笔记 |
|---|---|
| 姓名 | 命令的名称(将用于从控制台启动它) |
| Ispublic | 如果是正确的,则可以在控制台信息上可见 |
| 模式 | 可以是按需 |
| 命令 | 非按需,beforecommand或aftercommand的执行订单。 deamand上的选项是默认的选项,也许是您通常的默认选项。除非用户从命令行运行它,否则命令无能为力。在模式之后和之前,每当您运行一个ondemand命令时运行命令。例如,“欢迎”或“ pres键关闭”是事件前后。 |
该工具可用于执行一组操作。在此模式下,您可以定义许多原子命令,然后使用外部文件定义序列。
设置文件myscript.yaml
Commands :
welcome_step1 :
Action : welcome
Desctiption : This is the line of text that will shown first
Args :
header : my text (first line)
welcome_step2 :
Action : welcome
Desctiption : In this example we do it twice, to prove we can execute commands multiple times with different args.
Args :
header : my text (second line)
main_step :
Action : CommandOne
Desctiption : This is a custom command that diplay the test. Yes, another dummy thing.
Args :
text : I'm the central command output!
State :
text : myglobal
MyExec.exe exec MyScript.yaml该库是根据MIT许可证发布的。
该软件是“原样”提供的,没有任何形式的明示或暗示保证,包括但不限于适销性,特定目的的适用性和非侵权的保证。在任何情况下,作者或版权持有人都不应对任何索赔,损害赔偿或其他责任责任,无论是在合同,侵权的诉讼中还是其他责任,是由软件,使用或与软件中的使用或其他交易有关的。
Eucalyp从Flaticon制作的图标