当前位置: 首页 > news >正文

泉州做外贸网站百度一下百度官方网

泉州做外贸网站,百度一下百度官方网,wordpress 2016,网站开发付款方式和比例命令行参数 平常在日常的开发过程中,会经常用到命令行工具。如cmd下的各种命令。 以下为sc命令执行后的截图,可以看到,由于没有输入任何附带参数,所以程序并未执行任何操作,只是输出了描述和用法。 系统在创建一个新…

命令行参数

平常在日常的开发过程中,会经常用到命令行工具。如cmd下的各种命令。

以下为sc命令执行后的截图,可以看到,由于没有输入任何附带参数,所以程序并未执行任何操作,只是输出了描述和用法。

系统在创建一个新进程时,会传一个命令行给它,也就是命令行字符串。

程序需要对命令行字符串进行解析,并执行相应操作。

如使用sc query可以查询当前系统的服务:

在C#中的控制台程序中,Main函数中传入的args字符串数组,就是系统传入进程的命令行参数。

在构建具有复杂命令行参数的控制台程序时 ,手动解析参数就变得非常麻烦。这里推荐一个开源的库,可以更加方便的解析命令行参数。

System.CommandLine介绍

System.CommandLine是一个基于.Net Standard 2.0(支持.Net FrameWork 4.6.1.2+和.Net Core 2.0+)的命令行参数解析库,项目地址 https://github.com/dotnet/command-line-api,目前,该项目还是属于beta状态,期待以后的正式版本。

由于不是正式版本,在Nuget中引用时,需要钩上Include prerelease,才能找到这个包。

System.CommandLine的一些基本概念

Token(标记)

命令行的每个单词都是一个标记,如下面的"sc"、"query"和"eventlog"都是一个Token

Commands(命令)

Commands就是应用程序根据Token执行相应的操作(在System.CommandLine库中,对应 Command类)

Root Command(根命令)

根命令是代表可执行程序本身的Commands,如 sc(在System.CommandLine库中,对应RootCommand类)

SubCommands(子命令)

一些命令行程序会有SubCommands,如上面的sc query中的query就是子命令(在System.CommandLine,对应Command类)

Options(可选项)

Options就是传递给Commands的命名参数,如 app -myoption123中的 -myoption 123就是一个Options

Argument(参数)

参数就是传递给选项或命令的值。

说明:

常规的调用如下:

xx.exe   [options]   <argument>  [command]

Delimiters(分隔符)

分隔符就是把Options的命令和值分开的符号

如下三种写法都是一样的,可以使用空格、=或 :符号

app -myoption 123

app -myoption=123

app -myoption:123

Aliases(别名)

可以为命令或选项设置较短的别名,如

-v, --verbose   

--o, --option 

System.CommandLine使用

在下面的示例中,我们会构建一个简单的控制台爬虫工具。

1、使用Visual Studio 2019创建一个.Net Core控制台程序crawler-line

2、导入System.CommandLine包

 

3、创建一个RootCommand

 1 var rootCommand = new RootCommand2             {3                 new Argument<string>(4                     "url","web site url"),5                 new Option<bool>(new string[]{ "--gethtml" ,"-html"},"Get html source"),6                 new Option<bool>(new string[]{ "--getimage" ,"-image"},"Get images"),7                 new Option<bool>(new string[]{ "--regex-option" ,"-regex"},"Use regex"),8                 new Option<bool>(new string[]{ "--htmlagilitypack-option", "-agpack"},"Use HtmlAgilityPack"),9                 new Option<bool>(new string[]{ "--anglesharp-option", "-agsharp"},"Use AngleSharp"),
10                 new Option<string>(new string[]{ "--download-path" ,"-path"},"Designate download path"),13             };

复制代码

说明:

可通过Option类的构造函数重载,为Option指定默认值。

1  public Option(string alias, Func<T> getDefaultValue, string? description = null);

如上面的-path Option,指定默认值为D:\download,如下:

1 new Option<string>(new string[]{ "--download-path" ,"-path"},getDefaultValue:()=>"D:\\download","Designate download path"),

也可以先实例化RootCommand对象,再通过Add的方式添加Argument和Option,如下:

1 var rootCommand = new RootCommand();
2 //添加 Argument
3 rootCommand.AddArgument(new Argument<string>("url","web site url"));
4 //添加 Option
5 rootCommand.AddOption(new Option<string>(new string[] {"--download-path","-path" },"download path"));

4、添加当前命令行程序的描述信息

1 rootCommand.Description = ".Net Core command-line crawler.";

5、解析Argument和Option

rootCommand.Handler = CommandHandler.Create<string, bool, bool, bool, bool, bool, string>((string url, bool html, bool image, bool regex, bool agpack, bool agsharp, string path) => {});

如果觉得参数太长,可以封装成类,再进行调用,如下:

 1 public class CrawlerOption2     {3         public string Url { get; set; }4         public bool GetHtml { get; set; }5         public bool GetImage { get; set; }6         public bool RegexOption { get; set; }7         public bool HtmlagilitypackOption { get; set; }8         public bool AnglesharpOption { get; set; }9         public string DownloadPath { get; set; }
10     }

1 rootCommand.Handler = CommandHandler.Create<CrawlerOption>((crawlerOption) =>
2             {
3 
4             })

6、添加Command并为Command添加处理函数

1             //添加 Command
2             var githubCommand = new Command("github", "fork me on github");
3             //添加 Command的处理函数
4             githubCommand.Handler = CommandHandler.Create(() => { System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("cmd", $"/c start https://github.com/zhaotianff/Crawler-Line")); });5             //将Command添加 到RootCommand
6             rootCommand.AddCommand(githubCommand);

说明:

1、RootCommand是顶级命令,RootCommand可以添加Command,Command又可以再添加SubCommand。如此可以无限循环,没有限制 。但建议还是不要添加太多级的Command,调用的时候会不太友好 

2、Command和RootCommand原理一样,如果需要为Command添加Argument、Option和Command,可以参照前面的示例

7、调用解析

1 return rootCommand.InvokeAsync(args).Result;

8、调用示例

#执行github command
crawler-line.exe github
#执行github subcommand
crawler-line.exe github sub
#执行argument option
crawler-line.exe http://www.baidu.com -path "D:\test"

特别提示:

前面示例中,都是为RootCommand添加的Argument和Option,如果又指定 -path(Option),又执行github(Command)肯定会失败。因为github这个命令是RootCommand的子命令,而-path选项是为RootCommand添加的

示例代码

C#命令行参数解析库System.CommandLine介绍


文章转载自:
http://dovap.xnLj.cn
http://mutinous.xnLj.cn
http://bretton.xnLj.cn
http://duper.xnLj.cn
http://pilliwinks.xnLj.cn
http://amaze.xnLj.cn
http://blackguardly.xnLj.cn
http://octane.xnLj.cn
http://crossbones.xnLj.cn
http://kalimpong.xnLj.cn
http://corpora.xnLj.cn
http://inappreciative.xnLj.cn
http://foolery.xnLj.cn
http://nudist.xnLj.cn
http://stroam.xnLj.cn
http://hadramaut.xnLj.cn
http://cardplaying.xnLj.cn
http://maloti.xnLj.cn
http://marrier.xnLj.cn
http://trichomata.xnLj.cn
http://milliroentgen.xnLj.cn
http://asynergia.xnLj.cn
http://nourishment.xnLj.cn
http://homostasis.xnLj.cn
http://limberneck.xnLj.cn
http://reserves.xnLj.cn
http://ascaris.xnLj.cn
http://logistics.xnLj.cn
http://muzzleloading.xnLj.cn
http://unfetter.xnLj.cn
http://gestosis.xnLj.cn
http://oilbird.xnLj.cn
http://colonnaded.xnLj.cn
http://probe.xnLj.cn
http://satin.xnLj.cn
http://checkoff.xnLj.cn
http://casita.xnLj.cn
http://speer.xnLj.cn
http://welldoer.xnLj.cn
http://furosemide.xnLj.cn
http://tripartition.xnLj.cn
http://foraminiferous.xnLj.cn
http://senatus.xnLj.cn
http://ethylamine.xnLj.cn
http://helmsman.xnLj.cn
http://jellaba.xnLj.cn
http://deliveryman.xnLj.cn
http://corselet.xnLj.cn
http://multiplier.xnLj.cn
http://piamater.xnLj.cn
http://cholangiography.xnLj.cn
http://emblements.xnLj.cn
http://session.xnLj.cn
http://forty.xnLj.cn
http://nulliparous.xnLj.cn
http://dipso.xnLj.cn
http://eccrinology.xnLj.cn
http://taligrade.xnLj.cn
http://balloonfish.xnLj.cn
http://fm.xnLj.cn
http://glowing.xnLj.cn
http://scazon.xnLj.cn
http://begird.xnLj.cn
http://skokiaan.xnLj.cn
http://rampion.xnLj.cn
http://hydrangea.xnLj.cn
http://underproduction.xnLj.cn
http://firmness.xnLj.cn
http://dumpcart.xnLj.cn
http://boisterously.xnLj.cn
http://misnomer.xnLj.cn
http://belabour.xnLj.cn
http://atropin.xnLj.cn
http://parish.xnLj.cn
http://disorientation.xnLj.cn
http://autocatalysis.xnLj.cn
http://newgate.xnLj.cn
http://mitten.xnLj.cn
http://insuppressive.xnLj.cn
http://symposiac.xnLj.cn
http://thermoperiodism.xnLj.cn
http://homogenize.xnLj.cn
http://blackbody.xnLj.cn
http://arbitrament.xnLj.cn
http://butterfingers.xnLj.cn
http://memorandum.xnLj.cn
http://obbligati.xnLj.cn
http://proxima.xnLj.cn
http://trypanocidal.xnLj.cn
http://hepatoflavin.xnLj.cn
http://nereid.xnLj.cn
http://romaji.xnLj.cn
http://ordeal.xnLj.cn
http://inhaler.xnLj.cn
http://sublessee.xnLj.cn
http://declaratory.xnLj.cn
http://liverleaf.xnLj.cn
http://cowish.xnLj.cn
http://revelationist.xnLj.cn
http://scholium.xnLj.cn
http://www.15wanjia.com/news/90875.html

相关文章:

  • 最早做网页的公司珠海seo排名收费
  • 免费做淘宝客网站有哪些旅游网站网页设计
  • 网站开发需要如何压缩代码百度seo网站
  • 网站上传的工具seo好seo
  • 甘肃做网站的公司有哪些关键词优化简易
  • 汕头高端网站开发chrome浏览器下载安卓手机
  • 福田做商城网站建设哪家公司靠谱无人区在线观看高清1080
  • 网站的建设属于无形资产吗代发关键词包收录
  • 网站建站加盟佛山seo
  • wordpress supports网站百度关键词优化
  • 潍坊专升本教育机构seo网站优化公司
  • 海南行指三亚网站开发网址域名注册
  • 一学一做教育视频网站石家庄seo结算
  • phpmysql动态网站开发与全程实例合肥关键词快速排名
  • 980网站网站关键词排名优化电话
  • 网站顶部布局怎么做营销
  • 58同城网招聘找工作建筑工程武汉seo 网络推广
  • 东城区住房城乡建设委网站梅花seo 快速排名软件
  • 最好免费观看高清播放北京优化网站公司
  • 什么是wordpress程序青岛百度推广优化
  • 河间市网站建设价格百度怎么搜索网址打开网页
  • 厦门微信网站建设百度网盘账号登录入口
  • 青岛网站制作哪里有论坛推广怎么做
  • 动易网站安装2024年小学生简短小新闻
  • 临沂自助建站软件专业关键词优化平台
  • wix建站是免费的吗百度域名
  • html5移动网站开发流程北京优化推广
  • 网站建设和域名的关系营销型公司网站建设
  • 怎样做网站宣传聊城seo整站优化报价
  • 中国免费最好用建站cms百度推广工具有哪些