Skip to content

Instantly share code, notes, and snippets.

Forked from jasononeil/HaxeScript.hx
Last active July 12, 2018 19:55
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
haxex, a short shell script that can be used so you have Haxe shell scripting

Note: This is a modified version of the haxex script created by @jasononeil

This version allows for specifying compiler args in your script. so you can include and use libraries like mcli

This is a tiny shell script that you can use to make scripting with Haxe very straight forward.

Using a straight #!/usr/bin/haxe doesn't work currently, as the Haxe compiler will have to be a little more clever about realising it is a shell script and that we wish to execute it. Maybe this will be native some day, for now, you can install the script below into /usr/bin/haxex and then use that.

Installation: if you're using OS X El Capitan you'll probably need to put haxex in /usr/local/bin/

  1. sudo nano /usr/bin/haxex, paste the 'haxex' file in there
  2. sudo chmod +x /usr/bin/haxex


#!/usr/bin/env haxex -debug @
class MyScript 
public static function main() 
	trace('My Script is executing!');
	#if debug
		trace('with debug flag!');


chmod +x MyScript.hx

For the included HaxeScript:

 haxelib install mcli
 chmod +x HaxeScript.hx
./HaxeScript.hx [yourname] --loud

Pretty neat-o if you ask me.

What this script actually does:

  1. Replaces '@' with --run
  2. Gets rid of "./" at the start of filename
  3. Gets rid of the ".hx" at the end of filename
  4. Turns the "/" dir separators into "." package separators, in case your script is in a subpackage.


If your script is in the root package (has no package statement at the top of the file), then you need to call it from the directory the script is in. If your script is in a sub-package, you need to call it from the root-level package. Easy rule of thumb: don't use packages, and call from the directory the script is in.

#!/usr/bin/env haxex -lib mcli @
Taken from mcli example
Say hello.
Example inspired by ruby's "executable" lib example
class HaxeScript extends mcli.CommandLine
Say it in uppercase?
public var loud:Bool;
Show this message.
public function help()
public function runDefault(?name:String)
if(name == null)
name = "World";
var msg = 'Hello, $name!';
if (loud)
msg = msg.toUpperCase();
public static function main()
new mcli.Dispatch(Sys.args()).dispatch(new HaxeScript());
# Create compiler args
compArgs=${args/@*/" --run"}
# Create string to match and remove compiler args
compMatch=${compArgs/ --run/"@"}
# Get cli args by removing compiler args
set -- $cliArgs
# Give filename its own var
# Set args to be passed to script
# Remove ".hx" if it's there
# Remove "./" if it's there
# Change "/" to "."
haxe $compArgs $filename $cliArgs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment