Skip to content

Commit f4adcd9

Browse files
committed
Added CmdArgumentsBuilder.QuoteBatArgument
1 parent 40dd97c commit f4adcd9

7 files changed

Lines changed: 27 additions & 25 deletions

File tree

sample/PSWebApi.OwinSample/CmdArgumentResolver.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,10 @@ public string Quote(string rawArg, bool forceQuote = false)
1818

1919
switch (_fileExtension)
2020
{
21-
case ".EXE": return CmdArgumentsBuilder.EscapeExeArgument(rawArg, forceQuote);
22-
case ".BAT": return EscapeBatArgument(rawArg);
21+
case ".EXE": return CmdArgumentsBuilder.QuoteExeArgument(rawArg, forceQuote);
22+
case ".BAT": return CmdArgumentsBuilder.QuoteBatArgument(rawArg, forceQuote);
2323
default: return rawArg;
2424
}
2525
}
26-
27-
protected virtual string EscapeBatArgument(string rawArg)
28-
{
29-
// todo
30-
return rawArg;
31-
}
3226
}
3327
}

sample/PSWebApi.OwinSample/PSWebApi.OwinSample.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
<WarningLevel>4</WarningLevel>
4141
</PropertyGroup>
4242
<ItemGroup>
43-
<Reference Include="DataBooster.PSWebApi, Version=1.0.12.0, Culture=neutral, PublicKeyToken=ee1eb06d9feeb8dc, processorArchitecture=MSIL">
44-
<HintPath>..\..\packages\DataBooster.PSWebApi.1.0.12-alpha\lib\net45\DataBooster.PSWebApi.dll</HintPath>
43+
<Reference Include="DataBooster.PSWebApi, Version=1.0.13.0, Culture=neutral, PublicKeyToken=ee1eb06d9feeb8dc, processorArchitecture=MSIL">
44+
<HintPath>..\..\packages\DataBooster.PSWebApi.1.0.13-alpha\lib\net45\DataBooster.PSWebApi.dll</HintPath>
4545
<Private>True</Private>
4646
</Reference>
4747
<Reference Include="Microsoft.CSharp" />

sample/PSWebApi.OwinSample/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
//
3232
// You can specify all the values or you can default the Revision and Build Numbers
3333
// by using the '*' as shown below:
34-
[assembly: AssemblyVersion("1.0.12.0")]
35-
[assembly: AssemblyFileVersion("1.0.12.0")]
34+
[assembly: AssemblyVersion("1.0.13.0")]
35+
[assembly: AssemblyFileVersion("1.0.13.0")]

sample/PSWebApi.OwinSample/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="DataBooster.PSWebApi" version="1.0.12-alpha" targetFramework="net45" />
3+
<package id="DataBooster.PSWebApi" version="1.0.13-alpha" targetFramework="net45" />
44
<package id="Microsoft.AspNet.Cors" version="5.2.3" targetFramework="net45" />
55
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" />
66
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net45" />

sample/PSWebApi.OwinSample/scripts-root/bat-scripts/test-args.bat

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ SET /A args_cnt=0
99
:LOOP
1010
IF [%1]==[] GOTO END
1111
SET /A args_cnt+=1
12-
IF [%1]==[%~1] (
13-
ECHO arg[%args_cnt%]: (%1)
14-
) ELSE (
15-
ECHO arg[%args_cnt%]: quoted->(%1); dequoted->(%~1)
16-
)
12+
ECHO arg[%args_cnt%]:{%1}
1713
SHIFT
1814
GOTO LOOP
1915

sample/Test-Args/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ static int Main(string[] args)
1010
Console.OutputEncoding = Encoding.UTF8;
1111

1212
for (int i = 0; i < args.Length; i++)
13-
Console.WriteLine("args[{0}]:({1})", i, args[i]);
13+
Console.WriteLine("args[{0}]:{{{1}}}", i, args[i]);
1414

1515
return 0;
1616
}

src/DataBooster.PSWebApi/CmdArgumentsBuilder.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)