Skip to content

Commit

Permalink
Consider parser's special commands when generating default help text …
Browse files Browse the repository at this point in the history
…screen
  • Loading branch information
DoctorKrolic committed Jul 27, 2024
1 parent 14b88ba commit 8a7619e
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 105 deletions.
36 changes: 26 additions & 10 deletions src/ArgumentParsing.Generators/ArgumentParserGenerator.CodeGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -877,9 +877,9 @@ private static void EmitArgumentParser(SourceProductionContext context, (Argumen
context.AddSource($"{qualifiedName}.g.cs", writer.ToString().Trim());
}

private static void EmitHelpCommandHandler(SourceProductionContext context, (OptionsInfo, AssemblyVersionInfo) infos)
private static void EmitHelpCommandHandler(SourceProductionContext context, ArgumentParserHelpInfo helpInfo)
{
var (optionsInfo, assemblyVersionInfo) = infos;
var (optionsInfo, builtInCommandHandlers, additionalCommandHandlers, assemblyVersionInfo) = helpInfo;
var (qualifiedName, _, optionInfos, parameterInfos, remainingParametersInfo, helpTextGeneratorInfo) = optionsInfo;

var writer = new CodeWriter();
Expand Down Expand Up @@ -914,18 +914,18 @@ private static void EmitHelpCommandHandler(SourceProductionContext context, (Opt
writer.WriteLine("global::System.Text.StringBuilder helpBuilder = new();");
writer.WriteLine($"helpBuilder.AppendLine(\"{assemblyVersionInfo.Name} {assemblyVersionInfo.Version.ToString(3)}\");");
writer.WriteLine("helpBuilder.AppendLine(\"Copyright (C) \" + global::System.DateTime.UtcNow.Year.ToString());");
writer.WriteLine("helpBuilder.AppendLine();");
writer.WriteLine("if ((object)errors != null)");
writer.OpenBlock();
writer.WriteLine("helpBuilder.AppendLine();");
writer.WriteLine("helpBuilder.AppendLine(\"ERROR(S):\");");
writer.WriteLine("foreach (global::ArgumentParsing.Results.Errors.ParseError error in errors)");
writer.OpenBlock();
writer.WriteLine("helpBuilder.AppendLine(\" \" + error.GetMessage());");
writer.CloseBlock();
writer.WriteLine("helpBuilder.AppendLine();");
writer.CloseBlock();
if (optionInfos.Any())
{
writer.WriteLine("helpBuilder.AppendLine();");
writer.WriteLine("helpBuilder.AppendLine(\"OPTIONS:\");");
foreach (var info in optionInfos)
{
Expand Down Expand Up @@ -1007,12 +1007,28 @@ private static void EmitHelpCommandHandler(SourceProductionContext context, (Opt
}
writer.WriteLine("\");");
}
writer.WriteLine("helpBuilder.AppendLine();");
writer.WriteLine("helpBuilder.AppendLine(\"COMMANDS:\");");
writer.WriteLine("helpBuilder.AppendLine();");
writer.WriteLine("helpBuilder.AppendLine(\" --help\\tShow help screen\");");
writer.WriteLine("helpBuilder.AppendLine();");
writer.WriteLine("helpBuilder.AppendLine(\" --version\\tShow version information\");");
if (builtInCommandHandlers.HasFlag(BuiltInCommandHandlers.Help) ||
builtInCommandHandlers.HasFlag(BuiltInCommandHandlers.Version) ||
!additionalCommandHandlers.IsEmpty)
{
writer.WriteLine("helpBuilder.AppendLine();");
writer.WriteLine("helpBuilder.AppendLine(\"COMMANDS:\");");
if (builtInCommandHandlers.HasFlag(BuiltInCommandHandlers.Help))
{
writer.WriteLine("helpBuilder.AppendLine();");
writer.WriteLine("helpBuilder.AppendLine(\" --help\\tShow help screen\");");
}
if (builtInCommandHandlers.HasFlag(BuiltInCommandHandlers.Version))
{
writer.WriteLine("helpBuilder.AppendLine();");
writer.WriteLine("helpBuilder.AppendLine(\" --version\\tShow version information\");");
}
foreach (var additionalHandler in additionalCommandHandlers)
{
writer.WriteLine("helpBuilder.AppendLine();");
writer.WriteLine($"helpBuilder.AppendLine(\" {string.Join(", ", additionalHandler.Aliases)}\");");
}
}
writer.WriteLine("return helpBuilder.ToString();");
writer.CloseBlock();
writer.WriteLine();
Expand Down
5 changes: 2 additions & 3 deletions src/ArgumentParsing.Generators/ArgumentParserGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ public void Initialize(IncrementalGeneratorInitializationContext context)

var optionsHelpInfos = argumentParserInfos
.Where(info => info.BuiltInCommandHandlers.HasFlag(BuiltInCommandHandlers.Help))
.Select((info, _) => info.OptionsInfo)
.WithComparer(HelpOnlyOptionsInfoComparer.Instance)
.Combine(assemblyVersionInfo);
.Combine(assemblyVersionInfo)
.Select((pair, _) => new ArgumentParserHelpInfo(pair.Left.OptionsInfo, pair.Left.BuiltInCommandHandlers, pair.Left.AdditionalCommandHandlersInfos, pair.Right));

context.RegisterSourceOutput(optionsHelpInfos, EmitHelpCommandHandler);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using ArgumentParsing.Generators.Utils;

namespace ArgumentParsing.Generators.Models;

internal sealed record ArgumentParserHelpInfo(
OptionsInfo OptionsInfo,
BuiltInCommandHandlers BuiltInCommandHandlers,
ImmutableEquatableArray<SpecialCommandHandlerInfo> AdditionalCommandHandlersInfos,
AssemblyVersionInfo AssemblyVersionInfo);

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using ArgumentParsing.Generated;
using ArgumentParsing.Results;
using ArgumentParsing.SpecialCommands;

namespace ArgumentParsing.Tests.Functional;

[Collection("UsesSTDIO")]
public sealed partial class DefaultHelpScreenWithNonDefaultSpecialCommandHandlersTests
{
#region OptionsAndParser
[OptionsType]
private sealed class Options
{
}

[SpecialCommandAliases("--my-command")]
public sealed class MySpecialCommandHandler : ISpecialCommandHandler
{
public int HandleCommand() => 0;
}

[GeneratedArgumentParser(BuiltInCommandHandlers = BuiltInCommandHandlers.Help, AdditionalCommandHandlers = [typeof(MySpecialCommandHandler)])]
private static partial ParseResult<Options> ParseArguments(string[] args);
#endregion

[Fact]
public void ShowCorrectCommandsOnHelpScreen()
{
var result = ParseArguments(["--help"]);

Assert.Equal(ParseResultState.ParsedSpecialCommand, result.State);

Assert.Null(result.Options);
Assert.Null(result.Errors);

var helpCommandHandler = Assert.IsType<HelpCommandHandler_ArgumentParsing_Tests_Functional_DefaultHelpScreenWithNonDefaultSpecialCommandHandlersTests_Options>(result.SpecialCommandHandler);

using var stringWriter = new StringWriter();
Console.SetOut(stringWriter);

var exitCode = helpCommandHandler.HandleCommand();

Assert.Equal(0, exitCode);

var assemblyName = typeof(Options).Assembly.GetName();
var expectedOutput = $"""
{assemblyName.Name} {assemblyName.Version!.ToString(3)}
Copyright (C) {DateTime.UtcNow.Year}
COMMANDS:
--help{'\t'}Show help screen
--my-command
""";
var actualOutput = stringWriter.ToString();
Assert.Equal(expectedOutput.ReplaceLineEndings() + Environment.NewLine, actualOutput.ReplaceLineEndings());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1340,15 +1340,14 @@ public static string GenerateHelpText(global::ArgumentParsing.Results.Errors.Par
global::System.Text.StringBuilder helpBuilder = new();
helpBuilder.AppendLine("TestProject 0.0.0");
helpBuilder.AppendLine("Copyright (C) " + global::System.DateTime.UtcNow.Year.ToString());
helpBuilder.AppendLine();
if ((object)errors != null)
{
helpBuilder.AppendLine();
helpBuilder.AppendLine("ERROR(S):");
foreach (global::ArgumentParsing.Results.Errors.ParseError error in errors)
{
helpBuilder.AppendLine(" " + error.GetMessage());
}
helpBuilder.AppendLine();
}
helpBuilder.AppendLine();
helpBuilder.AppendLine("COMMANDS:");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1164,16 +1164,16 @@ public static string GenerateHelpText(global::ArgumentParsing.Results.Errors.Par
global::System.Text.StringBuilder helpBuilder = new();
helpBuilder.AppendLine("TestProject 0.0.0");
helpBuilder.AppendLine("Copyright (C) " + global::System.DateTime.UtcNow.Year.ToString());
helpBuilder.AppendLine();
if ((object)errors != null)
{
helpBuilder.AppendLine();
helpBuilder.AppendLine("ERROR(S):");
foreach (global::ArgumentParsing.Results.Errors.ParseError error in errors)
{
helpBuilder.AppendLine(" " + error.GetMessage());
}
helpBuilder.AppendLine();
}
helpBuilder.AppendLine();
helpBuilder.AppendLine("OPTIONS:");
helpBuilder.AppendLine();
helpBuilder.AppendLine(" --option");
Expand Down Expand Up @@ -1454,16 +1454,16 @@ public static string GenerateHelpText(global::ArgumentParsing.Results.Errors.Par
global::System.Text.StringBuilder helpBuilder = new();
helpBuilder.AppendLine("TestProject 0.0.0");
helpBuilder.AppendLine("Copyright (C) " + global::System.DateTime.UtcNow.Year.ToString());
helpBuilder.AppendLine();
if ((object)errors != null)
{
helpBuilder.AppendLine();
helpBuilder.AppendLine("ERROR(S):");
foreach (global::ArgumentParsing.Results.Errors.ParseError error in errors)
{
helpBuilder.AppendLine(" " + error.GetMessage());
}
helpBuilder.AppendLine();
}
helpBuilder.AppendLine();
helpBuilder.AppendLine("OPTIONS:");
helpBuilder.AppendLine();
helpBuilder.AppendLine(" --option");
Expand Down

0 comments on commit 8a7619e

Please sign in to comment.