Skip to content

Commit ef43270

Browse files
committed
Update to latest System.CommandLine
1 parent ef15f66 commit ef43270

File tree

35 files changed

+121
-259
lines changed

35 files changed

+121
-259
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,3 +399,5 @@ FodyWeavers.xsd
399399

400400
# Verify snapshots
401401
*.received.*
402+
403+
launchSettings.json

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
### Added
6+
57
- Build and test on each platform version available
68
- macOS 13
79
- macOS 14
@@ -15,6 +17,11 @@
1517
- Ubuntu 24.04
1618
- Windows 11
1719

20+
### Updated
21+
22+
- Bumped `System.CommandLine` from 2.0.0-beta4.22272.1 to 2.0.0-beta5.25306.1
23+
- Bumped `System.CommandLine.Rendering` from 0.4.0-alpha.22272.1 to 0.4.0-alpha.25306.1
24+
1825
## [0.6.0](https://github.com/xt0rted/dotnet-run-script/compare/v0.5.0...v0.6.0) - 2024-04-10
1926

2027
- Dropped support for .NET Core 3.1

src/ConsoleWriter.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
namespace RunScript;
22

3+
using System;
34
using System.CommandLine.Rendering;
45
using System.Globalization;
56

67
internal class ConsoleWriter : IConsoleWriter
78
{
8-
private readonly IConsole _console;
9-
private readonly IFormatProvider? _consoleFormatProvider;
10-
9+
private readonly TextWriter _output;
10+
private readonly IFormatProvider _consoleFormatProvider;
1111
private readonly bool _verbose;
1212

13-
public ConsoleWriter(IConsole console, IFormatProvider consoleFormatProvider, bool verbose)
13+
public ConsoleWriter(TextWriter output, IFormatProvider consoleFormatProvider, bool verbose)
1414
{
15-
_console = console ?? throw new ArgumentNullException(nameof(console));
16-
_consoleFormatProvider = consoleFormatProvider ?? throw new ArgumentNullException(nameof(consoleFormatProvider));
15+
ArgumentNullException.ThrowIfNull(output);
16+
ArgumentNullException.ThrowIfNull(consoleFormatProvider);
17+
18+
_output = output;
19+
_consoleFormatProvider = consoleFormatProvider;
1720
_verbose = verbose;
1821
}
1922

@@ -23,36 +26,36 @@ private void WriteLine(AnsiControlCode modifierOn, AnsiControlCode modifierOff,
2326
{
2427
if (message is not null)
2528
{
26-
_console.Out.Write(modifierOn.ToString(null, _consoleFormatProvider));
29+
_output.Write(modifierOn.ToString(null, _consoleFormatProvider));
2730

2831
if (args?.Length > 0)
2932
{
30-
_console.Out.Write(string.Format(CultureInfo.CurrentCulture, message, args));
33+
_output.Write(string.Format(CultureInfo.CurrentCulture, message, args));
3134
}
3235
else
3336
{
34-
_console.Out.Write(message);
37+
_output.Write(message);
3538
}
3639

37-
_console.Out.Write(modifierOff.ToString(null, _consoleFormatProvider));
38-
_console.Out.Write(Environment.NewLine);
40+
_output.Write(modifierOff.ToString(null, _consoleFormatProvider));
41+
_output.Write(Environment.NewLine);
3942
}
4043
}
4144

4245
public void Raw(string? message)
43-
=> _console.Out.Write(message);
46+
=> _output.Write(message);
4447

4548
public void VerboseBanner()
4649
=> LineVerbose("Verbose mode is on. This will print more information.");
4750

4851
public void BlankLine()
49-
=> _console.Out.Write(Environment.NewLine);
52+
=> _output.Write(Environment.NewLine);
5053

5154
public void BlankLineVerbose()
5255
{
5356
if (_verbose)
5457
{
55-
_console.Out.Write(Environment.NewLine);
58+
_output.Write(Environment.NewLine);
5659
}
5760
}
5861

src/GlobalArguments.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ namespace RunScript;
22

33
public static class GlobalArguments
44
{
5-
public static readonly Argument<string[]> Scripts = new("scripts", "One or more scripts to run")
5+
public static readonly Argument<string[]> Scripts = new("scripts")
66
{
77
Arity = ArgumentArity.ZeroOrMore,
8+
Description = "One or more scripts to run",
9+
DefaultValueFactory = _ => [],
810
};
911
}

src/GlobalOptions.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@ namespace RunScript;
22

33
public static class GlobalOptions
44
{
5-
public static readonly Option<bool> IfPresent = new("--if-present", "Don't exit with an error code if the script isn't found");
5+
public static readonly Option<bool> IfPresent = new("--if-present")
6+
{
7+
Description = "Don't exit with an error code if the script isn't found",
8+
};
69

7-
public static readonly Option<string> ScriptShell = new("--script-shell", "The shell to use when running scripts (cmd, pwsh, sh, etc.)")
10+
public static readonly Option<string> ScriptShell = new("--script-shell")
811
{
9-
ArgumentHelpName = "shell",
12+
Description = "The shell to use when running scripts (cmd, pwsh, sh, etc.)",
13+
HelpName = "shell",
1014
};
1115

12-
public static readonly Option<bool> Verbose = new(new[] { "-v", "--verbose" }, "Enable verbose output");
16+
public static readonly Option<bool> Verbose = new("--verbose", "-v")
17+
{
18+
Description = "Enable verbose output",
19+
};
1320
}

src/Program.cs

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,16 @@
1-
using System.CommandLine.Builder;
2-
using System.CommandLine.Parsing;
3-
41
using RunScript;
52

63
var environment = new EnvironmentWrapper();
74
var consoleFormatProvider = ConsoleHelpers.FormatInfo(environment);
85
var rootCommand = new RunScriptCommand(
96
environment,
107
consoleFormatProvider,
11-
environment.CurrentDirectory);
12-
13-
var parser = new CommandLineBuilder(rootCommand)
14-
.UseVersionOption()
15-
.UseHelp()
16-
.UseEnvironmentVariableDirective()
17-
.UseParseDirective()
18-
.UseSuggestDirective()
19-
.RegisterWithDotnetSuggest()
20-
.UseParseErrorReporting()
21-
.UseExceptionHandler((ex, ctx) =>
22-
{
23-
var verbose = ctx.ParseResult.HasOption(GlobalOptions.Verbose);
24-
var writer = new ConsoleWriter(ctx.Console, consoleFormatProvider, verbose);
25-
26-
if (verbose)
27-
{
28-
writer.Error(ex.ToString());
29-
}
30-
else
31-
{
32-
writer.Error(ex.Message);
33-
}
34-
})
35-
.CancelOnProcessTermination()
36-
.EnableLegacyDoubleDashBehavior()
37-
.Build();
8+
environment.CurrentDirectory)
9+
{
10+
new DiagramDirective(),
11+
new EnvironmentVariablesDirective()
12+
};
3813

39-
var parseResult = parser.Parse(args);
14+
var parseResult = rootCommand.Parse(args);
4015

4116
return await parseResult.InvokeAsync();

src/RunScriptCommand.cs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
namespace RunScript;
22

3-
using System.CommandLine.Invocation;
4-
53
using DotNet.Globbing;
64

75
using RunScript.Logging;
86

9-
internal class RunScriptCommand : RootCommand, ICommandHandler
7+
internal class RunScriptCommand : RootCommand
108
{
119
private readonly IEnvironment _environment;
1210
private readonly IFormatProvider _consoleFormatProvider;
@@ -25,28 +23,25 @@ internal RunScriptCommand(
2523

2624
_workingDirectory = workingDirectory;
2725

28-
AddArgument(GlobalArguments.Scripts);
26+
Arguments.Add(GlobalArguments.Scripts);
2927

30-
AddOption(GlobalOptions.IfPresent);
31-
AddOption(GlobalOptions.ScriptShell);
32-
AddOption(GlobalOptions.Verbose);
28+
Options.Add(GlobalOptions.IfPresent);
29+
Options.Add(GlobalOptions.ScriptShell);
30+
Options.Add(GlobalOptions.Verbose);
3331

34-
Handler = this;
32+
SetAction(InvokeAsync);
3533
}
3634

37-
public int Invoke(InvocationContext context)
38-
=> throw new NotImplementedException();
39-
40-
public async Task<int> InvokeAsync(InvocationContext context)
35+
internal async Task<int> InvokeAsync(ParseResult parseResult, CancellationToken cancellationToken)
4136
{
42-
ArgumentNullException.ThrowIfNull(context);
37+
ArgumentNullException.ThrowIfNull(parseResult);
4338

44-
var ifPresent = context.ParseResult.GetValueForOption(GlobalOptions.IfPresent);
45-
var scriptShell = context.ParseResult.GetValueForOption(GlobalOptions.ScriptShell);
46-
var verbose = context.ParseResult.GetValueForOption(GlobalOptions.Verbose);
47-
var scripts = context.ParseResult.GetValueForArgument(GlobalArguments.Scripts);
39+
var ifPresent = parseResult.GetValue(GlobalOptions.IfPresent);
40+
var scriptShell = parseResult.GetValue(GlobalOptions.ScriptShell);
41+
var verbose = parseResult.GetValue(GlobalOptions.Verbose);
42+
var scripts = parseResult.GetRequiredValue(GlobalArguments.Scripts);
4843

49-
var writer = new ConsoleWriter(context.Console, _consoleFormatProvider, verbose);
44+
var writer = new ConsoleWriter(parseResult.Configuration.Output, _consoleFormatProvider, verbose);
5045

5146
writer.VerboseBanner();
5247

@@ -113,9 +108,9 @@ public async Task<int> InvokeAsync(InvocationContext context)
113108
// UnparsedTokens is backed by string[] so if we cast
114109
// back to that we get a lot better perf down the line.
115110
// Hopefully this doesn't break in the future 🤞
116-
var scriptArgs = (string[])context.ParseResult.UnparsedTokens;
111+
var scriptArgs = (string[])parseResult.UnmatchedTokens;
117112

118-
var scriptRunner = builder.CreateGroupRunner(context.GetCancellationToken());
113+
var scriptRunner = builder.CreateGroupRunner(cancellationToken);
119114

120115
var result = await scriptRunner.RunAsync(
121116
script.Name,

test/CommandBuilderTests.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
namespace RunScript;
22

33
using System.Collections.Generic;
4-
using System.CommandLine.IO;
54
using System.CommandLine.Rendering;
65

76
[Trait("category", "unit")]
@@ -91,12 +90,12 @@ public void CreateGroupRunner_should_create_a_runner(bool isWindows)
9190

9291
private static CommandBuilder SetUpTest(bool isWindows, string? comSpec = DefaultComSpec)
9392
{
94-
var console = new TestConsole();
93+
var output = new StringWriter();
9594
var consoleFormatProvider = new ConsoleFormatInfo
9695
{
9796
SupportsAnsiCodes = false,
9897
};
99-
var consoleWriter = new ConsoleWriter(console, consoleFormatProvider, verbose: true);
98+
var consoleWriter = new ConsoleWriter(output, consoleFormatProvider, verbose: true);
10099

101100
var project = new Project
102101
{

test/CommandGroupRunnerTests.Should_emit_environment_variable_list_and_pre_and_post_scripts_when_env_script_not_defined_isWindows=False.verified.txt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
{
2-
console: {
3-
IsErrorRedirected: false,
4-
IsInputRedirected: false,
5-
IsOutputRedirected: false,
6-
Out:
2+
output:
73
> env
84

95
value1=value 1
106
value2=value 2
117
value3=value 3
12-
13-
},
8+
,
149
commandRunners: [
1510
{
1611
Name: preenv,

test/CommandGroupRunnerTests.Should_emit_environment_variable_list_and_pre_and_post_scripts_when_env_script_not_defined_isWindows=True.verified.txt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
{
2-
console: {
3-
IsErrorRedirected: false,
4-
IsInputRedirected: false,
5-
IsOutputRedirected: false,
6-
Out:
2+
output:
73
> env
84

95
value1=value 1
106
value2=value 2
117
value3=value 3
12-
13-
},
8+
,
149
commandRunners: [
1510
{
1611
Name: preenv,

0 commit comments

Comments
 (0)