Skip to content

Commit 435d6cc

Browse files
committed
support customize [Command("")]class
1 parent 8932db8 commit 435d6cc

File tree

9 files changed

+155
-17
lines changed

9 files changed

+155
-17
lines changed

.github/workflows/build-debug.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
- uses: actions/checkout@v2
2222
- uses: actions/setup-dotnet@v1
2323
with:
24-
dotnet-version: 3.1.x
24+
dotnet-version: 5.0.x
2525

2626
- run: dotnet build -c Debug
2727
- run: dotnet test -c Debug --no-build < /dev/null

.github/workflows/build-release.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ env:
1515
GIT_TAG: ${{ github.event.inputs.tag }}
1616
DRY_RUN: ${{ github.event.inputs.dry_run }}
1717
DOTNET_SDK_VERISON_3: 3.1.x
18+
DOTNET_SDK_VERISON_5: 5.0.x
1819

1920
jobs:
2021
build-dotnet:
@@ -27,7 +28,7 @@ jobs:
2728
- uses: actions/checkout@v2
2829
- uses: actions/setup-dotnet@v1
2930
with:
30-
dotnet-version: ${{ env.DOTNET_SDK_VERISON_3 }}
31+
dotnet-version: ${{ env.DOTNET_SDK_VERISON_5 }}
3132
# pack nuget
3233
- run: dotnet build -c Release -p:Version=${{ env.GIT_TAG }}
3334
- run: dotnet test -c Release --no-build
@@ -50,7 +51,7 @@ jobs:
5051
# setup dotnet for nuget push
5152
- uses: actions/setup-dotnet@v1
5253
with:
53-
dotnet-version: ${{ env.DOTNET_SDK_VERISON_3 }}
54+
dotnet-version: ${{ env.DOTNET_SDK_VERISON_5 }}
5455
# tag
5556
- uses: actions/checkout@v2
5657
- name: tag

ReadMe.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ public class App : ConsoleAppBase
334334
```
335335

336336
```
337-
RunConsoleAPpFramework();
337+
RunConsoleAppFramework();
338338

339339
public class App2 : ConsoleAppBase
340340
{
@@ -345,10 +345,11 @@ public class App2 : ConsoleAppBase
345345
}
346346
}
347347

348-
348+
// command attribute also can use to class.
349+
[Command("mycmd")
349350
public class App3 : ConsoleAppBase
350351
{
351-
// routing command: `app3 e2`
352+
// routing command: `mycmd e2`
352353
[Command("e2", "exec app 2.")]
353354
public void ExecExec()
354355
{
@@ -646,6 +647,28 @@ public class MyApp : ConsoleAppBase, IDisposable
646647
}
647648
```
648649

650+
also supports `IAsyncDisposable.Dispose`, however if implements both `IDisposable` and `IAsyncDisposable`, called only `IAsyncDisposable`(this is the limitation of default `ServiceProviderEngineScope`).
651+
652+
```csharp
653+
public class MyApp : ConsoleAppBase, IDisposable, IAsyncDisposable
654+
{
655+
public void Hello()
656+
{
657+
Console.WriteLine("Hello");
658+
}
659+
660+
void IDisposable.Dispose()
661+
{
662+
Console.WriteLine("Not called.");
663+
}
664+
665+
async ValueTask IAsyncDisposable.DisposeAsync()
666+
{
667+
Console.WriteLine("called.");
668+
}
669+
}
670+
```
671+
649672
ConsoleAppContext
650673
---
651674
ConsoleAppContext is injected to property on method executing.

sandbox/MultiContainedApp/Program.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ class Program
1111
{
1212
static async Task Main(string[] args)
1313
{
14-
//args = new string[] { "Bar.Hello3", "2" };
14+
//d
1515
//args = new string[] { "bar", "hello3", "-help" };
1616
//args = new string[] { "foo", "echo", "help"};
1717
//args = new string[] { "bar.hello2", "help" };
18+
args = new string[] { "foo-bar", "ec", "-msg", "tako" };
1819

1920

2021
await Host.CreateDefaultBuilder()
@@ -39,12 +40,13 @@ await Host.CreateDefaultBuilder()
3940

4041
[ConsoleAppFilter(typeof(MyFilter2), Order = 9999)]
4142
[ConsoleAppFilter(typeof(MyFilter2), Order = 9999)]
42-
public class Foo : ConsoleAppBase
43+
// [Command("AAA")]
44+
public class FooBar : ConsoleAppBase
4345
{
4446
[Command("ec", "My echo")]
4547
public void Echo(string msg)
4648
{
47-
Console.WriteLine(msg);
49+
Console.WriteLine(msg + "OK??");
4850
}
4951

5052
public void Sum(int x, int y)
@@ -55,9 +57,16 @@ public void Sum(int x, int y)
5557

5658
public class Bar : ConsoleAppBase
5759
{
58-
public void Hello2()
60+
[Command("ec", "My echo")]
61+
public void Hello2(string msg)
62+
{
63+
Console.WriteLine("H E L L O 2");
64+
}
65+
66+
67+
public void Sum(int x, int y)
5968
{
60-
Console.WriteLine("H E L L O");
69+
Console.WriteLine((x + y).ToString());
6170
}
6271
}
6372

sandbox/SingleContainedApp/Program.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,12 @@ void IDisposable.Dispose()
217217
// }
218218
//}
219219

220-
public class Program : ConsoleAppBase, IDisposable
220+
public class Program : ConsoleAppBase, IDisposable, IAsyncDisposable
221221
{
222222
static async Task Main(string[] args)
223223
{
224224
//args = new[] { "-m", "a ", "-b", "False" };
225-
args = new[] { "hello" };
225+
args = new[] { "hello" };
226226

227227
await Host.CreateDefaultBuilder().RunConsoleAppFrameworkAsync<Program>(args, new ConsoleAppOptions
228228
{
@@ -240,10 +240,15 @@ public void Hello(
240240
if (hello == null) hello = DateTime.Now;
241241
Console.WriteLine(hello);
242242
}
243-
243+
244244
void IDisposable.Dispose()
245245
{
246-
throw new NotImplementedException();
246+
Console.WriteLine("standard dispose");
247+
}
248+
249+
async ValueTask IAsyncDisposable.DisposeAsync()
250+
{
251+
Console.WriteLine("async dispose");
247252
}
248253
}
249254
}

src/ConsoleAppFramework/CommandAttribute.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ConsoleAppFramework
44
{
5+
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
56
public class CommandAttribute : Attribute
67
{
78
public string[] CommandNames { get; }

src/ConsoleAppFramework/ConsoleAppEngineHostBuilderExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,11 @@ static List<Type> GetConsoleAppTypes(Assembly[] searchAssemblies)
364364
isFound = true;
365365
}
366366
}
367+
367368
if (!isFound && split.Length == 2)
368369
{
369-
if (baseType.Name.Equals(split[0], StringComparison.OrdinalIgnoreCase))
370+
var baseNames = baseType.GetCustomAttribute<CommandAttribute>()?.CommandNames ?? new[] { baseType.Name };
371+
if (baseNames.Any(x => x.Equals(split[0], StringComparison.OrdinalIgnoreCase)))
370372
{
371373
if (foundType != null)
372374
{
@@ -395,7 +397,6 @@ static List<Type> GetConsoleAppTypes(Assembly[] searchAssemblies)
395397
return (foundType, foundMethod);
396398
}
397399
return (null, null);
398-
399400
}
400401
}
401402
}

src/ConsoleAppFramework/ConsoleAppOptions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@ public class ConsoleAppOptions
1717
public JsonSerializerOptions? JsonSerializerOptions { get; set; }
1818

1919
public ConsoleAppFilter[]? GlobalFilters { get; set; }
20+
21+
// for https://github.com/Cysharp/ConsoleAppFramework/issues/60
22+
// public INamingConverter NamingConverter { get; set; } = new HypenLowerNamingConverter();
2023
}
2124
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
//using System;
2+
//using System.Collections.Generic;
3+
//using System.Text;
4+
5+
//namespace ConsoleAppFramework
6+
//{
7+
// public interface INamingConverter
8+
// {
9+
// string ConvertToAliasName(string name);
10+
// string ConvertToOriginalName(string name);
11+
// }
12+
13+
// public class OriginalNamingConverter : INamingConverter
14+
// {
15+
// public string ConvertToAliasName(string name)
16+
// {
17+
// return name;
18+
// }
19+
20+
// public string ConvertToOriginalName(string name)
21+
// {
22+
// return name;
23+
// }
24+
// }
25+
26+
// public class LowerCaseNamingConverter : INamingConverter
27+
// {
28+
// public string ConvertToAliasName(string name)
29+
// {
30+
// return name.ToLower();
31+
// }
32+
33+
// // compare is case insensitive so use as is.
34+
// public string ConvertToOriginalName(string name)
35+
// {
36+
// return name;
37+
// }
38+
// }
39+
40+
// public class HypenLowerNamingConverter : INamingConverter
41+
// {
42+
// public string ConvertToAliasName(string s)
43+
// {
44+
// if (string.IsNullOrEmpty(s)) return s;
45+
46+
// var sb = new StringBuilder();
47+
// for (int i = 0; i < s.Length; i++)
48+
// {
49+
// var c = s[i];
50+
51+
// if (Char.IsUpper(c))
52+
// {
53+
// // first
54+
// if (i == 0)
55+
// {
56+
// sb.Append(char.ToLowerInvariant(c));
57+
// }
58+
// else if (char.IsUpper(s[i - 1])) // WriteIO => write-io
59+
// {
60+
// sb.Append(char.ToLowerInvariant(c));
61+
// }
62+
// else
63+
// {
64+
// sb.Append("-");
65+
// sb.Append(char.ToLowerInvariant(c));
66+
// }
67+
// }
68+
// else
69+
// {
70+
// sb.Append(c);
71+
// }
72+
// }
73+
74+
// return sb.ToString();
75+
// }
76+
77+
// public string ConvertToOriginalName(string s)
78+
// {
79+
// var sb = new StringBuilder();
80+
// for (int i = 0; i < s.Length; i++)
81+
// {
82+
// if (s[i] == '-')
83+
// {
84+
// continue;
85+
// }
86+
// else
87+
// {
88+
// sb.Append(s[i]);
89+
// }
90+
// }
91+
92+
// return sb.ToString();
93+
// }
94+
// }
95+
//}

0 commit comments

Comments
 (0)