List 从Scala中的命令行参数创建列表
在scala main方法中,我使用List 从Scala中的命令行参数创建列表,list,scala,command-line-arguments,List,Scala,Command Line Arguments,在scala main方法中,我使用import org.apache.commons.cli.{BasicParser,Options}来解析命令行参数。其中一个参数是逗号分隔的字符串,它列出了我要运行的方法 我有一个映射,它将字符串映射到方法,例如 val methods = Map("first" -> first, "second" -> second, "third" -> third) 现在,我想从命令行arg创建一个要运行的方法列表。i、 e.如果我将arg作为
import org.apache.commons.cli.{BasicParser,Options}
来解析命令行参数。其中一个参数是逗号分隔的字符串,它列出了我要运行的方法
我有一个映射,它将字符串映射到方法,例如
val methods = Map("first" -> first, "second" -> second, "third" -> third)
现在,我想从命令行arg创建一个要运行的方法列表。i、 e.如果我将arg作为“first,second”
,列表应包含列表(first,second)
,但如果我将arg作为“first,third”
,列表应包含列表(first,third)
我知道列表是不可变的,所以我不能迭代和添加
< P> > Scala的方法是什么?< P>假设CLI用逗号分隔的单词传递字符串,考虑将这样的兴趣子串分隔开来,
val methodNames = "first,second".split(",").toList
val methodNames = "first,second".split("\\W+").toList
或者像这样把单词分开
val methodNames = "first,second".split(",").toList
val methodNames = "first,second".split("\\W+").toList
因此,要从列表中获取方法,请考虑
methodsNames.map(methods)
但这会创建一个字符串列表(拆分本身就是这样做的)。我想要一个方法列表,其中的方法是从映射中获取的。@navinpai note更新到获取方法的问题和添加到答案。第一、第二、…,的类型是什么?