百度360必应搜狗淘宝本站头条
当前位置:网站首页 > IT知识 > 正文

PomeloCli :一整套的命令行开发、管理、维护方案

liuian 2025-04-29 02:06 58 浏览

我们已经有相当多的命令行工具实现或解析类库,PomeloCli 并不是替代版本,它基于 Nate McMaster 的杰出工作 CommandLineUtils、DotNetCorePlugins 实现了一整套的命令行开发、管理、维护方案,在此特别鸣谢 Nate。

为什么实现

作者述职于 devOps 部门,编写、维护 CLI 工具并将其部署到各个服务器节点上是很常规的需求,但是又常常面临一系列问题。

太多的工具太少的规范

命令行工具开发自由度过高,随之而来的是迥异的开发和使用体验:

  • 依赖和配置管理混乱;

  • 没有一致的参数、选项标准,缺失帮助命令;

  • 永远找不到版本对号的说明文档;

基于二进制拷贝分发难以为继

工具开发完了还需要部署到计算节点上,但是对运维人员极其不友好:

  • 永远不知道哪些机器有没有安装,安装了什么版本;

  • 需要进入工具目录配置运行参数;

快速开始

你可以直接开始,但是在此之前理解命令、参数和选项仍然有很大的帮助。相关内容可以参考 Introduction.

引用 PomeloCli 开发命令行应用

引用 PomeloCli 来快速创建自己的命令行应用

$ dotnet new console -n SampleApp
$ cd SampleApp
$ dotnet add package PomeloCli -v 1.3.0

在入口程序添加必要的处理逻辑,文件内容见于 docs/sample/3-sample-app/Program.cs。这里使用了依赖注入管理命令,相关参考见 .NET 依赖项注入。

using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using PomeloCli;

class Program
{
static async Task<int> Main(string[] args)
{
var services = new ServiceCollection()
.AddTransient<ICommand, EchoCommand>()
.AddTransient<ICommand, HeadCommand>()
.BuildServiceProvider();

var application = ApplicationFactory.ConstructFrom(services);
return await application.ExecuteAsync(args);
}
}

这里有两个命令:EchoCommand,是对 echo 命令的模拟,文件内容见于 docs/sample/3-sample-app/EchoCommand.cs

#able disable
using System;
using System.Threading;
using System.Threading.Tasks;
using McMaster.Extensions.CommandLineUtils;
using PomeloCli;

[Command("echo", Description = "display a line of text")]
class EchoCommand : Command
{
[Argument(0, "input")]
public String Input { get; set; }

[Option("-n|--newline", CommandOptionType.NoValue, Description = "do not output the trailing newline")]
public Boolean? Newline { get; set; }

protected override Task<int> OnExecuteAsync(CancellationToken cancellationToken)
{
if (Newline.HasValue)
{
Console.WriteLine(Input);
}
else
{
Console.Write(Input);
}
return Task.FromResult(0);
}
}

HeadCommand是对 head 命令的模拟,文件内容见于 docs/sample/3-sample-app/HeadCommand.cs。

#able disable
using System;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using McMaster.Extensions.CommandLineUtils;
using PomeloCli;

[Command("head", Description = "Print the first 10 lines of each FILE to standard output")]
class HeadCommand : Command
{
[Required]
[Argument(0)]
public String Path { get; set; }

[Option("-n|--line", CommandOptionType.SingleValue, Description = "print the first NUM lines instead of the first 10")]
public Int32 Line { get; set; } = 10;

protected override Task<int> OnExecuteAsync(CancellationToken cancellationToken)
{
if (!File.Exists(Path))
{
throw new FileNotFoundException($"file '{Path}' not found");
}

var lines = File.ReadLines(Path).Take(Line);
foreach (var line in lines)
{
Console.WriteLine(line);
}
return Task.FromResult(0);
}
}

进入目录 SampleApp 后,既可以通过 dotnet run -- --help 查看包含的 echohead 命令及使用说明。

$ dotnet run -- --help
Usage: SampleApp [command] [options]

Options:
-?|-h|--help Show help information.

Commands:
echo display a line of text
head Print the first 10 lines of each FILE to standard output

Run 'SampleApp [command] -?|-h|--help' for more information about a command.

$ dotnet run -- echo --help
display a line of text

Usage: SampleApp echo [options] <input>

Arguments:
input

Options:
-n|--newline do not output the trailing newline
-?|-h|--help Show help information.

也可以编译使用可执行的 SampleApp.exe 。

$ ./bin/Debug/net8.0/SampleApp.exe --help
Usage: SampleApp [command] [options]

Options:
-?|-h|--help Show help information.

Commands:
echo display a line of text
head Print the first 10 lines of each FILE to standard output

Run 'SampleApp [command] -?|-h|--help' for more information about a command.

$ ./bin/Debug/net8.0/SampleApp.exe echo --help
display a line of text

Usage: SampleApp echo [options] <input>

Arguments:
input

Options:
-n|--newline do not output the trailing newline
-?|-h|--help Show help information.

BRAVO 很简单对吧。

引用 PomeloCli 开发命令行插件

如果只是提供命令行应用的创建能力,作者大可不必发布这样一个项目,因为 McMaster.Extensions.CommandLineUtils 本身已经做得足够好了。如上文"为什么实现章节"所说,作者还希望解决命令行工具的分发维护问题。

为了实现这一目标,PomeloCli 继续基于 McMaster.NETCore.Plugins 实现了一套插件系统或者说架构:

  • 将命令行工具拆分成宿主插件两部分功能;

  • 宿主负责安装、卸载、加载插件,作为命令行入口将参数转交给对应的插件

  • 插件负责具体的业务功能的实现;

  • 宿主插件均打包成标准的 nuget 制品;

插件加载示意

命令行参数传递示意

通过将宿主的维护交由 dotnet tool 处理、交插件的维护交由宿主处理,我们希望解决命令行工具的分发维护问题:

  • 开发人员

    • 开发插件

    • 使用 dotnet nuget push 发布插件

  • 运维/使用人员

    • 使用 dotnet tool安装、更新、卸载宿主

    • 使用 pomelo-cli install/uninstall 安装、更新、卸载插件

现在现在我们来开发一个插件应用。

开发命令行插件

引用 PomeloCli 来创建自己的命令行插件

$ dotnet new classlib -n SamplePlugin
$ cd SamplePlugin
$ dotnet add package PomeloCli -v 1.3.0

我们把上文提到的 EchoCommand 和 HeadCommand 复制到该项目,再添加依赖注入文件 ServiceCollectionExtensions.cs,文件内容见于 docs/sample/4-sample-plugin/ServiceCollectionExtensions.cs

using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using PomeloCli;

public static class ServiceCollectionExtensions
{
/// <summary>
/// pomelo-cli load plugin by this method, see
/// <see cref="PomeloCli.Plugins.Runtime.PluginResolver.Loading()" />
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
public static IServiceCollection AddCommands(this IServiceCollection services)
{
return services
.AddTransient<ICommand, EchoCommand>()
.AddTransient<ICommand, HeadCommand>();
}
}

为了能够使得插件运行起来,我们还需要在打包时将依赖添加到 nupkg 文件中。为此需要修改 csproj 添加打包配置,参考 docs/sample/4-sample-plugin/SamplePlugin.csproj,相关原理见出处 How to include package reference files in your nuget

搭建私有 nuget 服务

为了托管我们的工具与插件,我们这里使用 BaGet 搭建轻量的 nuget 服务,docker-compose.yaml 已经提供见 baget。docker 等工具使用请自行查阅。

version: "3.3"
services:
baget:
image: loicsharma/baget
container_name: baget
ports:
- "8000:80"
volumes:
- $PWD/data:/var/baget

我们使用 docker-compose up -d 将其运行起来,baget 将在地址 http://localhost:8000/ 上提供服务。

发布命令行插件

现我们在有了插件和 nuget 服务,可以发布插件了。

$ cd SamplePlugin
$ dotnet pack -o nupkgs -c Debug
$ dotnet nuget push -s http://localhost:8000/v3/index.json nupkgs/SamplePlugin.1.0.0.nupkg

使用 PomeloCli 集成已发布插件

pomelo-cli 是一个 dotnet tool 应用,可以看作命令行宿主,它包含了一组 plugin 命令用来管理我们的命令行插件。

安装命令行宿主

我们使用标准的 dotnet tool CLI 命令安装 PomeloCli,相关参考见 How to manage .NET tools

$ dotnet tool install PomeloCli.Host --version 1.3.0 -g
$ pomelo-cli --help
Usage: PomeloCli.Host [command] [options]

Options:
-?|-h|--help Show help information.

Commands:
config
plugin
version

Run 'PomeloCli.Host [command] -?|-h|--help' for more information about a command.

可以看到 pomelo-cli 内置了部分命令。

集成命令行插件

pomelo-cli 内置了一组插件,包含了其他插件的管理命令

$ pomelo-cli plugin --help
Usage: PomeloCli.Host plugin [command] [options]

Options:
-?|-h|--help Show help information.

Commands:
install
list
uninstall

Run 'plugin [command] -?|-h|--help' for more information about a command.

我们用 plugin install 命令安装刚刚发布的插件 SamplePlugin

$ pomelo-cli plugin install SamplePlugin -v 1.0.0 -s http://localhost:8000/v3/index.json
$ pomelo-cli --help
Usage: PomeloCli.Host [command] [options]

Options:
-?|-h|--help Show help information.

Commands:
config
echo display a line of text
head Print the first 10 lines of each FILE to standard output
plugin
version

Run 'PomeloCli.Host [command] -?|-h|--help' for more information about a command.

$ pomelo-cli echo --help
display a line of text

Usage: PomeloCli.Host echo [options] <input>

Arguments:
input

Options:
-n|--newline do not output the trailing newline
-?|-h|--help Show help information.

可以看到 SamplePlugin 包含的 echo 和 head 命令已经被显示在子命令列表中。

卸载命令行插件

pomelo-cli 当然也可以卸载其他插件

$ pomelo-cli plugin uninstall SamplePlugin

卸载命令行宿主

我们使用标准的 dotnet tool CLI 命令卸载 PomeloCli

$ dotnet tool uninstall PomeloCli.Host -g

其他:异常 NU1102 的处理

当安装插件失败且错误码是NU1102 时,表示未找到对应版本,可以执行命令 $ dotnet nuget locals http-cache --clear 以清理 HTTP 缓存。

info : Restoring packages for C:\Users\leon\.PomeloCli.Host\Plugin.csproj...
info : GET http://localhost:8000/v3/package/sampleplugin/index.json
info : OK http://localhost:8000/v3/package/sampleplugin/index.json 2ms
error: NU1102: Unable to find package SamplePlugin with version (>= 1.1.0)
error: - Found 7 version(s) in http://localhost:8000/v3/index.json [ Nearest version: 1.0.0 ]
error: Package 'SamplePlugin' is incompatible with 'user specified' frameworks in project 'C:\Users\leon\.PomeloCli.Host\Plugin.csproj'.

其他事项

已知问题

  • refit 支持存在问题

路线图

  • 业务插件配置

项目仍然在开发中,欢迎与我交流想法:https://github.com/leoninew/PomeloCli/


相关推荐

人工智能ppt一键制作免费版(人工智能 ppt)

1目前市面上有很多ai生成ppt的工具,但是我不能绝对地说哪个是最好的。2因为每个工具都有其独特的优点和不足。例如,Canva是一个广受好评的在线平台,它提供许多模板和素材,用户可以根据自己的需要...

手游排行榜(手游排行榜前)
手游排行榜(手游排行榜前)

手机游戏排行榜前十的有:《王者荣耀》、《绝地求生:全军出击》、《荒野行动》、《剑侠》、《4D极速沙滩赛车》、《红色坦克4D》、《镇魔》、《坦克前线帝国》、《舰指太平洋》、《红警天启的狂怒》。1、《王者荣耀》《王者荣耀》已经出了几十位英雄,定...

2025-12-04 15:55 liuian

u盘格式转换软件(u盘格式转换软件下载)
  • u盘格式转换软件(u盘格式转换软件下载)
  • u盘格式转换软件(u盘格式转换软件下载)
  • u盘格式转换软件(u盘格式转换软件下载)
  • u盘格式转换软件(u盘格式转换软件下载)
台式机没有光驱怎么装系统(台式机没有光驱怎么办)

可以用U盘安装,首先用装机软件做一个启动U盘,把要安装的系统放到U盘里,U盘插到电脑上,设置BIOS,第一启动设置为U盘。启动顺序调整一般有两种方法:1、根据开机时候的快捷键,调整硬件启动顺序,选择你...

windows7用什么杀毒软件好
windows7用什么杀毒软件好

电脑win7安装的杀毒软件有卡巴、小红伞、360安全卫士等。1、卡巴(俄罗斯杀软):卡巴就不用多说了,超强,以前的侦测率超级的高,独孤求败!现在因为针对他的免杀多了所以侦测率比以前低了。卡巴斯基博士和soloman博士对早期杀软有重大贡献。...

2025-12-04 14:05 liuian

更改电脑锁屏密码(更改电脑锁屏密码更改已成灰色怎么办)
  • 更改电脑锁屏密码(更改电脑锁屏密码更改已成灰色怎么办)
  • 更改电脑锁屏密码(更改电脑锁屏密码更改已成灰色怎么办)
  • 更改电脑锁屏密码(更改电脑锁屏密码更改已成灰色怎么办)
  • 更改电脑锁屏密码(更改电脑锁屏密码更改已成灰色怎么办)
路由器亮红灯怎么解决(路由器亮红灯解决教程)

1、关闭电源,拔下光纤头  发现路由器los闪红灯的时候,我们首先可以关闭电源,然后拔下光纤头,用棉签清理一下光纤头表面以及路由器的插孔内部,然后插上电源,重新插回光纤头。如果这一步没有问题,那么红灯...

电脑启动模式怎么选择(电脑启动选择启动方式设置)

首先在运行里面输入“msconfig”,打开了系统配置在第一个“常规”下面的“启动选择”点“正常启动”,然后点确定,最后重启电脑就完事了1.冷启动:开机时候按主机开关按钮。2.复位启动:开关按钮旁边的...

pscs6安装(pscs6安装序列号永久免费)
  • pscs6安装(pscs6安装序列号永久免费)
  • pscs6安装(pscs6安装序列号永久免费)
  • pscs6安装(pscs6安装序列号永久免费)
  • pscs6安装(pscs6安装序列号永久免费)
自家wifi突然不可上网(家里的wifi突然不能用了)

自家WiFi无法上网,优先排查是否欠费停机。没有欠费停机,那接着排查是什么设备提供的WiFi信号,路由器还是猫。若是路由器的话,重启下路由器试试,然后再重启下猫(重启可以解决百分之90的问题);若是猫...

activation官网(activator官网)
  • activation官网(activator官网)
  • activation官网(activator官网)
  • activation官网(activator官网)
  • activation官网(activator官网)
windows7如何查看隐藏文件夹
windows7如何查看隐藏文件夹

打开资源管理器,在菜单栏的“组织”中打开“文件夹和搜索选项”。然后在弹出窗口的“查看”栏中,将“显示隐藏的文件、文件夹和驱动器”前面的○选上。这样就能在文件夹中查看隐藏文件了。1.打开电脑,单击鼠标右键,出现菜单后,点击个性化2.进入个性化...

2025-12-04 10:55 liuian

如何下载浏览器并安装(给我下载一个浏览器)

要在手机上下载电脑版浏览器的安装包,可以按照以下步骤操作:1.打开手机上的浏览器,进入浏览器的官方网站,例如GoogleChrome的官网https://www.google.com/chrome...

excel2007官方下载(excel2007官方下载免费版电脑版)
  • excel2007官方下载(excel2007官方下载免费版电脑版)
  • excel2007官方下载(excel2007官方下载免费版电脑版)
  • excel2007官方下载(excel2007官方下载免费版电脑版)
  • excel2007官方下载(excel2007官方下载免费版电脑版)
万能五笔拼音输入法下载(万能五笔输入法下载安装)
万能五笔拼音输入法下载(万能五笔输入法下载安装)

理论上是五笔打字最快,因为重码比较少,很多常用字一码、二码就可以打出来了!拼音是重码最多的输入法!拼音很多字打不出,因为你连那个字读什么都不知道,中国十三亿多人,能全懂中国汉字的没超过十个八个吧?五笔可以不用懂读音也可以打出五笔:速度快,...

2025-12-04 09:05 liuian