-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add subsystem implementing response file handling
- Loading branch information
1 parent
39afcc0
commit 9419c21
Showing
5 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/System.CommandLine.Subsystems.Tests/ResponseSubsystemTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--hello world |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
src/System.CommandLine.Subsystems/Directives/ResponseSubsystem.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
*/ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,5 @@ public enum SubsystemKind | |
ErrorReporting, | ||
Completion, | ||
Diagram, | ||
Response, | ||
} |