comparison src/luan/lib/init.luan @ 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 f0a4abe58593
children 3b384dc5ca91
comparison
equal deleted inserted replaced
132:14281d5bd36f 133:98aba462c422
36 catch e do 36 catch e do
37 print(e) 37 print(e)
38 end 38 end
39 end 39 end
40 end 40 end
41
42
43 local standalone_usage = [=[
44 usage: java luan.CmdLine [options] [script [args]]
45 Available options are:
46 -e stat execute string 'stat'
47 -i enter interactive mode after executing 'script'
48 -v show version information
49 -- stop handling options
50 - stop handling options and execute stdin
51 ]=]
52
53 local function standalone_error(msg)
54 Io.stderr.write( msg, "\n", standalone_usage )
55 end
56
57 function Basic.standalone(...)
58 local args = {...}
59 local interactive = false
60 local showVersion = false
61 local i = 1
62 if #args == 0 then
63 interactive = true
64 showVersion = true
65 else
66 while i <= #args do
67 local arg = args[i]
68 if arg.sub(1,1) ~= "-" or arg == "--" then
69 break
70 end
71 if arg == "-i" then
72 interactive = true
73 elseif arg == "-v" then
74 showVersion = true
75 elseif arg == "-e" then
76 i = i + 1
77 if i == #args then
78 standalone_error "'-e' needs argument"
79 return
80 end
81 local cmd = args[i]
82 local fn = load(cmd,"(command line)",true,true)
83 local result = Table.pack( fn() )
84 if result.n > 0 then
85 print( Table.unpack(result,1,result.n) )
86 end
87 elseif arg == "-" then
88 local src = Io.stdin.read_text()
89 local fn = load(src,"stdin")
90 fn()
91 return
92 else
93 standalone_error( "unrecognized option '"..arg.."'" )
94 return
95 end
96 i = i + 1
97 end
98 end
99 if showVersion then print(_VERSION) end
100 if i <= #args then
101 local file = args[i]
102 _G.arg = {}
103 for j,v in ipairs(args) do
104 _G.arg[j-i] = v
105 end
106 local fn = load_file(file)
107 fn( Table.unpack(_G.arg) )
108 end
109 if interactive then
110 Debug.debug("> ")
111 end
112 end