Mercurial Hosting > luan
comparison src/luan/tools/CmdLine.java @ 120:8d7730a5e3b4
move standalone logic from Java to Luan
git-svn-id: https://luan-java.googlecode.com/svn/trunk@121 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Mon, 02 Jun 2014 04:43:45 +0000 |
parents | e935581cf9fb |
children | 486a0641bca4 |
comparison
equal
deleted
inserted
replaced
119:f1bf2890d80f | 120:8d7730a5e3b4 |
---|---|
12 | 12 |
13 public class CmdLine { | 13 public class CmdLine { |
14 | 14 |
15 public static void main(String[] args) { | 15 public static void main(String[] args) { |
16 LuanState luan = LuanState.newStandard(); | 16 LuanState luan = LuanState.newStandard(); |
17 boolean interactive = false; | 17 try { |
18 boolean showVersion = false; | 18 LuanFunction standalone = (LuanFunction)luan.get("Basic.standalone"); |
19 int i = 0; | 19 luan.JAVA.call(standalone,"standalone",args); |
20 if( args.length == 0 ) { | 20 } catch(LuanException e) { |
21 interactive = true; | 21 System.err.println(e.getMessage()); |
22 showVersion = true; | 22 System.exit(-1); |
23 } else { | |
24 while( i < args.length ) { | |
25 String arg = args[i]; | |
26 if( !arg.startsWith("-") || arg.equals("--") ) | |
27 break; | |
28 if( arg.equals("-i") ) { | |
29 interactive = true; | |
30 } else if( arg.equals("-v") ) { | |
31 showVersion = true; | |
32 } else if( arg.equals("-e") ) { | |
33 if( ++i == args.length ) | |
34 error("'-e' needs argument"); | |
35 String cmd = args[i]; | |
36 try { | |
37 LuanFunction fn = BasicLib.load(luan,cmd,"(command line)",false); | |
38 luan.JAVA.call(fn,null); | |
39 } catch(LuanException e) { | |
40 System.err.println("command line error: "+e.getMessage()); | |
41 System.exit(-1); | |
42 } | |
43 } else if( arg.equals("-") ) { | |
44 try { | |
45 BasicLib.do_file(luan,"stdin"); | |
46 } catch(LuanException e) { | |
47 System.err.println(e.getMessage()); | |
48 System.exit(-1); | |
49 } | |
50 System.exit(0); | |
51 } else { | |
52 error("unrecognized option '"+arg+"'"); | |
53 } | |
54 i++; | |
55 } | |
56 } | 23 } |
57 if( showVersion ) | 24 System.exit(0); |
58 System.out.println(Luan.version); | |
59 if( i < args.length ) { | |
60 String file = args[i++]; | |
61 Object[] varArgs = new Object[args.length-1]; | |
62 System.arraycopy(args,1,varArgs,0,varArgs.length); | |
63 LuanTable argsTable = new LuanTable(); | |
64 for( int j=0; j<args.length; j++ ) { | |
65 argsTable.put( j, args[j] ); | |
66 } | |
67 luan.global().put("arg",argsTable); | |
68 try { | |
69 LuanFunction fn = BasicLib.load_file(luan,file); | |
70 luan.JAVA.call(fn,null,varArgs); | |
71 } catch(LuanException e) { | |
72 // System.err.println("error: "+e.getMessage()); | |
73 e.printStackTrace(); | |
74 System.exit(-1); | |
75 } | |
76 } | |
77 if( interactive ) | |
78 interactive(luan); | |
79 } | |
80 | |
81 private static void error(String msg) { | |
82 System.err.println(msg); | |
83 System.err.println("usage: java luan.CmdLine [options] [script [args]]"); | |
84 System.err.println("Available options are:"); | |
85 System.err.println(" -e stat execute string 'stat'"); | |
86 System.err.println(" -i enter interactive mode after executing 'script'"); | |
87 // System.err.println(" -l name require library 'name'"); | |
88 System.err.println(" -v show version information"); | |
89 // System.err.println(" -E ignore environment variables"); | |
90 System.err.println(" -- stop handling options"); | |
91 System.err.println(" - stop handling options and execute stdin"); | |
92 System.exit(-1); | |
93 } | |
94 | |
95 static void interactive(LuanState luan) { | |
96 Console console = System.console(); | |
97 LuanFunction print = (LuanFunction)luan.global().get("print"); | |
98 while( true ) { | |
99 String input = console.readLine("> "); | |
100 if( input==null ) | |
101 break; | |
102 try { | |
103 Object[] rtn = Luan.array(luan.eval(input,"stdin",true)); | |
104 if( rtn.length > 0 ) | |
105 luan.JAVA.call(print,"print",rtn); | |
106 } catch(LuanException e) { | |
107 System.out.println(e.getMessage()); | |
108 } | |
109 } | |
110 } | 25 } |
111 | 26 |
112 } | 27 } |