Skip to content

Commit 42817a9

Browse files
authored
Add files via upload
1 parent 0cbe544 commit 42817a9

1 file changed

Lines changed: 99 additions & 0 deletions

File tree

ConsoleMain.java

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.devkev.devscript.raw;
2+
3+
import java.io.BufferedReader;
4+
import java.io.File;
5+
import java.io.IOException;
6+
import java.io.InputStreamReader;
7+
import java.net.URLDecoder;
8+
9+
import com.devkev.gui.Window;
10+
11+
/**@author Philipp Gersch
12+
* @version 1.8.2 (stable)
13+
* Valid arguments are: <br>-e or --execute [some code] - Executes the String<br>
14+
* -f or --file [path] - Reads the file and executes the code from it.<br>
15+
* If no argument was passed, the default Editor GUI will open.*/
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+
22+
public static void main(String[] args) throws IOException {
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;
50+
}
51+
throw new IllegalArgumentException("Unknown argument " + args[i] + "Valid arguments are:\n-f or --file\tOpens the file in the editor (If --nogui is set, the file is executed)\n-e or --execute\tOpens the script with the editor (If --nogui is set, the script is executed)\n--nogui\tOpens the command line editor");
52+
}
53+
54+
System.out.println("InitGUI? " + initGUI + "\n" + "Script zo execute? " + scriptToExecute + "\nFilePath? " + filePath);
55+
return;
56+
//Apply rules
57+
58+
// Process p = new Process(true);
59+
// p.addSystemOutput();
60+
// p.setInput(System.in);
61+
//
62+
// if(args.length == 0) {
63+
// if(ConsoleMain.class.getResourceAsStream("/Editor.txt") == null) {
64+
// System.err.println("Editor file is missing at: " + URLDecoder.decode(ConsoleMain.class.getProtectionDomain().getCodeSource().getLocation().getPath(), "UTF-8"));
65+
// return;
66+
// }
67+
// BufferedReader reader = new BufferedReader(new InputStreamReader(ConsoleMain.class.getResourceAsStream("/Editor.txt")));
68+
// String code = "";
69+
// String line = reader.readLine();
70+
// while(line != null) {
71+
// code += line;
72+
// line = reader.readLine();
73+
// }
74+
// reader.close();
75+
// p.execute(code, false);
76+
// return;
77+
// }
78+
//
79+
// if(args[0].equals("-e") || args[0].equals("--execute")) {
80+
// if(args.length < 2) {
81+
// System.err.println("Argument " + args[0] + " expects an executable script, e.g.: -e \"println \"Hello World;\"\"");
82+
// System.exit(-1);
83+
// }
84+
// p.execute(args[1], true);
85+
// } else if(args[0].equals("-f") || args[0].equals("--file")) {
86+
// if(args.length < 2) {
87+
// System.err.println("Argument " + args[0] + " expects a path to a text file containing the script");
88+
// System.exit(-1);
89+
// }
90+
// p.execute(new File(args[1]), true);
91+
// } else if(args[0].equals("--gui") || args[0].equals("-g")) {
92+
// new Window();
93+
// }
94+
}
95+
96+
private static boolean isArgument(String arg) {
97+
return arg.equals("-f") || arg.equals("--file") || arg.equals("-e") || arg.equals("--execute") || arg.equals("--nogui");
98+
}
99+
}

0 commit comments

Comments
 (0)