gpt4 book ai didi

c# - 如何访问 .csx 脚本中的命令行参数?

转载 作者:太空宇宙 更新时间:2023-11-03 17:00:15 25 4
gpt4 key购买 nike

我正在使用 C# 交互式编译器 csi.exe 来运行 .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).

最佳答案

CSI 有一个 Args global which parses out the arguments for you .在大多数情况下,这将为您提供所需的参数,就像您正在访问 argv 一样。在 C/C++ 程序中或 args在 C# Main()签名static void Main(string[] args) .

Args有一个类型 IList<string> 而不是 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.”

关于c# - 如何访问 .csx 脚本中的命令行参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38529021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com