简体   繁体   English

PureScript - 使用命令行将 arguments 传递给 main

[英]PureScript - Pass arguments to main using the command line

PureScript programs export a main function which is invoked when you run the program. PureScript 程序导出一个main function,它在您运行程序时被调用。 But it's unclear how to pass arguments to it, or if that is possible.但不清楚如何将 arguments 传递给它,或者是否可能。

Consider the following simple program, which capitalizes a string.考虑以下将字符串大写的简单程序。

module Main where

import Effect (Effect)
import Effect.Console (log)
import Prelude (Unit, discard)

import Data.String (toUpper)

message :: String 
message = "Satisfaction Rating: 8.23"

main ∷ Effect Unit
main = do
  log ( toUpper ( message ) )

Which outputs哪些输出

SATISFACTION RATING: 8.23

This program is executed when spago run is invoked.该程序在调用spago run时执行。

My question is as follows: How can the argument message in the code example, be passed in via the command line, creating a custom message capitalizer?我的问题如下:代码示例中的参数message如何通过命令行传入,创建自定义消息大写字母?

The updated run command would look something like:更新后的运行命令类似于:

spago run --message="new_message"

And the updated output would look like更新后的 output 看起来像

NEW_MESSAGE

In short, how do you pass values to a PureScript program from outside?简而言之,如何从外部将值传递给 PureScript 程序?

When you execute a PureScript program with spago run , it runs under Node.当您使用spago run执行 PureScript 程序时,它会在 Node 下运行。 Therefore, to get command-line arguments, you need to as Node.因此,要获取命令行 arguments,您需要作为 Node.

And look: there is a PureScript binding for the relevant Node facilities - the argv function from purescript-node-process .看看:有一个用于相关 Node 设施的 PureScript 绑定 - 来自purescript-node-process argv function It's an effect that returns an array of arguments:这是一个返回 arguments 数组的效果:

import Node.Process (argv)

main = do
  args <- argv
  log $ show args

A quick examination of spago run --help reveals that arguments can be passed to the program via the -b parameter:快速检查spago run --help表明可以通过-b参数将 arguments 传递给程序:

spago run -b somearg

For multiple arguments, quote them:对于多个 arguments,引用它们:

spago run -b "somearg anotherarg"

Note that this works on Linux (and presumably macOS), but fails on Windows, where Spago for some reason ignores the quotes and tries to interpret anotherarg as its own argument.请注意,这适用于 Linux(并且可能是 macOS),但在 Windows 上失败,其中 Spago 出于某种原因忽略引号并尝试将anotherarg解释为它自己的参数。 This is clearly a bug.这显然是一个错误。 This issue seems relevant.这个问题似乎相关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM