How to use CSI.exe script argument

北战南征 提交于 2019-12-04 00:24:45

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

using the command line:

csi.exe test.csx arg1

will give the output:

Hello arg1!

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.

You can use Environment.GetCommandLineArgs() for that.

Example for your example:

using System;
Console.WriteLine("Hello {0}!", Environment.GetCommandLineArgs()[2]);

Notice that I'm reading the third item, because Environment.GetCommandLineArgs() gives the entire command line (if you run the script using csi test.csx David, the first one will be csi and the second one test.csx)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!