Skip to content

Commit fa906e5

Browse files
committed
WIP refactoring(compile error)
1 parent bfe9470 commit fa906e5

File tree

6 files changed

+132
-211
lines changed

6 files changed

+132
-211
lines changed

sandbox/GeneratorSandbox/Program.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
app.ConfigureGlobalOptions((ref ConsoleApp.GlobalOptionsBuilder builder) =>
1515
{
16-
var verbose = builder.AddGlobalOption($"-v", "", true);
17-
var noColor = builder.AddGlobalOption("--no-color", "Don't colorize output.");
18-
var dryRun = builder.AddGlobalOption("--dry-run", "");
16+
var verbose = builder.AddGlobalOption<bool>($"-v", "");
17+
var noColor = builder.AddGlobalOption<bool>("--no-color", "Don't colorize output.");
18+
var dryRun = builder.AddGlobalOption<bool>("--dry-run", "");
1919
var prefixOutput = builder.AddRequiredGlobalOption<string>("--prefix-output|-pp|-po", "Prefix output with level.");
2020

2121
// var tako = builder.AddGlobalOption<int>("--in", "");
@@ -73,7 +73,7 @@ internal class MyCommand(GlobalOptions globalOptions)
7373
/// <param name="xxx">-x, takoyaki</param>
7474
/// <param name="yyyy">-yy|-y, naninuneno</param>
7575
[Command("")]
76-
public void Run(int xxx, int yyyy, bool z, bool zzz, Int128 iiii, MyFruit myFruit = default, Version version = null)
76+
public void Run(int xxx, int yyyy, bool z, bool zzz, Int128 iiii, int? takoyaki = null, MyFruit myFruit = default, Version version = null)
7777
{
7878
Console.WriteLine(xxx + yyyy + ":" + globalOptions);
7979
}

src/ConsoleAppFramework/Command.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -473,10 +473,6 @@ public record class GlobalOptionInfo
473473
public CommandParameter ToDummyCommandParameter()
474474
{
475475
// see: CommandHelpBuilder.CreateCommandHelpDefinition / BuildOptionsMessage
476-
// IsParsable, name + aliases, description, HasDefaultValue/DefaultValue, IsHidden, IsParams
477-
478-
// TODO: IsRequired => DefaultValue == null && !IsParams
479-
480476
return new CommandParameter
481477
{
482478
// no need
@@ -496,7 +492,7 @@ public CommandParameter ToDummyCommandParameter()
496492
ArgumentIndex = -1,
497493

498494
Type = Type,
499-
HasDefaultValue = DefaultValue != null, // TODO
495+
HasDefaultValue = !IsRequired, // if not required, needs defaultValue
500496
DefaultValue = DefaultValue,
501497
Description = Description,
502498
Name = null!, // allows null in CreateCommandHelpDefinition

src/ConsoleAppFramework/ConsoleAppBaseCode.cs

Lines changed: 2 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -570,12 +570,7 @@ public GlobalOptionsBuilder(Span<string> args)
570570
this.args = args;
571571
}
572572
573-
public bool AddGlobalOption([ConstantExpected] string name, [ConstantExpected] string description)
574-
{
575-
return AddGlobalOption<bool>(name, description, false);
576-
}
577-
578-
public T AddGlobalOption<T>([ConstantExpected] string name, [ConstantExpected] string description, [ConstantExpected] T defaultValue)
573+
public T AddGlobalOption<T>([ConstantExpected] string name, [ConstantExpected] string description = "", [ConstantExpected] T defaultValue = default(T))
579574
{
580575
var aliasCount = name.AsSpan().Count("|") + 1;
581576
if (aliasCount == 1)
@@ -619,7 +614,7 @@ public T AddGlobalOption<T>([ConstantExpected] string name, [ConstantExpected] s
619614
return defaultValue;
620615
}
621616
622-
public T AddRequiredGlobalOption<T>([ConstantExpected] string name, [ConstantExpected] string description)
617+
public T AddRequiredGlobalOption<T>([ConstantExpected] string name, [ConstantExpected] string description = "")
623618
{
624619
if (typeof(T) == typeof(bool)) throw new InvalidOperationException("<bool> can not use in AddRequiredGlobalOption. use AddGlobalOption instead.");
625620
@@ -822,66 +817,6 @@ static bool TryParse<T>(string s, out T result)
822817
result = default;
823818
return false;
824819
}
825-
else if (typeof(T) == typeof(DateTime))
826-
{
827-
if (DateTime.TryParse(s, out var v))
828-
{
829-
result = Unsafe.As<DateTime, T>(ref v);
830-
return true;
831-
}
832-
result = default;
833-
return false;
834-
}
835-
else if (typeof(T) == typeof(DateTimeOffset))
836-
{
837-
if (DateTimeOffset.TryParse(s, out var v))
838-
{
839-
result = Unsafe.As<DateTimeOffset, T>(ref v);
840-
return true;
841-
}
842-
result = default;
843-
return false;
844-
}
845-
else if (typeof(T) == typeof(TimeOnly))
846-
{
847-
if (TimeOnly.TryParse(s, out var v))
848-
{
849-
result = Unsafe.As<TimeOnly, T>(ref v);
850-
return true;
851-
}
852-
result = default;
853-
return false;
854-
}
855-
else if (typeof(T) == typeof(DateOnly))
856-
{
857-
if (DateOnly.TryParse(s, out var v))
858-
{
859-
result = Unsafe.As<DateOnly, T>(ref v);
860-
return true;
861-
}
862-
result = default;
863-
return false;
864-
}
865-
else if (typeof(T) == typeof(Version))
866-
{
867-
if (System.Version.TryParse(s, out var v))
868-
{
869-
result = Unsafe.As<Version, T>(ref v);
870-
return true;
871-
}
872-
result = default;
873-
return false;
874-
}
875-
else if (typeof(T) == typeof(Guid))
876-
{
877-
if (Guid.TryParse(s, out var v))
878-
{
879-
result = Unsafe.As<Guid, T>(ref v);
880-
return true;
881-
}
882-
result = default;
883-
return false;
884-
}
885820
else
886821
{
887822
if (typeof(T).IsEnum)

src/ConsoleAppFramework/ConsoleAppGenerator.cs

Lines changed: 6 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -495,100 +495,21 @@ public CollectBuilderContext(ConsoleAppFrameworkGeneratorOptions generatorOption
495495
var configureGlobalOptionsGroup = methodGroup["ConfigureGlobalOptions"];
496496
if (configureGlobalOptionsGroup.Count() >= 2)
497497
{
498-
// TODO: Diagnostics
498+
// TODO: Diagnostics, not allows multiple configureglobaloptions.
499499
}
500500

501501
if (configureGlobalOptionsGroup.Count() == 1)
502502
{
503-
// TODO: move to Parser
504503
var configureGlobalOptions = configureGlobalOptionsGroup.First();
505504

506-
var lambdaExpr = (configureGlobalOptions.Item1.Node as InvocationExpressionSyntax);
505+
var node = configureGlobalOptions.Item1.Node;
506+
var model = configureGlobalOptions.Item1.Model;
507+
var wellKnownTypes = new WellKnownTypes(model.Compilation);
507508

508-
var symbolInfo = configureGlobalOptions.Item1.Model.GetSymbolInfo(lambdaExpr);
509-
if (symbolInfo.Symbol is IMethodSymbol methodSymbol)
510-
{
511-
ImmutableArray<ITypeSymbol> typeArguments = methodSymbol.TypeArguments;
512-
513-
if (typeArguments.Length > 0)
514-
{
515-
ITypeSymbol typeT = typeArguments[0];
516-
string typeName = typeT.ToDisplayString(); // TODO: get <T>.
517-
}
518-
}
519-
520-
var addOptions = configureGlobalOptions.Item1.Node
521-
.DescendantNodes()
522-
.OfType<InvocationExpressionSyntax>()
523-
.Select(x =>
524-
{
525-
var expr = x.Expression as MemberAccessExpressionSyntax;
526-
var methodName = expr?.Name.Identifier.Text;
527-
if (methodName is "AddGlobalOption")
528-
{
529-
return new { node = x, expr, required = false };
530-
}
531-
else if (methodName is "AddRequiredGlobalOption")
532-
{
533-
return new { node = x, expr, required = true };
534-
}
535-
536-
return null;
537-
})
538-
.Where(x => x != null);
539-
540-
GlobalOptions = addOptions
541-
.Select(x =>
542-
{
543-
var node = x!.node;
544-
var memberAccess = x.expr!;
545-
var model = configureGlobalOptions.Item1.Model;
546-
547-
EquatableTypeSymbol typeSymbol = default!;
548-
string name = "";
549-
string description = "";
550-
bool isRequired = x.required;
551-
object? defaultValue = null;
552-
bool isBool = false;
553-
554-
if (memberAccess.Name is GenericNameSyntax genericName)
555-
{
556-
var typeArgument = genericName.TypeArgumentList.Arguments[0];
557-
typeSymbol = new(model.GetTypeInfo(typeArgument).Type!); // TODO: not !
558-
}
559-
else
560-
{
561-
// maybe bool
562-
typeSymbol = new(model.Compilation.GetSpecialType(SpecialType.System_Boolean));
563-
isBool = true;
564-
}
565-
566-
var arguments = node.ArgumentList.Arguments;
567-
name = model.GetConstantValue(arguments[0].Expression).Value!.ToString();
568-
description = model.GetConstantValue(arguments[1].Expression).Value!.ToString();
569-
570-
if (!isRequired && !isBool)
571-
{
572-
var constant = model.GetConstantValue(arguments[2].Expression);
573-
defaultValue = constant.Value!;
574-
}
575-
576-
return new GlobalOptionInfo
577-
{
578-
Type = typeSymbol,
579-
IsRequired = isRequired,
580-
Name = name,
581-
Description = description,
582-
DefaultValue = defaultValue
583-
};
584-
})
585-
.Where(x => x != null)
586-
.ToArray();
509+
var parser = new Parser(generatorOptions, DiagnosticReporter, node, model, wellKnownTypes, DelegateBuildType.None, globalFilters);
510+
GlobalOptions = parser.ParseGlobalOptions();
587511
}
588512

589-
590-
591-
592513
if (DiagnosticReporter.HasDiagnostics)
593514
{
594515
return;

0 commit comments

Comments
 (0)