|
14 | 14 | * -f or --file [path] - Reads the file and executes the code from it.<br> |
15 | 15 | * If no argument was passed, the default Editor GUI will open.*/ |
16 | 16 | public class ConsoleMain { |
| 17 | + |
| 18 | + /*Argument schema: -f <string> -e <string> --nogui </> |
| 19 | + * When nogui is not given, the gui program will start with the given script from the command line |
| 20 | + * */ |
| 21 | + |
17 | 22 | public static void main(String[] args) throws IOException { |
18 | | - Process p = new Process(true); |
19 | | - p.addSystemOutput(); |
20 | | - p.setInput(System.in); |
21 | | - |
22 | | - if(args.length == 0) { |
23 | | - if(ConsoleMain.class.getResourceAsStream("/Editor.txt") == null) { |
24 | | - System.err.println("Editor file is missing at: " + URLDecoder.decode(ConsoleMain.class.getProtectionDomain().getCodeSource().getLocation().getPath(), "UTF-8")); |
25 | | - return; |
26 | | - } |
27 | | - BufferedReader reader = new BufferedReader(new InputStreamReader(ConsoleMain.class.getResourceAsStream("/Editor.txt"))); |
28 | | - String code = ""; |
29 | | - String line = reader.readLine(); |
30 | | - while(line != null) { |
31 | | - code += line; |
32 | | - line = reader.readLine(); |
| 23 | + boolean initGUI = true; |
| 24 | + String scriptToExecute = null; |
| 25 | + String filePath = null; |
| 26 | + //ALL arguments need to be checked |
| 27 | + |
| 28 | + for(int i = 0; i < args.length; i++) { |
| 29 | + if((args[i].equals("-e") || args[i].equals("--execute")) && scriptToExecute == null) { |
| 30 | + if(i + 1 < args.length) { |
| 31 | + if(!isArgument(args[i+1])) { |
| 32 | + scriptToExecute = args[i+1]; |
| 33 | + i++; //Skip the next iteration |
| 34 | + continue; |
| 35 | + } |
| 36 | + } |
| 37 | + throw new IllegalArgumentException("Expecting string after " + args[i] + " [script_to_execute]"); |
| 38 | + } else if((args[i].equals("-f") || args[i].equals("--file")) && filePath == null) { |
| 39 | + if(i + 1 < args.length) { |
| 40 | + if(!isArgument(args[i+1])) { |
| 41 | + filePath = args[i+1]; |
| 42 | + i++; |
| 43 | + continue; |
| 44 | + } |
| 45 | + } |
| 46 | + throw new IllegalArgumentException("Expecting string after " + args[i] + " [path_to_file]"); |
| 47 | + } else if(args[i].equals("--nogui")) { |
| 48 | + initGUI = false; |
| 49 | + continue; |
33 | 50 | } |
34 | | - reader.close(); |
35 | | - p.execute(code, false); |
36 | | - return; |
| 51 | + System.out.println("Valid arguments are:\n-f | --file\tOpens the file in the editor (If --nogui is set, the file is executed)\n-e | --execute\tOpens the script with the editor (If --nogui is set, the script is executed)\n | --nogui\tOpens the command line editor"); |
| 52 | + throw new IllegalArgumentException("Unknown argument " + args[i] ); |
37 | 53 | } |
38 | 54 |
|
39 | | - if(args[0].equals("-e") || args[0].equals("--execute")) { |
40 | | - if(args.length < 2) { |
41 | | - System.err.println("Argument " + args[0] + " expects an executable script, e.g.: -e \"println \"Hello World;\"\""); |
42 | | - System.exit(-1); |
| 55 | + if(initGUI) { |
| 56 | + Window w = new Window(); |
| 57 | + if(filePath != null) { |
| 58 | + w.openDocument(new File(filePath)); |
| 59 | + } else if(scriptToExecute != null) { |
| 60 | + w.setScript(scriptToExecute); |
43 | 61 | } |
44 | | - p.execute(args[1], true); |
45 | | - } else if(args[0].equals("-f") || args[0].equals("--file")) { |
46 | | - if(args.length < 2) { |
47 | | - System.err.println("Argument " + args[0] + " expects a path to a text file containing the script"); |
48 | | - System.exit(-1); |
| 62 | + } else { |
| 63 | + Process p = new Process(true); |
| 64 | + p.addSystemOutput(); |
| 65 | + p.setInput(System.in); |
| 66 | + |
| 67 | + if(filePath != null) { |
| 68 | + p.execute(new File(args[1]), true); |
| 69 | + } else if(scriptToExecute != null) { |
| 70 | + p.execute(args[1], true); |
| 71 | + } else { |
| 72 | + if(ConsoleMain.class.getResourceAsStream("/Editor.txt") == null) { |
| 73 | + System.err.println("Editor file is missing at: " + URLDecoder.decode(ConsoleMain.class.getProtectionDomain().getCodeSource().getLocation().getPath(), "UTF-8")); |
| 74 | + return; |
| 75 | + } |
| 76 | + BufferedReader reader = new BufferedReader(new InputStreamReader(ConsoleMain.class.getResourceAsStream("/Editor.txt"))); |
| 77 | + String code = ""; |
| 78 | + String line = reader.readLine(); |
| 79 | + while(line != null) { |
| 80 | + code += line; |
| 81 | + line = reader.readLine(); |
| 82 | + } |
| 83 | + reader.close(); |
| 84 | + p.execute(code, false); |
| 85 | + return; |
49 | 86 | } |
50 | | - p.execute(new File(args[1]), true); |
51 | | - } else if(args[0].equals("--gui") || args[0].equals("-g")) { |
52 | | - new Window(); |
53 | 87 | } |
54 | 88 | } |
| 89 | + |
| 90 | + private static boolean isArgument(String arg) { |
| 91 | + return arg.equals("-f") || arg.equals("--file") || arg.equals("-e") || arg.equals("--execute") || arg.equals("--nogui"); |
| 92 | + } |
55 | 93 | } |
0 commit comments