Skip to content

Commit 027a0c1

Browse files
authored
Use ArgumentList if available (netstandard2.1 and up) (#12)
* Use ArgumentList if available (netstandard2.1 and up) * Ensure new example is not packable
1 parent 57b271f commit 027a0c1

File tree

8 files changed

+73
-4
lines changed

8 files changed

+73
-4
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// For more information see https://aka.ms/fsharp-console-apps
2+
3+
open System
4+
5+
let args = Environment.GetCommandLineArgs()
6+
printfn "%i" args.Length
7+
for a in args do
8+
printfn "%s" a
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<Compile Include="Program.fs"/>
11+
</ItemGroup>
12+
13+
</Project>

examples/ScratchPad.Fs/Program.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,6 @@ exec { run "dotnet" args }
5454
let _ = shell { exec "dotnet" args }
5555
let statusCode = exec { exit_code_of "dotnet" "--help"}
5656

57+
exec { run "dotnet" "run" "--project" "examples/ScratchPad.Fs.ArgumentPrinter" "--" "With Space" }
5758

5859
printfn "That's all folks!"

proc.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Proc.Fs", "src\Proc.Fs\Proc
4141
EndProject
4242
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "ScratchPad.Fs", "examples\ScratchPad.Fs\ScratchPad.Fs.fsproj", "{C33E3F7C-0C2A-4DD2-91E4-328866195997}"
4343
EndProject
44+
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "ScratchPad.Fs.ArgumentPrinter", "examples\ScratchPad.Fs.ArgumentPrinter\ScratchPad.Fs.ArgumentPrinter.fsproj", "{50A40BEB-1C22-41CF-908F-F24FB34B1699}"
45+
EndProject
4446
Global
4547
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4648
Debug|Any CPU = Debug|Any CPU
@@ -79,6 +81,10 @@ Global
7981
{C33E3F7C-0C2A-4DD2-91E4-328866195997}.Debug|Any CPU.Build.0 = Debug|Any CPU
8082
{C33E3F7C-0C2A-4DD2-91E4-328866195997}.Release|Any CPU.ActiveCfg = Release|Any CPU
8183
{C33E3F7C-0C2A-4DD2-91E4-328866195997}.Release|Any CPU.Build.0 = Release|Any CPU
84+
{50A40BEB-1C22-41CF-908F-F24FB34B1699}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
85+
{50A40BEB-1C22-41CF-908F-F24FB34B1699}.Debug|Any CPU.Build.0 = Debug|Any CPU
86+
{50A40BEB-1C22-41CF-908F-F24FB34B1699}.Release|Any CPU.ActiveCfg = Release|Any CPU
87+
{50A40BEB-1C22-41CF-908F-F24FB34B1699}.Release|Any CPU.Build.0 = Release|Any CPU
8288
EndGlobalSection
8389
GlobalSection(SolutionProperties) = preSolution
8490
HideSolutionNode = FALSE
@@ -92,5 +98,6 @@ Global
9298
{D6997ADC-E933-418E-831C-DE1A78897493} = {E89606EC-111B-4151-997C-8006627F1926}
9399
{5EA4E26F-F623-473D-9CD7-E590A3E54239} = {9C336E9A-3FC8-4F77-A5B4-1D07E4E54924}
94100
{C33E3F7C-0C2A-4DD2-91E4-328866195997} = {E4B3DD3A-E36C-46D6-B35E-D824EB9B3C06}
101+
{50A40BEB-1C22-41CF-908F-F24FB34B1699} = {E4B3DD3A-E36C-46D6-B35E-D824EB9B3C06}
95102
EndGlobalSection
96103
EndGlobal
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace ProcNet.Extensions;
7+
8+
internal static class ArgumentExtensions
9+
{
10+
public static string NaivelyQuoteArguments(this IEnumerable<string> arguments)
11+
{
12+
if (arguments == null) return string.Empty;
13+
var args = arguments.ToList();
14+
if (args.Count == 0) return string.Empty;
15+
var quotedArgs = args
16+
.Select(a =>
17+
{
18+
if (!a.Contains(" ")) return a;
19+
return $"\"{a}\"";
20+
})
21+
.ToList();
22+
return string.Join(" ", quotedArgs);
23+
}
24+
25+
}

src/Proc/ObservableProcessBase.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Reflection;
88
using System.Threading;
99
using System.Threading.Tasks;
10+
using ProcNet.Extensions;
1011
using ProcNet.Std;
1112

1213
namespace ProcNet
@@ -140,13 +141,19 @@ private Process CreateProcess()
140141
var processStartInfo = new ProcessStartInfo
141142
{
142143
FileName = s.Binary,
143-
Arguments = args != null ? string.Join(" ", args) : string.Empty,
144+
#if !NETSTANDARD2_1
145+
Arguments = args.NaivelyQuoteArguments(),
146+
#endif
144147
CreateNoWindow = true,
145148
UseShellExecute = false,
146149
RedirectStandardOutput = true,
147150
RedirectStandardError = true,
148151
RedirectStandardInput = true
149152
};
153+
#if NETSTANDARD2_1
154+
foreach (var arg in s.Args)
155+
processStartInfo.ArgumentList.Add(arg);
156+
#endif
150157
if (s.Environment != null)
151158
{
152159
foreach (var kv in s.Environment)

src/Proc/Proc.Exec.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Diagnostics;
33
using System.Threading.Tasks;
4+
using ProcNet.Extensions;
45

56
namespace ProcNet
67
{
@@ -46,11 +47,18 @@ public static partial class Proc
4647
/// <returns>The exit code of the binary being run</returns>
4748
public static int? Exec(ExecArguments arguments, TimeSpan timeout)
4849
{
49-
var args = string.Join(" ", arguments.Args ?? new string[]{});
50-
var info = new ProcessStartInfo(arguments.Binary, args)
50+
var args = arguments.Args.NaivelyQuoteArguments();
51+
var info = new ProcessStartInfo(arguments.Binary)
5152
{
5253
UseShellExecute = false
54+
#if !NETSTANDARD2_1
55+
, Arguments = args
56+
#endif
5357
};
58+
#if NETSTANDARD2_1
59+
foreach (var arg in arguments.Args)
60+
info.ArgumentList.Add(arg);
61+
#endif
5462

5563
var pwd = arguments.WorkingDirectory;
5664
if (!string.IsNullOrWhiteSpace(pwd)) info.WorkingDirectory = pwd;

src/Proc/Proc.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Project Sdk="Microsoft.NET.Sdk">
33
<PropertyGroup>
44
<AssemblyName>proc</AssemblyName>
5-
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
5+
<TargetFrameworks>netstandard2.0;netstandard2.1;net461</TargetFrameworks>
66
<RootNamespace>ProcNet</RootNamespace>
77

88

0 commit comments

Comments
 (0)