Skip to content

Commit

Permalink
Add subsystem implementing response file handling
Browse files Browse the repository at this point in the history
  • Loading branch information
KathleenDollard authored and mhutch committed Apr 4, 2024
1 parent 39afcc0 commit 9419c21
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/System.CommandLine.Subsystems.Tests/ResponseSubsystemTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using FluentAssertions;
using System.CommandLine.Directives;
using System.CommandLine.Parsing;
using Xunit;

namespace System.CommandLine.Subsystems.Tests;

public class ResponseSubsystemTests
{

[Fact]
// TODO: Not sure why these tests are passing
public void Simple_response_file_contributes_to_parsing()
{
var option = new CliOption<string>("--hello");
var rootCommand = new CliRootCommand { option };
var configuration = new CliConfiguration(rootCommand);
var subsystem = new ResponseSubsystem();
string[] args = ["@Response_1.rsp"];

Subsystem.Initialize(subsystem, configuration, args);

var parseResult = CliParser.Parse(rootCommand, args, configuration);
var value = parseResult.GetValue(option);

value.Should().Be("world");
}
}
1 change: 1 addition & 0 deletions src/System.CommandLine.Subsystems.Tests/Response_1.rsp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--hello world
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<ItemGroup>
<Content Include="TestApps/**/*.csproj" CopyToOutputDirectory="PreserveNewest" />
<Content Include="TestApps/**/*.cs" CopyToOutputDirectory="PreserveNewest" />
<Content Include="Response_1.rsp" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

<ItemGroup>
Expand All @@ -31,6 +32,7 @@
-->
<Compile Include="AlternateSubsystems.cs" />
<Compile Include="Constants.cs" />
<Compile Include="ResponseSubsystemTests.cs" />
<Compile Include="DirectiveSubsystemTests.cs" />
<Compile Include="DiagramSubsystemTests.cs" />
<Compile Include="PipelineTests.cs" />
Expand Down
103 changes: 103 additions & 0 deletions src/System.CommandLine.Subsystems/Directives/ResponseSubsystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.CommandLine.Parsing;
using System.CommandLine.Subsystems;

namespace System.CommandLine.Directives;

public class ResponseSubsystem()
: CliSubsystem("Response", SubsystemKind.Response, null)
{
protected internal override CliConfiguration Initialize(InitializationContext context)
{
context.Configuration.ResponseFileTokenReplacer = Replacer;
return context.Configuration;
}

public static (List<string>? tokens, List<string>? errors) Replacer(string responseSourceName)
{
try
{
// TODO: Include checks from previous system.
var contents = File.ReadAllText(responseSourceName);
return (CliParser.SplitCommandLine(contents).ToList(), null);
}
catch
{
// TODO: Switch to proper errors
return (null,
errors:
[
$"Failed to open response file {responseSourceName}"
]);
}
}

// TODO: File handling from previous system - ensure these checks are done (note: no tests caught these oversights
/* internal static bool TryReadResponseFile(
string filePath,
out IReadOnlyList<string>? newTokens,
out string? error)
{
try
{
newTokens = ExpandResponseFile(filePath).ToArray();
error = null;
return true;
}
catch (FileNotFoundException)
{
error = LocalizationResources.ResponseFileNotFound(filePath);
}
catch (IOException e)
{
error = LocalizationResources.ErrorReadingResponseFile(filePath, e);
}
newTokens = null;
return false;
static IEnumerable<string> ExpandResponseFile(string filePath)
{
var lines = File.ReadAllLines(filePath);
for (var i = 0; i < lines.Length; i++)
{
var line = lines[i];
foreach (var p in SplitLine(line))
{
if (GetReplaceableTokenValue(p) is { } path)
{
foreach (var q in ExpandResponseFile(path))
{
yield return q;
}
}
else
{
yield return p;
}
}
}
}
static IEnumerable<string> SplitLine(string line)
{
var arg = line.Trim();
if (arg.Length == 0 || arg[0] == '#')
{
yield break;
}
foreach (var word in CliParser.SplitCommandLine(arg))
{
yield return word;
}
}
}
*/

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public enum SubsystemKind
ErrorReporting,
Completion,
Diagram,
Response,
}

0 comments on commit 9419c21

Please sign in to comment.