Skip to content

Commit 97a1881

Browse files
committed
custom version string, close #113
1 parent d267ef1 commit 97a1881

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

ReadMe.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ To add aliases to parameters, list the aliases separated by `|` before the comma
243243

244244
Unfortunately, due to current C# specifications, lambda expressions and [local functions do not support document comments](https://github.com/dotnet/csharplang/issues/2110), so a class is required.
245245

246-
In addition to `-h|--help`, there is another special built-in option: `--version`. This displays the `AssemblyInformationalVersion` or `AssemblyVersion`.
246+
In addition to `-h|--help`, there is another special built-in option: `--version`. In default, it displays the `AssemblyInformationalVersion` or `AssemblyVersion`. You can configure version string by `ConsoleApp.Version`, for example `ConsoleApp.Version = "2001.9.3f14-preview2";`.
247247

248248
Command
249249
---

sandbox/GeneratorSandbox/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.Diagnostics.CodeAnalysis;
33
using ConsoleAppFramework;
44

5+
6+
57
args = ["show", "--aaa", "a", "--value", "10.2"];
68

79
var app = ConsoleApp.Create();

src/ConsoleAppFramework/ConsoleAppGenerator.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ internal static partial class ConsoleApp
138138
public static IServiceProvider? ServiceProvider { get; set; }
139139
public static TimeSpan Timeout { get; set; } = TimeSpan.FromSeconds(5);
140140
public static System.Text.Json.JsonSerializerOptions? JsonSerializerOptions { get; set; }
141+
public static string? Version { get; set; }
141142
142143
static Action<string>? logAction;
143144
public static Action<string> Log
@@ -321,6 +322,12 @@ static bool TryShowHelpOrVersion(ReadOnlySpan<string> args, int requiredParamete
321322
322323
static void ShowVersion()
323324
{
325+
if (Version != null)
326+
{
327+
Log(Version);
328+
return;
329+
}
330+
324331
var asm = Assembly.GetEntryAssembly();
325332
var version = "1.0.0";
326333
var infoVersion = asm!.GetCustomAttribute<AssemblyInformationalVersionAttribute>();

tests/ConsoleAppFramework.GeneratorTests/HelpTest.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Reflection;
45
using System.Text;
56
using System.Threading.Tasks;
67
using Xunit.Abstractions;
@@ -11,6 +12,31 @@ public class HelpTest(ITestOutputHelper output)
1112
{
1213
VerifyHelper verifier = new VerifyHelper(output, "CAF");
1314

15+
[Fact]
16+
public void Version()
17+
{
18+
var version = Assembly.GetEntryAssembly()?.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion ?? "1.0.0";
19+
20+
verifier.Execute(code: $$"""
21+
ConsoleApp.Run(args, (int x, int y) => { });
22+
""",
23+
args: "--version",
24+
expected: $$"""
25+
{{version}}
26+
27+
""");
28+
// custom
29+
verifier.Execute(code: $$"""
30+
ConsoleApp.Version = "9999.9999999abcdefg";
31+
ConsoleApp.Run(args, (int x, int y) => { });
32+
""",
33+
args: "--version",
34+
expected: """
35+
9999.9999999abcdefg
36+
37+
""");
38+
}
39+
1440
[Fact]
1541
public void Run()
1642
{

0 commit comments

Comments
 (0)