Skip to content
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
51 changes: 0 additions & 51 deletions src/Common/ArgumentBuilder.cs

This file was deleted.

60 changes: 0 additions & 60 deletions src/Common/OptionBuilder.cs

This file was deleted.

58 changes: 27 additions & 31 deletions src/System.CommandLine.Tests/Binding/TypeConversionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using FluentAssertions;
using System.Collections;
using System.Collections.Generic;
using System.CommandLine.Utility;
using System.IO;
using System.Linq;
using System.Net;
Expand Down Expand Up @@ -134,14 +133,17 @@ public void Command_Argument_defaults_arity_to_ZeroOrOne_for_nullable_types()
command.Arguments.Single().Arity.Should().BeEquivalentTo(ArgumentArity.ZeroOrOne);
}

[Theory]
[InlineData(typeof(int[]))]
[InlineData(typeof(IEnumerable<int>))]
[InlineData(typeof(List<int>))]
public void Argument_infers_arity_of_IEnumerable_types_as_OneOrMore(Type type)
public static IEnumerable<object[]> EnumerableTypes()
{
var argument = ArgumentBuilder.CreateArgument(type);
yield return new object[] { new Argument<int[]>("array") };
yield return new object[] { new Argument<IEnumerable<int>>("enumerable") };
yield return new object[] { new Argument<List<int>>("list") };
}

[Theory]
[MemberData(nameof(EnumerableTypes))]
public void Argument_infers_arity_of_IEnumerable_types_as_OneOrMore(Argument argument)
{
argument.Arity.Should().BeEquivalentTo(ArgumentArity.OneOrMore);
}

Expand Down Expand Up @@ -693,34 +695,28 @@ public void Values_can_be_correctly_converted_to_nullable_uint_without_the_parse
public void Values_can_be_correctly_converted_to_array_of_int_without_the_parser_specifying_a_custom_converter()
=> GetValue(new Option<int[]>("-x"), "-x 1 -x 2 -x 3").Should().BeEquivalentTo(new[] { 1, 2, 3 });

public static IEnumerable<object[]> AritiesAndEnumerableTypes()
{
foreach (int minArity in new[] { 0, 1 })
{
foreach (int maxArity in new[] { 3, 100_000 })
{
yield return new object[] { minArity, maxArity, new Option<string[]>("--items") };
yield return new object[] { minArity, maxArity, new Option<IEnumerable<string>>("--items") };
yield return new object[] { minArity, maxArity, new Option<List<string>>("--items") };
yield return new object[] { minArity, maxArity, new Option<IList<string>>("--items") };
yield return new object[] { minArity, maxArity, new Option<ICollection<string>>("--items") };
}
}
}

[Theory]
[InlineData(0, 100_000, typeof(string[]))]
[InlineData(0, 3, typeof(string[]))]
[InlineData(0, 100_000, typeof(IEnumerable<string>))]
[InlineData(0, 3, typeof(IEnumerable<string>))]
[InlineData(0, 100_000, typeof(List<string>))]
[InlineData(0, 3, typeof(List<string>))]
[InlineData(0, 100_000, typeof(IList<string>))]
[InlineData(0, 3, typeof(IList<string>))]
[InlineData(0, 100_000, typeof(ICollection<string>))]
[InlineData(0, 3, typeof(ICollection<string>))]

[InlineData(1, 100_000, typeof(string[]))]
[InlineData(1, 3, typeof(string[]))]
[InlineData(1, 100_000, typeof(IEnumerable<string>))]
[InlineData(1, 3, typeof(IEnumerable<string>))]
[InlineData(1, 100_000, typeof(List<string>))]
[InlineData(1, 3, typeof(List<string>))]
[InlineData(1, 100_000, typeof(IList<string>))]
[InlineData(1, 3, typeof(IList<string>))]
[InlineData(1, 100_000, typeof(ICollection<string>))]
[InlineData(1, 3, typeof(ICollection<string>))]
[MemberData(nameof(AritiesAndEnumerableTypes))]
public void Max_arity_greater_than_1_converts_to_enumerable_types(
int minArity,
int maxArity,
Type argumentType)
Option option)
{
var option = OptionBuilder.CreateOption("--items", valueType: argumentType);
option.Arity = new ArgumentArity(minArity, maxArity);

var command = new RootCommand
Expand All @@ -731,7 +727,7 @@ public void Max_arity_greater_than_1_converts_to_enumerable_types(
var result = command.Parse("--items one --items two --items three");

result.Errors.Should().BeEmpty();
result.GetResult(option).GetValueOrDefault<object>().Should().BeAssignableTo(argumentType);
result.GetResult(option).GetValueOrDefault<object>().Should().BeAssignableTo(option.ValueType);
}

[Fact]
Expand Down
2 changes: 0 additions & 2 deletions src/System.CommandLine.Tests/System.CommandLine.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
</ItemGroup>

<ItemGroup>
<Compile Include="..\Common\ArgumentBuilder.cs" Link="Utility\ArgumentBuilder.cs" />
<Compile Include="..\Common\OptionBuilder.cs" Link="Utility\OptionBuilder.cs" />
<Compile Include="..\System.CommandLine.Suggest\DotnetMuxer.cs" Link="Utility\DotnetMuxer.cs" />
<Compile Include="..\System.CommandLine\LocalizationResources.cs" Link="LocalizationResources.cs" />
<Compile Include="..\System.CommandLine\Properties\Resources.Designer.cs" Link="Properties\Resources.Designer.cs" />
Expand Down
24 changes: 0 additions & 24 deletions src/System.CommandLine/RootCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.CommandLine.Completions;
using System.CommandLine.Help;
using System.IO;
using System.Reflection;

namespace System.CommandLine
{
Expand All @@ -19,10 +18,8 @@ namespace System.CommandLine
/// </remarks>
public class RootCommand : Command
{
private static Assembly? _assembly;
private static string? _executablePath;
private static string? _executableName;
private static string? _executableVersion;

/// <param name="description">The description of the command, shown in help.</param>
public RootCommand(string description = "") : base(ExecutableName, description)
Expand All @@ -45,9 +42,6 @@ public RootCommand(string description = "") : base(ExecutableName, description)
/// </summary>
public void Add(Directive directive) => Directives.Add(directive);

internal static Assembly GetAssembly()
=> _assembly ??= (Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly());

/// <summary>
/// The name of the currently running executable.
/// </summary>
Expand All @@ -58,23 +52,5 @@ public static string ExecutableName
/// The path to the currently running executable.
/// </summary>
public static string ExecutablePath => _executablePath ??= Environment.GetCommandLineArgs()[0];

internal static string ExecutableVersion => _executableVersion ??= GetExecutableVersion();

private static string GetExecutableVersion()
{
var assembly = GetAssembly();

var assemblyVersionAttribute = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();

if (assemblyVersionAttribute is null)
{
return assembly.GetName().Version?.ToString() ?? "";
}
else
{
return assemblyVersionAttribute.InformationalVersion;
}
}
}
}
19 changes: 18 additions & 1 deletion src/System.CommandLine/VersionOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.CommandLine.Invocation;
using System.CommandLine.Parsing;
using System.Linq;
using System.Reflection;

namespace System.CommandLine
{
Expand Down Expand Up @@ -64,11 +65,27 @@ private sealed class VersionOptionAction : SynchronousCommandLineAction
{
public override int Invoke(ParseResult parseResult)
{
parseResult.InvocationConfiguration.Output.WriteLine(RootCommand.ExecutableVersion);
parseResult.InvocationConfiguration.Output.WriteLine(GetExecutableVersion());
return 0;
}

public override bool ClearsParseErrors => true;

private static string GetExecutableVersion()
{
var assembly = Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly();

var assemblyVersionAttribute = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();

if (assemblyVersionAttribute is null)
{
return assembly.GetName().Version?.ToString() ?? "";
}
else
{
return assemblyVersionAttribute.InformationalVersion;
}
}
}
}
}