开发者

Having a white space issue with scala, I think?

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\-D开发者_开发知识库archetypeGroupId=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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜