Is it possible to create a 'command line' swf?

There's no way to do this with a bare SWF right now.

However, you can publish your Flash content as an AIR app. The app can then be invoked from the command line, and you can collect the arguments from the arguments property of an InvokeEvent. The basic idea looks like this:

NativeApplication.nativeApplication.addEventListener( 
            InvokeEvent.INVOKE, onInvoke );
// ...
function onInvoke( e:InvokeEvent ) {
    var numArguments:int = e.arguments.length;
    // ...
}

Note, however, that this is essentially a one-way street. You can grab the command-line arguments, but Flash still doesn't grok the idea of stdin and stdout.


YES! It actually is possible.

You can create a pure AS3 AIR project (without any application window) and run from the command line using ADL (AIR Debug Launcher).

ADL will execute your SWF and will pass whatever arguments you give it directly to your application at runtime—all from the command line! To read the arguments from AS3 just add this code to your main class:

package
{
   import flash.desktop.NativeApplication;
   import flash.display.Sprite;
   import flash.events.InvokeEvent;

   public class CmdLine extends Sprite
   {
      public function CmdLine()
      {
         NativeApplication.nativeApplication.addEventListener(
            InvokeEvent.INVOKE, onInvokeEvent); 

         function onInvokeEvent(invocation:InvokeEvent):void { 
            trace(invocation.arguments); 
         } 
      }
   }
}

Your main class will still extend Sprite, but you won't see any UI unless you create NativeWindow objects. If you're using Flash Builder, just create a new AIR project and rename the extension of the main .mxml file to .as (before you finish the wizard).

Here is more about ADL: Using the AIR Debug Launcher (ADL)

Also, this will be very useful: AIR application invocation and termination

You can do all your output using trace(), write files, or even write directly to stdout, as seen here.


Apparently there is the Tamarin project which aims to create an open source implementation of AS3. This page gives a little detail of compiling an AS3 script and running it from a command line.

I'm not getting a good idea of how stable Tamarin is, but it might be your best bet for now. On the other hand, I have to strongly agree with @zenazn that you would be better off long-term learning a language more designed for general purposes, but if really want to just use Actionscript, don't let anyone stop you :)