ClapCommandLineHandler is something like an handler (thing which handles) for the Clap-st in Pharo. Currently Clap-st is still in development. To brief about Clap-st it is an Command Line Argument Parser for Pharo. Read more about it here.
So, what does this ClapCommandLineHandler do
- This ClapCommandLineHandler is like a bridge which connects the commandline and the Clap-st package.
- It takes input from the command line, and uses the clap-st package and processes the input and returns the desired output.
- It is basically a medium to interact with clap-st.
- The input will look like this “<command> <arg1> <arg2> <flag1> <arg3> <flag2>”.
What does the ClapCommandLineHandler package contain
The ClapCommandLineHandler Package has two set of classes namely ClapCommandLineHandler and ClapHandlerCollectorStrategy.
- The first class ClapCommandLineHandler is the core part of the whole handler. It has all the methods required to take arguments, process them, run a match on them, and activate the handler according to the match, etc.
- For every new command, a new handler has to be created which should be a sub class of ClapCommandLineHandler. And the new handler should have methods namely
command(on class side),
activateand any other helper methods if needed.
- The second class ClapHandlerCollectorStrategy is used to implement strategy pattern in the ClapCommandLineHandler Class. This class has two subclasses ClapHandlerCollectionStrategy and ClapSubclassHandlerCollectorStrategy.
- The first subclass ClapHandlerCollectionStrategy takes an input collection and assigns it to its instance variable.
- The second subclass ClapSubclassHandlerCollectorStrategy searches for all subclasses of ClapCommandLineHandler class and returns it as a collection.
The steps of working of ClapCommandLineHandler
- Take the command line arguments as a input collection.
- Match the first argument (which would be the command name) with all the existing handlers. There is no need have a
namemethod in all the handlers to check for a match. The first argument can be matched directly with the clapCommand, which will return a ClapCommandMatch if found a match else a ClapMisMatch. The only problem with this would be incase there are compulsory arguments for a clap command, this fails. In that case it is preferable to have a
- After a finding a matched handler, all the argument set is converted into a ClapContext and a match is ran on the matched handler. This would return a match, which is then used to activate the
activatemethod of the sub class.
- The subclass (which is the matched handler)
activatewill have the implemention of what to return as output using the match.
Why use the strategy pattern?
- The Wikipedia definition of Strategy pattern is ” strategy pattern is a behavioural software design pattern that enables to select an algorithm at runtime” or
Strategy pattern is dynamically deciding what to do during the run time.
- In our case, this is helpful to differentiate collection to be used for testing and collection to be used for production. Suppose I have a large number of ClapCommand handlers and if I would like to test only a few of them rather than all, the ClapHandlerCollectionStrategy enables to make a collection and use it. And for production I can use the ClapSubclassHandlerCollectorStrategy to get all the handlers.
- So basically, my implemention for using this pattern will use the instance of parent class ClapHandlerCollectorStrategy and at run time, I make use of either of its subclass instances and thereby I run the algorithm I want.
Thanks for reading till here. This is what the current arcitecture is and how it works. This is probably gonna be my first version. So there might be changes in this in the coming versions. I wrote a few tests for testing both the working of strategy pattern and handler methods. Get the code for this here. To learn how to load and install refer this.