網站首頁 編程語言 正文
一直以來,當我們想讓我們的控制臺程序支持命令行啟動時,往往需要編寫大量代碼來實現這一看起來很簡單的功能。雖然有一些庫可以簡化一些操作,但整個過程仍然是一個相當枯燥而乏味的過程。
今天,我這里要介紹一個新的命令行庫:System.CommandLine,通過他我們可以幾乎無需任何額外的編碼就可以獲得命令行的支持,它能大幅減少程序員花在提供命令行API(CLI)上的時間,改善CLI程序用戶的體驗,讓開發者能專注于編寫應用程序。
目前這個庫還是預覽版本,要體驗的話需要可以使用如下庫:System.CommandLine.DragonFruit。首先以一個簡單的示例來演示它的功能。
static void Main(string input, string output)
{
Console.WriteLine($"Input: {input}, Output: {output}");
}
這里我們并沒有要顯式使用這個庫,只需要將Main函數的入參改成我們需要使用的類型,程序便自動實現了命令行的支持。我們甚至可以用—help查看程序的命令行的配置方式
????ConsoleApp1.exe --help
????Usage:
???? ConsoleApp1 [options]
????Options:
???? --input <INPUT> input
???? --output <OUTPUT> output
???? --version Display version information
可見,它能自動根據Main函數的參數自動解析出命令行的格式,并生成幫助文檔。
接著,我們再來看看命令行的使用:
????ConsoleApp1 --input ii --output out
????Input: ii, Output: out
完美的進行了命令行的解析,它也可以讀取xml注釋,實現更加復雜的說明。
/// <summary>
/// Converts an image file from one format to another.
/// </summary>
/// <param name="input">The path to the image file that is to be converted.</param>
/// <param name="output">The name of the output from the conversion.</param>
/// <param name="xCropSize">The x dimension size to crop the picture. The default is 0 indicating no cropping is required.</param>
/// <param name="yCropSize">The x dimension size to crop the picture. The default is 0 indicating no cropping is required.</param>
static void Main(string input, string output, int xCropSize = 0, int yCropSize = 0)
{
}
生成的幫助輸出效果如下:
????ConsoleApp1:
???? Converts an image file from one format to another.
????Usage:
???? ConsoleApp1 [options]
????Options:
???? --input <INPUT> The path to the image file that is to be converted.
???? --output <OUTPUT> The name of the output from the conversion.
???? --x-crop-size <X-CROP-SIZE> The x dimension size to crop the picture. The default is 0 indicating no cropping is required.
???? --y-crop-size <Y-CROP-SIZE> The x dimension size to crop the picture. The default is 0 indicating no cropping is required.
???? --version Display version information
相比傳統的命令行庫,這個庫的優勢非常明顯,我們可以幾乎不編寫任何代碼就可以獲得命令行程序的支持。對于復雜的命令行程序來說,可能這里的方式并不能滿足需求。System.CommandLine雖然也支持像傳統命令行的庫那樣編寫復雜的命令行支持程序,但這不在本文的介紹范圍內。感興趣的朋友可以看一下參考文章的內容。
參考文章:
使用 System.CommandLine 分析命令行
command-line-api
原文鏈接:https://www.cnblogs.com/TianFang/p/10618573.html
相關推薦
- 2022-08-21 golang協程與線程區別簡要介紹_Golang
- 2022-04-02 ?Python錯誤與異常處理_python
- 2022-12-06 React自定義視頻全屏按鈕實現全屏功能_React
- 2022-05-31 如何用python合并多個有規則命名的nc文件_python
- 2023-02-07 Pytorch中torch.cat()函數的使用及說明_python
- 2022-09-04 django連接數據庫獲取數據的簡單步驟記錄_python
- 2023-11-20 數據處理matlibplot繪圖顏色對照表
- 2022-07-09 C語言堆與二叉樹的順序結構與實現_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支