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
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 13 additions & 2 deletions src/ConsoleAppFramework/ConsoleAppGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)

var expr = invocationExpression.Expression as MemberAccessExpressionSyntax;
var methodName = expr?.Name.Identifier.Text;
if (methodName is "Add" or "UseFilter" or "Run" or "RunAsync")
if (methodName is "Add" or "SubcommandHelp" or "UseFilter" or "Run" or "RunAsync")
{
return true;
}
Expand Down Expand Up @@ -144,7 +144,7 @@ internal sealed class ArgumentAttribute : Attribute
{
}

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
internal sealed class CommandAttribute : Attribute
{
public string Command { get; }
Expand Down Expand Up @@ -457,6 +457,12 @@ public void Dispose()
}
}

public enum DisplayType
{
Default,
Hidden
}

internal partial struct ConsoleAppBuilder
{
public ConsoleAppBuilder()
Expand All @@ -468,6 +474,11 @@ public void Add(string commandName, Delegate command)
AddCore(commandName, command);
}

public void SubcommandHelp(DisplayType displayType)
{
// Not Implemented yet
}

[System.Diagnostics.Conditional("DEBUG")]
public void Add<T>() { }

Expand Down
35 changes: 33 additions & 2 deletions src/ConsoleAppFramework/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = model.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)
{
Expand All @@ -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
Expand Down
Loading