如何从命令行向 REPL 添加导入? [英] How to add imports to REPL from command line?

查看:72
本文介绍了如何从命令行向 REPL 添加导入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让 REPL 导入命令行中给出的包?

How can I make REPL to import packages given in commnad line?

示例:

scala -someMagicHere "import sys.error"
scala> :imports
1) import scala.Predef._          (162 terms, 78 are implicit)
2) import sys.error               (2 terms)

scala> _

PS:它不是重复.我想要自动化的解决方案,而不是每次运行 REPL 时都手动粘贴一些代码.此外,我不想使用 SBT 仅用于在 REPL 启动后在 REPL 中运行一个命令.

PS: It is not a duplicate. I want automated solution, not manually pasting some code every time I run REPL. Also I don't want to use SBT just for running one command in REPL after its start.

推荐答案

我写了一个脚本来自动化命令行参数到文件的事情(基于 A from som-snytt).它在 win 7 上使用 MSYS 进行了测试,但它也可以在 Linux 上运行.

I wrote a script to automatize the command line param to file thing (based on A from som-snytt). It's tested on win 7 with MSYS, but it should work on Linux as well.

bash-3.1$ repl "import sys.error"
Loading C:\Users\xxx\AppData\Local\Temp\tmp.GgEjauEqwz...
import sys.error

Welcome to Scala version 2.10.0 (Java HotSpot(TM) Client VM, Java 1.7.0_07).
Type in expressions to have them evaluated.
Type :help for more information.
scala> :impo
 1) import scala.Predef._ (162 terms, 78 are implicit)
 2) import sys.error (2 terms)

#!/bin/bash

function crash {
    echo "Error: $1"
    exit 10
}

if [ $# -ne 1 ]; then
    echo "Script requires one parameter."
    exit 1
fi

t=$(mktemp) || crash "Unable to create a temp file."
[ ${#t} -lt 1 ] && crash "Suspiciously short file name, aborting."
trap "rm -f -- '$t'" EXIT

echo "$1" > "$t"
scala -i "$t" || crash "Something wrong with scala binary."

rm -f -- "$t"
trap - EXIT
exit

这篇关于如何从命令行向 REPL 添加导入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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