comparison core/src/luan/cmd_line.luan @ 462:2e79b47d02a2

remove all command line options; add add_extension boolean arg to scheme fns;
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 06 May 2015 16:54:20 -0600
parents b31d614343e8
children 8dcf9e12446b
comparison
equal deleted inserted replaced
461:e12841f7edef 462:2e79b47d02a2
1 local Luan = require "luan:Luan" 1 local Luan = require "luan:Luan"
2 local ipairs = Luan.ipairs 2 local ipairs = Luan.ipairs
3 local load = Luan.load
4 local load_file = Luan.load_file 3 local load_file = Luan.load_file
5 local try = Luan.try 4 local try = Luan.try
6 require "luan:String" -- for String methods
7 local Table = require "luan:Table" 5 local Table = require "luan:Table"
8 local Io = require "luan:Io" 6 local Io = require "luan:Io"
9 local print = Io.print 7 local print = Io.print
10 local Debug = require "luan:Debug" 8 local Debug = require "luan:Debug"
11 9
12 10
13 local standalone_usage = [=[
14 usage: java luan.Luan [options] [script-URI [args]]
15 Available options are:
16 -e stat execute string 'stat'
17 -i enter interactive mode after executing 'script'
18 -v show version information
19 -- stop handling options
20 - stop handling options and execute stdin
21 ]=]
22
23 local function standalone_error(msg)
24 Io.stderr.write( msg, "\n", standalone_usage )
25 end
26
27
28 local args = {...} 11 local args = {...}
29 local interactive = false
30 local showVersion = false
31 local i = 1
32 if #args == 0 then 12 if #args == 0 then
33 interactive = true 13 print(Luan.VERSION)
34 showVersion = true 14 Debug.debug("> ")
35 else 15 else
36 while i <= #args do 16 local file = args[1]
37 local arg = args[i]
38 if arg.sub(1,1) ~= "-" or arg == "--" then
39 break
40 end
41 if arg == "-i" then
42 interactive = true
43 elseif arg == "-v" then
44 showVersion = true
45 elseif arg == "-e" then
46 i = i + 1
47 if i > #args then
48 standalone_error "'-e' needs argument"
49 return
50 end
51 local cmd = args[i]
52 local stat = load(cmd,"(command line)",{},true)
53 local result = Table.pack( stat() )
54 if result.n > 0 then
55 print( Table.unpack(result,1,result.n) )
56 end
57 elseif arg == "-" then
58 local src = Io.stdin.read_text()
59 local stdin = load(src,"stdin")
60 stdin()
61 return
62 else
63 standalone_error( "unrecognized option '"..arg.."'" )
64 return
65 end
66 i = i + 1
67 end
68 end
69 if showVersion then print(Luan.VERSION) end
70 if i <= #args then
71 local file = args[i]
72 Luan.arg = {} 17 Luan.arg = {}
73 for j,v in ipairs(args) do 18 for j,v in ipairs(args) do
74 Luan.arg[j-i] = v 19 Luan.arg[j-1] = v
75 end 20 end
76 try { 21 try {
77 function() 22 function()
78 local main_file = load_file(file..".luan") 23 local main_file = load_file(file,true)
79 main_file( Table.unpack(Luan.arg) ) 24 Debug.print_if_something( main_file( Table.unpack(Luan.arg) ) )
80 end; 25 end;
81 catch = function(e) 26 catch = function(e)
82 -- java(); e.printStackTrace(); return 27 -- java(); e.printStackTrace(); return
83 Io.print_to(Io.stderr, e ) 28 Io.print_to(Io.stderr, e )
84 end; 29 end;
85 } 30 }
86 end 31 end
87 if interactive then
88 Debug.debug("> ")
89 end