Skip to content

Commit

Permalink
Merge pull request #3388 from sunshykin/feature/gh-2690
Browse files Browse the repository at this point in the history
Added Arguments() method to retrieve command line arguments as IDictionary
  • Loading branch information
augustoproiete authored Jul 25, 2021
2 parents 5767cc5 + 0790bb2 commit 80c431f
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 11 deletions.
48 changes: 42 additions & 6 deletions src/Cake.Common.Tests/Unit/ArgumentAliasesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Linq;
using Cake.Core;
using Cake.Core.IO;
using NSubstitute;
Expand All @@ -11,17 +13,26 @@ namespace Cake.Common.Tests.Unit
{
public sealed class ArgumentAliases
{
private const string WorkingDirectoryArgumentName = "workdir";
private const string WorkingDirectoryArgumentValue = "c:/data/work";
private const string OutputFileArgumentName = "outputFile";
private const string OutputFileArgumentValue = "c:/data/work/output.txt";
private const string VerboseArgumentName = "verbose";
private const string VerboseArgumentValueOne = "enabled";
private const string VerboseArgumentValueTwo = "full";

public sealed class TheArgumentOfDirectoryPathMethod
{
[Fact]
public void Should_Convert_A_String_Value_To_A_DirectoryPath_If_Argument_Exist()
{
var context = Substitute.For<ICakeContext>();
context.Arguments.GetArguments("workdir").Returns(new[] { "c:/data/work" });
context.Arguments.GetArguments(WorkingDirectoryArgumentName)
.Returns(new[] { WorkingDirectoryArgumentValue });

var result = context.Argument<DirectoryPath>("workdir");
var result = context.Argument<DirectoryPath>(WorkingDirectoryArgumentName);

Assert.Equal("c:/data/work", result.FullPath);
Assert.Equal(WorkingDirectoryArgumentValue, result.FullPath);
}
}

Expand All @@ -31,11 +42,36 @@ public sealed class TheArgumentOfFilePathMethod
public void Should_Convert_A_String_Value_To_A_FilePath_If_Argument_Exist()
{
var context = Substitute.For<ICakeContext>();
context.Arguments.GetArguments("outputFile").Returns(new[] { "c:/data/work/output.txt" });
context.Arguments.GetArguments(OutputFileArgumentName)
.Returns(new[] { OutputFileArgumentValue });

var result = context.Argument<FilePath>(OutputFileArgumentName);

Assert.Equal(OutputFileArgumentValue, result.FullPath);
}
}

public sealed class TheArgumentCollectionMethod
{
[Fact]
public void Should_Return_An_Arguments_Dictionary()
{
var context = Substitute.For<ICakeContext>();
context.Arguments.GetArguments()
.Returns(new Dictionary<string, ICollection<string>>
{
{ WorkingDirectoryArgumentName, new[] { WorkingDirectoryArgumentValue } },
{ VerboseArgumentName, new[] { VerboseArgumentValueOne, VerboseArgumentValueTwo } },
});

var result = context.Argument<FilePath>("outputFile");
var result = context.Arguments();
var wdValues = result[WorkingDirectoryArgumentName];
var vValues = result[VerboseArgumentName];

Assert.Equal("c:/data/work/output.txt", result.FullPath);
Assert.Equal(WorkingDirectoryArgumentValue, wdValues.First());
Assert.Equal(2, vValues.Count);
Assert.Equal(VerboseArgumentValueOne, vValues.ElementAt(0));
Assert.Equal(VerboseArgumentValueTwo, vValues.ElementAt(1));
}
}
}
Expand Down
34 changes: 34 additions & 0 deletions src/Cake.Common/ArgumentAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,40 @@ public static T Argument<T>(this ICakeContext context, string name, T defaultVal
: Convert<T>(value);
}

/// <summary>
/// Retrieves all command line arguments.
/// </summary>
/// <example>
/// <code>
/// var args = context.Arguments();
///
/// if (args.ContainsKey("verbose"))
/// {
/// Information("Verbose output enabled");
/// }
///
/// foreach(var arg in args)
/// {
/// Information(
/// "Key: {0}\tValue: \"{1}\"",
/// arg.Key,
/// string.Join(";", arg.Value)
/// );
/// }
/// </code>
/// </example>
/// <param name="context">The context.</param>
/// <returns>The command line arguments.</returns>
[CakeMethodAlias]
public static IDictionary<string, ICollection<string>> Arguments(this ICakeContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
return context.Arguments.GetArguments();
}

private static T Convert<T>(string value)
{
var converter = TypeDescriptor.GetConverter(typeof(T));
Expand Down
9 changes: 9 additions & 0 deletions src/Cake.Core/CakeArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,14 @@ public ICollection<string> GetArguments(string name)
_arguments.TryGetValue(name, out var arguments);
return arguments ?? (ICollection<string>)Array.Empty<string>();
}

/// <inheritdoc/>
public IDictionary<string, ICollection<string>> GetArguments()
{
var arguments = _arguments
.ToDictionary(x => x.Key, x => (ICollection<string>)x.Value.ToList());

return arguments;
}
}
}
2 changes: 0 additions & 2 deletions src/Cake.Core/CakeEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Versioning;
using Cake.Core.Diagnostics;
using Cake.Core.IO;
using Cake.Core.Polyfill;

Expand Down
6 changes: 6 additions & 0 deletions src/Cake.Core/ICakeArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,11 @@ public interface ICakeArguments
/// <param name="name">The argument name.</param>
/// <returns>The argument values.</returns>
ICollection<string> GetArguments(string name);

/// <summary>
/// Gets all command line arguments.
/// </summary>
/// <returns>The command line arguments as IDictionary&lt;string, ICollection&lt;string&gt;&gt;.</returns>
IDictionary<string, ICollection<string>> GetArguments();
}
}
4 changes: 1 addition & 3 deletions src/Cake.Core/ICakeEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Runtime.Versioning;
using Cake.Core.IO;

namespace Cake.Core
Expand Down Expand Up @@ -43,7 +41,7 @@ public interface ICakeEnvironment
/// <summary>
/// Gets all environment variables.
/// </summary>
/// <returns>The environment variables as IDictionary&lt;string, string&gt;. </returns>
/// <returns>The environment variables as IDictionary&lt;string, string&gt;.</returns>
IDictionary<string, string> GetEnvironmentVariables();

/// <summary>
Expand Down
12 changes: 12 additions & 0 deletions src/Cake.Testing/FakeEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace Cake.Testing
public sealed class FakeEnvironment : ICakeEnvironment
{
private readonly Dictionary<string, string> _environmentVariables;
private readonly Dictionary<string, string> _commandLineArguments;
private readonly Dictionary<SpecialPath, DirectoryPath> _specialPaths;

/// <inheritdoc/>
Expand Down Expand Up @@ -53,6 +54,7 @@ public FakeEnvironment(PlatformFamily family, bool is64Bit = true)
Platform = new FakePlatform(family, is64Bit);
Runtime = new FakeRuntime();
_environmentVariables = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
_commandLineArguments = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
_specialPaths = new Dictionary<SpecialPath, DirectoryPath>();
}

Expand Down Expand Up @@ -147,6 +149,16 @@ public void SetEnvironmentVariable(string variable, string value)
_environmentVariables[variable] = value;
}

/// <summary>
/// Sets a command line argument.
/// </summary>
/// <param name="argument">The argument.</param>
/// <param name="value">The value.</param>
public void SetCommandLineArgument(string argument, string value)
{
_commandLineArguments[argument] = value;
}

/// <summary>
/// Sets the built framework.
/// </summary>
Expand Down

0 comments on commit 80c431f

Please sign in to comment.