Keeping program GUI front-end and CLI functionality separate

command lineguiprogramming

In Linux a lot of programs have a CLI version and a GUI front-end. I read somewhere that this follows the linux philosophy and is good practice. Of course it's true from a developers perspective to keep the GUI code separated from the actual program.

Given a program, which can be used happily from the command line, I would want to create a GUI for the command line version. What are my options here?

I can think of only 2 that the GUI developer would use.

  • calling the program with the configured paramters like so:

    system("someprogram –paramter1 -p2 -p3")

  • having the CLI version be able to run in server mode with sockets (unix, inet, …) and using those sockets to communicate with the "server"

The cleanest solution would be the second in my opinion, but this is not always possible without altering the CLI version.

The actual question is what is the proper way to have both a cli version and a GUI version built separately?

Best Answer

A different approach is to write your functionality as a library. Then you have a GUI which uses the library; and a CLI which also uses the library. Depending on the complexity of your task that may be the best solution, as both programs could work independently without the need of any kind of inter process communication.

Related Question