forked from mkhan45/RustScript
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Cli.java
72 lines (66 loc) · 2.47 KB
/
Cli.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import java.util.List;
import java.util.Arrays;
/**
* @author William Rågstad <[email protected]>
*
* The CLI is a simple command line interface for the RustScript
* language. It provides a simple way to run RustScript script files and
* interact in a playful way using the REPL mode. Sometime in the
* future, this might be extended to include a compiler to other
* languages like JavaScript, C++ or Python.
*/
public class Cli {
private static final String VERSION = "2.3.0";
private static final String DESCRIPTION = "A command line interface tool for the RustScript language.";
private static final String COPYRIGHT = "Copyright (c) 2021 William Ragstad";
private static final String HELP = String.format("""
RustScript CLI version %s.
%s
Usage: rsc (options) (files)
Options:
-h, --help
Prints this help message.
-v, --version
Prints the version of the program.
-r, --repl
Starts the REPL mode.
-l, --lint [files]
Lints the given files.
-c, --compile [files] (Not implemented)
Compiles the given files.
Execute scripts: rsc [files]
Interprets the given script files one at a time.
%s""", VERSION, DESCRIPTION, COPYRIGHT);
public static void main(String[] args) {
List<String> options = Arrays.asList(args).stream().filter(arg -> arg.startsWith("-")).map(String::toLowerCase)
.toList();
List<String> files = Arrays.asList(args).stream().filter(arg -> !arg.startsWith("-")).toList();
if (options.contains("--help") || options.contains("-h")) {
System.out.println(HELP);
} else if (options.contains("--version") || options.contains("-v")) {
System.out.println(VERSION);
} else if (options.contains("--repl") || options.contains("-r")) {
Repl.run();
} else if (options.contains("--lint") || options.contains("-l")) {
if (files.isEmpty()) {
System.out.println("No files given.");
} else {
Linter.run(files);
}
} else {
if (args.length > 0) {
if (options.size() == 1) {
System.out.println(String.format("Error: Unknown option '%s'", options.get(0)));
} else if (options.size() > 1) {
System.out.println(String.format("Error: Unknown options '%s'", String.join("', '", options)));
} else {
// All args are files
Runner.run(files);
}
} else {
System.out.println(HELP);
}
}
// TODO: Add linting and compilation
}
}