4

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]);
}
4

1 に答える 1

5

次の 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で説明されているように、準備されたオプションをに渡すことです

于 2012-06-01T20:21:14.813 に答える