将命令行参数从 C# 应用程序传递给 IronPython
本文关键字:IronPython 应用程序 命令行 参数 | 更新日期: 2023-09-27 18:35:57
如何将命令行参数从我的 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]);
}
可以通过以下 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]);
}
具有以下 Python 脚本
import sys
for arg in sys.argv:
print arg
并像 exe 一样称呼 exe
Test.exe SomeScript.py foo bar
给你输出
SomeScript.py
foo
bar
另一种选择是将准备好的选项传递给Python.CreateRuntime
,如本答案中所述