如何将命令行参数从我的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)
您可以通过以下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解释
| 归档时间: |
|
| 查看次数: |
3108 次 |
| 最近记录: |