我使用csi.exe -- C#交互式编译器--运行.csx脚本。如何访问提供给我的脚本的命令行参数?
csi script.csx 2000如果您不熟悉csi.exe,下面是使用信息:
>csi /?
Microsoft (R) Visual C# Interactive Compiler version 1.3.1.60616
Copyright (C) Microsoft Corporation. All rights reserved.
Usage: csi [option] ... [script-file.csx] [script-argument] ...
Executes script-file.csx if specified, otherwise launches an interactive REPL (Read Eval Print Loop).发布于 2016-07-22 14:30:24
Environment.GetCommandLineArgs()返回该示例的["csi", "script.csx", "2000"]。
发布于 2019-03-20 22:03:24
CSI有一个 global which parses out the arguments for you。在大多数情况下,这将使您获得所需的参数,就像在C/C++程序中访问argv或在C# Main()签名static void Main(string[] args)中访问Main()一样。
Args有一种类型的IList,而不是string[]。因此,您将使用.Count来查找参数的数量,而不是.Length。
下面是一些示例用法:
#!/usr/bin/env csi
Console.WriteLine($"There are {Args.Count} args: {string.Join(", ", Args.Select(arg => $"“{arg}”"))}");以及一些实例调用:
ohnob@DESKTOP-RC0QNSG MSYS ~/AppData/Local/Temp
$ ./blah.csx
There are 0 args:
ohnob@DESKTOP-RC0QNSG MSYS ~/AppData/Local/Temp
$ ./blah.csx hi, these are args.
There are 4 args: “hi,”, “these”, “are”, “args.”
ohnob@DESKTOP-RC0QNSG MSYS ~/AppData/Local/Temp
$ ./blah.csx 'hi, this is one arg.'
There are 1 args: “hi, this is one arg.”发布于 2016-08-10 19:01:45
这是我的剧本:
var t = Environment.GetCommandLineArgs();
foreach (var i in t)
Console.WriteLine(i);将参数传递给csx:
scriptcs hello.csx -- arg1 arg2 argx打印出来:
hello.csx
--
arg1
arg2
argx关键是csx和脚本参数之间的“--”。
https://stackoverflow.com/questions/38529021
复制相似问题