Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subcommands in Help print - Syntax review #129

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added parsable Class-based CommandAttribute assignment
BlackPhlox committed Jun 30, 2024
commit caba160cfd7e8ac9a556b14ec6f9557b4e4a204e
35 changes: 33 additions & 2 deletions src/ConsoleAppFramework/Parser.cs
Original file line number Diff line number Diff line change
@@ -55,8 +55,32 @@ internal class Parser(DiagnosticReporter context, InvocationExpressionSyntax nod
var genericName = (node.Expression as MemberAccessExpressionSyntax)?.Name as GenericNameSyntax;
var genericType = genericName!.TypeArgumentList.Arguments[0];

// Add<T>(string commandPath)
string? commandPath = null;

if (genericName != null)
{
var className = genericName.TypeArgumentList.Arguments.First().ToString();

// Find the class declaration with the matching class name
var classDeclaration = node.SyntaxTree.GetRoot().DescendantNodes()
.OfType<ClassDeclarationSyntax>()
.FirstOrDefault(c => c.Identifier.Text == className);

if (classDeclaration != null)
{
var commandAttribute = classDeclaration.AttributeLists
.SelectMany(al => al.Attributes)
.FirstOrDefault(a => a.Name.ToString() == "Command");

var attributeArgument = commandAttribute?.ArgumentList?.Arguments.FirstOrDefault()?.ToString().Trim('"');
if (attributeArgument != null)
{
commandPath = attributeArgument;
}
}
}

// Add<T>(string commandPath)
var args = node.ArgumentList.Arguments;
if (node.ArgumentList.Arguments.Count == 1)
{
@@ -67,7 +91,14 @@ internal class Parser(DiagnosticReporter context, InvocationExpressionSyntax nod
return [];
}

commandPath = (commandName.Expression as LiteralExpressionSyntax)!.Token.ValueText;
if (commandPath == null)
{
commandPath = (commandName.Expression as LiteralExpressionSyntax)!.Token.ValueText;
}
else
{
context.ReportDiagnostic(DiagnosticDescriptors.DuplicateCommandName, commandName.GetLocation(), commandPath);
}
}

// T
11 changes: 5 additions & 6 deletions tests/ConsoleAppFramework.GeneratorTests/HelpTest.cs
Original file line number Diff line number Diff line change
@@ -311,7 +311,7 @@ public void ClassCommandNoClassSummary()
{
var code = """
var app = ConsoleApp.Create();
app.Add<MyClass>();
app.Add<MyClass>("mc");
app.Run(args);
[Command("mc")]
@@ -406,7 +406,6 @@ public void AppWithNoRootDefaultDisplaySubcommand()
var app = ConsoleApp.Create();
app.Add<MyClass>();
app.Add<MyClass2>();
app.SubcommandHelp(DisplayType.Default);
app.Run(args);
/// <summary>
@@ -444,16 +443,16 @@ public void HelloWorld2([Argument]int boo, string fooBar)
}
""";
verifier.Execute(code, args: "--help", expected: """
Usage: [command] [options...] [-h|--help] [--version]
Usage: [command] [-h|--help] [--version]
Commands:
mc hello-world hello my world.
mc2 hello-world2 hello my world.
mc hello-world hello my world.
mc2 hello-world2 hello my world.
""");

verifier.Execute(code, args: "mc hello-world --help", expected: """
Usage: hello-world [arguments...] [options...] [-h|--help] [--version]
Usage: mc hello-world [arguments...] [options...] [-h|--help] [--version]
hello my world.