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