当前位置:网站首页>System. Commandline option
System. Commandline option
2022-06-12 00:43:00 【Dotnet cross platform】
I read the previous article briefly CommandLine Use of commands , In fact, in a command-line tool , There is another important point , That's the option ——Option, Option is to provide parameters for the command , It's like C# Medium method ,Command It's like the method ,Option Like the parameters of a method , When it comes to parameters, there are parameter types , Shape parameter ( Formal parameter name ), Empathy ,CommandLine Also have , Look at a tape Option Example :
// Create root command
var rootCommand = new RootCommand(" This is a command line tool : Dan cat ");
rootCommand.SetHandler(() =>
{
Console.WriteLine(" Welcome to use 《 Dan cat 》");
});
// Create subcommands show
var showCommand = new Command("show", " Show me some information ");
// Create subcommand options times Alias t
var timesOption = new Option<int>(name: "--times", description: " Number of times displayed ", getDefaultValue: () => 1)
{
IsHidden = true,
};
timesOption.AddAlias("-t");
// Add options to show In command
showCommand.AddOption(timesOption);
// Set command show The action performed , This is to take times Parameters , Type is integer
showCommand.SetHandler((int times) =>
{
for (var i = 0; i < times; i++)
{
Console.WriteLine($" This is a 《 Dan cat 》 Of show command , Display times ={i}");
}
}, timesOption);
// Add command show To In the root command
rootCommand.Add(showCommand);
await rootCommand.InvokeAsync(args); Options can be added , Add description , Set the default value , Also set whether you can hide , Or it is required , Options are displayed in the command execution method SetHandler receive .
Running results ,-t 5, loop 5 Time ; If the type of the option parameter is incorrect , There will be hints. ; Do not enter option parameters , Default straight 1, Here is a friendly offer Func<int>, Instead of a value , Because the default value may depend on other resource information , Not a constant .

边栏推荐
- C language string and pointer - learning 25
- 组态王如何利用无线Host-Link通信模块远程采集PLC数据?
- [answer] business use cases and system use cases are mixed together
- C language multidimensional array and pointer - learning 24
- 时间选择器样式错乱 中间文字被遮挡
- How to send Apple phone WPS files to QQ mailbox
- [answer] should the role with one end of the reflexive association be called "current version"
- 出门带着小溪
- DDD exaggeration, Eric Evans made a bad start
- How much does it cost to develop s2b2c mall system
猜你喜欢

Devops landing practice drip and pit stepping records - (1)

The time selector style is disordered and the middle text is blocked

Flink CDC + Hudi 海量数据入湖在顺丰的实践

Streaming Data Warehouse 存储:需求与架构

Dry goods | what do testers need to do for a complete performance test?

Tencent programmer roast: 1kW real estate +1kw stock +300w cash, ready to retire at the age of 35

Argodb 3.2 of star ring technology was officially released to comprehensively upgrade ease of use, performance and security

Characteristics of JS logical operators

How to cancel smart table style in WPS table

How to change the font size of Apple phone WPS
随机推荐
在玻璃上构建电路
ROS2之OpenCV基础代码对比foxy~galactic~humble
[industry application] an article on power digital transformation
At the digital data nextionbi online conference, traditional enterprises showed their in-depth understanding of data analysis
模块八-设计消息队列存储消息数据的 MySQL 表格
多年测试菜鸟对黑盒测试的理解
'virtue and art' in the field of recurrent+transformer video recovery
Lambda intermediate operation filter
Lambda quick start
Wechat applet Chinese English conversion
手机wps如何压缩文件打包发送
Characteristics of JS logical operators
How to uninstall pscs6 in win10 system
repeat_ L2-009 red envelope grabbing_ sort
JDBC exercises
How does Kingview use the wireless host link communication module to remotely collect PLC data?
System.CommandLine选项Option
Nat. Comm. | 超算+AI: 为天然产物生物合成路线规划提供导航
Month selector disable data after the current month to include the current month
Experiment 7 class construction and static member function