简体   繁体   English

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

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

How do I pass command line parameters from my C# application to IronPython 2.x? 如何将命令行参数从我的C#应用​​程序传递到IronPython 2.x? Google is only returning results about how to do it with Iron Python 1.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]);
}

You can either set the sys.argv via the following C# code: 您可以通过以下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]);
}

having the following python script 有以下python脚本

import sys
for arg in sys.argv:
    print arg

and calling the exe like 并调用exe之类的

Test.exe SomeScript.py foo bar

gives you the output 给你输出

SomeScript.py
foo
bar

Another option would be passing the prepared options to Python.CreateRuntime as explained in this answer 另一个选择是将准备好的选项传递给Python.CreateRuntime本答案中所述

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

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