-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Conversation
Note regarding the 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. |
Tagging subscribers to this area: @dotnet/area-system-diagnostics-process Issue DetailsAdd
|
@jbhensley If merging this should close the issue, you can put "fixes #66450" on a line in your top comment. |
src/libraries/System.Diagnostics.Process/tests/ProcessStartInfoTests.cs
Outdated
Show resolved
Hide resolved
|
||
foreach (string argument in arguments) | ||
{ | ||
ArgumentList.Add(argument); | ||
} |
There was a problem hiding this comment.
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:
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.
There was a problem hiding this 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 !
src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.cs
Show resolved
Hide resolved
{ | ||
_fileName = fileName; | ||
|
||
if(arguments != null) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
runtime/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/Process.cs
Lines 1328 to 1331 in af1171b
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.
There was a problem hiding this comment.
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.
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]); |
There was a problem hiding this comment.
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
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); |
/// <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> |
There was a problem hiding this comment.
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
/// <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> |
There was a problem hiding this 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) |
There was a problem hiding this comment.
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.
There was a problem hiding this 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/libraries/System.Diagnostics.Process/tests/ProcessStartInfoTests.cs
Outdated
Show resolved
Hide resolved
- Fix whitespace
There was a problem hiding this 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 !
@jbhensley if you are interested in continuing to contribute, we would welcome it. you may look at if you are interested in adding API, look for 'api-approved' label too |
Add
ProcessStartInfo(string fileName, IEnumerable<string> arguments)
. Fixes #66450.