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
This commit is contained in:
Родитель
5604fc6bdf
Коммит
cca71fefad
|
@ -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<object[]> CommandLineArgumentsData
|
||||
{
|
||||
get
|
||||
{
|
||||
static object[] TestCase(
|
||||
string expected, List<Parameter> parameters, Dictionary<string, ArgumentValue> arguments) =>
|
||||
new object[]
|
||||
{
|
||||
expected,
|
||||
new ExecutionInformation
|
||||
{
|
||||
EntryPoint = new EntryPointOperation { Parameters = parameters },
|
||||
ArgumentValues = arguments
|
||||
}
|
||||
};
|
||||
|
||||
yield return TestCase(
|
||||
"--foo 5",
|
||||
new List<Parameter> { new Parameter { Name = "foo", Position = 0, Type = DataType.IntegerType } },
|
||||
new Dictionary<string, ArgumentValue>
|
||||
{ ["foo"] = new ArgumentValue { Type = DataType.IntegerType, Integer = 5 } });
|
||||
|
||||
yield return TestCase(
|
||||
"-n 5",
|
||||
new List<Parameter> { new Parameter { Name = "n", Position = 0, Type = DataType.IntegerType } },
|
||||
new Dictionary<string, ArgumentValue>
|
||||
{ ["n"] = new ArgumentValue { Type = DataType.IntegerType, Integer = 5 } });
|
||||
|
||||
yield return TestCase(
|
||||
"--foo 5 --bar \"abc\"",
|
||||
new List<Parameter>
|
||||
{
|
||||
new Parameter { Name = "bar", Position = 1, Type = DataType.StringType },
|
||||
new Parameter { Name = "foo", Position = 0, Type = DataType.IntegerType }
|
||||
},
|
||||
new Dictionary<string, ArgumentValue>
|
||||
{
|
||||
["bar"] = new ArgumentValue { Type = DataType.StringType, String = "abc" },
|
||||
["foo"] = new ArgumentValue { Type = DataType.IntegerType, Integer = 5 }
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Загрузка…
Ссылка в новой задаче