Mercurial Hosting > luan
comparison src/luan/tools/cmd_line.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 | |
children | 7e160d2f6d9c |
comparison
equal
deleted
inserted
replaced
132:14281d5bd36f | 133:98aba462c422 |
---|---|
1 | |
2 local standalone_usage = [=[ | |
3 usage: java luan.CmdLine [options] [script [args]] | |
4 Available options are: | |
5 -e stat execute string 'stat' | |
6 -i enter interactive mode after executing 'script' | |
7 -v show version information | |
8 -- stop handling options | |
9 - stop handling options and execute stdin | |
10 ]=] | |
11 | |
12 local function standalone_error(msg) | |
13 Io.stderr.write( msg, "\n", standalone_usage ) | |
14 end | |
15 | |
16 | |
17 local args = {...} | |
18 local interactive = false | |
19 local showVersion = false | |
20 local i = 1 | |
21 if #args == 0 then | |
22 interactive = true | |
23 showVersion = true | |
24 else | |
25 while i <= #args do | |
26 local arg = args[i] | |
27 if arg.sub(1,1) ~= "-" or arg == "--" then | |
28 break | |
29 end | |
30 if arg == "-i" then | |
31 interactive = true | |
32 elseif arg == "-v" then | |
33 showVersion = true | |
34 elseif arg == "-e" then | |
35 i = i + 1 | |
36 if i == #args then | |
37 standalone_error "'-e' needs argument" | |
38 return | |
39 end | |
40 local cmd = args[i] | |
41 local fn = load(cmd,"(command line)",true,true) | |
42 local result = Table.pack( fn() ) | |
43 if result.n > 0 then | |
44 print( Table.unpack(result,1,result.n) ) | |
45 end | |
46 elseif arg == "-" then | |
47 local src = Io.stdin.read_text() | |
48 local fn = load(src,"stdin") | |
49 fn() | |
50 return | |
51 else | |
52 standalone_error( "unrecognized option '"..arg.."'" ) | |
53 return | |
54 end | |
55 i = i + 1 | |
56 end | |
57 end | |
58 if showVersion then print(_VERSION) end | |
59 if i <= #args then | |
60 local file = args[i] | |
61 _G.arg = {} | |
62 for j,v in ipairs(args) do | |
63 _G.arg[j-i] = v | |
64 end | |
65 local fn = load_file(file) | |
66 fn( Table.unpack(_G.arg) ) | |
67 end | |
68 if interactive then | |
69 Debug.debug("> ") | |
70 end |