#261 main and args

cbeust Wed 25 Jun 2008

I can't seem to find any example of declaring a "Void main" method with Str[] args in it, and doing so results in the cryptic:

sys::ArgErr: Too few arguments: 1 < instance+1..1
fan.sys.Method$MethodFunc.checkArgs (Method.java:493)
fan.sys.Method$MethodFunc.call1 (Method.java:249)
fan.sys.Method.call1 (Method.java:181)
fanx.tools.Fan.executeFile (Fan.java:91)
fanx.tools.Fan.execute (Fan.java:36)
fanx.tools.Fan.run (Fan.java:206)
fanx.tools.Fan$1.run (Fan.java:248)
fan.sys.Thread$Val.run (Thread.java:453)

What's the correct syntax?

-- Cedric

cbeust Wed 25 Jun 2008

Ah, thanks.

Maybe the compiler could give a more meaningful error message...

-- Cedric

brian Wed 25 Jun 2008

That is something I've been meaning to fix. I enhanced the launcher code to be more flexible calling main, and to print a more helpful error message. The updated docTool::Fan section follows:

The main method can return Void or Int. If the main method returns an Int, then it is returned as the exit code of the process, otherwise zero is returned (or non-zero if an exception is raised).

The main method either takes no arguments or Str[]. You can access any arguments passed on the command line via the Sys.args method. All of these are valid main methods:

Void make()
Void make(Str[] args)
Void make(Str[] args, Bool flag := true)
Int make()
Int make(Str[] args)
Int make(Str[] args, Bool flag := true)

All of the main method signatures above can be static or instance based. If the main method is static then it is simply invoked using reflection. However if the main method is instance based, a new instance is created via Type.make to use for the main method. This technique can be used perform common initialization via a base class - in fact it is used by the build scripts themselves.

tompalmer Wed 25 Jun 2008

That would be great.

jodastephen Sat 12 Nov 2011

These main method examples have a method name of "make". Surely the method name should be "main"?

brian Sun 13 Nov 2011

These main method examples have a method name of "make". Surely the method name should be "main"?

Yeah that is quite a bad typo - glad you caught it. I pushed a fix.

Login or Signup to reply.