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

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

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

我们已经有相当多的命令行工具实现或解析类库,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/


相关推荐

网卡驱动精灵离线版(驱动精灵万能网卡版离线)

驱动精灵离线装网卡驱动方法:首先用户需要先下驱动精灵离线版安卓包。1、用户到本站下载安卓包后,双击点开,exe文件进入下载界面。2.用户可以更改下载路径,也可以默认安装路径,点击一键安装。3、用户点...

电脑截屏如何操作(电脑如何截屏截图ctrl加什么)
  • 电脑截屏如何操作(电脑如何截屏截图ctrl加什么)
  • 电脑截屏如何操作(电脑如何截屏截图ctrl加什么)
  • 电脑截屏如何操作(电脑如何截屏截图ctrl加什么)
  • 电脑截屏如何操作(电脑如何截屏截图ctrl加什么)
office2010的优点(office2010介绍)

Office发行的版本很多,那么office哪个版本最好用呢?Office2010是2009年发行的版本,Office2016是2015年推出的版本,是目前最新版的Office产品套件,它们中...

window7下载ie浏览器(win7怎么下载ie)
window7下载ie浏览器(win7怎么下载ie)

暂时没有手机版的IE浏览器。InternetExplorer(旧称MicrosoftInternetExplorer和WindowsInternetExplorer,简称IE,俗称“网络探索者”),是微软公司推出的一款网页浏览器,I...

2026-01-22 07:37 liuian

联想平板驱动下载(联想平板驱动下载官方网站安装)
联想平板驱动下载(联想平板驱动下载官方网站安装)

具体方法如下:1、首先,用户需要打开自己的浏览器输入关键词“联想驱动”即可打开联想官网;2、查看自己联想笔记本型号;3、在搜索框中输入你刚刚查看到的笔记本型号,单击搜索即可查找;4、打开后切换上方选项到“驱动和软件下载”,点击下拉符号找到你...

2026-01-22 07:21 liuian

字体在哪里安装(字体安装在哪个)

把网上下载的字体安装到自己电脑上的具体操作如下:所需材料:电脑、字体安装包1、网上下载的字体文件一般是压缩包,先把文件解压。2、文件解压完成之后打开【计算机】在c盘中找到【WINDOWS】文件夹双击打...

ps软件下载安装教程(ps软件下载安装教程图片)
  • ps软件下载安装教程(ps软件下载安装教程图片)
  • ps软件下载安装教程(ps软件下载安装教程图片)
  • ps软件下载安装教程(ps软件下载安装教程图片)
  • ps软件下载安装教程(ps软件下载安装教程图片)
开机速度突然变慢很多(开机突然慢了很多)

电脑开机速度突然变慢的原因可能有以下几点:1.硬件问题:可能是电脑的硬件出现故障或老化,例如硬盘老化、内存故障等,导致数据读取速度变慢。2.病毒或恶意软件感染:如果电脑被病毒或恶意软件感染,这些恶...

project产品密钥2010(office project产品密钥)

我不知道您使用的是哪个project,但是一般来说,项目密钥的激活方法可能如下:1.在项目网站或应用程序中寻找“激活”或“许可证”选项。2.输入您的密钥(通常是一串字母和数字的组合)。3.按照屏...

qq消息群发助手(qq消息群发助手手机版)
  • qq消息群发助手(qq消息群发助手手机版)
  • qq消息群发助手(qq消息群发助手手机版)
  • qq消息群发助手(qq消息群发助手手机版)
  • qq消息群发助手(qq消息群发助手手机版)
笔记本品控最好的品牌(笔记本品控最好的品牌排行榜)

他属于一线品牌,质量很好,他的品控也非常好。联想是一个老品牌了,他的电脑技术非常成熟。联想小新笔记本啊,它的卖点是超薄机身+时尚造型,性能并不是它所追求的,所以一般买小新笔记本的都是用于普通的...

微软office官网下载中心(微软官方office下载)

07版本大概950M左右,1M=1024kb,自己算吧MicrosoftOffice是一个集办公软件于一体的应用软件套装,由微软公司推出,它包括Word、Excel、PowerPoint、Acces...

惠普售后电话怎么转人工(惠普售后人工电话是多少)

没有,可以找本地的售后中心那样就可以了2年部件说明是部分硬件是2年保修(比如电池他会说明只保修1年)还有一般是2年送修,要你送到维修中心的oppo手机可通过以下步骤连接打印机使用:1、oppo手机...

老版微信2019下载安装(微信7.6.5版本(可登录))
  • 老版微信2019下载安装(微信7.6.5版本(可登录))
  • 老版微信2019下载安装(微信7.6.5版本(可登录))
  • 老版微信2019下载安装(微信7.6.5版本(可登录))
  • 老版微信2019下载安装(微信7.6.5版本(可登录))
最好用一件ghost工具(一键ghost工具箱)
  • 最好用一件ghost工具(一键ghost工具箱)
  • 最好用一件ghost工具(一键ghost工具箱)
  • 最好用一件ghost工具(一键ghost工具箱)
  • 最好用一件ghost工具(一键ghost工具箱)