Mercurial Hosting > luan
changeset 133:98aba462c422
add tools/cmd_line.luan
git-svn-id: https://luan-java.googlecode.com/svn/trunk@134 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Wed, 11 Jun 2014 05:20:19 +0000 |
parents | 14281d5bd36f |
children | b585d0915621 |
files | src/luan/lib/init.luan src/luan/tools/CmdLine.java src/luan/tools/cmd_line.luan |
diffstat | 3 files changed, 71 insertions(+), 73 deletions(-) [+] |
line wrap: on
line diff
--- a/src/luan/lib/init.luan Tue Jun 10 09:17:25 2014 +0000 +++ b/src/luan/lib/init.luan Wed Jun 11 05:20:19 2014 +0000 @@ -38,75 +38,3 @@ end end end - - -local standalone_usage = [=[ -usage: java luan.CmdLine [options] [script [args]] -Available options are: - -e stat execute string 'stat' - -i enter interactive mode after executing 'script' - -v show version information - -- stop handling options - - stop handling options and execute stdin -]=] - -local function standalone_error(msg) - Io.stderr.write( msg, "\n", standalone_usage ) -end - -function Basic.standalone(...) - local args = {...} - local interactive = false - local showVersion = false - local i = 1 - if #args == 0 then - interactive = true - showVersion = true - else - while i <= #args do - local arg = args[i] - if arg.sub(1,1) ~= "-" or arg == "--" then - break - end - if arg == "-i" then - interactive = true - elseif arg == "-v" then - showVersion = true - elseif arg == "-e" then - i = i + 1 - if i == #args then - standalone_error "'-e' needs argument" - return - end - local cmd = args[i] - local fn = load(cmd,"(command line)",true,true) - local result = Table.pack( fn() ) - if result.n > 0 then - print( Table.unpack(result,1,result.n) ) - end - elseif arg == "-" then - local src = Io.stdin.read_text() - local fn = load(src,"stdin") - fn() - return - else - standalone_error( "unrecognized option '"..arg.."'" ) - return - end - i = i + 1 - end - end - if showVersion then print(_VERSION) end - if i <= #args then - local file = args[i] - _G.arg = {} - for j,v in ipairs(args) do - _G.arg[j-i] = v - end - local fn = load_file(file) - fn( Table.unpack(_G.arg) ) - end - if interactive then - Debug.debug("> ") - end -end
--- a/src/luan/tools/CmdLine.java Tue Jun 10 09:17:25 2014 +0000 +++ b/src/luan/tools/CmdLine.java Wed Jun 11 05:20:19 2014 +0000 @@ -15,7 +15,7 @@ public static void main(String[] args) { LuanState luan = LuanState.newStandard(); try { - LuanFunction standalone = (LuanFunction)luan.get("Basic.standalone"); + LuanFunction standalone = (LuanFunction)BasicLib.load_java_resource(luan,"luan/tools/cmd_line.luan"); luan.call(standalone,args); } catch(LuanException e) { // System.err.println(e.getMessage());
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/tools/cmd_line.luan Wed Jun 11 05:20:19 2014 +0000 @@ -0,0 +1,70 @@ + +local standalone_usage = [=[ +usage: java luan.CmdLine [options] [script [args]] +Available options are: + -e stat execute string 'stat' + -i enter interactive mode after executing 'script' + -v show version information + -- stop handling options + - stop handling options and execute stdin +]=] + +local function standalone_error(msg) + Io.stderr.write( msg, "\n", standalone_usage ) +end + + +local args = {...} +local interactive = false +local showVersion = false +local i = 1 +if #args == 0 then + interactive = true + showVersion = true +else + while i <= #args do + local arg = args[i] + if arg.sub(1,1) ~= "-" or arg == "--" then + break + end + if arg == "-i" then + interactive = true + elseif arg == "-v" then + showVersion = true + elseif arg == "-e" then + i = i + 1 + if i == #args then + standalone_error "'-e' needs argument" + return + end + local cmd = args[i] + local fn = load(cmd,"(command line)",true,true) + local result = Table.pack( fn() ) + if result.n > 0 then + print( Table.unpack(result,1,result.n) ) + end + elseif arg == "-" then + local src = Io.stdin.read_text() + local fn = load(src,"stdin") + fn() + return + else + standalone_error( "unrecognized option '"..arg.."'" ) + return + end + i = i + 1 + end +end +if showVersion then print(_VERSION) end +if i <= #args then + local file = args[i] + _G.arg = {} + for j,v in ipairs(args) do + _G.arg[j-i] = v + end + local fn = load_file(file) + fn( Table.unpack(_G.arg) ) +end +if interactive then + Debug.debug("> ") +end