I should update this blog more. Maybe just a little thing right now: I really like the option parsing library Trollop for Ruby. It's a great little system, and its values (or the ones important to me, at least) are that it is as easy as possible for the library consumer to use. I've been experimenting with making one with similar values for Scala, adding in the type safety and the like that you get with that language, while aiming to still keep a fluent interface.
Using it looks like this:
object Main extends OptionApplication {
val options = new Options {
banner = """
This is my test application.
There are many like it, but this one is mine.
Usage: Main [options]
"""
val name = string( "Name to greet", default( "Nobody" ) )
val alternativeGreeting = flag( "Use an alternative greeting" )
}
import options._
def main() {
val greeting = if( alternativeGreeting.value ) {
"Bonjour, %s!"
} else {
"Hello, %s!"
}
println( greeting format name.value )
}
}
Calling it from the "command line" (okay, I did it through Eclipse):
> Main --help
This is my test application.
There are many like it, but this one is mine.
Usage: Main [options]
Options:
-a, --alternative-greeting - Use an alternative greeting
-h, --help - This help page
-n, --name - Name to greet (default: Nobody)
> Main -n Calum --alternative-greeting Bonjour, Calum!
> Main --pleasehelpme Problem with options: unrecognised argument 'pleasehelpme' Try --help for help.
Anyway, beyond just a random tour of that, why do I think this sort of thing is interesting? I like the idea that Scala works as a statically-type "scripting" language, as well as in the domains where Java works. I like the idea that command line tools and so on could be built with this easily. Java tends to only be used for huge big systems run inside containers and the like, because it's geared very much towards building the sort of huge system where this makes sense. Scala is fluid enough to give a lot of the advantages in fluidity you see from languages like Python and Ruby, and while it's a little more verbose, I think it's a useful alternative depending on your values.
What Scala is really missing, to me, is little "fun" libraries like you find in the form of Python Eggs and Ruby Gems. There's a lot of nice, impressive, projects but I'd like to see more in the realm of small projects that aren't necessarily a work of architectural genius, but are useful (or quirky) and encourage people to dabble and play. Java lacks a sense of fun and there's no reason that Scala should end up down that route. I might go into more detail with that feeling later; this is pretty poorly-drafted. Oh well!


0 comments:
Post a Comment