using System; using System.IO; using System.Text.RegularExpressions; namespace BizTalkApplicationCatalog.Configuration { internal sealed class CommandLineOptions { private static readonly Regex UnsafeEnvironment = new Regex("[^A-Z0-9_-]+", RegexOptions.Compiled | RegexOptions.CultureInvariant); public string EnvironmentName { get; private set; } public string OutputDirectory { get; private set; } public string AccSnapshotPath { get; private set; } public string ProdSnapshotPath { get; private set; } public bool MergeMode { get; private set; } public bool SelfTest { get; private set; } public bool ShowHelp { get; private set; } public static CommandLineOptions Parse(string[] args) { var result = new CommandLineOptions(); for (var index = 0; index < args.Length; index++) { var argument = args[index]; switch (argument.ToLowerInvariant()) { case "--environment": result.EnvironmentName = Value(args, ref index, argument); break; case "--output": result.OutputDirectory = Value(args, ref index, argument); break; case "--merge": result.MergeMode = true; break; case "--acc-json": result.AccSnapshotPath = Value(args, ref index, argument); break; case "--prod-json": result.ProdSnapshotPath = Value(args, ref index, argument); break; case "--self-test": result.SelfTest = true; break; case "--help": case "-h": case "/?": result.ShowHelp = true; break; default: throw new ArgumentException("Unbekannte Option: " + argument); } } if (result.SelfTest || result.ShowHelp) return result; if (result.MergeMode) { if (!string.IsNullOrWhiteSpace(result.EnvironmentName)) { throw new ArgumentException( "--environment darf nicht mit --merge kombiniert werden."); } if (string.IsNullOrWhiteSpace(result.AccSnapshotPath) || string.IsNullOrWhiteSpace(result.ProdSnapshotPath)) { throw new ArgumentException( "--merge benötigt --acc-json und --prod-json."); } result.AccSnapshotPath = FullPath(result.AccSnapshotPath); result.ProdSnapshotPath = FullPath(result.ProdSnapshotPath); } else { if (!string.IsNullOrWhiteSpace(result.AccSnapshotPath) || !string.IsNullOrWhiteSpace(result.ProdSnapshotPath)) { throw new ArgumentException( "--acc-json und --prod-json sind nur mit --merge zulässig."); } if (string.IsNullOrWhiteSpace(result.EnvironmentName)) { throw new ArgumentException("--environment fehlt."); } result.EnvironmentName = UnsafeEnvironment.Replace( result.EnvironmentName.Trim().ToUpperInvariant(), "-").Trim('-'); if (result.EnvironmentName != "ACC" && result.EnvironmentName != "PROD") { throw new ArgumentException( "--environment muss ACC oder PROD sein."); } } if (string.IsNullOrWhiteSpace(result.OutputDirectory)) { result.OutputDirectory = Path.Combine( Environment.CurrentDirectory, "BizTalk-Anwendungskatalog", result.MergeMode ? "ACC-PROD" : result.EnvironmentName); } result.OutputDirectory = FullPath(result.OutputDirectory); return result; } public static string Usage() { return string.Join(Environment.NewLine, new[] { "BEW BizTalk Application Catalog – Phase 1", string.Empty, "Lokalen Snapshot und lokale Excel-Sicht erzeugen:", " BizTalkApplicationCatalog.exe --environment ACC --output PFAD", " BizTalkApplicationCatalog.exe --environment PROD --output PFAD", string.Empty, "ACC und PROD zur gemeinsamen Excel-Sicht zusammenführen:", " BizTalkApplicationCatalog.exe --merge --acc-json ACC.json --prod-json PROD.json --output PFAD", string.Empty, "Optionen:", " --environment ACC|PROD Lokal zu inventarisierende Umgebung.", " --output PFAD Zielordner für XLSX, JSON und Log.", " --merge Führt zwei vollständige JSON-Snapshots zusammen.", " --acc-json PFAD Vollständiger ACC-Snapshot.", " --prod-json PFAD Vollständiger PROD-Snapshot.", " --self-test Prüft JSON, Merge und XLSX ohne BizTalk.", " --help Diese Hilfe." }); } private static string Value(string[] args, ref int index, string option) { index++; if (index >= args.Length || args[index].StartsWith("--", StringComparison.Ordinal)) { throw new ArgumentException("Wert für " + option + " fehlt."); } return args[index]; } private static string FullPath(string value) { return Path.GetFullPath(Environment.ExpandEnvironmentVariables(value)); } } }