scala 带有来自 shell 的 CLI 参数的“sbt run”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13309738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 04:42:08  来源:igfitidea点击:

'sbt run' with CLI arguments from shell

scalasbt

提问by nietaki

I tried running my scala project with CLI arguments using sbt launcher version 0.12.1. It works OK when run from the scala prompt:

我尝试使用 sbt 启动器版本 0.12.1 使用 CLI 参数运行我的 scala 项目。从 scala 提示符运行时它工作正常:

$ sbt
[info] (...)
> run sth
(...)
[success] Total time: 0 s, completed Nov 9, 2012 3:04:47 PM

But when I want to run it whole from shell, as per this answerfor example, I'm getting an error:

但是当我想从 shell 运行它时,例如根据这个答案,我收到一个错误:

$ sbt "run sth"
[info] (...)
you need to provide source file name
[success] Total time: 0 s, completed Nov 9, 2012 3:07:07 PM
[error] Not a valid command: sth (similar: set, last, shell)
[error] Expected '/'
[error] Expected ':'
[error] Not a valid key: sth (similar: test, state, watch)
[error] sth
[error]    ^

And the "you need to provide source file name" info is given by my scala project to indicate that main didn't get any CLI arguments.

并且“您需要提供源文件名”信息由我的 scala 项目提供,以表明 main 没有获得任何 CLI 参数。

Is it something that worked in the previous version of sbt (in the referenced question), or am I doing something wrong?

它是否适用于先前版本的 sbt(在引用的问题中),还是我做错了什么?

采纳答案by Lukas Rytz

That's a bug in the 0.12.1 launcher: https://github.com/sbt/sbt-launcher-package/issues/34

这是 0.12.1 启动器中的一个错误:https: //github.com/sbt/sbt-launcher-package/issues/34

回答by bluenote10

The problem could be the way you pass arguments in your sbtlaunch file to Java. In my case it is:

问题可能是您将sbt启动文件中的参数传递给 Java 的方式。就我而言,它是:

java -Xmx4096M -jar `dirname 
$ sbt "run-main SomeMain blah blah"
Outut:
[blah, blah]
`/sbt-launch_0.12.1.jar "$@"

And I just confirmed: It works with both 0.12 and 0.12.1. For instance, for a program that just prints its arguments I get:

我刚刚确认:它适用于 0.12 和 0.12.1。例如,对于一个只打印其参数的程序,我得到:

##代码##

By using run-mainyou can also avoid the possibility that sbt somehow does not see your main function.

通过使用,run-main您还可以避免 sbt 以某种方式看不到您的主要功能的可能性。