Skip to content

Commit d955427

Browse files
committed
stop using GetValue<T>(name) as it throws when given symbol was not parsed
1 parent 3897299 commit d955427

File tree

5 files changed

+21
-16
lines changed

5 files changed

+21
-16
lines changed

src/Cli/dotnet/Commands/Test/ValidationUtility.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,8 @@ static void ValidateOptionsIrrelevantToModulesFilter(ParseResult parseResult)
4747
parseResult.HasOption(TestingPlatformOptions.ConfigurationOption) ||
4848
parseResult.HasOption(TestingPlatformOptions.FrameworkOption) ||
4949
parseResult.HasOption(CommonOptions.OperatingSystemOption) ||
50-
((parseResult.GetValue<string>(CommonOptions.RuntimeOptionLongName) ??
51-
parseResult.GetValue<string>(CommonOptions.RuntimeOptionShortName)) is not null)
52-
)
50+
parseResult.HasOption(CommonOptions.RuntimeOptionLongName) ||
51+
parseResult.HasOption(CommonOptions.RuntimeOptionShortName))
5352
{
5453
throw new GracefulException(CliCommandStrings.CmdOptionCannotBeUsedWithTestModulesDescription);
5554
}

src/Cli/dotnet/Commands/Tool/Common/ToolAppliedOption.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ internal static void EnsureNoConflictGlobalLocalToolPathOption(
6767
options.Add(LocalOptionName);
6868
}
6969

70-
if (!string.IsNullOrWhiteSpace(parseResult.GetValue<string>(ToolPathName)))
70+
if (parseResult.RootCommandResult.GetResult(ToolPathName) is not null)
7171
{
7272
options.Add(ToolPathName);
7373
}
@@ -108,7 +108,7 @@ internal static void EnsureNoConflictUpdateAllVersionOption(
108108
internal static void EnsureToolManifestAndOnlyLocalFlagCombination(ParseResult parseResult)
109109
{
110110
if (GlobalOrToolPath(parseResult) &&
111-
!string.IsNullOrWhiteSpace(parseResult.GetValue<string>(ToolManifestName)))
111+
parseResult.RootCommandResult.GetResult(ToolManifestName) is not null)
112112
{
113113
throw new GracefulException(
114114
string.Format(
@@ -117,10 +117,7 @@ internal static void EnsureToolManifestAndOnlyLocalFlagCombination(ParseResult p
117117
}
118118

119119
private static bool GlobalOrToolPath(ParseResult parseResult)
120-
{
121-
return GlobalOptionWasProvided(parseResult) ||
122-
!string.IsNullOrWhiteSpace(parseResult.GetValue<string>(ToolPathName));
123-
}
120+
=> GlobalOptionWasProvided(parseResult) || parseResult.RootCommandResult.GetResult(ToolPathName) is not null;
124121

125122
private static bool GlobalOptionWasProvided(ParseResult parseResult)
126123
=> (parseResult.RootCommandResult.GetResult(GlobalOptionName) ?? parseResult.RootCommandResult.GetResult(GlobalOptionAlias)) is not null;

src/Cli/dotnet/CommonOptions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,8 @@ public static void ValidateSelfContainedOptions(bool hasSelfContainedOption, boo
258258

259259
internal static IEnumerable<string> ResolveArchOptionToRuntimeIdentifier(string? arg, ParseResult parseResult)
260260
{
261-
if ((parseResult.GetValue<string>(RuntimeOptionLongName) ?? parseResult.GetValue<string>(RuntimeOptionShortName)) is not null)
261+
if ((parseResult.RootCommandResult.GetResult(RuntimeOptionLongName) ??
262+
parseResult.RootCommandResult.GetResult(RuntimeOptionShortName)) is not null)
262263
{
263264
throw new GracefulException(CliStrings.CannotSpecifyBothRuntimeAndArchOptions);
264265
}
@@ -274,7 +275,8 @@ internal static IEnumerable<string> ResolveArchOptionToRuntimeIdentifier(string?
274275

275276
internal static IEnumerable<string> ResolveOsOptionToRuntimeIdentifier(string? arg, ParseResult parseResult)
276277
{
277-
if ((parseResult.GetValue<string>(RuntimeOptionLongName) ?? parseResult.GetValue<string>(RuntimeOptionShortName)) is not null)
278+
if ((parseResult.RootCommandResult.GetResult(RuntimeOptionLongName) ??
279+
parseResult.RootCommandResult.GetResult(RuntimeOptionShortName)) is not null)
278280
{
279281
throw new GracefulException(CliStrings.CannotSpecifyBothRuntimeAndOsOptions);
280282
}

src/Cli/dotnet/Extensions/ParseResultExtensions.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,10 @@ public static bool BothArchAndOsOptionsSpecified(this ParseResult parseResult) =
178178

179179
internal static string GetCommandLineRuntimeIdentifier(this ParseResult parseResult)
180180
{
181-
string fromRuntimeOption = parseResult.GetValue<string>(CommonOptions.RuntimeOptionLongName) ??
182-
parseResult.GetValue<string>(CommonOptions.RuntimeOptionShortName);
183-
184-
return fromRuntimeOption is not null ? fromRuntimeOption :
181+
return parseResult.HasOption(CommonOptions.RuntimeOptionLongName) ?
182+
parseResult.GetValue<string>(CommonOptions.RuntimeOptionLongName) :
183+
parseResult.HasOption(CommonOptions.RuntimeOptionShortName) ?
184+
parseResult.GetValue<string>(CommonOptions.RuntimeOptionShortName) :
185185
parseResult.HasOption(CommonOptions.OperatingSystemOption) ||
186186
parseResult.HasOption(CommonOptions.ArchitectureOption) ||
187187
parseResult.HasOption(CommonOptions.LongFormArchitectureOption) ?
@@ -273,4 +273,11 @@ public static T SafelyGetValueForOption<T>(this ParseResult parseResult, Option<
273273
/// This is useful for checking if the user has explicitly set an option, as opposed to it being set by default.
274274
/// </summary>
275275
public static bool HasOption(this ParseResult parseResult, Option option) => parseResult.GetResult(option) is OptionResult or && !or.Implicit;
276+
277+
/// <summary>
278+
/// Checks if the option with given name/alias is present and not implicit (i.e. not set by default).
279+
/// This is useful for checking if the user has explicitly set an option, as opposed to it being set by default.
280+
/// </summary>
281+
public static bool HasOption(this ParseResult parseResult, string nameOrAlias)
282+
=> parseResult.RootCommandResult.GetResult(nameOrAlias) is OptionResult or && !or.Implicit;
276283
}

src/Cli/dotnet/Parser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ internal static int ExceptionHandler(Exception exception, ParseResult parseResul
254254

255255
return 1;
256256
}
257-
257+
258258
internal class DotnetHelpBuilder : HelpBuilder
259259
{
260260
private DotnetHelpBuilder(int maxWidth = int.MaxValue) : base(maxWidth) { }

0 commit comments

Comments
 (0)