Skip to content

Commit 0a51fd3

Browse files
committed
WIP building help for GlobalOptions
1 parent bf1744b commit 0a51fd3

File tree

3 files changed

+50
-15
lines changed

3 files changed

+50
-15
lines changed

sandbox/GeneratorSandbox/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using Microsoft.Extensions.DependencyInjection;
33
using Microsoft.Extensions.Logging;
44

5-
args = ["--x", "10", "--y", "20", "-v", "--prefix-output", "takoyakix"];
5+
//args = ["--x", "10", "--y", "20", "-v", "--prefix-output", "takoyakix"];
66

77
var app = ConsoleApp.Create();
88

src/ConsoleAppFramework/Command.cs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -472,11 +472,36 @@ public record class GlobalOptionInfo
472472

473473
public CommandParameter ToDummyCommandParameter()
474474
{
475-
//return new CommandParameter
476-
{
477-
/// DefaultValue
478-
}
475+
// see: CommandHelpBuilder.CreateCommandHelpDefinition / BuildOptionsMessage
476+
477+
// IsParsable, name + aliases, description, HasDefaultValue/DefaultValue, IsHidden, IsParams
479478

480-
throw new NotImplementedException();
479+
// TODO: IsRequired => DefaultValue == null && !IsParams
480+
481+
return new CommandParameter
482+
{
483+
// no need
484+
Location = default,
485+
WellKnownTypes = default,
486+
IsNullableReference = false, // not supported
487+
IsParams = false,
488+
IsHidden = false,
489+
OriginalParameterName = "",
490+
CustomParserType = null,
491+
IsFromServices = false,
492+
IsFromKeyedServices = false,
493+
KeyedServiceKey = null,
494+
IsConsoleAppContext = false,
495+
IsCancellationToken = false,
496+
HasValidation = false,
497+
ArgumentIndex = -1,
498+
499+
Type = Type,
500+
HasDefaultValue = DefaultValue != null, // TODO
501+
DefaultValue = DefaultValue,
502+
Description = Description,
503+
Name = Name, // TODO: convert with aliase
504+
Aliases = []
505+
};
481506
}
482507
}

src/ConsoleAppFramework/ConsoleAppGenerator.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
139139

140140
var expr = invocationExpression.Expression as MemberAccessExpressionSyntax;
141141
var methodName = expr?.Name.Identifier.Text;
142-
if (methodName is "Add" or "UseFilter" or "Run" or "RunAsync" or "ConfigureGlobalOption")
142+
if (methodName is "Add" or "UseFilter" or "Run" or "RunAsync" or "ConfigureGlobalOptions")
143143
{
144144
return true;
145145
}
@@ -295,7 +295,17 @@ static void EmitConsoleAppBuilder(SourceProductionContext sourceProductionContex
295295
using (help.BeginBlock("internal static partial class ConsoleApp"))
296296
using (help.BeginBlock("internal partial class ConsoleAppBuilder"))
297297
{
298-
// TODO: collectBuilderContext.GlobalOptions
298+
if (collectBuilderContext.GlobalOptions.Length != 0)
299+
{
300+
// override commandIds
301+
var globalOptionParameters = collectBuilderContext.GlobalOptions.Select(x => x.ToDummyCommandParameter());
302+
commandIds = commandIds.Select(x =>
303+
{
304+
var newCommand = x.Command with { Parameters = new(x.Command.Parameters.Concat(globalOptionParameters).ToArray()) };
305+
return x with { Command = newCommand };
306+
})
307+
.ToArray();
308+
}
299309

300310
var emitter = new Emitter(dllReference);
301311
emitter.EmitHelp(help, commandIds!);
@@ -482,7 +492,7 @@ public CollectBuilderContext(ConsoleAppFrameworkGeneratorOptions generatorOption
482492
return commands;
483493
});
484494

485-
var configureGlobalOptionsGroup = methodGroup["ConfigureGlobalOption"];
495+
var configureGlobalOptionsGroup = methodGroup["ConfigureGlobalOptions"];
486496
if (configureGlobalOptionsGroup.Count() >= 2)
487497
{
488498
// TODO: Diagnostics
@@ -547,29 +557,29 @@ public CollectBuilderContext(ConsoleAppFrameworkGeneratorOptions generatorOption
547557
}
548558

549559
var arguments = node.ArgumentList.Arguments;
550-
if (arguments.Count >= 2) // string name
560+
if (arguments.Count >= 1) // string name
551561
{
552-
var constant = model.GetConstantValue(arguments[1].Expression); // TODO: check
562+
var constant = model.GetConstantValue(arguments[0].Expression); // TODO: check
553563
name = constant.Value!.ToString();
554564
}
555565

556566

557567
// TODO: use named argument???
558568

559-
if (arguments.Count >= 3) // string description = ""
569+
if (arguments.Count >= 2) // string description = ""
560570
{
561571
// is defaultValue???
562572

563573

564-
var constant = model.GetConstantValue(arguments[2].Expression);
574+
var constant = model.GetConstantValue(arguments[1].Expression);
565575
description = constant.Value!.ToString();
566576
}
567577

568578
if (!isRequired)
569579
{
570-
if (arguments.Count >= 4) // T defaultValue = default(T)
580+
if (arguments.Count >= 3) // T defaultValue = default(T)
571581
{
572-
var constant = model.GetConstantValue(arguments[3].Expression);
582+
var constant = model.GetConstantValue(arguments[2].Expression); // ???
573583
defaultValue = constant.Value!;
574584
}
575585
else

0 commit comments

Comments
 (0)