-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Support '--' in a command line argument as a correct switch indicator #5786
Changes from 3 commits
0e96e27
6274af4
13c4556
d8e3ddd
990c225
9885b7f
3d1f45d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -436,6 +436,22 @@ public void ExtractSwitchParametersTest() | |||||||||||||||||||||
doubleQuotesRemovedFromArg.ShouldBe(6); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
[Fact] | ||||||||||||||||||||||
public void GetLengthOfSwitchIndicatorTest() | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
var commandLineSwitchWithSlash = "/Switch"; | ||||||||||||||||||||||
var commandLineSwitchWithSingleDash = "-Switch"; | ||||||||||||||||||||||
var commandLineSwitchWithDoubleDash = "--Switch"; | ||||||||||||||||||||||
|
||||||||||||||||||||||
var commandLineSwitchWithNoneOrIncorrectIndicator = "zSwitch"; | ||||||||||||||||||||||
|
||||||||||||||||||||||
MSBuildApp.GetLengthOfSwitchIndicator(commandLineSwitchWithSlash).ShouldBe(1); | ||||||||||||||||||||||
MSBuildApp.GetLengthOfSwitchIndicator(commandLineSwitchWithSingleDash).ShouldBe(1); | ||||||||||||||||||||||
MSBuildApp.GetLengthOfSwitchIndicator(commandLineSwitchWithDoubleDash).ShouldBe(2); | ||||||||||||||||||||||
|
||||||||||||||||||||||
MSBuildApp.GetLengthOfSwitchIndicator(commandLineSwitchWithNoneOrIncorrectIndicator).ShouldBe(0); | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
[Fact] | ||||||||||||||||||||||
public void Help() | ||||||||||||||||||||||
{ | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1675,8 +1675,8 @@ internal static void GatherCommandLineSwitches(ArrayList commandLineArgs, Comman | |
string switchName; | ||
string switchParameters; | ||
|
||
// all switches should start with - or / unless a project is being specified | ||
if (!unquotedCommandLineArg.StartsWith("-", StringComparison.Ordinal) && (!unquotedCommandLineArg.StartsWith("/", StringComparison.Ordinal) || FileUtilities.LooksLikeUnixFilePath(unquotedCommandLineArg))) | ||
// all switches should start with - or / or -- unless a project is being specified | ||
if (!ValidateSwitchIndicatorInUnquotedArgument(unquotedCommandLineArg) || FileUtilities.LooksLikeUnixFilePath(unquotedCommandLineArg)) | ||
{ | ||
switchName = null; | ||
// add a (fake) parameter indicator for later parsing | ||
|
@@ -1687,17 +1687,20 @@ internal static void GatherCommandLineSwitches(ArrayList commandLineArgs, Comman | |
// check if switch has parameters (look for the : parameter indicator) | ||
int switchParameterIndicator = unquotedCommandLineArg.IndexOf(':'); | ||
|
||
// get the length of the beginning sequence considered as a switch indicator (- or / or --) | ||
int switchIndicatorsLength = GetLengthOfSwitchIndicator(unquotedCommandLineArg); | ||
|
||
// extract the switch name and parameters -- the name is sandwiched between the switch indicator (the | ||
// leading - or /) and the parameter indicator (if the switch has parameters); the parameters (if any) | ||
// leading - or / or --) and the parameter indicator (if the switch has parameters); the parameters (if any) | ||
// follow the parameter indicator | ||
if (switchParameterIndicator == -1) | ||
{ | ||
switchName = unquotedCommandLineArg.Substring(1); | ||
switchName = unquotedCommandLineArg.Substring(switchIndicatorsLength); | ||
switchParameters = String.Empty; | ||
} | ||
else | ||
{ | ||
switchName = unquotedCommandLineArg.Substring(1, switchParameterIndicator - 1); | ||
switchName = unquotedCommandLineArg.Substring(switchIndicatorsLength, switchParameterIndicator - 1); | ||
switchParameters = ExtractSwitchParameters(commandLineArg, unquotedCommandLineArg, doubleQuotesRemovedFromArg, switchName, switchParameterIndicator); | ||
} | ||
} | ||
|
@@ -2908,6 +2911,47 @@ private static void ValidateExtensions(string[] projectExtensionsToIgnore) | |
} | ||
} | ||
|
||
/// <summary> | ||
/// Checks whether an argument given as a parameter starts with valid indicator, | ||
/// <br/>which means, whether switch begins with one of: "/", "-", "--" | ||
/// </summary> | ||
/// <param name="unquotedCommandLineArgument">Command line argument with beginning indicator (e.g. --help). | ||
/// <br/>This argument has to be unquoted, otherwise the first character will always be a quote character "</param> | ||
/// <returns>true if argument's beginning matches one of possible indicators | ||
/// <br/>false if argument's beginning doesn't match any of correct indicator | ||
/// </returns> | ||
private static bool ValidateSwitchIndicatorInUnquotedArgument(string unquotedCommandLineArgument) | ||
{ | ||
return unquotedCommandLineArgument.StartsWith("--", StringComparison.Ordinal) | ||
|| unquotedCommandLineArgument.StartsWith("-", StringComparison.Ordinal) | ||
BartoszKlonowski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|| unquotedCommandLineArgument.StartsWith("/", StringComparison.Ordinal); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the length of the switch indicator (- or / or --) | ||
/// <br/>The length returned from this method is deduced from the beginning sequence of unquoted argument. | ||
/// <br/>This way it will "assume" that there's no further error (e.g. // or ---) which would also be considered as a correct indicator. | ||
/// </summary> | ||
/// <param name="unquotedSwitch">Unquoted argument with leading indicator and name</param> | ||
/// <returns>Correct length of used indicator | ||
/// <br/>0 if no leading sequence recognized as correct indicator</returns> | ||
/// Internal for testing purposes | ||
internal static int GetLengthOfSwitchIndicator(string unquotedSwitch) | ||
{ | ||
if (unquotedSwitch.StartsWith("--", StringComparison.Ordinal)) | ||
{ | ||
return "--".Length; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd probably just put 2, 1, and 0 here to avoid allocating strings unnecessarily. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These strings are constants and so aren't allocated. This compiles to
However, it's probably worth avoiding that call/making the JIT constant-fold it, so I would also prefer the constants. |
||
} | ||
else if (unquotedSwitch.StartsWith("-", StringComparison.Ordinal) || unquotedSwitch.StartsWith("/", StringComparison.Ordinal)) | ||
{ | ||
return "-".Length; | ||
} | ||
else | ||
{ | ||
return "".Length; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Figures out which targets are to be built. | ||
/// </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.
It might be good to have a test validating that building with
--help
works.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.
+1
This might be overkill but: Consider a case for
-help
,/help
, and--help
and verifying part of the output is expected. Potentially in thehelp
test just below this test.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.
@benvillalobos Can you elaborate on:
Thing is, that
MSBuildApp.Execute()
method only returns theExitType
.How to get the string returned from command line execution?
Or perhaps checking whether each call (command with '--help', '-h, '/h') returns
ExitType.Success
will be enough?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.
Verifying that it succeeded would be enough for me. I think it's probably more complicated than it's worth to look at the output, but you might be able to capture it with a logger you can pass in alongside the --help switch.