Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ProcessStartInfo constructor that accepts IEnumerable<string> arguments #86346

Merged
merged 9 commits into from
May 19, 2023

Conversation

jbhensley
Copy link
Contributor

@jbhensley jbhensley commented May 16, 2023

Add ProcessStartInfo(string fileName, IEnumerable<string> arguments). Fixes #66450.

@dotnet-issue-labeler
Copy link

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label May 16, 2023
@ghost
Copy link

ghost commented May 16, 2023

Tagging subscribers to this area: @dotnet/area-system-diagnostics-process
See info in area-owners.md if you want to be subscribed.

Issue Details

Add ProcessStartInfo(string fileName, IEnumerable<string> arguments) as per #66450.

Author: jbhensley
Assignees: -
Labels:

area-System.Diagnostics.Process, new-api-needs-documentation

Milestone: -

@danmoseley
Copy link
Member

@jbhensley If merging this should close the issue, you can put "fixes #66450" on a line in your top comment.

Comment on lines 65 to 69

foreach (string argument in arguments)
{
ArgumentList.Add(argument);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding each argument individually, this could be:

Suggested change
foreach (string argument in arguments)
{
ArgumentList.Add(argument);
}
if (arguments is not null)
{
_argumentList = new Collection<string>(new List<string>(arguments));
}

and let List<T>'s ctor handle it however makes sense.

Copy link
Member

@adamsitnik adamsitnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution @jbhensley !

{
_fileName = fileName;

if(arguments != null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should we allow for nulls in the new API?

Since the argument is not marked as nullable, the method should throw for nulls.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can move these checks into the new ProcessStartInfo ctor

public static Process Start(string fileName, IEnumerable<string> arguments)
{
ArgumentNullException.ThrowIfNull(fileName);
ArgumentNullException.ThrowIfNull(arguments);

I'm a little worried about consistency. The other ctors for ProcessStartInfo are not throwing ArgumentNullException and Process.Start(string fileName, IEnumerable<string> arguments) is the only overload of that method that throws.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other ctors for ProcessStartInfo are not throwing

This is true, but they are old and we can't easily change their behavior. For the new APIs, we either allow for nulls by making an argument nullable, or we don't and throw. Since the API was not approved as nullable (no ?), we should throw. By doing that we send a clear message in the API contract: nulls are not allowed, if you have a null arguments, just don't use this API.

Comment on lines 1282 to 1290
ProcessStartInfo psi = new ProcessStartInfo("filename", new[] { "arg1", "arg2", " arg3", "arg4 ", "arg 5", $"arg{Environment.NewLine}6" });

Assert.Equal(6, psi.ArgumentList.Count);
Assert.Equal("arg1", psi.ArgumentList[0]);
Assert.Equal("arg2", psi.ArgumentList[1]);
Assert.Equal(" arg3", psi.ArgumentList[2]);
Assert.Equal("arg4 ", psi.ArgumentList[3]);
Assert.Equal("arg 5", psi.ArgumentList[4]);
Assert.Equal($"arg{Environment.NewLine}6", psi.ArgumentList[5]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: it could be simplified

Suggested change
ProcessStartInfo psi = new ProcessStartInfo("filename", new[] { "arg1", "arg2", " arg3", "arg4 ", "arg 5", $"arg{Environment.NewLine}6" });
Assert.Equal(6, psi.ArgumentList.Count);
Assert.Equal("arg1", psi.ArgumentList[0]);
Assert.Equal("arg2", psi.ArgumentList[1]);
Assert.Equal(" arg3", psi.ArgumentList[2]);
Assert.Equal("arg4 ", psi.ArgumentList[3]);
Assert.Equal("arg 5", psi.ArgumentList[4]);
Assert.Equal($"arg{Environment.NewLine}6", psi.ArgumentList[5]);
string[] args = new[] { "arg1", "arg2", " arg3", "arg4 ", "arg 5", $"arg{Environment.NewLine}6" };
ProcessStartInfo psi = new ProcessStartInfo("filename", args);
Assert.Equal(args, psi.ArgumentList);

Comment on lines 58 to 61
/// <devdoc>
/// Specifies the name of the application that is to be started, as well as a set
/// of command line arguments to pass to the application.
/// </devdoc>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

devdoc is relic of the past (it's not documented anywhere, you had no chance to know about it), please use standard summary tags

Suggested change
/// <devdoc>
/// Specifies the name of the application that is to be started, as well as a set
/// of command line arguments to pass to the application.
/// </devdoc>
/// <summary>
/// Specifies the name of the application that is to be started, as well as a set
/// of command line arguments to pass to the application.
/// </summary>

Copy link
Member

@adamsitnik adamsitnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution @jbhensley !

@adamsitnik adamsitnik self-assigned this May 17, 2023
@adamsitnik adamsitnik added this to the 8.0.0 milestone May 17, 2023
{
_fileName = fileName;

if(arguments != null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other ctors for ProcessStartInfo are not throwing

This is true, but they are old and we can't easily change their behavior. For the new APIs, we either allow for nulls by making an argument nullable, or we don't and throw. Since the API was not approved as nullable (no ?), we should throw. By doing that we send a clear message in the API contract: nulls are not allowed, if you have a null arguments, just don't use this API.

@ghost ghost added the needs-author-action An issue or pull request that requires more info or actions from the author. label May 18, 2023
@ghost ghost removed the needs-author-action An issue or pull request that requires more info or actions from the author. label May 18, 2023
Copy link
Member

@adamsitnik adamsitnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are almost there!

src/coreclr/tools/SuperFileCheck/Program.cs Outdated Show resolved Hide resolved
Copy link
Member

@adamsitnik adamsitnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you very much for your contribution @jbhensley !

@danmoseley
Copy link
Member

@jbhensley if you are interested in continuing to contribute, we would welcome it. you may look at
https://github.com/dotnet/runtime/issues?q=is%3Aopen+is%3Aissue+label%3A%22help+wanted%22

if you are interested in adding API, look for 'api-approved' label too

@adamsitnik adamsitnik merged commit 4c51bc5 into dotnet:main May 19, 2023
@jbhensley jbhensley deleted the issue-66450 branch May 19, 2023 14:54
@ghost ghost locked as resolved and limited conversation to collaborators Jun 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[API proposal]: add ProcessStartInfo constructor that accepts IEnumerable<string> arguments.
4 participants