C#的System.CommandLine怎么使用

本篇内容主要讲解“C#的System.CommandLine怎么使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C#的System.CommandLine怎么使用”吧!

创新互联是一家专业从事网站设计、成都网站设计的网络公司。作为专业网站设计公司,创新互联依托的技术实力、以及多年的网站运营经验,为您提供专业的成都网站建设、成都全网营销推广及网站设计开发服务!

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
     --output output
     --version Display version information

可见,它能自动根据Main函数的参数自动解析出命令行的格式,并生成帮助文档。

接着,我们再来看看命令行的使用:

    ConsoleApp1 --input ii --output out
    Input: ii, Output: out

完美的进行了命令行的解析,它也可以读取xml注释,实现更加复杂的说明。

/// 
/// Converts an image file from one format to another.
/// 
/// The path to the image file that is to be converted.
/// The name of the output from the conversion.
/// The x dimension size to crop the picture. The default is 0 indicating no cropping is required.
/// The x dimension size to crop the picture. The default is 0 indicating no cropping is required.
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 The path to the image file that is to be converted.
     --output The name of the output from the conversion.
     --x-crop-size The x dimension size to crop the picture. The default is 0 indicating no cropping is required.
     --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虽然也支持像传统命令行的库那样编写复杂的命令行支持程序。

到此,相信大家对“C#的System.CommandLine怎么使用”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!


当前文章:C#的System.CommandLine怎么使用
网址分享:http://bzwzjz.com/article/jpdjch.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 成都网站制作 网站制作 成都网站建设推广 网站制作 重庆手机网站建设 品牌网站建设 重庆网站建设 app网站建设 成都网站建设 手机网站建设套餐 广安网站设计 成都定制网站建设 营销网站建设 高端品牌网站建设 专业网站建设 响应式网站建设 成都网站建设 网站建设方案 成都企业网站制作 成都网站制作 成都响应式网站建设 成都网站设计