Skip to content

[API Proposal]: Expose API for escaping command-line arguments #119200

@Youssef1313

Description

@Youssef1313

Background and motivation

The core logic is in an internal PasteArguments.AppendArgument. But it's not exposed directly for users usage.

While ProcessStartInfo today already supports passing a list of unescaped arguments (ArgumentList property), it cannot be used together with Arguments property.

API Proposal

namespace System.Diagnostics;

public class ProcessStartInfo
{
    public static string EscapeArgument(string argument);
}

API Usage

// Fancy the value
var info = new ProcessStartInfo()
{
    FileName = ...;
}

var builder = new StringBuilder();
builder.Append(myAlreadyEscapedArgumentsComingFromElsewhere);
foreach (var arg in listOfUnescapedArguments)
{
    builder.Append(ProcessStartInfo.EscapeArgument(arg));
}
info.Arguments = builder.ToString(); 

Real-world example is SDK:

https://github.com/dotnet/sdk/blob/15c4fb1e5086e8a61876fd60b612a51cac1774c2/src/Cli/dotnet/Commands/Test/TestApplication.cs#L97

There, Module.RunProperties.Arguments is already a single string which can contain multiple arguments like --report-trx --results-directory "Path Containing Spaces" which is already escaped properly.
Then, there are multiple more arguments that need to be constructed and are not escaped.

NOTE: ArgumentEscaper.EscapeSingleArg implementation seems like it's already diverging from the implementation in dotnet/runtime. And somewhere in dotnet/msbuild there is even another implementation somewhere.

Alternative Designs

Allow both ProcessStartInfo.Arguments and ArgumentList to be used together.

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions