可以配置 F# 脚本文件,以便可以直接调用它们,而无需直接指定脚本运行程序应用程序及其文件扩展名,并且可以通过命令 PATH 环境变量进行访问。
操作步骤如下:
- 使用 Windows 资源管理器将特定脚本引擎设置为脚本文件类型扩展名的默认“打开方式”程序
- 将脚本扩展附加到 PathExt 环境变量,这会将其分类为可执行文件
- (可选)将包含脚本的目录路径包含到 Windows Path 环境变量中
我的问题:当您不直接调用脚本的运行程序应用程序时,如何将参数传递给脚本。
下面提供了一种简单的技术,用于将 Windows 系统配置为包含 F# 作为与 cmd 和批处理文件同等的脚本语言。因此,脚本可以从环境路径访问,无需文件类型扩展名即可调用,并且可以传递任意数量的参数。该技术应该适用于其他脚本语言,例如 powershell、python 等。
以下是用于在 .Net 4.0 中为 F# 3.1 建立此配置的批处理文件:
configure.bat
rem absolute filename of the F# script runner, Fsi.exe
set fsharpScriptRunner="C:\Program Files (x86)\Microsoft SDKs\F#\3.1\Framework\v4.0\Fsi.exe"
rem script runner command for fsi.exe to execute script and exit without advertising itself
set fsharpScriptRunnerCmd=%fsharpScriptRunner% --nologo --exec
rem associate "FSharpScript" files with fsharpScriptRunnerCmd, appending
rem the file name and remaining line arguments into the commandline
rem after the delimiter "--"
ftype FSharpScript=%fsharpScriptRunnerCmd% %%1 "--" %%*
rem associate file extension ".fsx" with the file type "FSharpScript"
assoc .fsx=FSharpScript
rem add ".fsx" to the list of file types treated as executable
set pathext=%pathext%;.fsx
请注意,“ftype”命令行中存在的“--”充当分隔符,以帮助脚本将其命令行参数与脚本运行程序的命令行参数区分开来; F# 脚本接收所有参数并且必须解析出它的参数。我使用提供的分隔符进行的解析的笨拙 F# 版本如下:
open System
let args =
Environment.GetCommandLineArgs()
|> Seq.skipWhile (fun p -> p <> "--")
|> Seq.skip 1
|> List.ofSeq
Enjoy.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)