Tuesday, April 02, 2013

 

Back to the command line with Pharo Smalltalk

Pharo 2.0 allows you to easily deal with the command line. This is very useful if you want to script some daily tasks or your CI builds.

You can pass arguments to the image and also register own command line commands. Just check out the class comment in class "CommandLineHandler" and check its subclasses.

Creating an own handler is easy: just subclass CommandLineHandler with a custom class:


CommandLineHandler subclass: #TimeCommandLineHandler
 instanceVariableNames: ''
 classVariableNames: ''
 poolDictionaries: ''
 category: 'Custom-CommandLine'

Implement a class side method to return the command name:

commandName
 ^ 'time'

and implement an instance side #activate method:

activate
 self activateHelp.
 
 FileStream stdout 
  nextPutAll: '[time] ';
  nextPutAll: Time now asString

Now call it from the command line

    $PATH_TO_VM myImage.image time

and it will print the current time on stdout. Note that the #activateHelp will activate the --help option which displays the class comment of your custom handler class.

Also note that it is now possible to write in colors in a terminal from Pharo as this photo shows (click to enlarge):

 

Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?