Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would you create a console application from an existing object oriented API?

I have:

  • existing object oriented native code API (non GUI)
  • GUI application that works with this API

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.

like image 297
Sergey Aldoukhov Avatar asked Sep 09 '26 22:09

Sergey Aldoukhov


2 Answers

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.

like image 50
Tetsujin no Oni Avatar answered Sep 11 '26 10:09

Tetsujin no Oni


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.

like image 37
cwash Avatar answered Sep 11 '26 11:09

cwash