@@ -16,6 +16,7 @@ public class CmdArgumentsBuilder
1616 private const string _argSeparator = " " ;
1717 private static readonly Regex _rgxNoneedQuotes_Exe = new Regex ( @"^((\\\S|\\$|[^""\s\\])|(""(\\.|""""|[^""\\])*""))+$" ) ;
1818 private static readonly Regex _rgxEscapeBackslash_Exe = new Regex ( @"(\\+)(?=""|$)" ) ;
19+ private static readonly Regex _rgxNoneedQuotes_Bat = new Regex ( @"^([^""\s&|<>()^]|\^[&|<>()^]|(""[^""]*""))+$" ) ;
1920 private readonly List < string > _rawArguments ;
2021 public Collection < string > RawArguments { get ; private set ; }
2122
@@ -116,19 +117,30 @@ public string ToString(Func<string, string> escapeArgument)
116117
117118 // https://msdn.microsoft.com/en-us/library/17w5ykft.aspx
118119 // https://msdn.microsoft.com/en-us/library/a1y7w461.aspx
119- public static string EscapeExeArgument ( string arg , bool forceQuote )
120+ public static string QuoteExeArgument ( string rawArg , bool forceQuote )
120121 {
121- if ( string . IsNullOrWhiteSpace ( arg ) )
122- return "\" " + ( arg ?? string . Empty ) + "\" " ;
122+ if ( string . IsNullOrWhiteSpace ( rawArg ) )
123+ return "\" " + ( rawArg ?? string . Empty ) + "\" " ;
123124
124- if ( forceQuote == false && _rgxNoneedQuotes_Exe . IsMatch ( arg ) )
125- return arg ;
125+ if ( forceQuote == false && _rgxNoneedQuotes_Exe . IsMatch ( rawArg ) )
126+ return rawArg ;
126127
127- string escArg = _rgxEscapeBackslash_Exe . Replace ( arg , m => m . Groups [ 1 ] . Value + m . Groups [ 1 ] . Value ) ;
128+ string escArg = _rgxEscapeBackslash_Exe . Replace ( rawArg , m => m . Groups [ 1 ] . Value + m . Groups [ 1 ] . Value ) ;
128129
129130 return "\" " + escArg . Replace ( "\" " , "\\ \" " ) + "\" " ;
130131 }
131132
133+ public static string QuoteBatArgument ( string rawArg , bool forceQuote )
134+ {
135+ if ( string . IsNullOrWhiteSpace ( rawArg ) )
136+ return "\" " + ( rawArg ?? string . Empty ) + "\" " ;
137+
138+ if ( forceQuote == false && _rgxNoneedQuotes_Bat . IsMatch ( rawArg ) )
139+ return rawArg ;
140+
141+ return "\" " + rawArg . Replace ( "\" " , "\" \" " ) + "\" " ;
142+ }
143+
132144 public override string ToString ( )
133145 {
134146 return ToString ( null ) ;
0 commit comments