comparison src/luan/tools/CmdLine.java @ 38:30fcb4fb0f76

move CmdLine to luan.tools git-svn-id: https://luan-java.googlecode.com/svn/trunk@39 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Thu, 20 Dec 2012 02:43:37 +0000
parents src/luan/CmdLine.java@8a57ebfdfd78
children e5bcb1eeafc1
comparison
equal deleted inserted replaced
37:8a57ebfdfd78 38:30fcb4fb0f76
1 package luan.tools;
2
3 import java.util.Arrays;
4 import java.util.Scanner;
5 import luan.lib.BasicLib;
6 import luan.lib.JavaLib;
7 import luan.Lua;
8 import luan.LuaState;
9 import luan.LuaFunction;
10 import luan.LuaTable;
11 import luan.LuaNumber;
12 import luan.LuaException;
13 import luan.interp.LuaCompiler;
14
15
16 public class CmdLine {
17
18 public static void main(String[] args) throws Exception {
19 LuaState lua = LuaCompiler.newLuaState();
20 BasicLib.register(lua);
21 JavaLib.register(lua);
22 boolean interactive = false;
23 boolean showVersion = false;
24 int i = 0;
25 if( args.length == 0 ) {
26 interactive = true;
27 showVersion = true;
28 } else {
29 while( i < args.length ) {
30 String arg = args[i];
31 if( !arg.startsWith("-") || arg.equals("--") )
32 break;
33 if( arg.equals("-i") ) {
34 interactive = true;
35 } else if( arg.equals("-v") ) {
36 showVersion = true;
37 } else if( arg.equals("-e") ) {
38 if( ++i == args.length )
39 error("'-e' needs argument");
40 String cmd = args[i];
41 try {
42 LuaFunction fn = BasicLib.load(lua,cmd);
43 fn.call(lua);
44 } catch(LuaException e) {
45 System.err.println("command line error: "+e.getMessage());
46 System.exit(-1);
47 }
48 } else if( arg.equals("-") ) {
49 try {
50 BasicLib.dofile(lua,null);
51 } catch(LuaException e) {
52 System.err.println(e.getMessage());
53 System.exit(-1);
54 }
55 System.exit(0);
56 } else {
57 error("unrecognized option '"+arg+"'");
58 }
59 i++;
60 }
61 }
62 if( showVersion )
63 System.out.println(Lua.version);
64 if( i < args.length ) {
65 String file = args[i++];
66 Object[] varArgs = new Object[args.length-1];
67 System.arraycopy(args,1,varArgs,0,varArgs.length);
68 LuaTable argsTable = new LuaTable();
69 for( int j=0; j<args.length; j++ ) {
70 argsTable.put( new LuaNumber(j), args[j] );
71 }
72 lua.global().put("arg",argsTable);
73 try {
74 LuaFunction fn = BasicLib.loadfile(lua,file);
75 fn.call(lua,varArgs);
76 } catch(LuaException e) {
77 // System.err.println(e.getMessage());
78 e.printStackTrace();
79 System.exit(-1);
80 }
81 }
82 if( interactive )
83 interactive(lua);
84 }
85
86 private static void error(String msg) {
87 System.err.println(msg);
88 System.err.println("usage: java luan.CmdLine [options] [script [args]]");
89 System.err.println("Available options are:");
90 System.err.println(" -e stat execute string 'stat'");
91 System.err.println(" -i enter interactive mode after executing 'script'");
92 // System.err.println(" -l name require library 'name'");
93 System.err.println(" -v show version information");
94 // System.err.println(" -E ignore environment variables");
95 System.err.println(" -- stop handling options");
96 System.err.println(" - stop handling options and execute stdin");
97 System.exit(-1);
98 }
99
100 static void interactive(LuaState lua) {
101 while( true ) {
102 System.out.print("> ");
103 String input = new Scanner(System.in).nextLine();
104 try {
105 LuaFunction fn = BasicLib.load(lua,input);
106 Object[] rtn = fn.call(lua);
107 if( rtn.length > 0 )
108 BasicLib.print(lua,rtn);
109 } catch(LuaException e) {
110 System.out.println(e.getMessage());
111 }
112 }
113 }
114 }