I have:
The goal: To create an additional console application that lets user do some set of workflows (similar to ones of the above GUI app) by typing commands. This app should be "stateful" - available commands and their results would depend on the previously issued commands.
The problem: I do not want to "reinvent the wheel". Are there existing patterns for both building the app and defining the "vocabulary"? Currently, it seems to me the best option would be to write a set of helpers and command parser "from scratch".
P.S. If my API would be in .Net, I would look into PowerShell direction, but the API is large and wrapping it into .Net is very time consuming.
The pattern of use you are describing sounds like a Read-Eval-Print loop (REPL), the common mode of interaction for interpreted languages.
In point of fact, you seem to be describing a command language and interpreter, so I would suggest examining that domain for patterns matching your existing binding for the API.
To get started on the command line, first don't reinvent the wheel. There are a lot of options out there to parse commands.
In Java there is Commons CLI which provides you everything you need. There is a .NET CLI port as well.
InfiniteRed has a good writeup of how to do this in Ruby.
As far as implementation goes, you have the right idea. But don't reinvent the wheel here, either. Encapsulate work in Command objects and look at using the Chain of Responsibility pattern; Commons Chain works well. There is also a .NET Chain port.
If using these frameworks isn't an option, take a look at how they're implemented. Also if you've got a problem doing interop with some of these options, Ruby is really a nice swiss-army knife for doing this type of thing. It's relatively portable and the code can end up being really clean and easy to maintain.
UPDATE: JCommander also looks interesting.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With