Browse Source

abp batch command should handle spaces and quotes in arguments.

pull/8914/head
Halil İbrahim Kalkan 5 years ago
parent
commit
9d90f16e16
  1. 47
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Args/CommandLineArgumentParser.cs
  2. 2
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Args/ICommandLineArgumentParser.cs
  3. 6
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliService.cs
  4. 21
      framework/test/Volo.Abp.Cli.Core.Tests/Volo/Abp/Cli/CommandLineArgumentParser_Tests.cs

47
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Args/CommandLineArgumentParser.cs

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
namespace Volo.Abp.Cli.Args namespace Volo.Abp.Cli.Args
@ -71,6 +72,11 @@ namespace Volo.Abp.Cli.Args
return commandLineArgs; return commandLineArgs;
} }
public CommandLineArgs Parse(string lineText)
{
return Parse(GetArgsArrayFromLine(lineText));
}
private static bool IsOptionName(string argument) private static bool IsOptionName(string argument)
{ {
return argument.StartsWith("-") || argument.StartsWith("--"); return argument.StartsWith("-") || argument.StartsWith("--");
@ -100,5 +106,46 @@ namespace Volo.Abp.Cli.Args
throw new ArgumentException("Option names should start with '-' or '--'."); throw new ArgumentException("Option names should start with '-' or '--'.");
} }
private static string[] GetArgsArrayFromLine(string lineText)
{
var args = new List<string>();
var currentArgBuilder = new StringBuilder();
string currentArg = null;
bool isInQuotes = false;
for (int i = 0; i < lineText.Length; i++)
{
var c = lineText[i];
if (c == ' ' && !isInQuotes)
{
currentArg = currentArgBuilder.ToString();
if (!currentArg.IsNullOrWhiteSpace())
{
args.Add(currentArg);
}
currentArgBuilder = new StringBuilder();
}
else
{
if (c == '\"')
{
isInQuotes = !isInQuotes;
}
else
{
currentArgBuilder.Append(c);
}
}
}
currentArg = currentArgBuilder.ToString();
if (!currentArg.IsNullOrWhiteSpace())
{
args.Add(currentArg);
}
return args.ToArray();
}
} }
} }

2
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Args/ICommandLineArgumentParser.cs

@ -3,5 +3,7 @@
public interface ICommandLineArgumentParser public interface ICommandLineArgumentParser
{ {
CommandLineArgs Parse(string[] args); CommandLineArgs Parse(string[] args);
CommandLineArgs Parse(string lineText);
} }
} }

6
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliService.cs

@ -3,9 +3,11 @@ using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using NuGet.Versioning; using NuGet.Versioning;
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.Cli.Args; using Volo.Abp.Cli.Args;
using Volo.Abp.Cli.Commands; using Volo.Abp.Cli.Commands;
@ -98,11 +100,11 @@ namespace Volo.Abp.Cli
lineText = lineText.Substring(0, lineText.IndexOf('#')); lineText = lineText.Substring(0, lineText.IndexOf('#'));
} }
var args = CommandLineArgumentParser.Parse(lineText.Split(' ')); var args = CommandLineArgumentParser.Parse(lineText);
await RunInternalAsync(args); await RunInternalAsync(args);
} }
} }
private async Task RunInternalAsync(CommandLineArgs commandLineArgs) private async Task RunInternalAsync(CommandLineArgs commandLineArgs)
{ {
var commandType = CommandSelector.Select(commandLineArgs); var commandType = CommandSelector.Select(commandLineArgs);

21
framework/test/Volo.Abp.Cli.Core.Tests/Volo/Abp/Cli/CommandLineArgumentParser_Tests.cs

@ -64,5 +64,26 @@ namespace Volo.Abp.Cli
commandLineArgs.Options.ShouldContainKey("optionC"); commandLineArgs.Options.ShouldContainKey("optionC");
commandLineArgs.Options["optionC"].ShouldBe("value2"); commandLineArgs.Options["optionC"].ShouldBe("value2");
} }
[Fact]
public void Should_Parse_From_Line_Text()
{
var commandLineArgs = _commandLineArgumentParser.Parse(
"mycommand mytarget -a value1 -b \"value with space\" --optionC"
);
commandLineArgs.Command.ShouldBe("mycommand");
commandLineArgs.Target.ShouldBe("mytarget");
commandLineArgs.Options.Any().ShouldBeTrue();
commandLineArgs.Options.ShouldContainKey("a");
commandLineArgs.Options["a"].ShouldBe("value1");
commandLineArgs.Options.ShouldContainKey("b");
commandLineArgs.Options["b"].ShouldBe("value with space");
commandLineArgs.Options.ShouldContainKey("optionC");
commandLineArgs.Options["optionC"].ShouldBeNull();
}
} }
} }

Loading…
Cancel
Save