# 命令行扩展 **Repository Path**: zijian666/commandline-extensions ## Basic Information - **Project Name**: 命令行扩展 - **Description**: 基于官方组件 System.CommandLine 扩展 - **Primary Language**: Unknown - **License**: MulanPSL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-10-05 - **Last Updated**: 2025-10-06 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # zijian666.CommandlineExtensions ## 介绍 基于官方组件 System.CommandLine 扩展 ## 更新日志 [点击查看](UPLOGS.md) ## 安装教程 ## 项目示例 [点击查看](example/) ## 功能说明 ### 先定义一个命令对象 ```csharp //[Description("打个招呼吧")] [CommandLineAction("hello", Alias = ["hi"], Description = "打个招呼吧")] internal class Hello //: ICommandLineAction { [Option(Alias = ["n"])] [Description("你的名字")] //[Required] public required string Name { get; set; } [Option(Alias = ["j"])] [Description("你的职业")] public string[]? Job { get; set; } public void Execute() { if (Job?.Length is 0 or null) { Console.WriteLine($"你好, {Name}!"); } else { Console.WriteLine($"你好, {string.Join("/", Job)} {Name}!"); } } } ``` ### 注册到 命令行 ```csharp var root = new RootCommand(); root.BindCommand(); // 绑定动作到根命令 root.AddCommand(); // 添加子命令 // 同步或异步执行 root.Invoke(); //await root.InvokeAsync(); ``` ### 效果 ```bash > demo Description: 用法: demo [command] [options] 选项: --test 打印测试信息 -?, -h, --help Show help and usage information --version 显示版本信息 命令: hello, hi 打个招呼吧 ``` ```bash > demo hi -? Description: 打个招呼吧 用法: demo hello [options] 选项: -n, --name (REQUIRED) 你的名字 -j, --job 你的职业 -?, -h, --help Show help and usage information ``` ```bash > demo hi -n zijian666 -j 程序员 -j 机修工 你好, 程序员/机修工 zijian666! ```