Skip to content

Commit 01ce8d0

Browse files
committed
allow take multiple parameters as Argument #213
1 parent 80f85f2 commit 01ce8d0

6 files changed

Lines changed: 64 additions & 82 deletions

File tree

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,8 @@
11
using ConsoleAppFramework;
2-
using DryIoc.Microsoft.DependencyInjection;
3-
using Microsoft.Extensions.DependencyInjection;
4-
using System.Threading.Tasks.Dataflow;
52

6-
// args = "some-command hello --global-flag flag-value -- more args here".Split(" ");
3+
args = "9999 --x 1000".Split(' ');
74

8-
var app = ConsoleApp.Create()
9-
// setup DryIoc as the DI container
10-
.ConfigureContainer(new DryIocServiceProviderFactory())
11-
.ConfigureServices(services => services.AddSingleton<MyService>());
12-
13-
app.Add("", ([FromServices] MyService service) => { });
14-
15-
app.Run(args);
16-
17-
18-
19-
//app.UseFilter<MyFilter>();
20-
// app.Run(["cmd", "test"]);
21-
22-
public class MyService
23-
{
24-
public void Test() => Console.WriteLine("Test");
25-
}
26-
27-
internal class MyFilter(ConsoleAppFilter next, MyService myService) : ConsoleAppFilter(next)
28-
{
29-
public override async Task InvokeAsync(ConsoleAppContext context, CancellationToken cancellationToken)
30-
{
31-
myService.Test();
32-
await Next.InvokeAsync(context, cancellationToken);
33-
}
34-
}
35-
36-
[RegisterCommands("cmd")]
37-
public class MyCommand
5+
ConsoleApp.Run(args, (int x, [Argument] int y) =>
386
{
39-
[Command("test")]
40-
public int Test()
41-
{
42-
return 1;
43-
}
44-
}
7+
Console.WriteLine((x, y));
8+
});

src/ConsoleAppFramework/DiagnosticDescriptors.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ public static DiagnosticDescriptor Create(int id, string title, string messageFo
7070
"Command method return type must be void or int or async Task or async Task<int>.",
7171
"Command method return type must be void or int or async Task or async Task<int> but returned '{0}'.");
7272

73-
public static DiagnosticDescriptor SequentialArgument { get; } = Create(
74-
4,
75-
"All Argument parameters must be sequential from first.");
73+
// v5.7.7 supports non-first argument parameters
74+
//public static DiagnosticDescriptor SequentialArgument { get; } = Create(
75+
// 4,
76+
// "All Argument parameters must be sequential from first.");
7677

7778
public static DiagnosticDescriptor FunctionPointerCanNotHaveValidation { get; } = Create(
7879
5,

src/ConsoleAppFramework/Parser.cs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ bool IsParsableType(ITypeSymbol type)
424424
}
425425

426426
var parsableIndex = 0;
427+
var argumentIndexCounter = 0;
427428
var parameters = lambda.ParameterList.Parameters
428429
.Where(x => x.Type != null)
429430
.Select(x =>
@@ -554,7 +555,7 @@ bool IsParsableType(ITypeSymbol type)
554555
{
555556
if (hasArgument)
556557
{
557-
argumentIndex = parsableIndex++;
558+
argumentIndex = argumentIndexCounter++;
558559
}
559560
else
560561
{
@@ -727,6 +728,7 @@ bool IsParsableType(ITypeSymbol type)
727728
}
728729

729730
var parsableIndex = 0;
731+
var argumentIndexCounter = 0;
730732
var parameters = methodSymbol.Parameters
731733
.Select(x =>
732734
{
@@ -759,7 +761,7 @@ bool IsParsableType(ITypeSymbol type)
759761
{
760762
if (hasArgument)
761763
{
762-
argumentIndex = parsableIndex++;
764+
argumentIndex = argumentIndexCounter++;
763765
}
764766
else
765767
{
@@ -817,23 +819,23 @@ bool IsParsableType(ITypeSymbol type)
817819
{
818820
var hasDiagnostic = false;
819821

820-
// Sequential Argument
821-
var existsNotArgument = false;
822-
foreach (var parameter in command.Parameters)
823-
{
824-
if (!parameter.IsParsable) continue;
825-
826-
if (!parameter.IsArgument)
827-
{
828-
existsNotArgument = true;
829-
}
830-
831-
if (parameter.IsArgument && existsNotArgument)
832-
{
833-
context.ReportDiagnostic(DiagnosticDescriptors.SequentialArgument, parameter.Location);
834-
hasDiagnostic = true;
835-
}
836-
}
822+
// Sequential Argument(v5.7.7 support it).
823+
//var existsNotArgument = false;
824+
//foreach (var parameter in command.Parameters)
825+
//{
826+
// if (!parameter.IsParsable) continue;
827+
828+
// if (!parameter.IsArgument)
829+
// {
830+
// existsNotArgument = true;
831+
// }
832+
833+
// if (parameter.IsArgument && existsNotArgument)
834+
// {
835+
// context.ReportDiagnostic(DiagnosticDescriptors.SequentialArgument, parameter.Location);
836+
// hasDiagnostic = true;
837+
// }
838+
//}
837839

838840
// FunctionPointer can not use validation
839841
if (command.MethodKind == MethodKind.FunctionPointer)

tests/ConsoleAppFramework.GeneratorTests/ConsoleAppBuilderTest.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace ConsoleAppFramework.GeneratorTests;
1+
using Microsoft.VisualStudio.TestPlatform.Utilities;
2+
3+
namespace ConsoleAppFramework.GeneratorTests;
24

35
public class ConsoleAppBuilderTest(ITestOutputHelper output) : IDisposable
46
{
@@ -17,14 +19,13 @@ public void BuilderRun()
1719
builder.Add("boz", async Task (int x) => { await Task.Yield(); Console.Write(x * 2); });
1820
builder.Run(args);
1921
""";
20-
2122
verifier.Execute(code, "foo --x 10 --y 20", "30");
2223
verifier.Execute(code, "bar --x 20 --y 30", "50");
2324
verifier.Execute(code, "bar --x 20", "30");
24-
Environment.ExitCode.ShouldBe(0);
25-
verifier.Execute(code, "baz --x 40 --y takoyaki", "40takoyaki");
26-
Environment.ExitCode.ShouldBe(10);
27-
Environment.ExitCode = 0;
25+
//Environment.ExitCode.ShouldBe(0);
26+
//verifier.Execute(code, "baz --x 40 --y takoyaki", "40takoyaki");
27+
//Environment.ExitCode.ShouldBe(10);
28+
//Environment.ExitCode = 0;
2829

2930
verifier.Execute(code, "boz --x 40", "80");
3031
}
@@ -44,10 +45,10 @@ public void BuilderRunAsync()
4445
verifier.Execute(code, "foo --x 10 --y 20", "30");
4546
verifier.Execute(code, "bar --x 20 --y 30", "50");
4647
verifier.Execute(code, "bar --x 20", "30");
47-
Environment.ExitCode.ShouldBe(0);
48-
verifier.Execute(code, "baz --x 40 --y takoyaki", "40takoyaki");
49-
Environment.ExitCode.ShouldBe(10);
50-
Environment.ExitCode = 0;
48+
//Environment.ExitCode.ShouldBe(0);
49+
//verifier.Execute(code, "baz --x 40 --y takoyaki", "40takoyaki");
50+
//Environment.ExitCode.ShouldBe(10);
51+
//Environment.ExitCode = 0;
5152

5253
verifier.Execute(code, "boz --x 40", "80");
5354
}

tests/ConsoleAppFramework.GeneratorTests/DiagnosticsTest.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,17 @@ public void RunAsyncValidation()
6666
verifier.Ok("ConsoleApp.RunAsync(args, Run); async Task<int> Run(int x, int y) => -1;");
6767
}
6868

69-
[Fact]
70-
public void Argument()
71-
{
72-
verifier.Verify(4, "ConsoleApp.Run(args, (int x, [Argument]int y) => { })", "[Argument]int y");
73-
verifier.Verify(4, "ConsoleApp.Run(args, ([Argument]int x, int y, [Argument]int z) => { })", "[Argument]int z");
74-
verifier.Verify(4, "ConsoleApp.Run(args, Run); void Run(int x, [Argument]int y) { };", "[Argument]int y");
75-
76-
verifier.Ok("ConsoleApp.Run(args, ([Argument]int x, [Argument]int y) => { })");
77-
verifier.Ok("ConsoleApp.Run(args, Run); void Run([Argument]int x, [Argument]int y) { };");
78-
}
69+
// v5.7.7 supports non-first argument parameters
70+
//[Fact]
71+
//public void Argument()
72+
//{
73+
// verifier.Verify(4, "ConsoleApp.Run(args, (int x, [Argument]int y) => { })", "[Argument]int y");
74+
// verifier.Verify(4, "ConsoleApp.Run(args, ([Argument]int x, int y, [Argument]int z) => { })", "[Argument]int z");
75+
// verifier.Verify(4, "ConsoleApp.Run(args, Run); void Run(int x, [Argument]int y) { };", "[Argument]int y");
76+
77+
// verifier.Ok("ConsoleApp.Run(args, ([Argument]int x, [Argument]int y) => { })");
78+
// verifier.Ok("ConsoleApp.Run(args, Run); void Run([Argument]int x, [Argument]int y) { };");
79+
//}
7980

8081
[Fact]
8182
public void FunctionPointerValidation()

tests/ConsoleAppFramework.GeneratorTests/RunTest.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,17 @@ public class FileCommand
271271
verifier.Execute(code, "--input sample.txt", "sample.txt");
272272
verifier.Execute(code, "-i sample.txt", "sample.txt");
273273
}
274+
275+
[Fact]
276+
public void ArgumentLastParams()
277+
{
278+
var code = """
279+
ConsoleApp.Run(args, (string opt1, [Argument]params string[] args) =>
280+
{
281+
Console.Write($"{opt1}, {string.Join("|", args)}");
282+
});
283+
""";
284+
285+
verifier.Execute(code, "--opt1 abc a b c d", "abc, a|b|c|d");
286+
}
274287
}

0 commit comments

Comments
 (0)