繁体   English   中英

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

[英]Passing command line parameters to IronPython from C# application?

如何将命令行参数从我的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之类的

Test.exe SomeScript.py foo bar

给你输出

SomeScript.py
foo
bar

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM