脚本参数和嵌入式IronPython的 [英] Script arguments and Embedded IronPython

查看:383
本文介绍了脚本参数和嵌入式IronPython的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的C#应用​​程序使用IronPython的2.嵌入式脚本引擎创建的Python运行,并添加一些类全局命名空间,使得脚本可以导入它们的模块。

I have an embedded scripting engine in my C# application that uses IronPython 2. I create the Python runtime and add a few classes to the global namespace so that the script can import them as modules.

然而,一个(pretty的基础)的事情我无法弄清楚是如何发送脚本参数。我意识到,我可以只创建一个变量与参数列表中,但必须有一个适当的方式做到这一点。此外,这样做的正确的Python的方式让我可以编译脚本,并使用自动文档构建所谓的狮身人面像。所以,最终目标是能够使用:

However, one (pretty basic) thing I can't figure out is how to send script arguments. I realize that I could just create a variable with a list of arguments, but there must be a proper way to do it. Also, doing it in the proper 'Python' way allows me to compile the scripts and use an automated document builder called Sphinx. So the ultimate goal is to be able to use:

import sys
sys.argv

在我的剧本之一,有它获得参数中指定的用户(通过C#应用程序)。

In one of my scripts and have it get the arguments the user specified (through the C# app).

现在,我调用脚本使用:

Right now, I call the script by using:

// set up iron python runtime engine
_engine = Python.CreateEngine();
_runtime = _engine.Runtime;
_scope = _engine.CreateScope();

// run script
_script = _engine.CreateScriptSourceFromFile(_path);
_script.Execute(_scope);

和我试着寻找一个API添加脚本参数,没有运气。我也尝试过附加到脚本路径(_path的例子),没有运气。我试着用CreateScriptSourceFrom文件和CreateScriptSourceFromSting(这是一个很长反正拍...)。

And I've tried searching for an API to add script arguments with no luck. I've also tried appending them to the script path (_path in example) with no luck. I tried with CreateScriptSourceFrom File and CreateScriptSourceFromSting (which was a long shot anyway...).

就是我试图做甚至可能吗?

Is what I'm trying to do even possible?

推荐答案

在创建引擎,你可以添加一个参数选项中包含您的观点:

When you create the engine, you can add an "Arguments" option the contains your arguments:

IDictionary<string, object> options = new Dictionary<string, object>();
options["Arguments"] = new [] { "foo", "bar" };
_engine = Python.CreateEngine(options);

现在 sys.argv中将包含 [富,酒吧]

您可以设置选项[参数] 来任何实现的ICollection&LT;字符串&GT;

You can set options["Arguments"] to anything that implements ICollection<string>.

这篇关于脚本参数和嵌入式IronPython的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆