CommandLineParser use case

View: New views
1 Messages — Rating Filter:   Alert me  

CommandLineParser use case

by Dawid Marcin Grzesiak :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi guys,

I just want to show some code that will hopefully help to improve CommandLineParser usability. For the moment I don't know how to integrate such behaviour directly to the CommandLineParser class.

Generally you can define the command line option (-x, --exclude) by this:

  val exclude = new StringOption('x', "exclude", "Exclude the given file") with AllowAll

and then evaluate it in this way:

  exclude { f =>
    println("I'll exclude the " + f + " file")
  }

Invocation and output log:

$ scala -cp scalax-0.1.jar optparser.scala --exclude somefile.txt
I'll exclude the somefile.txt file

And here is the whole code:

import scalax.io._

object Options extends CommandLineParser {
  val version = new Flag("version", "Show version info") with AllowNone
  val exclude = new StringOption('x', "exclude", "Exclude the given file") with AllowAll
}

Options.parseOrHelp(argv) { cmd =>
  object dummy {
    def extrude(o: Options.Flag)(block: => Unit) {
      if (cmd(o)) block
    }

    def extrude[T](o: Options.OptionType[T])(block: T => Unit) {
      val f = cmd(o)
      if (!f.isEmpty) block(f.get)
    }
    
    implicit def o2b(o: Options.Flag) = extrude(o)_
    implicit def o2b[T](o: Options.OptionType[T]) = extrude(o)_
  }
  
  import dummy._, Options._  
  
  version {
    println("0.1")
  }

  exclude { f =>
    println("I'll exclude the " + f + " file")
  }
}
Thanks,
Dawid Marcin Grzesiak
http://www.dmg.pl/