3

In my scala main method, I use import org.apache.commons.cli.{BasicParser, Options} to parse command line arguments. One of the arguments is a Comma seperated string which lists which methods I want to run.

I have a map which maps the string to methods eg.

val methods = Map("first" -> first, "second" -> second, "third" -> third)

Now from my command line arg, I want to create a list of methods which I want to run. i.e. if I give arg as "first,second", the list should contain List(first,second) but if I give arg as "first,third", the list should contain List(first,third)

I know a list is immutable, so I can't iterate and add.

What is the scala way of doing this?

1
  • What is the type of first, second, ... ? Sep 3, 2015 at 13:36

1 Answer 1

0

Assuming that CLI delivers a string with the comma separated words, consider separating the substrings of interest like this,

val methodNames = "first,second".split(",").toList

or separating the words like this,

val methodNames = "first,second".split("\\W+").toList

Hence to fetch the methods from the list consider

methodsNames.map(methods)
2
  • But this creates a List of strings (which split itself does). I want a list of methods, where the methods are gotten from the <String, method> map.
    – navinpai
    Sep 3, 2015 at 11:11
  • @navinpai note update to question and addition to answer on fetching methods.
    – elm
    Sep 3, 2015 at 11:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.