internal static bool DisplayHelp(string commandLine, out CommandLineOptions options, out CommandLineParser parser) { options = new CommandLineOptions(); parser = new CommandLineParser(commandLine, options); parser.Parse(); return options.Help; }
internal static MigrationOptions ParseCommandLineArguments(CommandLineOptions options, CommandLineParser parser, ConnectionStringSettingsCollection connectionStrings, out string connectionString, out DbPlatform dbPlatform, out string assemblyPath, out string[] additionalAssemblyPaths, out long timestamp, out SourceLevels traceLevels) { if (parser.Parameters.Length < 2 || // expect at least the target and one assemlby parser.UnhandledSwitches.Length > 0) { throw new InvalidCommandLineArgumentException("Invalid command line arguments. Specify at least the target and one assembly." + Environment.NewLine + Environment.NewLine + GetUsageMessage(parser), InvalidArgumentsExitCode); } // connection string string target = parser.Parameters[0]; ConnectionStringSettings settings = connectionStrings[target]; if (settings == null) { throw new InvalidCommandLineArgumentException(string.Format(CultureInfo.CurrentCulture, "Missing target: '{0}'. Could not find entry in the configuration file.", target), InvalidTargetExitCode); } connectionString = settings.ConnectionString; if (connectionString == null) { throw new InvalidCommandLineArgumentException(string.Format(CultureInfo.CurrentCulture, "Empty target: '{0}'. The entry in the configuration file is empty.", target), InvalidTargetExitCode); } // provider name dbPlatform = new DbPlatform(options.Platform, options.MajorVersion, options.Driver); // assembly paths assemblyPath = parser.Parameters[1]; additionalAssemblyPaths = parser.Parameters.Skip(2).ToArray(); // timestamp timestamp = long.MaxValue; if (options.To != null) { try { timestamp = long.Parse(options.To, CultureInfo.CurrentCulture); } catch (FormatException x) { throw new InvalidCommandLineArgumentException(string.Format(CultureInfo.CurrentCulture, "Could not parse timestamp: '{0}': {1}", options.To, x.Message), InvalidArgumentsExitCode); } } // trace level traceLevels = SourceLevels.Warning; if (!string.IsNullOrEmpty(options.TraceLevel)) { try { traceLevels = (SourceLevels)Enum.Parse(typeof(SourceLevels), options.TraceLevel, true); } catch (ArgumentException x) { throw new InvalidCommandLineArgumentException(string.Format(CultureInfo.CurrentCulture, "Could not parse traceLevel: '{0}': {1}", options.TraceLevel, x.Message), InvalidArgumentsExitCode); } } // // other migration options // var migrationOptions = !string.IsNullOrEmpty(options.Module) ? new MigrationOptions(options.Module) : new MigrationOptions(); // scripting if (!string.IsNullOrEmpty(options.ScriptTo)) { if (options.ScriptOnly) { migrationOptions.OnlyScriptSqlTo(options.ScriptTo); } else { migrationOptions.ExecuteAndScriptSqlTo(options.ScriptTo); } } else { if (options.ScriptOnly) { throw new InvalidCommandLineArgumentException("The -scriptOnly switch requires a -scriptTo argument.", InvalidArgumentsExitCode); } } // versioning table if (!string.IsNullOrEmpty(options.VersioningTable)) { migrationOptions.VersioningTableName = options.VersioningTable; } return migrationOptions; }
private static string GetUsageMessage(CommandLineParser parser) { string usage = "Migrate.exe <target> <assembly> [..<assembly>] [<Arguments>]" + Environment.NewLine + Environment.NewLine + "target: name of the connectionString as specified in Migrate.exe.config" + Environment.NewLine + "assembly: space separated path(s) to the assembly dll(s) containing the migrations to execute" + Environment.NewLine + Environment.NewLine + parser.GetUsage(); return string.Format(CultureInfo.CurrentCulture, "Usage:{0}{1}", Environment.NewLine, usage); }