Command Line Arguments in ActionScript

Our first program demonstrates how to access command-line arguments. Here’s the full source code:

package {
    import flash.display.Sprite;
    import flash.system.Capabilities;

    public class CommandLineArguments extends Sprite {
        public function CommandLineArguments() {
            var argsWithProg:Array = Capabilities.commandLine.split(" ");
            var argsWithoutProg:Array = argsWithProg.slice(1);

            // You can get individual args with normal indexing.
            var arg:String = argsWithProg[3];

            trace(argsWithProg);
            trace(argsWithoutProg);
            trace(arg);
        }
    }
}

In ActionScript, accessing command-line arguments is not as straightforward as in some other languages. The Capabilities.commandLine property provides access to the command-line arguments as a single string. We need to split this string to get individual arguments.

To experiment with command-line arguments, you’ll need to compile your ActionScript code into a SWF file and then run it using the Flash Player projector or AIR runtime, passing arguments to it.

$ mxmlc CommandLineArguments.as
$ adl application.xml -- arg1 arg2 arg3 arg4

This would output something like:

[adl arg1 arg2 arg3 arg4]
[arg1, arg2, arg3, arg4]
arg3

Note that the exact output may vary depending on how you run the ActionScript application and what runtime you use.

In ActionScript, there’s no direct equivalent to building a standalone executable like in some other languages. Instead, you typically compile to a SWF file which can be run in the Flash Player or packaged into an AIR application for desktop or mobile platforms.

Next, we could look at more advanced ways of processing input in ActionScript applications, such as handling user interactions or reading from external files.