Book Image

Groovy for Domain-Specific Languages, Second Edition

By : Fergal Dearle
Book Image

Groovy for Domain-Specific Languages, Second Edition

By: Fergal Dearle

Overview of this book

Table of Contents (20 chapters)
Groovy for Domain-specific Languages Second Edition
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Free Chapter
1
Introduction to DSLs and Groovy
Index

Adding a command-line interface


One more step in making our DSL roadworthy is to add a command-line interface to it. In doing so, we move from invoking the DSL directly to allowing it to be loaded by a DSL command. This gives us more control over the environment in which the DSL will run, and allows us to take care of the housekeeping, such as adding the search method to the String class.

Groovy being Groovy, adding a command line is surprisingly easy:

#!/usr/bin/env groovy
@Grab(group='org.twitter4j', module='twitter4j-core', version='[4.0,)')

if (args) 
   evaluate(new File(args[0]))
else
   println "Usage: GeeTwitter <script>"

The preceding shell script is all that we need in order to launch and run our GeeTwitter DSL. Being a shell script, we can run this directly on most Linux environments and Mac OS X, and on Windows if you have the Cygwin shell installed. In the script, we will test to see if we have any arguments passed, and evaluate the first argument as the name of a file containing...