Skip to content

Commit

Permalink
support customize [Command("")]class
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Apr 7, 2021
1 parent 8932db8 commit 435d6cc
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-debug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.x
dotnet-version: 5.0.x

- run: dotnet build -c Debug
- run: dotnet test -c Debug --no-build < /dev/null
5 changes: 3 additions & 2 deletions .github/workflows/build-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ env:
GIT_TAG: ${{ github.event.inputs.tag }}
DRY_RUN: ${{ github.event.inputs.dry_run }}
DOTNET_SDK_VERISON_3: 3.1.x
DOTNET_SDK_VERISON_5: 5.0.x

jobs:
build-dotnet:
Expand All @@ -27,7 +28,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-dotnet@v1
with:
dotnet-version: ${{ env.DOTNET_SDK_VERISON_3 }}
dotnet-version: ${{ env.DOTNET_SDK_VERISON_5 }}
# pack nuget
- run: dotnet build -c Release -p:Version=${{ env.GIT_TAG }}
- run: dotnet test -c Release --no-build
Expand All @@ -50,7 +51,7 @@ jobs:
# setup dotnet for nuget push
- uses: actions/setup-dotnet@v1
with:
dotnet-version: ${{ env.DOTNET_SDK_VERISON_3 }}
dotnet-version: ${{ env.DOTNET_SDK_VERISON_5 }}
# tag
- uses: actions/checkout@v2
- name: tag
Expand Down
29 changes: 26 additions & 3 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ public class App : ConsoleAppBase
```

```
RunConsoleAPpFramework();
RunConsoleAppFramework();

public class App2 : ConsoleAppBase
{
Expand All @@ -345,10 +345,11 @@ public class App2 : ConsoleAppBase
}
}


// command attribute also can use to class.
[Command("mycmd")
public class App3 : ConsoleAppBase
{
// routing command: `app3 e2`
// routing command: `mycmd e2`
[Command("e2", "exec app 2.")]
public void ExecExec()
{
Expand Down Expand Up @@ -646,6 +647,28 @@ public class MyApp : ConsoleAppBase, IDisposable
}
```

also supports `IAsyncDisposable.Dispose`, however if implements both `IDisposable` and `IAsyncDisposable`, called only `IAsyncDisposable`(this is the limitation of default `ServiceProviderEngineScope`).

```csharp
public class MyApp : ConsoleAppBase, IDisposable, IAsyncDisposable
{
public void Hello()
{
Console.WriteLine("Hello");
}

void IDisposable.Dispose()
{
Console.WriteLine("Not called.");
}

async ValueTask IAsyncDisposable.DisposeAsync()
{
Console.WriteLine("called.");
}
}
```

ConsoleAppContext
---
ConsoleAppContext is injected to property on method executing.
Expand Down
19 changes: 14 additions & 5 deletions sandbox/MultiContainedApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ class Program
{
static async Task Main(string[] args)
{
//args = new string[] { "Bar.Hello3", "2" };
//d
//args = new string[] { "bar", "hello3", "-help" };
//args = new string[] { "foo", "echo", "help"};
//args = new string[] { "bar.hello2", "help" };
args = new string[] { "foo-bar", "ec", "-msg", "tako" };


await Host.CreateDefaultBuilder()
Expand All @@ -39,12 +40,13 @@ await Host.CreateDefaultBuilder()

[ConsoleAppFilter(typeof(MyFilter2), Order = 9999)]
[ConsoleAppFilter(typeof(MyFilter2), Order = 9999)]
public class Foo : ConsoleAppBase
// [Command("AAA")]
public class FooBar : ConsoleAppBase
{
[Command("ec", "My echo")]
public void Echo(string msg)
{
Console.WriteLine(msg);
Console.WriteLine(msg + "OK??");
}

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

public class Bar : ConsoleAppBase
{
public void Hello2()
[Command("ec", "My echo")]
public void Hello2(string msg)
{
Console.WriteLine("H E L L O 2");
}


public void Sum(int x, int y)
{
Console.WriteLine("H E L L O");
Console.WriteLine((x + y).ToString());
}
}

Expand Down
13 changes: 9 additions & 4 deletions sandbox/SingleContainedApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,12 @@ void IDisposable.Dispose()
// }
//}

public class Program : ConsoleAppBase, IDisposable
public class Program : ConsoleAppBase, IDisposable, IAsyncDisposable
{
static async Task Main(string[] args)
{
//args = new[] { "-m", "a ", "-b", "False" };
args = new[] { "hello" };
args = new[] { "hello" };

await Host.CreateDefaultBuilder().RunConsoleAppFrameworkAsync<Program>(args, new ConsoleAppOptions
{
Expand All @@ -240,10 +240,15 @@ public void Hello(
if (hello == null) hello = DateTime.Now;
Console.WriteLine(hello);
}

void IDisposable.Dispose()
{
throw new NotImplementedException();
Console.WriteLine("standard dispose");
}

async ValueTask IAsyncDisposable.DisposeAsync()
{
Console.WriteLine("async dispose");
}
}
}
1 change: 1 addition & 0 deletions src/ConsoleAppFramework/CommandAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ConsoleAppFramework
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
public class CommandAttribute : Attribute
{
public string[] CommandNames { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,11 @@ static List<Type> GetConsoleAppTypes(Assembly[] searchAssemblies)
isFound = true;
}
}

if (!isFound && split.Length == 2)
{
if (baseType.Name.Equals(split[0], StringComparison.OrdinalIgnoreCase))
var baseNames = baseType.GetCustomAttribute<CommandAttribute>()?.CommandNames ?? new[] { baseType.Name };
if (baseNames.Any(x => x.Equals(split[0], StringComparison.OrdinalIgnoreCase)))
{
if (foundType != null)
{
Expand Down Expand Up @@ -395,7 +397,6 @@ static List<Type> GetConsoleAppTypes(Assembly[] searchAssemblies)
return (foundType, foundMethod);
}
return (null, null);

}
}
}
3 changes: 3 additions & 0 deletions src/ConsoleAppFramework/ConsoleAppOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ public class ConsoleAppOptions
public JsonSerializerOptions? JsonSerializerOptions { get; set; }

public ConsoleAppFilter[]? GlobalFilters { get; set; }

// for https://github.com/Cysharp/ConsoleAppFramework/issues/60
// public INamingConverter NamingConverter { get; set; } = new HypenLowerNamingConverter();
}
}
95 changes: 95 additions & 0 deletions src/ConsoleAppFramework/INamingConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
//using System;
//using System.Collections.Generic;
//using System.Text;

//namespace ConsoleAppFramework
//{
// public interface INamingConverter
// {
// string ConvertToAliasName(string name);
// string ConvertToOriginalName(string name);
// }

// public class OriginalNamingConverter : INamingConverter
// {
// public string ConvertToAliasName(string name)
// {
// return name;
// }

// public string ConvertToOriginalName(string name)
// {
// return name;
// }
// }

// public class LowerCaseNamingConverter : INamingConverter
// {
// public string ConvertToAliasName(string name)
// {
// return name.ToLower();
// }

// // compare is case insensitive so use as is.
// public string ConvertToOriginalName(string name)
// {
// return name;
// }
// }

// public class HypenLowerNamingConverter : INamingConverter
// {
// public string ConvertToAliasName(string s)
// {
// if (string.IsNullOrEmpty(s)) return s;

// var sb = new StringBuilder();
// for (int i = 0; i < s.Length; i++)
// {
// var c = s[i];

// if (Char.IsUpper(c))
// {
// // first
// if (i == 0)
// {
// sb.Append(char.ToLowerInvariant(c));
// }
// else if (char.IsUpper(s[i - 1])) // WriteIO => write-io
// {
// sb.Append(char.ToLowerInvariant(c));
// }
// else
// {
// sb.Append("-");
// sb.Append(char.ToLowerInvariant(c));
// }
// }
// else
// {
// sb.Append(c);
// }
// }

// return sb.ToString();
// }

// public string ConvertToOriginalName(string s)
// {
// var sb = new StringBuilder();
// for (int i = 0; i < s.Length; i++)
// {
// if (s[i] == '-')
// {
// continue;
// }
// else
// {
// sb.Append(s[i]);
// }
// }

// return sb.ToString();
// }
// }
//}

0 comments on commit 435d6cc

Please sign in to comment.