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製作的圖標