Cracker.CommandLine
1.0.3
dotnet add package Cracker.CommandLine --version 1.0.3
NuGet\Install-Package Cracker.CommandLine -Version 1.0.3
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Cracker.CommandLine" Version="1.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Cracker.CommandLine" Version="1.0.3" />
<PackageReference Include="Cracker.CommandLine" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Cracker.CommandLine --version 1.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Cracker.CommandLine, 1.0.3"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Cracker.CommandLine@1.0.3
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Cracker.CommandLine&version=1.0.3
#tool nuget:?package=Cracker.CommandLine&version=1.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Cracker.CommandLine
- 不依赖第三方包
- 解析命令行参数
- 支持选项、子命令
- 核心使用反射
使用步骤
- 安装.NET SDK8.x
- 添加nuget包
dotnet add package Cracker.CommandLine
or
Install-Package Cracker.CommandLine -Version 1.0.3
- 示例Program.cs
var app = new CommandApp();
app.Add<ConvertCommand>("convert")
.Child(() => new CommandConfiguration("ts", new TimeToTimestampCommand()).WithDescription("时间戳转时间"))
.Child(() => new CommandConfiguration("time", new TimestampToTimeCommand()).WithDescription("时间转时间戳"))
.WithDescription("格式转换");
await app.StartAsync(args);
- 示例ConvertCommand.cs
public class ConvertCommand : CommandBase
{
public override Task ExecuteAsync(CommandContext context)
{
return Task.CompletedTask;
}
}
public class TimeParameter
{
[CliArgument(0, "要转换的时间|时间戳", true)]
public string? Value { get; set; }
[CliOption("-f", "指定时间戳的格式,可选值:<s|ms>,s:秒,ms:毫秒", "s")]
public string? Format { get; set; }
}
public class TimeToTimestampCommand : CommandBase<TimeParameter>
{
public override Task ExecuteAsync(CommandContext context)
{
if (DateTime.TryParse(Receive.Value, out var time))
{
if (Receive.Format == "s")
{
Console.WriteLine(TimeUtils.DateTimeToTimestamp(time, true));
}
else
{
Console.WriteLine(TimeUtils.DateTimeToTimestamp(time, false));
}
}
else
{
Console.WriteLine("时间格式错误");
}
return Task.CompletedTask;
}
}
public class TimestampToTimeCommand : CommandBase<TimeParameter>
{
public override Task ExecuteAsync(CommandContext context)
{
if (long.TryParse(Receive.Value, out var ts))
{
Console.WriteLine(TimeUtils.TimestampToDateTime(ts).ToString("yyyy-MM-dd HH:mm:ss"));
}
else
{
Console.WriteLine("时间戳格式错误");
}
return Task.CompletedTask;
}
}
作者
crackerwork@outlook.com
QQ: 1491184849
MySite: https://crackerwork.cn
您也可以在贡献者名单中参看所有参与该项目的开发者。
版权说明
该项目签署了MIT 授权许可,详情请参阅 LICENSE.txt
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.