如何使用 CSI.exe 脚本参数 [英] How to use CSI.exe script argument
问题描述
当你运行 csi.exe/?(安装了 Visual Studio 2015 更新 2),您将获得以下语法
When you run csi.exe /? (with Visual Studio 2015 update 2 installed), you will get the following syntax
Microsoft (R) Visual C# Interactive Compiler version 1.2.0.51106
Copyright (C) Microsoft Corporation. All rights reserved.
Usage: csi [option] ... [script-file.csx] [script-argument] ...
我只是想知道如何将这个 [script-argument] 传递到我的 csx 脚本文件中.假设,我的 csx 脚本 (c:\temp\a.csx) 只有 2 行,如下所示
I am just wondering how I can pass this [script-argument] into my csx script file. Let's say, my csx script (c:\temp\a.csx) has only 2 line as follows
using System;
Console.WriteLine("Hello {0} !", <argument_from_commandLine>);
我期望的是在我运行以下命令行后
What I expect is after I run the following command line
csi.exe c:\temp\a.csx David
我会得到
Hello David !
但我只是不知道我应该在我的脚本文件中做什么,以便我可以将 csi.exe [script_argument] 传递给我的脚本文件(以替换 ).
But I just do not know what I should do in my script file so I can pass the csi.exe [script_argument] to my script file (to replace ).
提前感谢您的时间和帮助.
Thanks in advance for your time and help.
推荐答案
脚本中有一个名为 Args
的全局变量,它具有这些脚本参数"值.我能找到的最接近文档的是在 对 roslyn repo 的拉取请求 中提到它.在 csx 文件 (test.csx) 中:
There is a global variable in scripts called Args
which has these "script argument" values. The closest thing I can find to documentation is mention of it in pull requests for the roslyn repo. In a csx file (test.csx):
using System;
Console.WriteLine("Hello {0}!", Args[0]);
使用命令行:
csi.exe test.csx arg1
将给出输出:
你好 arg1!
使用 Environment.GetCommandLineArgs()
的替代方法可以工作,但问题是这会获取传递给 csi 进程的所有参数.然后,您必须将脚本参数"与 csi 本身的选项分开.使用内置的 Args 变量可以避免这项工作,该变量也将更易于维护.
An alternative approach using Environment.GetCommandLineArgs()
could be made to work, but the problem is that this picks up all the arguments passed to csi process. Then you have to separate the "script arguments" from the options for csi itself. This work can be avoided by using the builtin Args variable which is going to be more maintainable as well.
这篇关于如何使用 CSI.exe 脚本参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!