public ConsoleColoredString Validate() { if (FromPath.Length != ToPath.Length) { return(CommandLineParser.Colorize(RhoML.Parse("The number of {option}--from{} arguments must match the number of {option}--to{} arguments."))); } for (int i = 0; i < FromPath.Length; i++) { FromPath[i] = Path.GetFullPath(FromPath[i]).WithSlash(); ToPath[i] = Path.GetFullPath(ToPath[i]).WithSlash(); } for (int i = 0; i < IgnorePath.Length; i++) { IgnorePath[i] = Path.GetFullPath(IgnorePath[i]).WithSlash(); } foreach (var path in FromPath.Concat(ToPath)) { if (!Directory.Exists(path)) { return(CommandLineParser.Colorize(RhoML.Parse($"Directory not found: {{h}}{path}{{}}."))); } } if (SettingsPath != null && !Directory.Exists(Path.GetDirectoryName(SettingsPath))) { return(CommandLineParser.Colorize(RhoML.Parse($"The {{option}}--settings{{}} file does not exist and will not be created because this directory does not exist: {{h}}{Path.GetDirectoryName(SettingsPath)}{{}}."))); } return(null); }
public ConsoleColoredString Validate() { if (CommandToRun.Length == 0) { return("You must specify the command to be executed (CommandToRun)."); } if (WorkingDir != null) { try { Directory.SetCurrentDirectory(WorkingDir); } catch { return("Cannot set working directory - check that the directory exists and that the path is valid. \"{0}\"".Fmt(WorkingDir)); } } if (LogFilename == null) { LogFilename = Path.GetFileName(CommandToRun[0]).Replace(".", "_") + "--{}.log"; } LogFilename = Path.GetFullPath(LogFilename.Replace("{}", DateTime.Now.ToString("yyyy-MM-dd"))); if (SuccessCodes != null && FailureCodes != null) { return(CommandLineParser.Colorize(RhoML.Parse("The options {option}--success-codes{} and {option}--failure-codes{} are mutually exclusive and cannot be specified together."))); } // Parse the success/failure codes list var codes = SuccessCodes ?? FailureCodes; if (codes != null) { var result = new List <Tuple <int, int> >(); foreach (var part in codes.Split(',')) { var match = Regex.Match(part, @"^\s*(?<fr>-?\d+)\s*(-\s*(?<to>-?\d+\s*))?$"); if (!match.Success) { return(CommandLineParser.Colorize(RhoML.Parse("Could not parse the exit code list for {option}{0}{}: cannot parse segment \"{h}{1}{}\".".Fmt(SuccessCodes != null ? "--success-codes" : "--failure-codes", part)))); } var fr = int.Parse(match.Groups["fr"].Value); if (!match.Groups["to"].Success) { result.Add(Tuple.Create(fr, fr)); } else { var to = int.Parse(match.Groups["to"].Value); result.Add(Tuple.Create(Math.Min(fr, to), Math.Max(fr, to))); } } if (SuccessCodes != null) { SuccessCodesParsed = result; } else { FailureCodesParsed = result; } } return(null); }
public ConsoleColoredString Validate() { ConfigPath = Path.GetFullPath(ConfigPath); if (!File.Exists(ConfigPath)) { var cfg = new Config { Domain = "example.com", NotifyEmail = "*****@*****.**", PfxPassword = "******", CountryName = "GB", Locality = "London", State = "London" }; File.WriteAllText(ConfigPath, JsonConvert.SerializeObject(cfg, Formatting.Indented)); return(CommandLineParser.Colorize(RhoML.Parse($"Config file not found: {{h}}{ConfigPath}{{}}\r\n\r\nA template file has been created at the above path."))); } try { Config = JsonConvert.DeserializeObject <Config>(File.ReadAllText(ConfigPath)); } catch { return(CommandLineParser.Colorize(RhoML.Parse($"Could not parse config file: {{h}}{ConfigPath}{{}}"))); } return(null); }
/// <summary> /// Outputs text to the console, parsing and colorizing where required. For syntax, see /// https://docs.timwi.de/M:RT.Util.CommandLine.CommandLineParser.Colorize(RT.Util.RhoElement) and /// https://docs.timwi.de/T:RT.Util.RhoML. Example: "Normal text {red}Red text{} normal text."</summary> public static void WriteLineColored(string str) { ConsoleUtil.WriteLine(CommandLineParser.Colorize(RhoML.Parse(str))); }
public ConsoleColoredString Validate() { if (SvgPath == null && (Filename == null || Id == null)) { return(CommandLineParser.Colorize(RhoML.Parse("If {option}-p{} is not used, {option}-f{} and {option}-i{} must both be present."))); } if (SvgPath != null && (Filename != null || Id != null)) { return(CommandLineParser.Colorize(RhoML.Parse("If {option}-p{} is used, neither {option}-f{} nor {option}-i{} must be present."))); } Matrix matrix = new Matrix(); if (Filename != null) { if (!File.Exists(Filename)) { return(CommandLineParser.Colorize(RhoML.Parse("The specified file, {{h}}{0}{{}}, does not exist.".Fmt(Filename)))); } var xml = XDocument.Parse(File.ReadAllText(Filename)); var element = xml.Descendants().FirstOrDefault(el => el.AttributeI("id").NullOr(id => id.Value == Id) == true); if (element == null) { return(CommandLineParser.Colorize(RhoML.Parse("An XML element with the specified ID, {{h}}{0}{{}}, does not exist in the specified file.".Fmt(Id)))); } SvgPath = element.AttributeI("d")?.Value; if (SvgPath == null) { return(CommandLineParser.Colorize(RhoML.Parse("The XML element with the specified ID, {{h}}{0}{{}}, does not have the {{h}}d{{}} attribute.".Fmt(Id)))); } var tr = element.AttributeI("transform")?.Value; Match m; if (tr != null && (m = Regex.Match(tr, @"^\s*matrix\s*\(\s*(-?\d*(?:\d|\.\d+)),\s*(-?\d*(?:\d|\.\d+)),\s*(-?\d*(?:\d|\.\d+)),\s*(-?\d*(?:\d|\.\d+)),\s*(-?\d*(?:\d|\.\d+)),\s*(-?\d*(?:\d|\.\d+))\s*\)\s*$")).Success) { matrix = new Matrix(float.Parse(m.Groups[1].Value), float.Parse(m.Groups[2].Value), float.Parse(m.Groups[3].Value), float.Parse(m.Groups[4].Value), float.Parse(m.Groups[5].Value), float.Parse(m.Groups[6].Value)); System.Console.WriteLine("Using matrix!"); } } PointF?prev = null; bool figure = false; SvgPath = SvgPath.Trim(); var num = @"-?\d*(?:\d|\.\d+)(?=$|\s|,)"; ParsedPath = new GraphicsPath(UseWindingFill ? FillMode.Winding : FillMode.Alternate); var index = 0; char?prevCommand = null; var commands = "MLCZz"; while (index < SvgPath.Length) { var match = Regex.Match(SvgPath.Substring(index), @"^(?: [ML]{1}(?:\s|,)*({0})(?:\s|,)+({0})(?:\s|,)*| [C]{2}(?:\s|,)*({0})(?:\s|,)+({0})(?:\s|,)+({0})(?:\s|,)+({0})(?:\s|,)+({0})(?:\s|,)+({0})(?:\s|,)*| [Zz](?:\s|,)* )".Fmt( /* {0} */ num, /* {1} */ prevCommand == 'M' || prevCommand == 'L' ? "?" : "", /* {2} */ prevCommand == 'C' ? "?" : "" ), RegexOptions.IgnorePatternWhitespace); if (!match.Success) { return("The specified path data does not conform to the expected syntax at index {0}.".Fmt(index)); } index += match.Length; if (match.Value[0] != 'M' && prev.Value == null) { return("The path data cannot start with any command other than M."); } if (commands.Contains(match.Value[0])) { prevCommand = match.Value[0]; } switch (prevCommand) { case 'M': if (figure) { ParsedPath.CloseFigure(); } prev = new PointF(float.Parse(match.Groups[1].Value), float.Parse(match.Groups[2].Value)); break; case 'L': var p = new PointF(float.Parse(match.Groups[1].Value), float.Parse(match.Groups[2].Value)); ParsedPath.AddLine(prev.Value, p); prev = p; figure = true; break; case 'C': var p1 = new PointF(float.Parse(match.Groups[3].Value), float.Parse(match.Groups[4].Value)); var p2 = new PointF(float.Parse(match.Groups[5].Value), float.Parse(match.Groups[6].Value)); var p3 = new PointF(float.Parse(match.Groups[7].Value), float.Parse(match.Groups[8].Value)); ParsedPath.AddBezier(prev.Value, p1, p2, p3); prev = p3; figure = true; break; case 'Z': case 'z': if (figure) { ParsedPath.CloseFigure(); figure = false; } break; default: return("The specified path data does not conform to the expected syntax. (2)"); } } if (figure) { ParsedPath.CloseFigure(); } ParsedPath.Transform(matrix); return(null); }