/// <summary> /// Get argument as string. If no argument given for parameter name, defaultValue will be returned. /// </summary> /// <param name="parameterName"></param> /// <param name="defaultValue"></param> /// <returns></returns> public static string GetArgument(string parameterName, string defaultValue = null) { string ret = null; if (!AllArguments.TryGetValue(parameterName, out ret)) { Dictionary <string, string> tempStacked = null; if (StackedArguments.TryGetValue(parameterName, out tempStacked) && tempStacked.Count == 1) { foreach (var entry in tempStacked) { return(entry.Value); } } return(defaultValue); } return(ret); }
/// <summary> /// Gets the stacked arguments as stackname to content for parameters defined like: --parameterName<stackname>=content /// </summary> /// <param name="parameterName"></param> /// <returns></returns> public static Dictionary <string, string> GetStackedArguments(string parameterName) { Dictionary <string, string> ret = null; if (!StackedArguments.TryGetValue(parameterName, out ret)) { // maybe in the none stacked arguments? var arg = GetArgument(parameterName); if (arg != null) { return(new Dictionary <string, string> { { "", arg } }); } return(null); } return(ret); }
private static void parseCommandLineArgs(string[] commandLineArgs) { // check if only config file given if (commandLineArgs.Length == 2 && !commandLineArgs.Last().StartsWith("--")) { string path = commandLineArgs.Last(); if (path.StartsWith(@"""") && path.EndsWith(@"""")) { path = path.Substring(1, path.Length - 2); } // is a file? => load it if (Common.IsValidLocalPath(path) && File.Exists(path)) { string configFileArg = @"--config-file=""" + path + @""""; commandLineArgs[1] = configFileArg; } else { // direct error output because no logger setting specified yet Program.ExitWithError((int)Program.Exitcode.ConfigfileNotFound, "Invalid path to config file given: " + commandLineArgs.Last()); } } List <string> arguments = new List <string>(); // load default script if nothing given if (commandLineArgs.Length == 1) { string defaultString = Encoding.UTF8.GetString(ScChrom.Properties.Resources._default); arguments.AddRange(defaultString.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries)); } foreach (string arg in commandLineArgs) { if (!arg.StartsWith("--")) { continue; } string keyval = arg.Substring(2); string val = ""; string key = null; if (!keyval.Contains("=")) { key = keyval; } else { int indexOfSeperator = keyval.IndexOf('='); key = keyval.Substring(0, indexOfSeperator); val = keyval.Substring(indexOfSeperator + 1); if (val.StartsWith(@"""") && val.EndsWith(@"""")) { val = val.Substring(1, val.Length - 2); } if (key.Contains("<") && key.EndsWith(">")) { var parts = key.Split('<'); key = parts[0]; string stackedId = parts[1].Replace(">", ""); if (!StackedArguments.ContainsKey(key)) { _stackedArgs.Add(key, new Dictionary <string, string>()); } _stackedArgs[key].Add(stackedId, val); continue; } } arguments.Add("--" + key + "=" + val); string[] lines = null; if (key == "config-file") { string path = null; try { path = Path.GetFullPath(val); } catch (Exception) { Program.ExitWithError((int)Program.Exitcode.InvalidConfigPath, "Invalid path for config file given: " + path); } if (!File.Exists(path)) { Program.ExitWithError((int)Program.Exitcode.ConfigfileNotFound, "Could not find specified config file: " + path); } _configfilePath = path; lines = readConfigFile(val); } if (key == "config-base64") { lines = DecodeBase64String(val); } if (lines != null && lines.Length > 0) { _infos = ParseInfos(lines[0]); arguments.AddRange(lines); } } ParseConfigArgs(arguments.ToArray()); }