我认为 scala 存在空格问题?

发布于 2024-09-04 01:55:35 字数 394 浏览 3 评论 0原文

我正在尝试编写一个脚本来更快地生成 Lift 项目,但我相信我遇到了空白问题。

val strLiftGen = "mvn archetype:generate -U\-DarchetypeGroupId=net.liftweb\
 -DarchetypeArtifactId=lift-archetype-blank\
 -DarchetypeVersion=1.0\
 -DremoteRepositories=http://scala-tools.org/repo-releases\-DgroupId=" + args(0)"\-DartifactId=" + args(1)"\-Dversion=1.0-SNAPSHOT */"

有人愿意用智慧的棍子打击新手并告诉我处理这样长绳子的聪明方法吗?

I'm trying to write a script to make generating Lift projects quicker but I believe i'm running into a white space issue.

val strLiftGen = "mvn archetype:generate -U\-DarchetypeGroupId=net.liftweb\
 -DarchetypeArtifactId=lift-archetype-blank\
 -DarchetypeVersion=1.0\
 -DremoteRepositories=http://scala-tools.org/repo-releases\-DgroupId=" + args(0)"\-DartifactId=" + args(1)"\-Dversion=1.0-SNAPSHOT */"

Anyone care to hit the newb with the stick of wisdom and tell me a smart way of handling a long string like this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

み零 2024-09-11 01:55:35

您的示例中存在多个语法错误("\ " 且在 args(0)args(1)< 之后缺少 + /code>.(复制粘贴错误?)。这是您可以执行的操作:

val strLiftGen =
  """mvn
  archetype:generate
  -U
  -DarchetypeGroupId=net.liftweb
  -DarchetypeArtifactId=lift-archetype-blank
  -DarchetypeVersion=1.0
  -DremoteRepositories=http://scala-tools.org/repo-releases
  -DgroupId=%s
  -DartifactId=%s
  -Dversion=1.0-SNAPSHOT"""

val cleanStr = strLiftGen.replace('\n',' ').replaceAll("\\s{2,}"," ").trim
println(cleanStr.format(args(0), args(1)))

然后,如何处理参数之间的空格取决于您将如何执行命令。

There are multiple syntax errors in your example ("\ " and missing + after args(0) and args(1). (copy paste error?). Here is what you can do:

val strLiftGen =
  """mvn
  archetype:generate
  -U
  -DarchetypeGroupId=net.liftweb
  -DarchetypeArtifactId=lift-archetype-blank
  -DarchetypeVersion=1.0
  -DremoteRepositories=http://scala-tools.org/repo-releases
  -DgroupId=%s
  -DartifactId=%s
  -Dversion=1.0-SNAPSHOT"""

val cleanStr = strLiftGen.replace('\n',' ').replaceAll("\\s{2,}"," ").trim
println(cleanStr.format(args(0), args(1)))

Then how you handle whitespace between arguments depends a bit on how you'll execute the command.

零度℉ 2024-09-11 01:55:35

如果您尝试获取出现某些反斜杠转义空格的字符串,则需要将反斜杠加倍。就目前情况而言,您所显示的内容实际上不会编译,因为单个反斜杠可能不会紧接在空格之前。

您可能需要使用三引号字符串,它会暂停所有反斜杠处理并允许嵌入换行符。在三引号字符串中,反斜杠永远不需要双倍。

If you're trying to get a string with some occurrences of a backslash-escaped space, then you need to double up the backslash. As it stands, what you've shown will not actually compile because a single backslash may not immediately precede a space.

You may want to use a triple-quoted string, which suspends all backslash processing and allows embedded newlines. Backslashes never need to be doubled in triple-quoted strings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文