tech

[English] [Japanese]

28. shebang and command line arguments

LuneScript supports shebangs.

shebang correspondence

Shebang is a function that specifies the interpreter to be used by specifying the interpreter to be used as a comment in the first line of a script file often seen in shell scripts, etc., and the specified interpreter is executed when the script file is executed. For example in bash you would specify:

#! /bin/bash

In LuneScript, specify:

#! /usr/bin/lnsc -shebang

Here, /usr/bin/lnsc must be specified according to the environment.

This allows you to run the LuneScript file containing #! as is.

For example, if you have a file hello.lns like this:

#! /usr/bin/lnsc -shebang
print( "hello world" );

You can run this hello.lns directly.

$ hello.lns
hello world

At this time, there are the following restrictions.

  • In the go version of lnsc, .lua corresponding to the .lns file you are importing is++Must be generated in advance.

command line arguments

There are cases where you want to process command line arguments, such as when invoked with shebang or transcompiled to go.

To process this command line argument, define the following __main function and process the command line arguments passed to this function.

// @lnsFront: skip   
pub fn __main( argList:&List<str> ):int

The type of the __main function must be declared with the above type, except for the argument name argList.

Execution order of __main

The execution order of this __main function is performed after the processing of the top scope is finished. That is, for the script

#! /usr/bin/lnsc -shebang
// @lnsFront: skip   
print( "hello world" );

pub fn __main( argList:&List<str> ):int {
   print( "hoge" );
   return 0;
}

The output will be:

$ ./mini.lns 
hello world
hoge

Arguments for __main

The argument of __main is &List<str>.

This list contains the path of the script at the top, followed by the command line arguments.

#! /usr/bin/lnsc -shebang
// @lnsFront: skip   
pub fn __main( argList:&List<str> ):int {
   foreach arg in argList {
      print( arg );
   }
   return 0;
}

__main() function behavior when transpiling to lua

The following LuneScript code prints the command line options.

// @lnsFront: skip
pub fn __main( argList:&List<str> ) : int {
   foreach arg, index in argList {
      print( index, arg );
   }
   return 0;
}

Below is a sample execution result.

$ lnsc -shebang mini.lns a b c 
1	mini.lns
2	a
3	b
4	c

To transcompile this code, run the following command:

lnsc mini.lns save

This will output mini.lua.

Running this mini.lua gives:

$ lua5.3 mini.lua a b c

What this means is that "the __main() function is not called".

When lnsc is run with the -shebang option, it runs the __main() function defined in the .lns file. On the other hand, the transcompiled lua code doesn't emit code to do __main() , which is why it behaves this way.

To run the __main() of your transcompiled lua code, you need either:

  • Create your own code to execute __main()
  • Specify the --main option when transcompiling

--main option

By specifying the --main option when transcompiling, the processing that executes the __main() function is output to the specified module.

Below is a sample --main option.

$ lnsc mini.lns save --main mini

where mini in --main mini is the module name containing the __main() to run.

This will output the code to run __main() in mini.lua.

Below is the execution result of mini.lua generated with --main mini specified.

$ lua5.3 mini.lua a b c
1	mini.lua
2	a
3	b
4	c

Note that the first argument of argList when executed with lnsc's shebang contains the path of .lns, while the first argument of argList after transpiling to lua contains the path of .lua is the difference.