cake-build / cake-build/cake

WithRawArgument(string) extension method on ToolSettings

Open
#2,273 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
4.2k
Forks
778
Avg merge
1h 15m
Merged PRs (30d)
19

Description

### Problem

There are times when I'm either debugging a Cake script or using an incomplete settings object that I want to customize the arguments, but I don't want to use the longer syntax `settings.ArgumentCustomization = a => a.Append("--some-argument")`.

The longer syntax also does not compose well. For example, let's say I already have this code and now I need to test how it runs if I add `--some-argument`. It doesn't end up being a simple and quick:

```cs
var settings = new NUnit3Settings { … };

if (parameters != null && parameters.Count != 0)
{
settings.ArgumentCustomization = a =>
{
foreach (var parameter in parameters)
a = a.AppendSwitch("-p", EscapeProcessArgument(parameter.Key + '=' + parameter.Value));

return a;
};
}
```

### Suggestion

What if we had a terse, composable helper do this?

```cs
var settings = new NUnit3Settings { … }
.AddRawArgument("--some-argument");

if (parameters != null)
foreach (var parameter in parameters)
settings.AppendRawArgument("-p=" + EscapeProcessArgument(parameter.Key + '=' + parameter.Value));
```

Here's what I end up adding to Cake scripts:

```cs
public static T WithRawArgument(this T settings, string rawArgument)
where T : Cake.Core.Tooling.ToolSettings
{
if (settings == null) throw new ArgumentNullException(nameof(settings));

if (!string.IsNullOrEmpty(rawArgument))
{
var previousCustomizer = settings.ArgumentCustomization;
if (previousCustomizer != null)
settings.ArgumentCustomization = builder => previousCustomizer.Invoke(builder).Append(rawArgument);
else
settings.ArgumentCustomization = builder => builder.Append(rawArgument);
}

return settings;
}
```

That's just a straw man, but would you consider something similar?

Contributor guide

Open the contributing guide

Research direction

Start at Cake.Core.Tooling.ToolSettings and its ArgumentCustomization property, then compare the proposed WithRawArgument extension with the existing argument-builder behavior shown in the issue. Determine the final composable API and add coverage for empty arguments, existing customizers, and returning the same settings object; done means raw arguments can be appended without replacing earlier customization.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.