| | | 1 | | namespace ZhEaIsNsAaBn.DbSeeder.Tool; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// <c>dbseeder [tool options] <command> [command options] [-- app args]</c>. Tool options come before the command |
| | | 5 | | /// the command on goes to the DbSeeder commands unchanged, except what follows <c>--</c> (the app's entry point and fac |
| | | 6 | | /// </summary> |
| | | 7 | | internal sealed record ToolOptions |
| | | 8 | | { |
| | | 9 | | public string? Project { get; private init; } |
| | | 10 | | public string? StartupProject { get; private init; } |
| | | 11 | | public string? Context { get; private init; } |
| | | 12 | | public string Configuration { get; private init; } = "Debug"; |
| | | 13 | | public string? Framework { get; private init; } |
| | | 14 | | public string? Environment { get; private init; } |
| | | 15 | | public bool NoBuild { get; private init; } |
| | | 16 | | public bool Verbose { get; private init; } |
| | | 17 | | public bool Help { get; private init; } |
| | | 18 | | public bool Version { get; private init; } |
| | | 19 | | |
| | | 20 | | public IReadOnlyList<string> Command { get; private init; } = []; |
| | | 21 | | public IReadOnlyList<string> AppArgs { get; private init; } = []; |
| | | 22 | | |
| | | 23 | | |
| | | 24 | | public const string Usage = """ |
| | | 25 | | Usage: dbseeder [options] <command> [command options] [-- app args] |
| | | 26 | | |
| | | 27 | | Options (before the command): |
| | | 28 | | -p|--project <path> Project with .dbseeder/ and the migrations folders (defau |
| | | 29 | | -s|--startup-project <path> Project the app runs from: host or IDesignTimeDbContextFa |
| | | 30 | | -c|--context <name> DbContext (short or full name) when there is more than on |
| | | 31 | | -f|--framework <tfm> Target framework, required when the startup project has s |
| | | 32 | | --configuration <name> Build configuration (default: Debug) |
| | | 33 | | -e|--environment <name> DOTNET_ENVIRONMENT / ASPNETCORE_ENVIRONMENT for the app ( |
| | | 34 | | --no-build Use the last build |
| | | 35 | | -v|--verbose Show the build output and the commands run |
| | | 36 | | --version Show the tool version |
| | | 37 | | -h|--help Show this help |
| | | 38 | | |
| | | 39 | | Commands: profile, baseline, add, snapshot, validate, list, update, down, script. |
| | | 40 | | Run "dbseeder <command> --help" for a command's options. |
| | | 41 | | """; |
| | | 42 | | |
| | | 43 | | |
| | | 44 | | public static bool TryParse( IReadOnlyList<string> args, out ToolOptions options, out string error ) |
| | | 45 | | { |
| | 24 | 46 | | options = new ToolOptions(); |
| | 24 | 47 | | error = ""; |
| | | 48 | | |
| | 24 | 49 | | var i = 0; |
| | | 50 | | |
| | 96 | 51 | | for ( ; i < args.Count && args[ i ].StartsWith( '-' ) && args[ i ] != "--"; i++ ) |
| | | 52 | | { |
| | 44 | 53 | | var name = args[ i ]; |
| | | 54 | | |
| | | 55 | | switch ( name ) |
| | | 56 | | { |
| | 8 | 57 | | case "--no-build": options = options with { NoBuild = true }; continue; |
| | 8 | 58 | | case "-v" or "--verbose": options = options with { Verbose = true }; continue; |
| | 0 | 59 | | case "-h" or "--help": options = options with { Help = true }; continue; |
| | 0 | 60 | | case "--version": options = options with { Version = true }; continue; |
| | | 61 | | } |
| | | 62 | | |
| | 36 | 63 | | if ( name is not ("-p" or "--project" or "-s" or "--startup-project" or "-c" or "--context" or "-f" or "--fr |
| | 36 | 64 | | or "--environment") ) |
| | | 65 | | { |
| | 4 | 66 | | error = $"Unknown option {name}. Tool options go before the command; see dbseeder --help."; |
| | | 67 | | |
| | 4 | 68 | | return false; |
| | | 69 | | } |
| | | 70 | | |
| | 32 | 71 | | if ( i + 1 >= args.Count || args[ i + 1 ].StartsWith( '-' ) ) |
| | | 72 | | { |
| | 4 | 73 | | error = $"{name} needs a value."; |
| | | 74 | | |
| | 4 | 75 | | return false; |
| | | 76 | | } |
| | | 77 | | |
| | 28 | 78 | | var value = args[ ++i ]; |
| | | 79 | | |
| | 28 | 80 | | options = name switch |
| | 28 | 81 | | { |
| | 4 | 82 | | "-p" or "--project" => options with { Project = value }, |
| | 4 | 83 | | "-s" or "--startup-project" => options with { StartupProject = value }, |
| | 4 | 84 | | "-c" or "--context" => options with { Context = value }, |
| | 4 | 85 | | "-f" or "--framework" => options with { Framework = value }, |
| | 8 | 86 | | "-e" or "--environment" => options with { Environment = value }, |
| | 4 | 87 | | _ => options with { Configuration = value } |
| | 28 | 88 | | }; |
| | | 89 | | } |
| | | 90 | | |
| | 16 | 91 | | var rest = args.Skip( i ).ToList(); |
| | 16 | 92 | | var separator = rest.IndexOf( "--" ); |
| | | 93 | | |
| | 16 | 94 | | options = separator < 0 |
| | 16 | 95 | | ? options with { Command = rest } |
| | 16 | 96 | | : options with { Command = rest[ ..separator ], AppArgs = rest[ ( separator + 1 ).. ] }; |
| | | 97 | | |
| | 16 | 98 | | return true; |
| | | 99 | | } |
| | | 100 | | } |