从C#应用程序将命令行参数传递给IronPython?

Dan*_*elb 4 c# ironpython

如何将命令行参数从我的C#应用​​程序传递到IronPython 2.x?Google仅返回有关如何使用Iron Python 1.x执行此操作的结果.

static void Main(string[] args)
{
    ScriptRuntime scriptRuntime = IronPython.Hosting.Python.CreateRuntime();
    // Pass in script file to execute but how to pass in other arguments in args?
    ScriptScope scope = scriptRuntime.ExecuteFile(args[0]);
}
Run Code Online (Sandbox Code Playgroud)

Sim*_*elt 5

您可以通过以下C#代码设置sys.argv:

static void Main(string[] args)
{
    var scriptRuntime = Python.CreateRuntime();
    var argv = new List();
    args.ToList().ForEach(a => argv.Add(a));
    scriptRuntime.GetSysModule().SetVariable("argv", argv);
    scriptRuntime.ExecuteFile(args[0]);
}
Run Code Online (Sandbox Code Playgroud)

有以下python脚本

import sys
for arg in sys.argv:
    print arg
Run Code Online (Sandbox Code Playgroud)

并调用exe之类的

Test.exe SomeScript.py foo bar
Run Code Online (Sandbox Code Playgroud)

给你输出

SomeScript.py
foo
bar
Run Code Online (Sandbox Code Playgroud)

另一个选择是将准备好的选项传递给本答案中的Python.CreateRuntime解释