0

我尝试了 luaj 提供的实用方法来调用带有命令行参数的 lua 文件(这个http://lua-users.org/wiki/SourceCodeFormatter

Globals globals = JsePlatform.standardGlobals();        
String script ="src/codeformatter.lua";
File f = new File(script);
LuaValue chunk = globals.loadfile(f.getCanonicalPath());
List<String> argList = Arrays.asList("--file","test.lua");                          
JsePlatform.luaMain(chunk, argList.toArray(new String[argList.size()]));

但是,我总是尝试在代码尝试访问 arg 表的地方调用 nil(而 i < table.getn(arg) 做)-我尝试了其他示例,它们都导致相同的错误-luaj 似乎没有设置“arg”表正确 - 即使是简单的打印 arg[1] 也不起作用。

4

2 回答 2

0

有两个问题:

  • 对table.getn(arg)的调用应替换为#arg
  • luaj 3.0.1 未正确设置块的环境,因此未设置arg

但是,作为一种解决方法,您可以使用 varargs "..." 语法捕获输入,方法是在codeformatter.lua的顶部添加一行,例如

arg = {...}

这是一个代码片段来说明:

Globals globals = JsePlatform.standardGlobals();   
LuaValue chunk = globals.load(
        "arg = {...};" +
        "print(#arg, arg[1], arg[2])");
JsePlatform.luaMain(chunk, new String[] {"--file","test.lua"});

产生输出:

2   --file  test.lua
于 2016-01-23T17:00:43.537 回答
0

LuaJ 不再支持 table.getn 因为它在 lua 5.1 中被删除 - 用 #varname 替换 table.getn 的每个出现 - 并在顶部使用 ocal args={...} 初始化 args 数组使其工作。

尽管如此,代码格式化程序并没有真正做到我期望的那样

于 2015-11-29T19:07:26.337 回答