public static String EscapeArgument(String Argument, ArgumentStyle ArgumentStyle) { //\0 \r \n can not be escaped if (Argument.Any(c => c == '\0')) { throw new ArgumentException("InvalidChar"); } if (ArgumentStyle == ArgumentStyle.Windows) { //https://docs.microsoft.com/en-us/cpp/cpp/parsing-cpp-command-line-arguments?view=vs-2017 //http://csharptest.net/529/how-to-correctly-escape-command-line-arguments-in-c/index.html //backslashes before double quotes must be doubled return(rComplexChars.IsMatch(Argument) ? "\"" + rBackslashBeforeDoubleQuotes.Replace(Argument, s => s.Value + s.Value).Replace("\"", "\\\"") + "\"" : Argument); } else if (ArgumentStyle == ArgumentStyle.Unix) { //in mono it was originally implemented using g_shell_parse_argv //https://bugzilla.xamarin.com/show_bug.cgi?id=19296 //https://developer.gnome.org/glib/stable/glib-Shell-related-Utilities.html //but upon testing it is found that backslash need to be double in single quotes, before mono 6.6 //it is now fixed, https://github.com/mono/mono/issues/14724 return(rComplexChars.IsMatch(Argument) ? "'" + String.Join("", Argument.SelectMany(c => c == '\\' ? @"'\\'" : c == '\'' ? @"'\''" : new String(c, 1))) + "'" : Argument); } else { throw new InvalidOperationException(); } }
public ArgumentOptionsAttribute(string ProgramName = null, ArgumentStyle ArgumentStyle = ArgumentStyle.Unix) { this.ProgramName = ProgramName; this.ArgumentStyle = ArgumentStyle; }
public ArgumentStyleAttribute(ArgumentStyle argumentStyle) { ArgumentStyle = argumentStyle; }