mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-19 13:28:16 +08:00
Parse quoted strings correctly
When parsing quoted strings, space was not handled properly in remaining arguments. Fixes #186
This commit is contained in:

committed by
Patrik Svensson

parent
241423dd16
commit
79742ce9e3
@ -24,6 +24,7 @@ namespace Spectre.Console.Cli.Internal
|
||||
}
|
||||
|
||||
_registrar.RegisterInstance(typeof(IConfiguration), configuration);
|
||||
_registrar.RegisterInstance(typeof(IAnsiConsole), configuration.Settings.Console.GetConsole());
|
||||
|
||||
// Create the command model.
|
||||
var model = CommandModelBuilder.Build(configuration);
|
||||
|
@ -116,34 +116,41 @@ namespace Spectre.Console.Cli.Internal
|
||||
{
|
||||
var position = reader.Position;
|
||||
|
||||
context.FlushRemaining();
|
||||
reader.Consume('\"');
|
||||
context.AddRemaining('\"');
|
||||
|
||||
var builder = new StringBuilder();
|
||||
var terminated = false;
|
||||
while (!reader.ReachedEnd)
|
||||
{
|
||||
var character = reader.Peek();
|
||||
if (character == '\"')
|
||||
{
|
||||
terminated = true;
|
||||
reader.Read();
|
||||
break;
|
||||
}
|
||||
|
||||
context.AddRemaining(character);
|
||||
builder.Append(reader.Read());
|
||||
}
|
||||
|
||||
if (reader.Peek() != '\"')
|
||||
if (!terminated)
|
||||
{
|
||||
var unterminatedQuote = builder.ToString();
|
||||
var token = new CommandTreeToken(CommandTreeToken.Kind.String, position, unterminatedQuote, $"\"{unterminatedQuote}");
|
||||
throw CommandParseException.UnterminatedQuote(reader.Original, token);
|
||||
}
|
||||
|
||||
reader.Read();
|
||||
context.AddRemaining('\"');
|
||||
var quotedString = builder.ToString();
|
||||
|
||||
var value = builder.ToString();
|
||||
return new CommandTreeToken(CommandTreeToken.Kind.String, position, value, $"\"{value}\"");
|
||||
// Add to the context
|
||||
context.AddRemaining(quotedString);
|
||||
context.FlushRemaining();
|
||||
|
||||
return new CommandTreeToken(
|
||||
CommandTreeToken.Kind.String,
|
||||
position, quotedString,
|
||||
quotedString);
|
||||
}
|
||||
|
||||
private static IEnumerable<CommandTreeToken> ScanOptions(CommandTreeTokenizerContext context, TextBuffer reader)
|
||||
|
@ -32,6 +32,14 @@ namespace Spectre.Console.Cli.Internal
|
||||
}
|
||||
}
|
||||
|
||||
public void AddRemaining(string text)
|
||||
{
|
||||
if (Mode == CommandTreeTokenizer.Mode.Remaining)
|
||||
{
|
||||
_builder.Append(text);
|
||||
}
|
||||
}
|
||||
|
||||
public void FlushRemaining()
|
||||
{
|
||||
if (Mode == CommandTreeTokenizer.Mode.Remaining)
|
||||
|
Reference in New Issue
Block a user