|
| 1 | +using BitNetSharp.Core; |
| 2 | +using System.Text.Json; |
| 3 | + |
| 4 | +namespace BitNetSharp.App; |
| 5 | + |
| 6 | +public sealed record DataGenCommandOptions( |
| 7 | + string Domain, |
| 8 | + int Count, |
| 9 | + string SeedsPath, |
| 10 | + string OutputPath, |
| 11 | + string? LoraPath) |
| 12 | +{ |
| 13 | + public static DataGenCommandOptions Parse(string[] args) |
| 14 | + { |
| 15 | + ArgumentNullException.ThrowIfNull(args); |
| 16 | + |
| 17 | + var domain = ReadRequiredOption(args, "--domain"); |
| 18 | + var seedsPath = ReadRequiredOption(args, "--seeds"); |
| 19 | + var outputPath = ReadRequiredOption(args, "--output"); |
| 20 | + var countValue = ReadRequiredOption(args, "--count"); |
| 21 | + |
| 22 | + if (!int.TryParse(countValue, out var count) || count <= 0) |
| 23 | + { |
| 24 | + throw new ArgumentException("The datagen count must be a positive integer.", nameof(args)); |
| 25 | + } |
| 26 | + |
| 27 | + return new DataGenCommandOptions( |
| 28 | + domain, |
| 29 | + count, |
| 30 | + Path.GetFullPath(seedsPath), |
| 31 | + Path.GetFullPath(outputPath), |
| 32 | + ReadOptionalOption(args, "--lora")); |
| 33 | + } |
| 34 | + |
| 35 | + private static string ReadRequiredOption(string[] args, string optionName) |
| 36 | + { |
| 37 | + var value = ReadOptionalOption(args, optionName); |
| 38 | + if (value is null) |
| 39 | + { |
| 40 | + throw new ArgumentException($"Missing required option '{optionName}'.", nameof(args)); |
| 41 | + } |
| 42 | + |
| 43 | + if (string.IsNullOrWhiteSpace(value)) |
| 44 | + { |
| 45 | + throw new ArgumentException($"Option '{optionName}' requires a non-empty value.", nameof(args)); |
| 46 | + } |
| 47 | + |
| 48 | + return value; |
| 49 | + } |
| 50 | + |
| 51 | + private static string? ReadOptionalOption(string[] args, string optionName) |
| 52 | + { |
| 53 | + var equalsPrefix = $"{optionName}="; |
| 54 | + var missingValueDetected = false; |
| 55 | + for (var index = 0; index < args.Length; index++) |
| 56 | + { |
| 57 | + var argument = args[index]; |
| 58 | + if (argument.StartsWith(equalsPrefix, StringComparison.OrdinalIgnoreCase)) |
| 59 | + { |
| 60 | + return argument[equalsPrefix.Length..]; |
| 61 | + } |
| 62 | + |
| 63 | + if (string.Equals(argument, optionName, StringComparison.OrdinalIgnoreCase)) |
| 64 | + { |
| 65 | + var nextIndex = index + 1; |
| 66 | + if (nextIndex >= args.Length) |
| 67 | + { |
| 68 | + missingValueDetected = true; |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + var nextArgument = args[nextIndex]; |
| 73 | + if (nextArgument.StartsWith("--", StringComparison.Ordinal)) |
| 74 | + { |
| 75 | + missingValueDetected = true; |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + return nextArgument; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + if (missingValueDetected) |
| 84 | + { |
| 85 | + throw new ArgumentException($"Option '{optionName}' requires a value.", nameof(args)); |
| 86 | + } |
| 87 | + |
| 88 | + return null; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +public static class DataGenCommand |
| 93 | +{ |
| 94 | + private static readonly JsonSerializerOptions OutputJsonOptions = new() |
| 95 | + { |
| 96 | + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
| 97 | + WriteIndented = false |
| 98 | + }; |
| 99 | + |
| 100 | + public static async Task<string> RunAsync(string[] args, VerbosityLevel verbosity, CancellationToken cancellationToken = default) |
| 101 | + { |
| 102 | + var options = DataGenCommandOptions.Parse(args); |
| 103 | + var seeds = DataGenGenerator.LoadSeeds(options.SeedsPath); |
| 104 | + var generator = new DataGenGenerator(BitNetBootstrap.CreatePaperModel(verbosity)); |
| 105 | + var outputDirectory = Path.GetDirectoryName(options.OutputPath); |
| 106 | + |
| 107 | + if (!string.IsNullOrWhiteSpace(outputDirectory)) |
| 108 | + { |
| 109 | + Directory.CreateDirectory(outputDirectory); |
| 110 | + } |
| 111 | + |
| 112 | + await using var stream = File.Create(options.OutputPath); |
| 113 | + await using var writer = new StreamWriter(stream); |
| 114 | + |
| 115 | + foreach (var example in generator.Generate(options.Domain, options.Count, seeds, options.LoraPath)) |
| 116 | + { |
| 117 | + cancellationToken.ThrowIfCancellationRequested(); |
| 118 | + var line = JsonSerializer.Serialize(example, OutputJsonOptions); |
| 119 | + await writer.WriteLineAsync(line.AsMemory(), cancellationToken); |
| 120 | + } |
| 121 | + |
| 122 | + return options.OutputPath; |
| 123 | + } |
| 124 | +} |
0 commit comments