This is a second part of my blog post on Clap-st. Find the 1st part of my blog post here.
Currently, I am mostly done with the implementation of the ClapCommandLineHandler. and in the final stage of my submission. In my 1st post I have gone through the basics of the clap-st and how to use it. In this blog post I would like to give a tutorial sort of thing on using the ClapCommandLineHandler.
What is ClapCommandLineHandler and why is it needed? ClapCommandLineHandler is a basic command line support for the clap-st package in Pharo. Read more about it on my last post here. With the ClapCommandLineHandler clap-st can be integrated with the command line and can be used as an ‘actual’ command line argument parser for Pharo.
The beauty of clap-st will be clearly depicted when it is integrated with the ClapCommandLineHandler and used from command line. Let me go step by step on how to use this. ( I am assuming you have loaded the latest ClapCommandLineHandler package and clap-st package ). In this example I will create a simple ‘Eval’ command handler.
- Created a sub class to the class
- Name the handler with whatever command you want to create. Ex: ‘EvalHandler’
- On the class side of the command, create a method named
commandand in this method write down the clap command you want to create as I explained in this blog post. And return the command at the end of the implementation.Put this code in the command method
"This method defines a clapCommand for the handler."
| positional command |
positional := ClapPositional withName: 'smalltalkExpression'. "Create a smalltalk expression positional"
command := (ClapCommand withName: 'Eval') addPositional: positional. "Create a clap command with the positional and name 'Eval' "
^ command "return the command"
- Now, on the instance side, create a method named
handleActivate:, which will handle how the output should be managed.Put this code in the
| output |
output := OpalCompiler evaluate: (aMatch atName: 'smalltalkExpression') value. " Evaluate the expression at the positional"
self print: output asString. "Print it to the command line"
^ self exitSuccess "Exits the image after evaluating"
- Now, let me check if this works from the command line. From the command
Eureka! You have succesfully written a simple clap command and you are able to run it from the command line.
To improve the working of the handler, it is suggested to write a few tests to the new handler we have created. Tests can be added to the ClapCommandLineHandler tests package SubCommandHandlersTest Class. Clap-st makes it easier to write clap command with flags and positionals which in turn will make it easier to use various types of commands from command line. This is one of the good thing of having a great command line parser. Clap-st is still in developing phase and version of it is yet to be released as of now.