Making crossplatform CLI application with HAXE

Written by - Saumya

19 January 2015

When I first heard about CLI applications, I thought it might be shipping with the OS itself. Then I found that we can write our own CLI applications. At that point I assume that well, just a CLI application, means it runs everywhere. But actually CLI applications by default are not crossplatform! Yes, I realised it much later. In order to have a crossplatform CLI app, one needs all the required environment to compile to and compile for a particular OS.

HAXE comes in handy in this case. One can make a crossplatfom CLI app with just one code base. Ofcourse you need the required environment to compile to that particular environment. There is even a shorter way to this, compile for NEKO virtual machine. This NEKO VM ships with HAXE. Once you compile to NEKO, all you do is package all the NEKO dependencies and there you have a crossplatform CLI for every OS. Here we will compile to NEKO and run through NEKO, but just keep in mind that the same code can be compiled to CPP(C++) to produce true native CLI.
Lets begin with the compiler settings or the build file. The .hxml file has the following content.

# HAXE build
# Defining the application entry
-main MyCliApp
# NEKO : compilation
-neko bin/myCliApp.n
#
# CPP : compilation
#-cpp bin/cpp/

We are setting up for compiling to NEKO but then we have the options commented out for compiling to CPP. If we want to compile for the C++ target, then comment out the NEKO target and enable the CPP(C++) target. Well, this compilation will create a file named myCliApp.n inside the ‘bin’ folder. To run the file, we have to navigate to the ‘bin’ folder and run with NEKO as below.

neko ./myCliApp.n

Thats about compilation and running. Lets get back to coding. There is no surprise here as all we have to do is, get the right package and class from HAXE and use it. Here is the Git repo for this example. The application entry initialises ‘PrintInfo’ Class. That class prints the information that we want to show to the user and finally asks the name of the use. As user inputs the name, it greets the user and moves to our defined command prompt. We have setup some commands to display. Depending upon user input the necessary commands are executed. All these are happening in MyCli.hx Class. The code is commented for easy reference in this repo. Here is the direct link to the class.

Happy coding.