|
| 1 | +# 源生成器生成的源示例 |
| 2 | + |
| 3 | +代码中的类型: |
| 4 | + |
| 5 | +```csharp |
| 6 | +internal record DotNet03_MixedOptions |
| 7 | +{ |
| 8 | + [Option] |
| 9 | + public int Number { get; init; } |
| 10 | + |
| 11 | + [Option] |
| 12 | + public required string Text { get; init; } |
| 13 | + |
| 14 | + [Option] |
| 15 | + public bool Flag { get; init; } |
| 16 | +} |
| 17 | +``` |
| 18 | + |
| 19 | +对应生成的源: |
| 20 | + |
| 21 | +```csharp |
| 22 | +#nullable enable |
| 23 | +namespace DotNetCampus.Cli.Tests; |
| 24 | + |
| 25 | +/// <summary> |
| 26 | +/// 辅助 <see cref="global::DotNetCampus.Cli.Tests.DotNet03_MixedOptions"/> 生成命令行选项、谓词或处理函数的创建。 |
| 27 | +/// </summary> |
| 28 | +internal sealed class DotNet03_MixedOptionsBuilder |
| 29 | +{ |
| 30 | + public static object CreateInstance(global::DotNetCampus.Cli.CommandLine commandLine) |
| 31 | + { |
| 32 | + var caseSensitive = commandLine.DefaultCaseSensitive; |
| 33 | + var result = new global::DotNetCampus.Cli.Tests.DotNet03_MixedOptions |
| 34 | + { |
| 35 | + Number = commandLine.GetOption("number") ?? default, |
| 36 | + Text = commandLine.GetOption("text") ?? throw new global::DotNetCampus.Cli.Exceptions.RequiredPropertyNotAssignedException($"The command line arguments doesn't contain a required option '--text'. Command line: {commandLine}", "Text"), |
| 37 | + Flag = commandLine.GetOption("flag") ?? default, |
| 38 | + // There is no positional argument to be initialized. |
| 39 | + }; |
| 40 | + // There is no option to be assigned. |
| 41 | + // There is no positional argument to be assigned. |
| 42 | + return result; |
| 43 | + } |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +代码中的方法调用: |
| 48 | + |
| 49 | +```csharp |
| 50 | +_ = CommandLine.Parse(args, CommandLineParsingOptions.DotNet).As<Options>(); |
| 51 | +``` |
| 52 | + |
| 53 | +对应生成的源(拦截器): |
| 54 | + |
| 55 | +```csharp |
| 56 | +#nullable enable |
| 57 | + |
| 58 | +namespace DotNetCampus.CommandLine.Compiler |
| 59 | +{ |
| 60 | + file static class Interceptors |
| 61 | + { |
| 62 | + /// <summary> |
| 63 | + /// <see cref="global::DotNetCampus.Cli.CommandLine.As{Options}()"/> 方法的拦截器。拦截以提高性能。 |
| 64 | + /// </summary> |
| 65 | + [global::System.Runtime.CompilerServices.InterceptsLocation(1, /* Program.Run4xInterceptor @Program.cs */ "G4GJAK7udHFnPkRUqV6VzzgRAABQcm9ncmFtLmNz")] |
| 66 | + [global::System.Runtime.CompilerServices.InterceptsLocation(1, /* Program.Run4xModule @Program.cs */ "G4GJAK7udHFnPkRUqV6VzxkSAABQcm9ncmFtLmNz")] |
| 67 | + public static T CommandLine_As_DotNetCampusCliTestsFakesOptions<T>(this global::DotNetCampus.Cli.CommandLine commandLine) |
| 68 | + where T : global::DotNetCampus.Cli.Tests.Fakes.Options |
| 69 | + { |
| 70 | + return (T)global::DotNetCampus.Cli.Tests.Fakes.OptionsBuilder.CreateInstance(commandLine); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +namespace System.Runtime.CompilerServices |
| 76 | +{ |
| 77 | + [global::System.AttributeUsage(global::System.AttributeTargets.Method, AllowMultiple = true)] |
| 78 | + file sealed class InterceptsLocationAttribute : global::System.Attribute |
| 79 | + { |
| 80 | + public InterceptsLocationAttribute(int version, string data) |
| 81 | + { |
| 82 | + _ = version; |
| 83 | + _ = data; |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +代码中的程序集命令处理器搜集: |
| 90 | + |
| 91 | +```csharp |
| 92 | +[CollectCommandHandlersFromThisAssembly] |
| 93 | +internal partial class AssemblyCommandHandler; |
| 94 | +``` |
| 95 | + |
| 96 | +对应生成的源: |
| 97 | + |
| 98 | +```csharp |
| 99 | +#nullable enable |
| 100 | +namespace DotNetCampus.Cli.Tests.Fakes; |
| 101 | + |
| 102 | +/// <summary> |
| 103 | +/// 提供一种辅助自动搜集并执行本程序集中所有命令行处理器的方式。 |
| 104 | +/// </summary> |
| 105 | +partial class AssemblyCommandHandler : global::DotNetCampus.Cli.Compiler.ICommandHandlerCollection |
| 106 | +{ |
| 107 | + public global::DotNetCampus.Cli.ICommandHandler? TryMatch(string? verb, global::DotNetCampus.Cli.CommandLine cl) => verb switch |
| 108 | + { |
| 109 | + null => throw new global::DotNetCampus.Cli.Exceptions.CommandVerbAmbiguityException($"Multiple command handlers match the same verb name 'null': AmbiguousOptions, CollectionOptions, ComparedOptions, DefaultVerbCommandHandler, DictionaryOptions, FakeCommandOptions, Options, PrimaryOptions, UnlimitedValueOptions, ValueOptions.", null), |
| 110 | + // 类型 EditOptions 没有继承 ICommandHandler 接口,因此无法统一调度执行,只能由开发者单独调用。 |
| 111 | + "Fake" => (global::DotNetCampus.Cli.ICommandHandler)global::DotNetCampus.Cli.Tests.Fakes.FakeVerbCommandHandlerBuilder.CreateInstance(cl), |
| 112 | + // 类型 PrintOptions 没有继承 ICommandHandler 接口,因此无法统一调度执行,只能由开发者单独调用。 |
| 113 | + // 类型 ShareOptions 没有继承 ICommandHandler 接口,因此无法统一调度执行,只能由开发者单独调用。 |
| 114 | + _ => null, |
| 115 | + }; |
| 116 | +} |
| 117 | +``` |
0 commit comments