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

[Group 4] Enable nullable annotations for Microsoft.Extensions.Configuration.CommandLine #57432

Merged
merged 14 commits into from
Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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