0

I'd like to use the args4j Java library in my Scala v2.10 application. However, I am running into some trouble. As an example, I have a class to store my arguments in Java as follows.

public class MyArgs {
 @Option(name = "-i", usage = "file input", required = true)
 private String input;

 public String getInput() { return input; }
}

I then have a Scala program like the following.

object TestArgs {
 def main(args: Array[String]): Unit = {
  val myArgs = new MyArgs()
  val cmdLineParser = new CmdLineParser()
  try {
   cmdLineParser.parseArgument(java.util.Arrays.asList(args: _*))
  } catch {
   case e: Exception => {
    System.err.println(e.getMessage)
   }
  }
 }
}

In IntelliJ, I pass in something like -i some/path/to/file.txt, but i keep getting the following message.

"-i" is not a valid option

Any ideas on what's going on?

Jane Wayne
  • 8,205
  • 17
  • 75
  • 120

1 Answers1

1

You can use the args4j Java library directly; however, might I suggest this wrapper library.

Scala helper using args4j

This implementation works for me using Scala 2.10, Spark 1.6

// import the Java args4j
import org.kohsuke.args4j.Option
// import the scala wrapper
import OptionsHelper.optionsOrExit
object SparkScalaTest {
    def main(args: scala.Array[String]) {
        // setupt the options
        class MyOptions extends OptionsWithHelp {
            @Option(name="--foo", aliases=Array("-f"), usage="foo bar", required=false)
            var update: Boolean = false // this is the default value, since it is not required
         }
        //Parse arguments
         val options = optionsOrExit(args, new MyOptions)

         sc.stop
     }
}
ForRealHomie
  • 176
  • 1
  • 3