From cca71fefad8aa36d579fa965f7a535ea5062b8c2 Mon Sep 17 00:00:00 2001 From: Sarah Marshall <33814365+samarsha@users.noreply.github.com> Date: Fri, 17 Sep 2021 10:55:04 -0700 Subject: [PATCH] Use single dash for single letter command line options in QIR driver generator (#820) * Use single dash for single letter command line options * Use property * Rename Argument to ToArgument --- .../Tests/Tools/QirDriverGeneratorTests.cs | 51 +++++++++++++++++++ src/Qir/Tools/Driver/QirCppDriverGenerator.cs | 17 +++---- 2 files changed, 57 insertions(+), 11 deletions(-) diff --git a/src/Qir/Tests/Tools/QirDriverGeneratorTests.cs b/src/Qir/Tests/Tools/QirDriverGeneratorTests.cs index e3e1cd3e..395877bc 100644 --- a/src/Qir/Tests/Tools/QirDriverGeneratorTests.cs +++ b/src/Qir/Tests/Tools/QirDriverGeneratorTests.cs @@ -180,5 +180,56 @@ namespace Tests.Microsoft.Quantum.Qir.Runtime.Tools Assert.Equal(verificationCppSourceCode, generatedCppSourceCode); generatedStream.Close(); } + + [Theory] + [MemberData(nameof(CommandLineArgumentsData))] + public void GenerateCommandLineArguments(string expected, ExecutionInformation info) + { + var generator = new QirFullStateDriverGenerator(false); + Assert.Equal(expected, generator.GetCommandLineArguments(info)); + } + + public static IEnumerable CommandLineArgumentsData + { + get + { + static object[] TestCase( + string expected, List parameters, Dictionary arguments) => + new object[] + { + expected, + new ExecutionInformation + { + EntryPoint = new EntryPointOperation { Parameters = parameters }, + ArgumentValues = arguments + } + }; + + yield return TestCase( + "--foo 5", + new List { new Parameter { Name = "foo", Position = 0, Type = DataType.IntegerType } }, + new Dictionary + { ["foo"] = new ArgumentValue { Type = DataType.IntegerType, Integer = 5 } }); + + yield return TestCase( + "-n 5", + new List { new Parameter { Name = "n", Position = 0, Type = DataType.IntegerType } }, + new Dictionary + { ["n"] = new ArgumentValue { Type = DataType.IntegerType, Integer = 5 } }); + + yield return TestCase( + "--foo 5 --bar \"abc\"", + new List + { + new Parameter { Name = "bar", Position = 1, Type = DataType.StringType }, + new Parameter { Name = "foo", Position = 0, Type = DataType.IntegerType } + }, + new Dictionary + { + ["bar"] = new ArgumentValue { Type = DataType.StringType, String = "abc" }, + ["foo"] = new ArgumentValue { Type = DataType.IntegerType, Integer = 5 } + }); + } + } } } diff --git a/src/Qir/Tools/Driver/QirCppDriverGenerator.cs b/src/Qir/Tools/Driver/QirCppDriverGenerator.cs index 57b7b4ad..36f9349b 100644 --- a/src/Qir/Tools/Driver/QirCppDriverGenerator.cs +++ b/src/Qir/Tools/Driver/QirCppDriverGenerator.cs @@ -33,20 +33,15 @@ namespace Microsoft.Quantum.Qir.Runtime.Tools.Driver public string GetCommandLineArguments(ExecutionInformation executionInformation) { - // Sort arguments by position. - var sortedArguments = executionInformation.EntryPoint.Parameters.OrderBy(arg => arg.Position); - var argumentBuilder = new StringBuilder(); - foreach (var arg in sortedArguments) + string ToArgument(Parameter param) { - if (argumentBuilder.Length != 0) - { - argumentBuilder.Append(' '); - } - - argumentBuilder.Append($"--{arg.Name}").Append(' ').Append(GetArgumentValueString(arg, executionInformation.ArgumentValues[arg.Name])); + var prefix = param.Name.Length > 1 ? "--" : "-"; + var value = GetArgumentValueString(param, executionInformation.ArgumentValues[param.Name]); + return $"{prefix}{param.Name} {value}"; } - return argumentBuilder.ToString(); + var parameters = executionInformation.EntryPoint.Parameters.OrderBy(param => param.Position); + return string.Join(" ", parameters.Select(ToArgument)); } private static string GetArgumentValueString(Parameter argument, ArgumentValue argumentValue)