如何使F#脚本文件和其他脚本语言在Windows中像.exe,.cmd和.bat文件一样工作 [英] How to get F# scripts files and other script languages to work like .exe, .cmd, and .bat files in Windows

查看:92
本文介绍了如何使F#脚本文件和其他脚本语言在Windows中像.exe,.cmd和.bat文件一样工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以配置F#脚本文件,以便可以直接调用它们而无需直接指定脚本运行器应用程序及其文件扩展名,并且可以通过命令PATH环境变量进行访问.

It is possible to configure F# script files so that they may be invoked directly without directly specifying the script runner application and their file extension, and made accessible through the command PATH environment variable.

执行此操作的步骤如下:

The steps to do so are as follows:

  1. 使用Windows资源管理器将特定的脚本引擎设置为脚本文件类型扩展名的默认打开方式"程序
  2. 将脚本扩展名附加到PathExt环境变量中,该变量将归类为可执行文件
  3. (可选)包括包含脚本的目录路径到Windows Path环境变量

我的问题:当您不直接使用脚本的运行应用程序时,如何通过脚本获取参数.

My question: how to get arguments through to the script when you are not directly invoking it's runner application with it.

推荐答案

以下内容提供了一种简单的技术,可用于配置Windows系统以将F#作为脚本语言与cmd和批处理文件相提并论.因此,这些脚本可以从环境路径访问,无需文件类型扩展名即可调用,并且可以传递任意数量的参数.此技术应适用于其他脚本语言,例如powershell,python等.

The following provides a simple technique for configuring your windows system to include F# as a scripting language on par with cmd and batch files. Consequently, the scripts are accessible from the environment path, invocable without a file type extension, and may be passed any number of arguments. This technique should be applicable to other scripting languages such as powershell, python, etc.

以下是用于在.Net 4.0中为F#3.1建立此配置的批处理文件:

The following is a batch file for establishing this configuration for F# 3.1 in .Net 4.0:

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#版本如下:

Note, the presence of "--" in the "ftype" command line serves as a delimiter to assist the script in distinguish it's command line arguments from those of the script runner; an F# script receives all the arguments and must parse out it's arguments. My clumsy F# version of this parsing using the provided delimiter is as follows:

open System

let args = 
   Environment.GetCommandLineArgs()
   |> Seq.skipWhile (fun p -> p <> "--") 
   |> Seq.skip 1
   |> List.ofSeq

享受.

这篇关于如何使F#脚本文件和其他脚本语言在Windows中像.exe,.cmd和.bat文件一样工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆