Skip to content

Latest commit

 

History

History
49 lines (40 loc) · 1.47 KB

command-line-parser.md

File metadata and controls

49 lines (40 loc) · 1.47 KB

Cli with CommandLineParser

Install

  • CommandLineParser

Then parse the argument

private const string MigrateUp = "migrate-up";
private const string MigrateDown = "migrate-down";

static void Main(string[] args)
{
    Parser.Default.ParseArguments<MigrationOptions>(args)
        .WithParsed(x =>
        {
            if (string.IsNullOrEmpty(x.Command))
            {
                Console.WriteLine("Available commands:\n");
                Console.WriteLine($"  dotnet run");
                Console.WriteLine($"    --command {MigrateUp}");
                Console.WriteLine($"    --command {MigrateDown} --version 0");
                return;
            }

            var sections = GetConnectionStrings();

            foreach (var section in sections)
            {
                var serviceProvider = CreateServices(section.Value);
                using var scope = serviceProvider.CreateScope();
                var runner = serviceProvider.GetRequiredService<IMigrationRunner>();

                switch (x.Command)
                {
                    case MigrateUp:
                        Console.WriteLine($"Migrating up for {section.Key} ...");
                        runner.MigrateUp();
                        break;
                    default:
                        Console.WriteLine($"Migrating down for {section.Key} ...");
                        runner.MigrateDown(x.Version);
                        break;
                }
            }
        });