Search code examples
user-interfacelanguage-agnosticscriptingopen-source

How to design a command line program reusable for a future development of a GUI?


What are some best practices to keep in mind when developing a script program that could be integrated with a GUI, probably by somebody else, in the future?

Possible scenario:

  1. I develop a fancy python CLI program that scrapes every unicorn images from the web
  2. I decide to publish it on github
  3. A unicorn fan programmer decides to take the sources and build a GUI on them
  4. he/she gives up because my code is not reusable

How to prevent the step four letting the unicorn fan programmer build his/her GUI without too much hassle?


Solution

  • You do it by applying a good portion of layering (maybe implementing the MVP pattern) and treating your CLI as a UI in it's own right.

    UPDATE

    This text from the wikipedia article about the Model-View-Presenter pattern explains it quite well.

    Model-view-presenter (MVP) is a user interface design pattern engineered to facilitate automated unit testing and improve the separation of concerns in presentation logic.

    • The model is an interface defining the data to be displayed or otherwise acted upon in the user interface.

    • The view is an interface that displays data (the model) and routes user commands (events) to the presenter to act upon that data.

    • The presenter acts upon the model and the view. It retrieves data from repositories (the model), persists it, and formats it for display in the view.

    The main point being that you need to work on separation of concern in your application. Your CLI would be one implementation of a view, whereas the unicorn fan would implement another view for a rich client. The unicorn fan, would base his view on the same presenters as your CLI. If those presenters are not sufficient for his rich client he could easily add more, because each presenter is based on data from the model. The model, in turn, is where all the core logic of your application is based. Designing a good model is an entire subject in itself. You may be interested in reading, for example, about Domain-Driven Design, even though I don't know how well it applies to your current application. But it's interesting reading anyway. As you can see, the wikipedia article on MVP also talks about testability, which is also crucial if you want to provide a robust framework for others to build on. To reach a high level of testability in your code-base, it is often a good idea to use some kind of Dependency Injection framework.

    I hope this gives you a general idea of the techniques you need to employ, although I understand that it may be a little overwhelming. Don't hesitate to ask if you have any further doubts.

    /Klaus