Mercurial Hosting > luan
changeset 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 | df923e5835b2 |
children | 8d8f4f5caef4 |
files | src/luan/CmdLine.java src/luan/lib/BasicLib.java |
diffstat | 2 files changed, 82 insertions(+), 25 deletions(-) [+] |
line wrap: on
line diff
diff -r df923e5835b2 -r 9bc66d09ea48 src/luan/CmdLine.java --- a/src/luan/CmdLine.java Fri Dec 07 05:49:17 2012 +0000 +++ b/src/luan/CmdLine.java Fri Dec 07 09:33:31 2012 +0000 @@ -1,31 +1,61 @@ package luan; +import java.io.InputStreamReader; import java.util.Arrays; import java.util.Scanner; import luan.lib.BasicLib; public class CmdLine { + static final String version = "Luan 0.0"; public static void main(String[] args) throws Exception { LuaState lua = new LuaState(); BasicLib.register(lua); boolean interactive = false; + boolean showVersion = false; int i = 0; - while( i < args.length ) { - String arg = args[i]; - if( !arg.startsWith("-") ) - break; - if( arg.equals("-i") ) { - interactive = true; - } else { - throw new RuntimeException("invalid option: "+arg); + if( args.length == 0 ) { + interactive = true; + showVersion = true; + } else { + while( i < args.length ) { + String arg = args[i++]; + if( !arg.startsWith("-") || arg.equals("--") ) + break; + if( arg.equals("-i") ) { + interactive = true; + } else if( arg.equals("-v") ) { + showVersion = true; + } else if( arg.equals("-e") ) { + if( i == args.length ) + error("'-e' needs argument"); + String cmd = args[i++]; + try { + LuaFunction fn = BasicLib.load(lua,cmd); + fn.call(lua); + } catch(LuaException e) { + System.err.println("command line error: "+e.getMessage()); + System.exit(-1); + } + } else if( arg.equals("-") ) { + String cmd = BasicLib.readAll(new InputStreamReader(System.in)); + try { + LuaFunction fn = BasicLib.load(lua,cmd); + fn.call(lua); + } catch(LuaException e) { + System.err.println(e.getMessage()); + System.exit(-1); + } + System.exit(0); + } else { + error("unrecognized option '"+arg+"'"); + } } - i++; } - if( i == args.length ) { - interactive = true; - } else { + if( showVersion ) + System.out.println(version); + if( i < args.length ) { String file = args[i++]; Object[] varArgs = new Object[args.length-1]; System.arraycopy(args,1,varArgs,0,varArgs.length); @@ -35,18 +65,32 @@ } lua.env().set("arg",argsTable); try { - LuaFunction fn = BasicLib.loadFile(lua,file); + LuaFunction fn = BasicLib.loadfile(lua,file); fn.call(lua,varArgs); } catch(LuaException e) { -// System.out.println(e.getMessage()); +// System.err.println(e.getMessage()); e.printStackTrace(); - return; + System.exit(-1); } } if( interactive ) interactive(lua); } + private static void error(String msg) { + System.err.println(msg); + System.err.println("usage: java luan.CmdLine [options] [script [args]]"); + System.err.println("Available options are:"); + System.err.println(" -e stat execute string 'stat'"); + System.err.println(" -i enter interactive mode after executing 'script'"); +// System.err.println(" -l name require library 'name'"); + System.err.println(" -v show version information"); +// System.err.println(" -E ignore environment variables"); + System.err.println(" -- stop handling options"); + System.err.println(" - stop handling options and execute stdin"); + System.exit(-1); + } + static void interactive(LuaState lua) { while( true ) { System.out.print("> ");
diff -r df923e5835b2 -r 9bc66d09ea48 src/luan/lib/BasicLib.java --- a/src/luan/lib/BasicLib.java Fri Dec 07 05:49:17 2012 +0000 +++ b/src/luan/lib/BasicLib.java Fri Dec 07 09:33:31 2012 +0000 @@ -1,5 +1,7 @@ package luan.lib; +import java.io.File; +import java.io.Reader; import java.io.FileReader; import java.io.IOException; import java.lang.reflect.Method; @@ -29,7 +31,7 @@ add( t, "print", new Object[0].getClass() ); add( t, "type", Object.class ); add( t, "load", LuaState.class, String.class ); - add( t, "loadFile", LuaState.class, String.class ); + add( t, "loadfile", LuaState.class, String.class ); add( t, "pairs", LuaTable.class ); add( t, "ipairs", LuaTable.class ); } @@ -74,19 +76,30 @@ return chunk.newClosure(lua); } - public static String readFile(String fileName) throws IOException { - StringBuilder sb = new StringBuilder(); - FileReader in = new FileReader(fileName); - char[] buf = new char[8192]; + public static String readAll(Reader in) + throws IOException + { + char[] a = new char[8192]; + StringBuilder buf = new StringBuilder(); int n; - while( (n=in.read(buf)) != -1 ) { - sb.append(buf,0,n); + while( (n=in.read(a)) != -1 ) { + buf.append(a,0,n); } - return sb.toString(); + return buf.toString(); } - public static LuaFunction loadFile(LuaState lua,String fileName) throws LuaException,IOException { - return load(lua,readFile(fileName)); + public static String read(File file) + throws IOException + { + Reader in = new FileReader(file); + String s = readAll(in); + in.close(); + return s; + } + + + public static LuaFunction loadfile(LuaState lua,String fileName) throws LuaException,IOException { + return load(lua,read(new File(fileName))); } private static class TableIter {