Skip to content

Commit

Permalink
[Group 4] Enable nullable annotations for `Microsoft.Extensions.Confi…
Browse files Browse the repository at this point in the history
…guration.CommandLine` (#57432)

* Annotate

* Add net6 to parent projects

* DisableImplicitAssemblyReferences

* NetCoreAppMinimum

* configureSource is nullable

* Add NetCoreAppMinimum to System.Runtime and System.Collections

* Revert "Add NetCoreAppMinimum to System.Runtime and System.Collections"

This reverts commit fbfb3ba.

* Fix NetCoreAppMinimum build

Co-authored-by: Eric Erhardt <eric.erhardt@microsoft.com>
  • Loading branch information
maxkoshevoi and eerhardt committed Nov 19, 2021
1 parent 4e2d3a6 commit 8b1a0c8
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ namespace Microsoft.Extensions.Configuration
{
public static partial class CommandLineConfigurationExtensions
{
public static Microsoft.Extensions.Configuration.IConfigurationBuilder AddCommandLine(this Microsoft.Extensions.Configuration.IConfigurationBuilder builder, System.Action<Microsoft.Extensions.Configuration.CommandLine.CommandLineConfigurationSource> configureSource) { throw null; }
public static Microsoft.Extensions.Configuration.IConfigurationBuilder AddCommandLine(this Microsoft.Extensions.Configuration.IConfigurationBuilder builder, System.Action<Microsoft.Extensions.Configuration.CommandLine.CommandLineConfigurationSource>? configureSource) { throw null; }
public static Microsoft.Extensions.Configuration.IConfigurationBuilder AddCommandLine(this Microsoft.Extensions.Configuration.IConfigurationBuilder configurationBuilder, string[] args) { throw null; }
public static Microsoft.Extensions.Configuration.IConfigurationBuilder AddCommandLine(this Microsoft.Extensions.Configuration.IConfigurationBuilder configurationBuilder, string[] args, System.Collections.Generic.IDictionary<string, string> switchMappings) { throw null; }
public static Microsoft.Extensions.Configuration.IConfigurationBuilder AddCommandLine(this Microsoft.Extensions.Configuration.IConfigurationBuilder configurationBuilder, string[] args, System.Collections.Generic.IDictionary<string, string>? switchMappings) { throw null; }
}
}
namespace Microsoft.Extensions.Configuration.CommandLine
{
public partial class CommandLineConfigurationProvider : Microsoft.Extensions.Configuration.ConfigurationProvider
{
public CommandLineConfigurationProvider(System.Collections.Generic.IEnumerable<string> args, System.Collections.Generic.IDictionary<string, string> switchMappings = null) { }
public CommandLineConfigurationProvider(System.Collections.Generic.IEnumerable<string> args, System.Collections.Generic.IDictionary<string, string>? switchMappings = null) { }
protected System.Collections.Generic.IEnumerable<string> Args { get { throw null; } }
public override void Load() { }
}
public partial class CommandLineConfigurationSource : Microsoft.Extensions.Configuration.IConfigurationSource
{
public CommandLineConfigurationSource() { }
public System.Collections.Generic.IEnumerable<string> Args { get { throw null; } set { } }
public System.Collections.Generic.IDictionary<string, string> SwitchMappings { get { throw null; } set { } }
public System.Collections.Generic.IDictionary<string, string>? SwitchMappings { get { throw null; } set { } }
public Microsoft.Extensions.Configuration.IConfigurationProvider Build(Microsoft.Extensions.Configuration.IConfigurationBuilder builder) { throw null; }
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<Compile Include="Microsoft.Extensions.Configuration.CommandLine.cs" />
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Configuration\ref\Microsoft.Extensions.Configuration.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Configuration.Abstractions\ref\Microsoft.Extensions.Configuration.Abstractions.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == '$(NetCoreAppCurrent)'">
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime\ref\System.Runtime.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp' and '$(TargetFramework)' != '$(NetCoreAppCurrent)'">
<Reference Include="System.Runtime" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public static IConfigurationBuilder AddCommandLine(this IConfigurationBuilder co
public static IConfigurationBuilder AddCommandLine(
this IConfigurationBuilder configurationBuilder,
string[] args,
IDictionary<string, string> switchMappings)
IDictionary<string, string>? switchMappings)
{
configurationBuilder.Add(new CommandLineConfigurationSource { Args = args, SwitchMappings = switchMappings });
return configurationBuilder;
Expand All @@ -164,7 +164,7 @@ public static IConfigurationBuilder AddCommandLine(
/// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param>
/// <param name="configureSource">Configures the source.</param>
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
public static IConfigurationBuilder AddCommandLine(this IConfigurationBuilder builder, Action<CommandLineConfigurationSource> configureSource)
public static IConfigurationBuilder AddCommandLine(this IConfigurationBuilder builder, Action<CommandLineConfigurationSource>? configureSource)
=> builder.Add(configureSource);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ namespace Microsoft.Extensions.Configuration.CommandLine
/// </summary>
public class CommandLineConfigurationProvider : ConfigurationProvider
{
private readonly Dictionary<string, string> _switchMappings;
private readonly Dictionary<string, string>? _switchMappings;

/// <summary>
/// Initializes a new instance.
/// </summary>
/// <param name="args">The command line args.</param>
/// <param name="switchMappings">The switch mappings.</param>
public CommandLineConfigurationProvider(IEnumerable<string> args, IDictionary<string, string> switchMappings = null)
public CommandLineConfigurationProvider(IEnumerable<string> args, IDictionary<string, string>? switchMappings = null)
{
Args = args ?? throw new ArgumentNullException(nameof(args));

Expand All @@ -38,7 +38,7 @@ public CommandLineConfigurationProvider(IEnumerable<string> args, IDictionary<st
/// </summary>
public override void Load()
{
var data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
var data = new Dictionary<string, string?>(StringComparer.OrdinalIgnoreCase);
string key, value;

using (IEnumerator<string> enumerator = Args.GetEnumerator())
Expand Down Expand Up @@ -68,15 +68,15 @@ public override void Load()

if (separator < 0)
{
// If there is neither equal sign nor prefix in current arugment, it is an invalid format
// If there is neither equal sign nor prefix in current argument, it is an invalid format
if (keyStartIndex == 0)
{
// Ignore invalid formats
continue;
}

// If the switch is a key in given switch mappings, interpret it
if (_switchMappings != null && _switchMappings.TryGetValue(currentArg, out string mappedKey))
if (_switchMappings != null && _switchMappings.TryGetValue(currentArg, out string? mappedKey))
{
key = mappedKey;
}
Expand Down Expand Up @@ -105,7 +105,7 @@ public override void Load()
string keySegment = currentArg.Substring(0, separator);

// If the switch is a key in given switch mappings, interpret it
if (_switchMappings != null && _switchMappings.TryGetValue(keySegment, out string mappedKeySegment))
if (_switchMappings != null && _switchMappings.TryGetValue(keySegment, out string? mappedKeySegment))
{
key = mappedKeySegment;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;

namespace Microsoft.Extensions.Configuration.CommandLine
Expand All @@ -13,12 +14,12 @@ public class CommandLineConfigurationSource : IConfigurationSource
/// <summary>
/// Gets or sets the switch mappings.
/// </summary>
public IDictionary<string, string> SwitchMappings { get; set; }
public IDictionary<string, string>? SwitchMappings { get; set; }

/// <summary>
/// Gets or sets the command line args.
/// </summary>
public IEnumerable<string> Args { get; set; }
public IEnumerable<string> Args { get; set; } = Array.Empty<string>();

/// <summary>
/// Builds the <see cref="CommandLineConfigurationProvider"/> for this source.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppMinimum);netstandard2.0;$(NetFrameworkMinimum)</TargetFrameworks>
<Nullable>enable</Nullable>
<EnableDefaultItems>true</EnableDefaultItems>
<!-- Use targeting pack references instead of granular ones in the project file. -->
<DisableImplicitAssemblyReferences>false</DisableImplicitAssemblyReferences>
<PackageDescription>Command line configuration provider implementation for Microsoft.Extensions.Configuration.</PackageDescription>
</PropertyGroup>

Expand Down

0 comments on commit 8b1a0c8

Please sign in to comment.